Skip to content

Instantly share code, notes, and snippets.

@loganrosen
Created February 6, 2011 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loganrosen/813035 to your computer and use it in GitHub Desktop.
Save loganrosen/813035 to your computer and use it in GitHub Desktop.
#require 'fox16'
#include Fox
#
#puts "Welcome to ELM."
#
## regexp for bold text: <\*\w*\*>
## italic text: \</\w*\/>
## underline: <_\w*_>
#
#randomstring = "Logan _underlines_ stuff and is kind of /italic/ and he *bolds* stuff."
#
#if randomstring.chr==">" && randomstring.chr.chr=="*" && randomstring.reverse.chr=="<" && randomstring.reverse.chr.chr=="*"
# randomstring.gsub(/\w/)
# #(however you bold stuff in FXRuby)
#end
#
#if randomstring.chr==">" && randomstring.chr.chr=="/" && randomstring.reverse.chr=="<" && randomstring.reverse.chr.chr=="/"
# randomstring.gsub(/\w/)
# #(however you italicize stuff in FXRuby)
#end
#
#if randomstring.chr==">" && randomstring.chr.chr=="_" && randomstring.reverse.chr=="<" && randomstring.reverse.chr.chr=="_"
# randomstring.gsub(/\w/)
# #(however you underline stuff in FXRuby)
#end
require 'fox16'
include Fox
class SampleWindow < FXMainWindow
def initialize(app)
super(app, "Interpret Window", :width => 640, :height => 480)
interpret_btn = FXButton.new(self, "Interpret...")
h_frame = FXHorizontalFrame.new(self, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y)
left_txt = FXText.new(h_frame, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y)
inputbox = FXTextField.new(h_frame, 50, nil)
inputbox.text = "Enter the text file you want interpreted (write entire file path)"
right_txt = FXText.new(h_frame, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y|TEXT_READONLY)
right_txt.styled = true # turn on styles
# create the bold style
bold_style = FXHiliteStyle.from_text(right_txt)
bold_style.style = FXText::STYLE_BOLD
right_txt.hiliteStyles = [bold_style] # add additional styles here
interpret_btn.connect(SEL_COMMAND) do |sender, sel, event|
file = inputbox.text
f = File.open(file, 'rb')
left_txt.text = f.read
right_txt.text = left_txt.text
first, last = right_txt.findText("<\\*[a-zA-Z]+\\*>", :flags => SEARCH_REGEX) # use a regex to find the first mark-up
while first != nil
start_pos = first[0] # find the start of the mark-up
end_pos = last[0] - 1
length = last[0] - first[0] # find the length of the mark-up
inner_text = right_txt.text[start_pos..end_pos].gsub("<*", "").gsub("*>", "") # find the text inside the mark-up
right_txt.replaceStyledText(start_pos, length, inner_text, 1) # replace the mark up with just the text and set the style to 1
first, last = right_txt.findText("<\\*[a-zA-Z]+\\*>", :flags => SEARCH_REGEX) # use a regex to find the next mark-up
end
end
end
def create
super
self.show(PLACEMENT_SCREEN)
end
end
app = FXApp.new
SampleWindow.new(app)
app.create
app.run
@loganrosen
Copy link
Author

require 'fox16'
include Fox
class SampleWindow < FXMainWindow
def initialize(app)
super(app, "Matt and Logan's Interpreter © 2011. All rights reserved.", :width => 900, :height => 500)
v_frame = FXVerticalFrame.new(self, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y)
h_frame = FXHorizontalFrame.new(v_frame, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y)
h2_frame = FXHorizontalFrame.new(v_frame, :opts => LAYOUT_FILL_X)
v2_frame = FXVerticalFrame.new(h2_frame, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y)
v3_frame = FXVerticalFrame.new(h2_frame, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y)
h3_frame = FXHorizontalFrame.new(v2_frame, :opts => LAYOUT_FILL_X)
h4_frame = FXHorizontalFrame.new(v2_frame, :opts => LAYOUT_FILL_X)
left_txt = FXText.new(h_frame, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y)
right_txt = FXText.new(h_frame, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y|TEXT_READONLY)
inputbox = FXTextField.new(h3_frame, 50, nil)
interpret_btn = FXButton.new(h3_frame, "Interpret")
inputbox.text = "Enter the text file you want interpreted (write entire file path)"
inputbox2 = FXLabel.new(h4_frame, "Or type in your own text and press this Interpret button")
interpret_btn2 = FXButton.new(h4_frame, "Interpret")
clear_frame = FXHorizontalFrame.new(v3_frame, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y)
clearbutton = FXButton.new(clear_frame, "Clear all input", :padLeft => 100, :padRight => 100, :padTop => 40, :padBottom => 40)
clearbutton.connect(SEL_COMMAND) do |sender, sel, event|
left_txt.text = ""
right_txt.text = ""
button2count = 0
end
right_txt.styled = true # turn on styles
# create the bold, strikeout, and underline style
bold_style = FXHiliteStyle.from_text(right_txt)
bold_style.style = FXText::STYLE_BOLD
strikeout_style = FXHiliteStyle.from_text(right_txt)
strikeout_style.style = FXText::STYLE_STRIKEOUT
underline_style = FXHiliteStyle.from_text(right_txt)
underline_style.style = FXText::STYLE_UNDERLINE

right_txt.hiliteStyles = [bold_style, strikeout_style, underline_style] # add additional styles here
interpret_btn.connect(SEL_COMMAND) do |sender, sel, event|
 file = inputbox.text

f = File.open(file, 'rb')
g = File.open(file, 'rb')
left_txt.text = "Original text:\n\n" + f.read
right_txt.text = "Formatted text:\n\n" + g.read

#convert text into bold

    first, last = right_txt.findText("<\\*[a-zA-Z]+\\*>", :flags => SEARCH_REGEX) # use a regex to find the first mark-up
    while first != nil
        start_pos = first[0] # find the start of the mark-up
    end_pos = last[0] - 1
        length = last[0] - first[0] # find the length of the mark-up
            inner_text = right_txt.text[start_pos..end_pos].gsub("<*", "").gsub("*>", "") # find the text inside the mark-up
            right_txt.replaceStyledText(start_pos, length, inner_text, 1) # replace the mark up with just the text and set the style to 1
        first, last = right_txt.findText("<\\*[a-zA-Z]+\\*>", :flags => SEARCH_REGEX) # use a regex to find the next mark-up
    end

#convert text into strikeout

    first, last = right_txt.findText("<\\-[a-zA-Z]+\\->", :flags => SEARCH_REGEX) # use a regex to find the first mark-up
    while first != nil
        start_pos = first[0] # find the start of the mark-up
    end_pos = last[0] - 1
        length = last[0] - first[0] # find the length of the mark-up
            inner_text = right_txt.text[start_pos..end_pos].gsub("<-", "").gsub("->", "") # find the text inside the mark-up
            right_txt.replaceStyledText(start_pos, length, inner_text, 2) # replace the mark up with just the text and set the style to 2
        first, last = right_txt.findText("<\\-[a-zA-Z]+\\->", :flags => SEARCH_REGEX) # use a regex to find the next mark-up
    end

#convert text into underline

    first, last = right_txt.findText("<\\_[a-zA-Z]+\\_>", :flags => SEARCH_REGEX) # use a regex to find the first mark-up
    while first != nil
        start_pos = first[0] # find the start of the mark-up
    end_pos = last[0] - 1
        length = last[0] - first[0] # find the length of the mark-up
            inner_text = right_txt.text[start_pos..end_pos].gsub("<_", "").gsub("_>", "") #find the text inside the mark-up
            right_txt.replaceStyledText(start_pos, length, inner_text, 3) # replace the mark up with just the text and set the style to 3
        first, last = right_txt.findText("<\\_[a-zA-Z]+\\_>", :flags => SEARCH_REGEX) # use a regex to find the next mark-up
    end

end

button2count = 0
interpret_btn2.connect(SEL_COMMAND) do |sender, sel, event| #If user wants self-inputted text interpreted instead
button2count = button2count+1

left = left_txt.text
right = left_txt.text
  left_txt.text = left.to_s

if button2count <= 1
  right_txt.text = "Formatted text:\n\n" + right.to_s + "\n\n"
  left_txt.text = "Original text:\n\n" + left.to_s + "\n\n"
else
  right_txt.text = "Formatted text from " + right.to_s + "\n\n"
  left_txt.text = left.to_s + "\n\n"
end

#convert text into bold

    first, last = right_txt.findText("<\\*[a-zA-Z]+\\*>", :flags => SEARCH_REGEX) # use a regex to find the first mark-up
    while first != nil
        start_pos = first[0] # find the start of the mark-up
    end_pos = last[0] - 1
        length = last[0] - first[0] # find the length of the mark-up
            inner_text = right_txt.text[start_pos..end_pos].gsub("<*", "").gsub("*>", "") # find the text inside the mark-up
            right_txt.replaceStyledText(start_pos, length, inner_text, 1) # replace the mark up with just the text and set the style to 1
        first, last = right_txt.findText("<\\*[a-zA-Z]+\\*>", :flags => SEARCH_REGEX) # use a regex to find the next mark-up
    end

#convert text into strikeout

    first, last = right_txt.findText("<\\-[a-zA-Z]+\\->", :flags => SEARCH_REGEX) # use a regex to find the first mark-up
    while first != nil
        start_pos = first[0] # find the start of the mark-up
    end_pos = last[0] - 1
        length = last[0] - first[0] # find the length of the mark-up
            inner_text = right_txt.text[start_pos..end_pos].gsub("<-", "").gsub("->", "") # find the text inside the mark-up
            right_txt.replaceStyledText(start_pos, length, inner_text, 2) # replace the mark up with just the text and set the style to 2
        first, last = right_txt.findText("<\\-[a-zA-Z]+\\->", :flags => SEARCH_REGEX) # use a regex to find the next mark-up
    end

#convert text into underline

    first, last = right_txt.findText("<\\_[a-zA-Z]+\\_>", :flags => SEARCH_REGEX) # use a regex to find the first mark-up
    while first != nil
        start_pos = first[0] # find the start of the mark-up
    end_pos = last[0] - 1
        length = last[0] - first[0] # find the length of the mark-up
            inner_text = right_txt.text[start_pos..end_pos].gsub("<_", "").gsub("_>", "") #find the text inside the mark-up
            right_txt.replaceStyledText(start_pos, length, inner_text, 3) # replace the mark up with just the text and set the style to 3
        first, last = right_txt.findText("<\\_[a-zA-Z]+\\_>", :flags => SEARCH_REGEX) # use a regex to find the next mark-up
    end

end

# convert text into strikethrough

right_txt.hiliteStyles = [bold_style, strikeout_style, underline_style] # add additional styles here

interpret_btn.connect(SEL_COMMAND) do |sender, sel, event|

end

# convert text into underline

right_txt.hiliteStyles = [bold_style, strikeout_style, underline_style] # add additional styles here

interpret_btn.connect(SEL_COMMAND) do |sender, sel, event|

end

end

def create
super
self.show(PLACEMENT_SCREEN)
end
end
end
app = FXApp.new
SampleWindow.new(app)
app.create
app.run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment