module SimpleMarkdown::ActionView::Helpers
Constants
- BOLD
- CENTER_BLOCK_1
- CENTER_BLOCK_2
- CENTER_INLINE
- CODE_BLOCK
- CODE_INLINE
- END_OF_P
- FLEX
- FLEX_HEAD
- IMAGE
- ITALIC
- LINK
- LIST
- RETURN
- TITLE
- USELESS_LINE
Public Instance Methods
simple_markdown(text)
click to toggle source
Main entry
# File lib/simple_markdown/action_view/helpers.rb, line 30 def simple_markdown(text) text = text.gsub(/\r\n?/, "\n").split(/\n/) @text_map = text.map @io = StringIO.new begin while(true) parse_block end rescue StopIteration ensure return @io.string.html_safe end end
Private Instance Methods
parse_block()
click to toggle source
# File lib/simple_markdown/action_view/helpers.rb, line 46 def parse_block if @text_map.peek.match(USELESS_LINE) # don't want empty <p></p> @text_map.next elsif @text_map.peek.match(CODE_BLOCK) # code block @text_map.next parse_code elsif @text_map.peek.match(TITLE) parse_title # title, only works if has return before (except first time) elsif @text_map.peek.match(FLEX_HEAD) parse_flex elsif @text_map.peek.match(CENTER_BLOCK_1) parse_center else # normal block parse_p end end
parse_center()
click to toggle source
# File lib/simple_markdown/action_view/helpers.rb, line 145 def parse_center @io << "<center>\n" @text_map.next while(!@text_map.peek.match(CENTER_BLOCK_2)) parse_block end @io << "\n</center>" @text_map.next end
parse_code()
click to toggle source
# File lib/simple_markdown/action_view/helpers.rb, line 90 def parse_code @io << "<pre><code>" continue = true while(continue) # begin line = @text_map.next if line.match(CODE_BLOCK) continue = false else @io << CGI::escapeHTML(line) @io << "\n" unless @text_map.peek.match(CODE_BLOCK) end # rescue StopIteration # continue = false # end end @io << "</code></pre>" end
parse_flex()
click to toggle source
# File lib/simple_markdown/action_view/helpers.rb, line 118 def parse_flex # begin @io << "<div style=\"display:flex; justify-content:space-between; align-items: flex-start;\">\n" line = @text_map.next scan = line.scan(/[0-9]+/) number = scan[0].to_i space = scan[1] 1.upto(number) do |i| if space @io << "<div style=\"flex:#{space};\">\n" else @io << "<div>\n" end while(!@text_map.peek.match(FLEX)) parse_block end line = @text_map.next space = line.scan(/[0-9]+/)[0] @io << "\n</div>" end # rescue StopIteration # # do nothing # ensure @io << "\n</div>" # end end
parse_normal()
click to toggle source
# File lib/simple_markdown/action_view/helpers.rb, line 77 def parse_normal line = @text_map.next line.gsub!(CENTER_INLINE, "<center>#{'\1'}</center>#{'\2'}") line.gsub!(LINK, "#{'\1'}<a href=\"#{'\3'.strip}\">#{'\2'}</a>") # link line.gsub!(IMAGE, "<img src=\"#{'\2'}\" alt=\"#{'\1'.strip}\">") # image line.gsub!(LIST, "• #{'\1'}<br>") # list line.gsub!(CODE_INLINE) { |match| "<code>#{CGI::escapeHTML(Regexp.last_match[1])}</code>"} # inline code line.gsub!(ITALIC, "#{'\1'}<em>#{'\2'}</em>") # italic line.gsub!(BOLD, "<strong>#{'\1'}</strong>") # bold @io << line.gsub(/^([^\s]*)\s+$/, '\1 ') # prints one space if one or more at then end of the line @io << "<br>\n" if line.match(RETURN) # return if more than 2 spaces at the end of the line end
parse_p()
click to toggle source
# File lib/simple_markdown/action_view/helpers.rb, line 63 def parse_p begin @io << "<p>\n" while(!@text_map.peek.match(END_OF_P)) # end paragraph if empty line parse_normal end @text_map.next if !@text_map.peek.match(END_OF_P); rescue StopIteration # do nothing ensure @io << "\n</p>" end end
parse_title()
click to toggle source
# File lib/simple_markdown/action_view/helpers.rb, line 109 def parse_title line = @text_map.next line.gsub!(/^\s*(\#{1,6})(.*)$/) { |match| num = Regexp.last_match[1].size # number of # = type of <hn></hn> "<h#{num}>#{Regexp.last_match[2].strip}</h#{num}>" } @io << line end