Борис обнови решението на 23.11.2011 14:38 (преди около 13 години)
+# encoding: utf-8
+
+class Formatter
+ attr_accessor :markdown, :tags, :html
+
+ SYMBOLS =
+ { '&' => '&', '<' => '<', /(^.+)>/ => '\1>', '"' => '"' }
+ PARAGRAPH_REGEX = /^(\d+\. |\* |> |[#]{1,4}\s+[^\s]+|[ ]{4})/
+
+ IS_TAG =
+ { BlockQuote: ->(line) { line.start_with?('> ') },
+ CodeBlock: ->(line) { line.start_with?(' ') },
+ Header: ->(line) { !(line =~ /^[#]{1,4}\s+[^\s]+/).nil? },
+ OList: ->(line) { !(line =~ /^\d+\. /u).nil? },
+ Paragraph: ->(line) { line != "\n" && (line =~ PARAGRAPH_REGEX).nil? },
+ UList: ->(line) { line.start_with?('* ') } }
+
+ def initialize(markdown)
+ @markdown = markdown
+ @html = @markdown.gsub(/\A\n+/u, '').gsub(/\n+\z/u, '')
+ @html = @html.lines.to_a.chunk{|line| !(line =~ /^[ ]{,3}[^\s]/u).nil?}
+ stripped = @html.map { |is_code, text| strip_lines(is_code, text) }
+ @html = stripped.flatten.inject(&:+)
+ end
+
+ def to_html
+ format
+ @tags = IS_TAG.keys.inject({}) do |hash, tag|
+ hash.merge(get_element(tag) { |line| IS_TAG[tag][line] })
+ end
+ @tags.keys.each { |tag| @html.gsub!(tag, @tags[tag].to_html) }
+ @html = html.gsub(/(<strong>.*)<em>(.*<\/strong>.*)<\/em>/, '\1_\2_')
+ @html.gsub(/(<em>.*)<strong>(.*<\/em>.*)<\/strong>/, '\1**\2**')
+ end
+
+ def strip_lines(is_code, text)
+ is_code ? text.map { |line| line.lstrip } : text
+ end
+
+ def format
+ SYMBOLS.keys.each { |symbol| @html.gsub!(symbol, SYMBOLS[symbol]) }
+ code_blocks = find_tag(:CodeBlock) { |line| IS_TAG[:CodeBlock][line] }
+ @html.gsub!(/_(.*?)_/mu, '<em>\1</em>')
+ @html.gsub!(/\*\*(.*?)\*\*/mu, '<strong>\1</strong>')
+ @html.gsub!(/\[(.*)\]\((.+)\)/u, '<a href="\2">\1</a>')
+ code_blocks_change = find_tag(:CodeBlock) { |line| IS_TAG[:CodeBlock][line] }
+ Hash[code_blocks_change.zip(code_blocks)].each { |k, v| @html.gsub!(k, v) }
+ end
+
+ def get_element(tag_type)
+ all_lines = @html.lines.chunk{|line| yield line }
+ tags = all_lines.select{|key, value| key}.map{|key, value| value.join }
+ tags.inject({}) do |hash, tag|
+ hash.merge Hash[tag.chomp, Kernel.const_get(tag_type).new(tag.chomp)]
+ end
+ end
+
+ def find_tag(tag_type)
+ all_lines = @html.lines.chunk{|line| yield line }
+ all_lines.select{|key, value| key}.map{|key, value| value.join }
+ end
+
+ def inspect
+ @markdown
+ end
+
+ def to_s
+ to_html
+ end
+end
+
+class Header
+ attr_accessor :line
+
+ def initialize(text)
+ @header = text
+ end
+
+ def to_html
+ @header.gsub(/^(\#{1,4})\s+(.+?)\s*$/u) do
+ header_size, title = $1.size, $2
+ %{<h%d>%s</h%d>} % [header_size, title, header_size]
+ end
+ end
+end
+
+class Paragraph
+ def initialize(text)
+ @paragraph = text
+ end
+
+ def to_html
+ "<p>"+@paragraph.strip+"</p>"
+ end
+end
+
+class CodeBlock
+ attr_accessor :block
+ def initialize(text)
+ @codeblock = text
+ end
+
+ def to_html
+ "<pre><code>"+@codeblock.gsub(/^[ ]{4}/u, '')+"</code></pre>"
+ end
+end
+
+class BlockQuote
+ def initialize(text)
+ @blockquote = text+"\n"
+ end
+
+ def to_html
+ all_lines = @blockquote.gsub!(/^> /, '').lines
+ par_map = get_paragraphs all_lines
+ par_map.keys.each { |par| @blockquote.gsub!(par, par_map[par].to_html) }
+ "<blockquote>"+@blockquote[0..-2]+"</blockquote>"
+ end
+
+ def get_paragraphs(all)
+ ls = all.chunk{|line| (line =~ /^(\d+\. |\* |[ ]*\n|#|[ ]{4})/u).nil? }
+ tags = ls.select{|key, value| key}.map{|key, value| value.join }
+ tags.inject({}) do |hash, tag|
+ hash.merge Hash[tag.chomp, Kernel.const_get(:Paragraph).new(tag.chomp)]
+ end
+ end
+end
+
+class OList
+ def initialize(text)
+ @o_list = text
+ end
+
+ def to_html
+ "<ol>\n"+@o_list.gsub(/^[0-9]+\. (.+)/u, " <li>"+'\1'+"</li>")+"\n</ol>"
+ end
+end
+
+class UList
+ def initialize(text)
+ @u_list = text
+ end
+
+ def to_html
+ "<ul>\n"+@u_list.gsub(/^\* (.+)/u, " <li>"+'\1'+"</li>")+"\n</ul>"
+ end
+end