Решение на Пета задача от Георги Бойваленков

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

Към профила на Георги Бойваленков

Резултати

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

Код

# (VERY) LIMITED EDITION :D
class Fucker
def self.links(text)
reg = /\[(.)*\]\((.)*\)/
match = text.match(reg)
return text unless match
crnt = text[reg]
pos = crnt.index(']')
res = "<a href=\""+crnt[pos+2,crnt.length-pos-3]+"\">"+crnt[1,pos-1]+"</a>"
text.gsub(reg, res)
end
def self.strongem(text)
n = text.length
strong = (text.index("**") ? text.index("**") : n + 1)
em = (text.index("_") ? text.index("_") : n + 1)
return text if (strong == n + 1 and em == n + 1)
fin = nil
if strong < em
len = text[strong + 2, text.length - strong - 2].index("**")
return text unless len
res = text[0, strong] + "<strong>" + strongem(text[strong + 2, len]) + "</strong>"
res += strongem(text[strong + 2 + len + 2, n - strong - len - 4])
else
len = text[em + 1, text.length - em - 1].index("_")
return text unless len
res = text[0, em] + "<em>" + strongem(text[em + 1, len])
res += "</em>" + strongem(text[em + 1 + len + 1, n - em - len - 1])
end
res
end
def self.escape(t)
fuck = t.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;").gsub("\"", "&quot;")
res = Fucker.links(Fucker.strongem(fuck))
end
def self.fix_quote(block)
lines = block.split("\n")
newblock = ""
lines.each do |line|
newblock += (line.strip)[/[^>\s](.)*/] + "\n"
end
newblock.strip
end
def self.fix_header(block)
lines = block.split("\n")
newblock = ""
lines.each do |line|
header_num = line.match(/\A(#){1,4}/)[0].length
newblock += "<h" + header_num.to_s + ">"
newblock += Fucker.escape(line[/[^#\s](.)*/]) + "</h" + header_num.to_s + ">\n"
end
newblock
end
def self.entags(type, block)
case type
when "ord_list" then "<ol>\n" + Formatter.fix_ol(block) + "</ol>\n"
when "code" then "<pre><code>" + Formatter.fix_code(block) + "</code></pre>\n"
when "whitespace" then "\n"
else block
end
end
def self.fix_ul(block)
lines = block.split("\n")
newblock = ""
lines.each do |line|
newblock += " <li>" + Fucker.escape(line[/[^*\s](.)*/]) + "</li>\n"
end
newblock
end
def self.entag(type, block)
u = "<blockquote><p>"
case type
when "normal" then "<p>" + Fucker.escape(block.rstrip) + "</p>\n"
when "quote" then u+Fucker.escape(Fucker.fix_quote(block))+"</p></blockquote>\n"
when "header" then Fucker.fix_header(block)
when "unord_list" then "<ul>\n" + self.fix_ul(block)+ "</ul>\n"
else self.entags(type, block)
end
end
def self.fix_them(blocks, html)
blocks[1, blocks.length].each do |b|
html += self.entag(b[0], b[1])
end
html
end
end
class Formatter
def fix_ul(block)
lines = block.split("\n")
newblock = ""
lines.each do |line|
newblock += " <li>" + Fucker.escape(line[/[^*\s](.)*/]) + "</li>\n"
end
newblock
end
def self.fix_ol(block)
lines = block.split("\n")
newblock = ""
lines.each do |line|
newblock += " <li>" + Fucker.escape(line[/[^\d\.\s](.)*/]) + "</li>\n"
end
newblock
end
def self.fix_code(block)
lines = block.split("\n")
newblock = ""
lines.each do |line|
newblock += (line.length > 4 ? line[4, line.length - 4] : "") + "\n"
end
newblock.strip
end
def entag(type, block)
u = "<blockquote><p>"
case type
when "normal" then "<p>" + Fucker.escape(block.rstrip) + "</p>\n"
when "quote" then u+Fucker.escape(Fucker.fix_quote(block))+"</p></blockquote>\n"
when "header" then Fucker.fix_header(block)
when "unord_list" then "<ul>\n" + fix_ul(block)+ "</ul>\n"
else Fucker.entags(type, block)
end
end
def line_type(line)
return "empty" if line.length == 0
return "header" if line.match(/\A(#){1,4}\s+\S/)
return "code" if line.match(/\A\s{4,}\S/)
return "whitespace" unless line.match(/\S/)
return "quote" if line.match(/\A(>)\s+\S/)
return "unord_list" if line.match(/\A(\*)\s+\S/)
return "ord_list" if line.match(/\A\d\.\s+\S/)
"normal"
end
def convert_it()
prev_type, lines, blocks, cr_block = "none", @text.split("\n"), [], ""
lines.each do |l|
type, cr_block = line_type(l), (line_type(l) == prev_type) ? cr_block+l+"\n":l+"\n"
unless type == prev_type
blocks << [prev_type, cr_block]
end
prev_type = type
end
blocks << [prev_type, cr_block]
@html = Fucker.fix_them(blocks, @html)
end
def initialize(input_text)
@text = input_text
@html = ""
convert_it()
end
def to_html()
@html.strip()
end
def to_s()
@html.strip()
end
def inspect()
@text
end
end

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

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

Failures:

  1) Formatter paragraphs renders multiple paragraphs
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p>First line.</p>\n\n<p>Second line.</p>\n\n<p>Third line.</p>"
            got: "<p></p>\nSecond line.\n<p></p>\nThird line.\n<p>Third line.</p>"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,6 @@
       -<p>First line.</p>
       -
       -<p>Second line.</p>
       -
       +<p></p>
       +Second line.
       +<p></p>
       +Third line.
        <p>Third line.</p>
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:25: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 paragraphs renders multiline paragraps
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p>First line.\nSecond line.\nThird line.</p>\n\n<p>Last line, of course.</p>"
            got: "<p></p>\nLast line, of course.\n<p>Last line, of course.</p>"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,4 @@
       -<p>First line.
       -Second line.
       -Third line.</p>
       -
       +<p></p>
       +Last line, of course.
        <p>Last line, of course.</p>
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/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)>'

  3) Formatter paragraphs breaks paragraphs with block-level elements
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20111129-16859-r8u8ma/solution.rb:53:in `block in fix_header'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:52:in `each'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:52:in `fix_header'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:83:in `entag'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:91:in `block in fix_them'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:90:in `each'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:90:in `fix_them'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:157:in `convert_it'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:163:in `initialize'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `new'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:69: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 paragraphs escapes special entities
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p>Cats &amp; Cash</p>\n\n<p>Cool, &lt;right&gt;?</p>"
            got: "<p></p>\nCool, <right>?\n<p>Cool, &lt;right&gt;?</p>"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
       -<p>Cats &amp; Cash</p>
       -
       +<p></p>
       +Cool, <right>?
        <p>Cool, &lt;right&gt;?</p>
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:85: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 headers renders tricky ones
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<h2>Leading wsp</h2>"
            got: "<p>   ## Leading wsp</p>"
       
       (compared using ==)
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:665:in `block in expect_transformations'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:664:in `each'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:664:in `expect_transformations'
     # /tmp/d20111129-16859-r8u8ma/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)>'

  6) Formatter code blocks renders multiline blocks with empty lines
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<pre><code>require 'gravity'\n\n# I'm flying! Just like in Python!</code></pre>"
            got: "<pre><code></code></pre>\n\n<pre><code># I'm flying! Just like in Python!</code></pre>"
       
       (compared using ==)
       
       Diff:
       
       @@ -1,4 +1,4 @@
       -<pre><code>require 'gravity'
       +<pre><code></code></pre>
        
       -# I'm flying! Just like in Python!</code></pre>
       +<pre><code># I'm flying! Just like in Python!</code></pre>
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:140: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 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></code></pre>\n    Second block of code\n<pre><code>Second block of code</code></pre>"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
       -<pre><code>First code-block</code></pre>
       -
       +<pre><code></code></pre>
       +    Second block of code
        <pre><code>Second block of code</code></pre>
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/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)>'

  8) Formatter code blocks escapes special entities
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<pre><code>quote = &quot;Simple &amp; efficient&quot;;</code></pre>"
            got: "<pre><code>quote = \"Simple & efficient\";</code></pre>"
       
       (compared using ==)
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:160: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 code blocks renders properly a longer example with tabs and Unicode
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<pre><code>// \u041F\u0440\u0438\u043C\u0435\u0440 \u0437\u0430 \u0431\u043B\u043E\u043A \u0441 \u043A\u043E\u0434.\n// \u0412 \u043D\u0435\u0433\u043E \u0432\u0441\u0435\u043A\u0438 \u0440\u0435\u0434, \u0434\u043E\u0440\u0438 \u043F\u0440\u0430\u0437\u043D\u0438\u0442\u0435, \u0435 \u043F\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0430\u043D \u043E\u0442 \u0442\u043E\u0447\u043D\u043E \u0447\u0435\u0442\u0438\u0440\u0438 \u0438\u043D\u0442\u0435\u0440\u0432\u0430\u043B\u0430.\ninclude &lt;stdio.h&gt;\n\nint main(int, char**) {\n\t// Whitespace \u0441\u043B\u0435\u0434 \u0447\u0435\u0442\u0438\u0440\u0438\u0442\u0435 \u0437\u0430\u0434\u044A\u043B\u0436\u0438\u0442\u0435\u043B\u043D\u0438 \u0438\u043D\u0442\u0435\u0440\u0432\u0430\u043B\u0430 \u0432 \u043D\u0430\u0447\u0430\u043B\u043E\u0442\u043E, \u0441\u0435 \u0437\u0430\u043F\u0430\u0437\u0432\u0430 \u0432\u0438\u043D\u0430\u0433\u0438.\n\treturn 42;\n}</code></pre>"
            got: "<pre><code></code></pre>\n\n<pre><code>int main(int, char**) {\n\t// Whitespace \u0441\u043B\u0435\u0434 \u0447\u0435\u0442\u0438\u0440\u0438\u0442\u0435 \u0437\u0430\u0434\u044A\u043B\u0436\u0438\u0442\u0435\u043B\u043D\u0438 \u0438\u043D\u0442\u0435\u0440\u0432\u0430\u043B\u0430 \u0432 \u043D\u0430\u0447\u0430\u043B\u043E\u0442\u043E, \u0441\u0435 \u0437\u0430\u043F\u0430\u0437\u0432\u0430 \u0432\u0438\u043D\u0430\u0433\u0438.\n\treturn 42;\n}</code></pre>"
       
       (compared using ==)
       
       Diff:
       
       @@ -1,8 +1,6 @@
       -<pre><code>// Пример за блок с код.
       -// В него всеки ред, дори празните, е предшестван от точно четири интервала.
       -include &lt;stdio.h&gt;
       +<pre><code></code></pre>
        
       -int main(int, char**) {
       +<pre><code>int main(int, char**) {
        	// Whitespace след четирите задължителни интервала в началото, се запазва винаги.
        	return 42;
        }</code></pre>
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:186: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 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: "Some parahraphs here\n<p></p>\n    Some clean code\n<pre><code></code></pre>\nMore paragraphs there?\n<p>More paragraphs there?</p>"
       
       (compared using ==)
       
       Diff:
       @@ -1,10 +1,7 @@
       -<h1>This is a header</h1>
       -
       -<p>Some parahraphs here</p>
       -
       -<pre><code>Some clean code
       -Which is also beautiful
       -And maybe also compiles!</code></pre>
       -
       +Some parahraphs here
       +<p></p>
       +    Some clean code
       +<pre><code></code></pre>
       +More paragraphs there?
        <p>More paragraphs there?</p>
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/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)>'

  11) 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></p></blockquote>\n> Second quote.\n<blockquote><p>Second quote.</p></blockquote>"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
       -<blockquote><p>First quote.</p></blockquote>
       -
       +<blockquote><p></p></blockquote>
       +> Second quote.
        <blockquote><p>Second quote.</p></blockquote>
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/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)>'

  12) Formatter blockquotes renders multiline ones with multiple paragraphs
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     NoMethodError:
       undefined method `+' for nil:NilClass
     # /tmp/d20111129-16859-r8u8ma/solution.rb:44:in `block in fix_quote'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:43:in `each'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:43:in `fix_quote'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:82:in `entag'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:91:in `block in fix_them'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:90:in `each'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:90:in `fix_them'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:157:in `convert_it'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:163:in `initialize'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `new'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:268: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 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) and [Second](another-url\">a first</a>.</p>"
       
       (compared using ==)
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/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)>'

  14) Formatter lists does not choke on malformed lists
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p>1) \u041F\u044A\u0440\u0432\u043E.\n2 \u0412\u0442\u043E\u0440\u043E.\n3.\u0422\u0440\u0435\u0442\u043E</p>\n<ol>\n  <li>\u0427\u0435\u0442\u0432\u044A\u0440\u0442\u043E</li>\n</ol>"
            got: "<p>4. \u0427\u0435\u0442\u0432\u044A\u0440\u0442\u043E</p>\n<ol>\n  <li>\u0427\u0435\u0442\u0432\u044A\u0440\u0442\u043E</li>\n</ol>"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,4 @@
       -<p>1) Първо.
       -2 Второ.
       -3.Трето</p>
       +<p>4. Четвърто</p>
        <ol>
          <li>Четвърто</li>
        </ol>
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/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)>'

  15) Formatter special entities escapes them in code blocks
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<pre><code>brand = &quot;Black &amp; Decker&quot;!</code></pre>"
            got: "<pre><code>brand = \"Black & Decker\"!</code></pre>"
       
       (compared using ==)
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:665:in `block in expect_transformations'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:664:in `each'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:664:in `expect_transformations'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:524: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 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\n<p>Some txt</p>"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,3 @@
       +Some txt
        <p>Some txt</p>
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:665:in `block in expect_transformations'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:664:in `each'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:664:in `expect_transformations'
     # /tmp/d20111129-16859-r8u8ma/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)>'

  17) 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: "<p>   A line here</p>"
       
       (compared using ==)
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:665:in `block in expect_transformations'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:664:in `each'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:664:in `expect_transformations'
     # /tmp/d20111129-16859-r8u8ma/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)>'

  18) 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-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:665:in `block in expect_transformations'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:664:in `each'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:664:in `expect_transformations'
     # /tmp/d20111129-16859-r8u8ma/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)>'

  19) Formatter mixed, complex input renders properly
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20111129-16859-r8u8ma/solution.rb:53:in `block in fix_header'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:52:in `each'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:52:in `fix_header'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:83:in `entag'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:91:in `block in fix_them'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:90:in `each'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:90:in `fix_them'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:157:in `convert_it'
     # /tmp/d20111129-16859-r8u8ma/solution.rb:163:in `initialize'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `new'
     # /tmp/d20111129-16859-r8u8ma/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-r8u8ma/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.60955 seconds
57 examples, 19 failures

Failed examples:

rspec /tmp/d20111129-16859-r8u8ma/spec.rb:9 # Formatter paragraphs renders multiple paragraphs
rspec /tmp/d20111129-16859-r8u8ma/spec.rb:28 # Formatter paragraphs renders multiline paragraps
rspec /tmp/d20111129-16859-r8u8ma/spec.rb:52 # Formatter paragraphs breaks paragraphs with block-level elements
rspec /tmp/d20111129-16859-r8u8ma/spec.rb:72 # Formatter paragraphs escapes special entities
rspec /tmp/d20111129-16859-r8u8ma/spec.rb:99 # Formatter headers renders tricky ones
rspec /tmp/d20111129-16859-r8u8ma/spec.rb:127 # Formatter code blocks renders multiline blocks with empty lines
rspec /tmp/d20111129-16859-r8u8ma/spec.rb:143 # Formatter code blocks renders multiple ones
rspec /tmp/d20111129-16859-r8u8ma/spec.rb:159 # Formatter code blocks escapes special entities
rspec /tmp/d20111129-16859-r8u8ma/spec.rb:163 # Formatter code blocks renders properly a longer example with tabs and Unicode
rspec /tmp/d20111129-16859-r8u8ma/spec.rb:189 # Formatter code blocks renders properly with mixed content
rspec /tmp/d20111129-16859-r8u8ma/spec.rb:239 # Formatter blockquotes renders multiple ones
rspec /tmp/d20111129-16859-r8u8ma/spec.rb:255 # Formatter blockquotes renders multiline ones with multiple paragraphs
rspec /tmp/d20111129-16859-r8u8ma/spec.rb:285 # Formatter links allows multiple links on a single line
rspec /tmp/d20111129-16859-r8u8ma/spec.rb:377 # Formatter lists does not choke on malformed lists
rspec /tmp/d20111129-16859-r8u8ma/spec.rb:521 # Formatter special entities escapes them in code blocks
rspec /tmp/d20111129-16859-r8u8ma/spec.rb:541 # Formatter whitespace removes excess leading and trailing whitespace
rspec /tmp/d20111129-16859-r8u8ma/spec.rb:550 # Formatter whitespace ignores leading and trailing whitespace of lines whenever possible
rspec /tmp/d20111129-16859-r8u8ma/spec.rb:558 # Formatter whitespace does not touch trailing whitespace in code blocks
rspec /tmp/d20111129-16859-r8u8ma/spec.rb:586 # Formatter mixed, complex input renders properly

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

Георги обнови решението на 23.11.2011 22:58 (преди около 13 години)

+# (VERY) LIMITED EDITION :D
+
+class Fucker
+
+ def self.links(text)
+ reg = /\[(.)*\]\((.)*\)/
+ match = text.match(reg)
+ return text unless match
+ crnt = text[reg]
+ pos = crnt.index(']')
+ res = "<a href=\""+crnt[pos+2,crnt.length-pos-3]+"\">"+crnt[1,pos-1]+"</a>"
+ text.gsub(reg, res)
+ end
+
+ def self.strongem(text)
+ n = text.length
+ strong = (text.index("**") ? text.index("**") : n + 1)
+ em = (text.index("_") ? text.index("_") : n + 1)
+ return text if (strong == n + 1 and em == n + 1)
+ fin = nil
+ if strong < em
+ len = text[strong + 2, text.length - strong - 2].index("**")
+ return text unless len
+ res = text[0, strong] + "<strong>" + strongem(text[strong + 2, len]) + "</strong>"
+ res += strongem(text[strong + 2 + len + 2, n - strong - len - 4])
+ else
+ len = text[em + 1, text.length - em - 1].index("_")
+ return text unless len
+ res = text[0, em] + "<em>" + strongem(text[em + 1, len])
+ res += "</em>" + strongem(text[em + 1 + len + 1, n - em - len - 1])
+ end
+ res
+ end
+
+ def self.escape(t)
+ fuck = t.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;").gsub("\"", "&quot;")
+ res = Fucker.links(Fucker.strongem(fuck))
+ end
+
+ def self.fix_quote(block)
+ lines = block.split("\n")
+ newblock = ""
+ lines.each do |line|
+ newblock += (line.strip)[/[^>\s](.)*/] + "\n"
+ end
+ newblock.strip
+ end
+
+ def self.fix_header(block)
+ lines = block.split("\n")
+ newblock = ""
+ lines.each do |line|
+ header_num = line.match(/\A(#){1,4}/)[0].length
+ newblock += "<h" + header_num.to_s + ">"
+ newblock += Fucker.escape(line[/[^#\s](.)*/]) + "</h" + header_num.to_s + ">\n"
+ end
+ newblock
+ end
+
+ def self.entags(type, block)
+ case type
+ when "ord_list" then "<ol>\n" + Formatter.fix_ol(block) + "</ol>\n"
+ when "code" then "<pre><code>" + Formatter.fix_code(block) + "</code></pre>\n"
+ when "whitespace" then "\n"
+ else block
+ end
+ end
+
+ def self.fix_ul(block)
+ lines = block.split("\n")
+ newblock = ""
+ lines.each do |line|
+ newblock += " <li>" + Fucker.escape(line[/[^*\s](.)*/]) + "</li>\n"
+ end
+ newblock
+ end
+
+ def self.entag(type, block)
+ u = "<blockquote><p>"
+ case type
+ when "normal" then "<p>" + Fucker.escape(block.rstrip) + "</p>\n"
+ when "quote" then u+Fucker.escape(Fucker.fix_quote(block))+"</p></blockquote>\n"
+ when "header" then Fucker.fix_header(block)
+ when "unord_list" then "<ul>\n" + self.fix_ul(block)+ "</ul>\n"
+ else self.entags(type, block)
+ end
+ end
+
+ def self.fix_them(blocks, html)
+ blocks[1, blocks.length].each do |b|
+ html += self.entag(b[0], b[1])
+ end
+ html
+ end
+
+end
+
+class Formatter
+
+ def fix_ul(block)
+ lines = block.split("\n")
+ newblock = ""
+ lines.each do |line|
+ newblock += " <li>" + Fucker.escape(line[/[^*\s](.)*/]) + "</li>\n"
+ end
+ newblock
+ end
+ def self.fix_ol(block)
+ lines = block.split("\n")
+ newblock = ""
+ lines.each do |line|
+ newblock += " <li>" + Fucker.escape(line[/[^\d\.\s](.)*/]) + "</li>\n"
+ end
+ newblock
+ end
+ def self.fix_code(block)
+ lines = block.split("\n")
+ newblock = ""
+ lines.each do |line|
+ newblock += (line.length > 4 ? line[4, line.length - 4] : "") + "\n"
+ end
+ newblock.strip
+ end
+
+ def entag(type, block)
+ u = "<blockquote><p>"
+ case type
+ when "normal" then "<p>" + Fucker.escape(block.rstrip) + "</p>\n"
+ when "quote" then u+Fucker.escape(Fucker.fix_quote(block))+"</p></blockquote>\n"
+ when "header" then Fucker.fix_header(block)
+ when "unord_list" then "<ul>\n" + fix_ul(block)+ "</ul>\n"
+ else Fucker.entags(type, block)
+ end
+ end
+
+ def line_type(line)
+ return "empty" if line.length == 0
+ return "header" if line.match(/\A(#){1,4}\s+\S/)
+ return "code" if line.match(/\A\s{4,}\S/)
+ return "whitespace" unless line.match(/\S/)
+ return "quote" if line.match(/\A(>)\s+\S/)
+ return "unord_list" if line.match(/\A(\*)\s+\S/)
+ return "ord_list" if line.match(/\A\d\.\s+\S/)
+ "normal"
+ end
+
+ def convert_it()
+ prev_type, lines, blocks, cr_block = "none", @text.split("\n"), [], ""
+ lines.each do |l|
+ type, cr_block = line_type(l), (line_type(l) == prev_type) ? cr_block+l+"\n":l+"\n"
+ unless type == prev_type
+ blocks << [prev_type, cr_block]
+ end
+ prev_type = type
+ end
+ blocks << [prev_type, cr_block]
+ @html = Fucker.fix_them(blocks, @html)
+ end
+
+ def initialize(input_text)
+ @text = input_text
+ @html = ""
+ convert_it()
+ end
+
+ def to_html()
+ @html.strip()
+ end
+
+ def to_s()
+ @html.strip()
+ end
+
+ def inspect()
+ @text
+ end
+end