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

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

Към профила на Мария Матева

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 37 успешни тест(а)
  • 20 неуспешни тест(а)

Код

# encoding: UTF-8
class Formatter
def initialize(markdown)
@text = markdown
end
def to_html
apply_rules
end
def to_s
to_html
end
def inspect
@text
end
private
def apply_rules
rules = Rules.specials_rules + Rules.headers_rules +
Rules.lists_rules + Rules.paragraph_rules +
Rules.blockquotes_rule + Rules.code_rules +
Rules.links_rules + Rules.font_rules
text = @text
rules.each do |rule|
text = rule.apply_rule(text, "").dup
end
text.strip
end
end
class Rule
def initialize(lhs, lrhs, rrhs, strip = false, final = lambda { |x| x })
@LHS = lhs # match of the rule
@leftRHS = lrhs # right-hand side of the rule; left substitute
@rightRHS = rrhs # right-hans side of the rule; right substitute
@strip = strip # if the single lines are going to be stripped
@final_proc = final # a final function to be executed over the result
end
def apply_rule(text, passed)
while (match = @LHS.match text) #these brackets are needed
if @strip
group = $1.strip
else
group = $1
end
substitute = match.to_s.sub(@LHS, @leftRHS + group + @rightRHS)
passed << match.pre_match << substitute
text = match.post_match
end
passed.length > 0 ? @final_proc.call(passed + text) : text
end
end
class SimpleRule < Rule
def initialize(lhs, lrhs, rrhs)
super(lhs, lrhs, rrhs)
end
def apply_rule(text, passed)
result = text.dup
result.gsub(@LHS, @leftRHS)
end
end
class LinksRule < Rule
def apply_rule(text, passed)
if @LHS.match text
text.gsub!(@LHS, @leftRHS + $2 + "\">" + $1 + @rightRHS)
end
text
end
end
class Rules
def self.paragraph_rules
remove_extra_pars = lambda { |text|
text.gsub(/<\/p>\n<p>/, "\n")
}
[ Rule.new(/^([^< &\n].+?$)/, "<p>", "<\/p>", true),
Rule.new(/<\/p>\n<p>/, "", "", false, remove_extra_pars),
]
end
def self.headers_rules
[ Rule.new(/^\#{4}\s+(\S.+)$/, "<h4>", "<\/h4>", true),
Rule.new(/^\#{3}\s+(\S.+)$/, "<h3>", "<\/h3>", true),
Rule.new(/^\#{2}\s+(\S.+)$/, "<h2>", "<\/h2>", true),
Rule.new(/^\#\s+(\S.+)$/, "<h1>", "<\/h1>", true),
]
end
def self.blockquotes_rule
add_tags = lambda { |text|
text.gsub! /^&gt; /, ""
text = Rules.paragraph_rules[0].apply_rule(text, "")
text = Rules.paragraph_rules[1].apply_rule(text, "")
"<blockquote>" + text.strip + "<\/blockquote>"
}
[ Rule.new(/(^\&gt; .+$)+/, "", "", false, add_tags) ]
end
def self.font_rules
[ Rule.new(/_([^\*]+?)_/, "<em>", "<\/em>"),
Rule.new(/\*\*([^_]+?)\*\*/, "<strong>", "<\/strong>")
]
end
def self.specials_rules
[ SimpleRule.new(/&/, "&amp;", ""),
SimpleRule.new(/</, "&lt;", ""),
SimpleRule.new(/>/, "&gt;", ""),
SimpleRule.new(/\"/, "&quot;", ""),
]
end
def self.code_rules
remove_extra_spaces = lambda { |text|
text.gsub(/&lt;/, "<")
text.gsub(/&gt;/, ">")
text.gsub(/&quot;/, "\"")
text.gsub(/&amp;/, "&")
"<pre><code>" + text.gsub(/^ {4}/, "").strip + "<\/code><\/pre>"
}
[ Rule.new(/(^ {4}\S.+?$)+/, "", "", false, remove_extra_spaces) ]
end
def self.lists_rules
numbered_final = lambda { |text|
("<ol>\n" + text.chop + "\n<\/ol>").gsub />\d\. /, ">"
}
dots_final = lambda { |text|
("<ul>\n" + text.chop + "\n<\/ul>").gsub />\* /, ">"
}
[ Rule.new(/(^\d\. \S.+$)+/, " <li>", "<\/li>", true, numbered_final),
Rule.new(/(^\* \S.+$)+/, " <li>", "<\/li>", true, dots_final),
]
end
def self.links_rules
[ LinksRule.new(/\[([^\n]+?)\]\(([^\n]+?)\)/, "<a href=\"", "<\/a>") ]
end
end

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

..F....F....F..F.FF....F.F.....F......F.FF.FFF..F.FFF...F

Failures:

  1) Formatter paragraphs renders multiline paragraps
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     TypeError:
       can't convert nil into String
     # /tmp/d20111129-16859-yd77gv/solution.rb:52:in `+'
     # /tmp/d20111129-16859-yd77gv/solution.rb:52:in `apply_rule'
     # /tmp/d20111129-16859-yd77gv/solution.rb:29:in `block in apply_rules'
     # /tmp/d20111129-16859-yd77gv/solution.rb:28:in `each'
     # /tmp/d20111129-16859-yd77gv/solution.rb:28:in `apply_rules'
     # /tmp/d20111129-16859-yd77gv/solution.rb:10:in `to_html'
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:45: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 headers renders tricky ones
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<h2>Leading wsp</h2>"
            got: "## Leading wsp"
       
       (compared using ==)
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:665:in `block in expect_transformations'
     # /tmp/d20111129-16859-yd77gv/spec.rb:664:in `each'
     # /tmp/d20111129-16859-yd77gv/spec.rb:664:in `expect_transformations'
     # /tmp/d20111129-16859-yd77gv/spec.rb:106: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)>'

  3) Formatter code blocks renders multiple ones
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<pre><code>First code-block</code></pre>\n\n<pre><code>Second block of code</code></pre>"
            got: "<pre><code>First code-block\n\nSecond block of code</code></pre>"
       
       (compared using ==)
       
       Diff:
       
       @@ -1,4 +1,4 @@
       -<pre><code>First code-block</code></pre>
       +<pre><code>First code-block
        
       -<pre><code>Second block of code</code></pre>
       +Second block of code</code></pre>
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:156: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)>'

  4) Formatter code blocks renders properly with mixed content
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<h1>This is a header</h1>\n\n<p>Some parahraphs here</p>\n\n<pre><code>Some clean code\nWhich is also beautiful\nAnd maybe also compiles!</code></pre>\n\n<p>More paragraphs there?</p>"
            got: "<pre><code><h1>This is a header</h1>\n\n<p>Some parahraphs here</p>\n\nSome clean code\nWhich is also beautiful\nAnd maybe also compiles!\n\n<p>More paragraphs there?</p></code></pre>"
       
       (compared using ==)
       
       Diff:
       
       
       
       @@ -1,10 +1,10 @@
       -<h1>This is a header</h1>
       +<pre><code><h1>This is a header</h1>
        
        <p>Some parahraphs here</p>
        
       -<pre><code>Some clean code
       +Some clean code
        Which is also beautiful
       -And maybe also compiles!</code></pre>
       +And maybe also compiles!
        
       -<p>More paragraphs there?</p>
       +<p>More paragraphs there?</p></code></pre>
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:214: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)>'

  5) Formatter blockquotes renders multiline ones
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     TypeError:
       can't convert nil into String
     # /tmp/d20111129-16859-yd77gv/solution.rb:52:in `+'
     # /tmp/d20111129-16859-yd77gv/solution.rb:52:in `apply_rule'
     # /tmp/d20111129-16859-yd77gv/solution.rb:102:in `block in blockquotes_rule'
     # /tmp/d20111129-16859-yd77gv/solution.rb:56:in `call'
     # /tmp/d20111129-16859-yd77gv/solution.rb:56:in `apply_rule'
     # /tmp/d20111129-16859-yd77gv/solution.rb:29:in `block in apply_rules'
     # /tmp/d20111129-16859-yd77gv/solution.rb:28:in `each'
     # /tmp/d20111129-16859-yd77gv/solution.rb:28:in `apply_rules'
     # /tmp/d20111129-16859-yd77gv/solution.rb:10:in `to_html'
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:236: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)>'

  6) Formatter blockquotes renders multiple ones
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<blockquote><p>First quote.</p></blockquote>\n\n<blockquote><p>Second quote.</p></blockquote>"
            got: "<blockquote><p>First quote.</p>\n\n<p>Second quote.</p></blockquote>"
       
       (compared using ==)
       
       Diff:
       
       @@ -1,4 +1,4 @@
       -<blockquote><p>First quote.</p></blockquote>
       +<blockquote><p>First quote.</p>
        
       -<blockquote><p>Second quote.</p></blockquote>
       +<p>Second quote.</p></blockquote>
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:252: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)>'

  7) 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=\"some-url\">a first</a> and <a href=\"some-url\">a first</a>.</p>"
       
       (compared using ==)
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/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)>'

  8) Formatter links does not render them in code blocks
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<pre><code>This one [is a link](in-a-code-block) - keep as-is.</code></pre>"
            got: "<pre><code>This one <a href=\"in-a-code-block\">is a link</a> - keep as-is.</code></pre>"
       
       (compared using ==)
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:299: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)>'

  9) Formatter lists does not choke on malformed lists
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     TypeError:
       can't convert nil into String
     # /tmp/d20111129-16859-yd77gv/solution.rb:52:in `+'
     # /tmp/d20111129-16859-yd77gv/solution.rb:52:in `apply_rule'
     # /tmp/d20111129-16859-yd77gv/solution.rb:29:in `block in apply_rules'
     # /tmp/d20111129-16859-yd77gv/solution.rb:28:in `each'
     # /tmp/d20111129-16859-yd77gv/solution.rb:28:in `apply_rules'
     # /tmp/d20111129-16859-yd77gv/solution.rb:10:in `to_html'
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:394: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)>'

  10) Formatter bold and italic text rendering does not render in code blocks
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<pre><code>Some _more words_ _to be_ **emphasized**?</code></pre>"
            got: "<pre><code>Some <em>more words</em> <em>to be</em> <strong>emphasized</strong>?</code></pre>"
       
       (compared using ==)
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:459: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)>'

  11) Formatter bold and italic text rendering works in list elements
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<ul>\n  <li>Some <em>more words</em> <em>to be</em> <strong>emphasized</strong></li>\n</ul>"
            got: "<ul>\n  <li>Some <em>more words</em> <em>to be</em> <strong>emphasized</strong></li\n</ul>"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
        <ul>
       -  <li>Some <em>more words</em> <em>to be</em> <strong>emphasized</strong></li>
       +  <li>Some <em>more words</em> <em>to be</em> <strong>emphasized</strong></li
        </ul>
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:476: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)>'

  12) Formatter bold and italic text rendering works in links in list elements
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<ul>\n  <li>Some <a href=\"okay\"><em>more words</em> <em>to be</em> <strong>emphasized</strong></a>!</li>\n</ul>"
            got: "<ul>\n  <li>Some <a href=\"okay\"><em>more words</em> <em>to be</em> <strong>emphasized</strong></a>!</li\n</ul>"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
        <ul>
       -  <li>Some <a href="okay"><em>more words</em> <em>to be</em> <strong>emphasized</strong></a>!</li>
       +  <li>Some <a href="okay"><em>more words</em> <em>to be</em> <strong>emphasized</strong></a>!</li
        </ul>
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:487: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)>'

  13) Formatter bold and italic text rendering does not allow parial overlapping
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p>Some <em>more words **to be</em> emphasized**!</p>"
            got: "<p>Some _more words **to be_ emphasized**!</p>"
       
       (compared using ==)
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:496: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)>'

  14) Formatter bold and italic text rendering allows simple nesting
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p>Some <em>more words <strong>to be</strong> emphasized</em>!</p>"
            got: "<p>Some _more words <strong>to be</strong> emphasized_!</p>"
       
       (compared using ==)
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:501: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)>'

  15) Formatter special entities escapes them in paragraphs
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p>&quot;Black &amp; Decker&quot;!</p>"
            got: "&quot;Black &amp; Decker&quot;!"
       
       (compared using ==)
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:665:in `block in expect_transformations'
     # /tmp/d20111129-16859-yd77gv/spec.rb:664:in `each'
     # /tmp/d20111129-16859-yd77gv/spec.rb:664:in `expect_transformations'
     # /tmp/d20111129-16859-yd77gv/spec.rb:512: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)>'

  16) Formatter special entities escapes them in blockquotes
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<blockquote><p>&quot;Black &amp; Decker&quot;!</p></blockquote>"
            got: "<blockquote>&quot;Black &amp; Decker&quot;!</blockquote>"
       
       (compared using ==)
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:665:in `block in expect_transformations'
     # /tmp/d20111129-16859-yd77gv/spec.rb:664:in `each'
     # /tmp/d20111129-16859-yd77gv/spec.rb:664:in `expect_transformations'
     # /tmp/d20111129-16859-yd77gv/spec.rb:530: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)>'

  17) Formatter whitespace removes excess leading and trailing whitespace
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p>Some txt</p>"
            got: "Some txt"
       
       (compared using ==)
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:665:in `block in expect_transformations'
     # /tmp/d20111129-16859-yd77gv/spec.rb:664:in `each'
     # /tmp/d20111129-16859-yd77gv/spec.rb:664:in `expect_transformations'
     # /tmp/d20111129-16859-yd77gv/spec.rb:547: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)>'

  18) Formatter whitespace ignores leading and trailing whitespace of lines whenever possible
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p>A line here</p>"
            got: "A line here"
       
       (compared using ==)
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:665:in `block in expect_transformations'
     # /tmp/d20111129-16859-yd77gv/spec.rb:664:in `each'
     # /tmp/d20111129-16859-yd77gv/spec.rb:664:in `expect_transformations'
     # /tmp/d20111129-16859-yd77gv/spec.rb:555: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)>'

  19) Formatter whitespace does not touch trailing whitespace in code blocks
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<pre><code>Simple code blk  </code></pre>"
            got: "<pre><code>Simple code blk</code></pre>"
       
       (compared using ==)
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:665:in `block in expect_transformations'
     # /tmp/d20111129-16859-yd77gv/spec.rb:664:in `each'
     # /tmp/d20111129-16859-yd77gv/spec.rb:664:in `expect_transformations'
     # /tmp/d20111129-16859-yd77gv/spec.rb:563: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)>'

  20) Formatter mixed, complex input renders properly
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     TypeError:
       can't convert nil into String
     # /tmp/d20111129-16859-yd77gv/solution.rb:52:in `+'
     # /tmp/d20111129-16859-yd77gv/solution.rb:52:in `apply_rule'
     # /tmp/d20111129-16859-yd77gv/solution.rb:29:in `block in apply_rules'
     # /tmp/d20111129-16859-yd77gv/solution.rb:28:in `each'
     # /tmp/d20111129-16859-yd77gv/solution.rb:28:in `apply_rules'
     # /tmp/d20111129-16859-yd77gv/solution.rb:10:in `to_html'
     # /tmp/d20111129-16859-yd77gv/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-yd77gv/spec.rb:655: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.6186 seconds
57 examples, 20 failures

Failed examples:

rspec /tmp/d20111129-16859-yd77gv/spec.rb:28 # Formatter paragraphs renders multiline paragraps
rspec /tmp/d20111129-16859-yd77gv/spec.rb:99 # Formatter headers renders tricky ones
rspec /tmp/d20111129-16859-yd77gv/spec.rb:143 # Formatter code blocks renders multiple ones
rspec /tmp/d20111129-16859-yd77gv/spec.rb:189 # Formatter code blocks renders properly with mixed content
rspec /tmp/d20111129-16859-yd77gv/spec.rb:223 # Formatter blockquotes renders multiline ones
rspec /tmp/d20111129-16859-yd77gv/spec.rb:239 # Formatter blockquotes renders multiple ones
rspec /tmp/d20111129-16859-yd77gv/spec.rb:285 # Formatter links allows multiple links on a single line
rspec /tmp/d20111129-16859-yd77gv/spec.rb:298 # Formatter links does not render them in code blocks
rspec /tmp/d20111129-16859-yd77gv/spec.rb:377 # Formatter lists does not choke on malformed lists
rspec /tmp/d20111129-16859-yd77gv/spec.rb:458 # Formatter bold and italic text rendering does not render in code blocks
rspec /tmp/d20111129-16859-yd77gv/spec.rb:468 # Formatter bold and italic text rendering works in list elements
rspec /tmp/d20111129-16859-yd77gv/spec.rb:479 # Formatter bold and italic text rendering works in links in list elements
rspec /tmp/d20111129-16859-yd77gv/spec.rb:495 # Formatter bold and italic text rendering does not allow parial overlapping
rspec /tmp/d20111129-16859-yd77gv/spec.rb:500 # Formatter bold and italic text rendering allows simple nesting
rspec /tmp/d20111129-16859-yd77gv/spec.rb:509 # Formatter special entities escapes them in paragraphs
rspec /tmp/d20111129-16859-yd77gv/spec.rb:527 # Formatter special entities escapes them in blockquotes
rspec /tmp/d20111129-16859-yd77gv/spec.rb:541 # Formatter whitespace removes excess leading and trailing whitespace
rspec /tmp/d20111129-16859-yd77gv/spec.rb:550 # Formatter whitespace ignores leading and trailing whitespace of lines whenever possible
rspec /tmp/d20111129-16859-yd77gv/spec.rb:558 # Formatter whitespace does not touch trailing whitespace in code blocks
rspec /tmp/d20111129-16859-yd77gv/spec.rb:586 # Formatter mixed, complex input renders properly

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

Мария обнови решението на 21.11.2011 16:36 (преди около 13 години)

+# encoding: UTF-8
+
+class Formatter
+ def initialize(markdown)
+ @text = markdown
+ end
+
+
+ def to_html
+ apply_rules
+ end
+
+ def to_s
+ to_html
+ end
+
+ def inspect
+ @text
+ end
+
+ private
+ def apply_rules
+ rules = Rules.specials_rules + Rules.headers_rules +
+ Rules.lists_rules + Rules.paragraph_rules +
+ Rules.blockquotes_rule + Rules.code_rules +
+ Rules.links_rules + Rules.font_rules
+ text = @text
+ rules.each do |rule|
+ text = rule.apply_rule(text, "").dup
+ end
+ text.strip
+ end
+end
+
+
+class Rule
+ def initialize(lhs, lrhs, rrhs, strip = false, final = lambda { |x| x })
+ @LHS = lhs # match of the rule
+ @leftRHS = lrhs # right-hand side of the rule; left substitute
+ @rightRHS = rrhs # right-hans side of the rule; right substitute
+ @strip = strip # if the single lines are going to be stripped
+ @final_proc = final # a final function to be executed over the result
+ end
+
+ def apply_rule(text, passed)
+ while (match = @LHS.match text) #these brackets are needed
+ if @strip
+ group = $1.strip
+ else
+ group = $1
+ end
+ substitute = match.to_s.sub(@LHS, @leftRHS + group + @rightRHS)
+ passed << match.pre_match << substitute
+ text = match.post_match
+ end
+ passed.length > 0 ? @final_proc.call(passed + text) : text
+ end
+end
+
+class SimpleRule < Rule
+ def initialize(lhs, lrhs, rrhs)
+ super(lhs, lrhs, rrhs)
+ end
+
+ def apply_rule(text, passed)
+ result = text.dup
+ result.gsub(@LHS, @leftRHS)
+ end
+end
+
+class LinksRule < Rule
+ def apply_rule(text, passed)
+ if @LHS.match text
+ text.gsub!(@LHS, @leftRHS + $2 + "\">" + $1 + @rightRHS)
+ end
+ text
+ end
+end
+
+class Rules
+ def self.paragraph_rules
+ remove_extra_pars = lambda { |text|
+ text.gsub(/<\/p>\n<p>/, "\n")
+ }
+ [ Rule.new(/^([^< &\n].+?$)/, "<p>", "<\/p>", true),
+ Rule.new(/<\/p>\n<p>/, "", "", false, remove_extra_pars),
+ ]
+ end
+
+ def self.headers_rules
+ [ Rule.new(/^\#{4}\s+(\S.+)$/, "<h4>", "<\/h4>", true),
+ Rule.new(/^\#{3}\s+(\S.+)$/, "<h3>", "<\/h3>", true),
+ Rule.new(/^\#{2}\s+(\S.+)$/, "<h2>", "<\/h2>", true),
+ Rule.new(/^\#\s+(\S.+)$/, "<h1>", "<\/h1>", true),
+ ]
+ end
+
+ def self.blockquotes_rule
+ add_tags = lambda { |text|
+ text.gsub! /^&gt; /, ""
+ text = Rules.paragraph_rules[0].apply_rule(text, "")
+ text = Rules.paragraph_rules[1].apply_rule(text, "")
+ "<blockquote>" + text.strip + "<\/blockquote>"
+ }
+ [ Rule.new(/(^\&gt; .+$)+/, "", "", false, add_tags) ]
+ end
+
+ def self.font_rules
+ [ Rule.new(/_([^\*]+?)_/, "<em>", "<\/em>"),
+ Rule.new(/\*\*([^_]+?)\*\*/, "<strong>", "<\/strong>")
+ ]
+ end
+
+ def self.specials_rules
+ [ SimpleRule.new(/&/, "&amp;", ""),
+ SimpleRule.new(/</, "&lt;", ""),
+ SimpleRule.new(/>/, "&gt;", ""),
+ SimpleRule.new(/\"/, "&quot;", ""),
+ ]
+ end
+
+ def self.code_rules
+ remove_extra_spaces = lambda { |text|
+ text.gsub(/&lt;/, "<")
+ text.gsub(/&gt;/, ">")
+ text.gsub(/&quot;/, "\"")
+ text.gsub(/&amp;/, "&")
+ "<pre><code>" + text.gsub(/^ {4}/, "").strip + "<\/code><\/pre>"
+ }
+ [ Rule.new(/(^ {4}\S.+?$)+/, "", "", false, remove_extra_spaces) ]
+ end
+
+ def self.lists_rules
+ numbered_final = lambda { |text|
+ ("<ol>\n" + text.chop + "\n<\/ol>").gsub />\d\. /, ">"
+ }
+ dots_final = lambda { |text|
+ ("<ul>\n" + text.chop + "\n<\/ul>").gsub />\* /, ">"
+ }
+ [ Rule.new(/(^\d\. \S.+$)+/, " <li>", "<\/li>", true, numbered_final),
+ Rule.new(/(^\* \S.+$)+/, " <li>", "<\/li>", true, dots_final),
+ ]
+ end
+
+ def self.links_rules
+ [ LinksRule.new(/\[([^\n]+?)\]\(([^\n]+?)\)/, "<a href=\"", "<\/a>") ]
+ end
+end