Решение на Пета задача от Борис Минев

Обратно към всички решения

Към профила на Борис Минев

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 55 успешни тест(а)
  • 2 неуспешни тест(а)

Код

# encoding: utf-8
class Formatter
attr_accessor :markdown, :tags, :html
SYMBOLS =
{ '&' => '&amp;', '<' => '&lt;', /(^.+)>/ => '\1&gt;', '"' => '&quot;' }
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

Лог от изпълнението

...F...................F.................................

Failures:

  1) Formatter paragraphs does not render empty paragraphs
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     NoMethodError:
       undefined method `gsub!' for nil:NilClass
     # /tmp/d20111129-16859-12z0d69/solution.rb:41:in `block in format'
     # /tmp/d20111129-16859-12z0d69/solution.rb:41:in `each'
     # /tmp/d20111129-16859-12z0d69/solution.rb:41:in `format'
     # /tmp/d20111129-16859-12z0d69/solution.rb:27:in `to_html'
     # /tmp/d20111129-16859-12z0d69/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-12z0d69/spec.rb:49:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Formatter links allows multiple links on a single line
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p>We have <a href=\"some-url\">a first</a> and <a href=\"another-url\">Second</a>.</p>"
            got: "<p>We have <a href=\"another-url\">a first](some-url) and [Second</a>.</p>"
       
       (compared using ==)
     # /tmp/d20111129-16859-12z0d69/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-12z0d69/spec.rb:286:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.68076 seconds
57 examples, 2 failures

Failed examples:

rspec /tmp/d20111129-16859-12z0d69/spec.rb:48 # Formatter paragraphs does not render empty paragraphs
rspec /tmp/d20111129-16859-12z0d69/spec.rb:285 # Formatter links allows multiple links on a single line

История (1 версия и 0 коментара)

Борис обнови решението на 23.11.2011 14:38 (преди около 13 години)

+# encoding: utf-8
+
+class Formatter
+ attr_accessor :markdown, :tags, :html
+
+ SYMBOLS =
+ { '&' => '&amp;', '<' => '&lt;', /(^.+)>/ => '\1&gt;', '"' => '&quot;' }
+ 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