Решение на Пета задача от Димитър Илиев

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

Към профила на Димитър Илиев

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 29 успешни тест(а)
  • 28 неуспешни тест(а)

Код

class Formatter
attr_accessor :raw_text, :html
def initialize raw_text
@raw_text = raw_text
#trim_text = raw_text.gsub(/^\n|\n$/,'')
@html = HtmlConvertor.new raw_text
end
def to_html
@html.convert
end
def inspect
@raw_text
end
def render_to file_name
File.open(file_name,'w'){|file| file.write(to_html)}
end
def to_s
@html.convert
end
end
class Markdown
def txt_to_list txt
case txt
when /^[0-9]+./ then
HtmlObject.new txt.gsub(/[0-9]+. /,''), 'li', 'ul'
when /^* / then
HtmlObject.new txt.gsub(/\* /,''), 'li', 'ol'
end
end
def txt_to_heading txt
case txt
when /^# / then HtmlObject.new( txt.gsub(/^#( )+/,''),'h1')
when /^## / then HtmlObject.new( txt.gsub(/^##( )+/,''), 'h2')
when /^### / then HtmlObject.new( txt.gsub(/^###( )+/,''), 'h3')
when /^#### / then HtmlObject.new( txt.gsub(/^####( )+/,''), 'h4')
else HtmlObject.new(txt,'p')
end
end
def txt_to_code txt
HtmlObject.new txt, 'code'
end
def txt_to_bquotes txt
HtmlObject.new txt.gsub(/^> /,''), 'blockquote'
end
def to_tag line
case line
when /^(#){1,4} / then txt_to_heading line
when /^( ){4,}/ then txt_to_code line
when /^[0-9]. |^\* / then txt_to_list line
when /^> / then txt_to_bquotes line
else HtmlObject.new line, 'p'
end
end
end
class HtmlConvertor
attr_accessor :tags
def initialize trimmed_text
@tags = []
tmp_tags = []
mark = Markdown.new
trimmed_text.split(/\n+/).each do |line|
tmp_tags.push mark.to_tag line
end
generate_tags tmp_tags
end
def generate_tags tags_array
tags_array.each do |tag|
case tag.type
when 'ul' then push_ul_list tag
when 'ol' then push_ol_list tag
when 'blockquote' then puts push_block tag
when 'code' then push_code tag
when /h\d/ then @tags.push tag.escape
else @tags.push tag.escape
end
end
end
def push_ul_list tag
if @tags.size == 0 then
return @tags.push HtmlObject.new tag.escape, 'ul'
end
if @tags.class == String || @tags.last.type !='ul' then
@tags.push HtmlObject.new tag.escape, 'ul'
else
@tags.last.text << +"\n "+tag.escape
end
end
def push_ol_list tag
if @tags.size == 0 then
return @tags.push HtmlObject.new tag.escape, 'ol'
end
if @tags.class == String || @tags.last.type !='ol' then
@tags.push HtmlObject.new tag.escape, 'ol'
else
@tags.last.text << tag.escape
end
end
def push_block tag
if @tags.size == 0 then
return @tags.push HtmlObject.new tag.convert_to('p'), 'blockquote'
end
if @tags.class == HtmlObject || @tags.last.type != 'blockquote' then
@tags.push HtmlObject.new tag.convert_to('p'), 'blockquote'
else
@tags.last.text << (tag.convert_to 'p')
end
end
def push_code tag
if @tags.size == 0 || @tags.last.type != 'code' then
@tags.push HtmlObject.new tag.to_text, ['pre','code'], 'code'
else
@tags.last.text << "\n"+tag.to_text
end
end
def convert
#join-ваме с \n за да ни по красив и четим html-a :)
@tags.map(&:to_s).join("\n\n")
end
end
class HtmlObject
attr_accessor :tag_name, :text, :type
def initialize text, tag_name, tag_type = nil
@tag_name = tag_name
@text = text
@type = tag_type || tag_name
end
def wrap_tag tag_name
"<#{tag_name}>#{self.escape}</#{tag_name}>"
end
def to_s
if @tag_name.class == 'str'.class then
"<#{@tag_name}>#{@text}</#{@tag_name}>"
else
"<#{@tag_name.join('><')}>#{@text}</#{@tag_name.reverse.join('></')}>"
end
end
def to_text
@text.gsub(/^( ){4}/,'')
end
def convert_to new_tag
@tag_name = new_tag
escape
end
def escape
@text = @text.gsub(/\&/,'&amp;')
@text = @text.gsub(/</,'&lt;')
@text = @text.gsub(/>/,'&gt;')
@text = @text.gsub(/\"/,'&quot;')
wrap_inner_tags
end
def wrap_inner_tags
if @text.match(/\[(.*)\]\((.*)\)/) then
@text = @text.gsub(/(\[(.*)\])\((.*)\)/,"<a href=\"#{$2}\">#{$1}</a>")
end
if @text.match(/(\*\*)(.+)(\*\*)/) then
@text = @text.gsub(/(\*\*)(.+)(\*\*)/,"<strong>#{$2}</strong>")
end
if @text.match(/(_)(.+)(_)/) then
@text = @text.gsub(/(_)(.+)(_)/,"<em>#{$2}</em>")
end
to_s
end
end

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

..F.F..FF...FFFF<blockquote><p>Simple quote</p></blockquote>
.<blockquote><p>First line.</p></blockquote>
<p>First line.</p><p>Second line.</p>
<p>First line.</p><p>Second line.</p><p>Third line.</p>
F<blockquote><p>First quote.</p></blockquote>
<p>First quote.</p><p>Second quote.</p>
F<blockquote><p>First quote.</p></blockquote>
<p>First quote.</p><p></p>
<p>First quote.</p><p></p><p>Second quote.</p>
F<blockquote><p>Cuttin &amp; Pastin, w/o Quotin (&quot;)</p></blockquote>
...F....FFFFFF.F.<blockquote><p><em>Simplest</em> case</p></blockquote>
<blockquote><p><em>Simplest case</em></p></blockquote>
<blockquote><p><strong>Strongest</strong> case</p></blockquote>
..FFFFF...F<blockquote><p>&quot;Black &amp; Decker&quot;!</p></blockquote>
..FF....F

Failures:

  1) 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>First line.</p>\n\n<p>Second line.</p>\n\n<p>Third line.</p>\n\n<p>Last line, of course.</p>"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,8 @@
       -<p>First line.
       -Second line.
       -Third line.</p>
       +<p>First line.</p>
       +
       +<p>Second line.</p>
       +
       +<p>Third line.</p>
        
        <p>Last line, of course.</p>
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/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 paragraphs breaks paragraphs with block-level elements
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p>First line.</p>\n<h1>Header...</h1>\n<p>Third line.</p>\n\n<p>Separate line</p>"
            got: "<p>First line.</p>\n\n<h1>Header...</h1>\n\n<p>Third line.</p>\n\n<p>Separate line</p>"
       
       (compared using ==)
       
       Diff:
       
       @@ -1,5 +1,7 @@
        <p>First line.</p>
       +
        <h1>Header...</h1>
       +
        <p>Third line.</p>
        
        <p>Separate line</p>
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/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)>'

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

  4) Formatter headers skips the malformed ones
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p>###</p>"
            got: "<h3></h3>"
       
       (compared using ==)
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:665:in `block in expect_transformations'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:664:in `each'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:664:in `expect_transformations'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:114: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 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\nSecond block of code</code></pre>"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,3 @@
       -<pre><code>First code-block</code></pre>
       -
       -<pre><code>Second block of code</code></pre>
       +<pre><code>First code-block
       +Second block of code</code></pre>
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/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)>'

  6) 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-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/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)>'

  7) 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>// \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 <stdio.h>\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>"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,6 @@
        <pre><code>// Пример за блок с код.
        // В него всеки ред, дори празните, е предшестван от точно четири интервала.
       -include &lt;stdio.h&gt;
       +include <stdio.h>
        
        int main(int, char**) {
        	// Whitespace след четирите задължителни интервала в началото, се запазва винаги.
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/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)>'

  8) Formatter code blocks renders properly with mixed content
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     NoMethodError:
       undefined method `type' for "<p>Some parahraphs here</p>":String
     # /tmp/d20111129-16859-h2kp4b/solution.rb:129:in `push_code'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:89:in `block in generate_tags'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:84:in `each'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:84:in `generate_tags'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:80:in `initialize'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:7:in `new'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:7:in `initialize'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `new'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/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)>'

  9) Formatter blockquotes renders multiline ones
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<blockquote><p>First line.\nSecond line.\nThird line.</p></blockquote>"
            got: "<blockquote><p>First line.</p><p>Second line.</p><p>Third line.</p></blockquote>"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,2 @@
       -<blockquote><p>First line.
       -Second line.
       -Third line.</p></blockquote>
       +<blockquote><p>First line.</p><p>Second line.</p><p>Third line.</p></blockquote>
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/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)>'

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

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

  12) 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-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/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)>'

  13) Formatter lists renders simple unordered lists
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<ul>\n  <li>\u0415\u0434\u043D\u043E.</li>\n  <li>\u0414\u0440\u0443\u0433\u043E.</li>\n  <li>\u0422\u0440\u0435\u0442\u043E...</li>\n</ul>"
            got: "<ol><li>\u0415\u0434\u043D\u043E.</li><li>\u0414\u0440\u0443\u0433\u043E.</li><li>\u0422\u0440\u0435\u0442\u043E...</li></ol>"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,2 @@
       -<ul>
       -  <li>Едно.</li>
       -  <li>Друго.</li>
       -  <li>Трето...</li>
       -</ul>
       +<ol><li>Едно.</li><li>Друго.</li><li>Трето...</li></ol>
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:330: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 renders simple ordered lists
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     NoMethodError:
       undefined method `+@' for "\n  ":String
     # /tmp/d20111129-16859-h2kp4b/solution.rb:103:in `push_ul_list'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:86:in `block in generate_tags'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:84:in `each'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:84:in `generate_tags'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:80:in `initialize'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:7:in `new'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:7:in `initialize'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `new'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:348: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 lists renders lists with a single item
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<ul>\n  <li>Single item.</li>\n</ul>"
            got: "<ol><li>Single item.</li></ol>"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,2 @@
       -<ul>
       -  <li>Single item.</li>
       -</ul>
       +<ol><li>Single item.</li></ol>
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:362: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 lists does not choke on malformed lists
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     NoMethodError:
       undefined method `type' for "<p>3.\u0422\u0440\u0435\u0442\u043E</p>":String
     # /tmp/d20111129-16859-h2kp4b/solution.rb:100:in `push_ul_list'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:86:in `block in generate_tags'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:84:in `each'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:84:in `generate_tags'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:80:in `initialize'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:7:in `new'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:7:in `initialize'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `new'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/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)>'

  17) Formatter lists escapes special entities in the list elements
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<ul>\n  <li>The &amp;&amp; and || are logical operators</li>\n  <li>The `&quot;` symbol</li>\n</ul>"
            got: "<ol><li>The &amp;&amp; and || are logical operators</li><li>The `&quot;` symbol</li></ol>"
       
       (compared using ==)
       
       Diff:
       @@ -1,5 +1,2 @@
       -<ul>
       -  <li>The &amp;&amp; and || are logical operators</li>
       -  <li>The `&quot;` symbol</li>
       -</ul>
       +<ol><li>The &amp;&amp; and || are logical operators</li><li>The `&quot;` symbol</li></ol>
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:410: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 lists allows links in list elements
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<ul>\n  <li>A <a href=\" here \">simple link</a> or there?</li>\n</ul>"
            got: "<ol><li>A <a href=\" here \">simple link</a> or there?</li></ol>"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,2 @@
       -<ul>
       -  <li>A <a href=" here ">simple link</a> or there?</li>
       -</ul>
       +<ol><li>A <a href=" here ">simple link</a> or there?</li></ol>
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:424: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 bold and italic text rendering allows multiple ones per line
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p>Some <em>more words here</em> <em>to be</em> <strong>emphasized</strong>, okay?</p>"
            got: "<p>Some <em>more words here_ _to be</em> <strong>emphasized</strong>, okay?</p>"
       
       (compared using ==)
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:438: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 bold and italic text rendering works in links
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p>Some <a href=\"okay\"><em>more words here</em> <em>to be</em> <strong>emphasized</strong></a>?</p>"
            got: "<p>Some <a href=\"okay\"><em>more words here_ _to be</em> <strong>emphasized</strong></a>?</p>"
       
       (compared using ==)
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:464: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)>'

  21) 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: "<ol><li>Some <em>more words_ _to be</em> <strong>emphasized</strong></li></ol>"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,2 @@
       -<ul>
       -  <li>Some <em>more words</em> <em>to be</em> <strong>emphasized</strong></li>
       -</ul>
       +<ol><li>Some <em>more words_ _to be</em> <strong>emphasized</strong></li></ol>
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/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)>'

  22) 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: "<ol><li>Some <a href=\"okay\"><em>more words_ _to be</em> <strong>emphasized</strong></a>!</li></ol>"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,2 @@
       -<ul>
       -  <li>Some <a href="okay"><em>more words</em> <em>to be</em> <strong>emphasized</strong></a>!</li>
       -</ul>
       +<ol><li>Some <a href="okay"><em>more words_ _to be</em> <strong>emphasized</strong></a>!</li></ol>
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/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)>'

  23) Formatter bold and italic text rendering does not brake HTML entities inside it
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p>Some <em>more &amp; words</em> <em>to be</em> <strong>&quot;emphasized&quot;</strong>!</p>"
            got: "<p>Some <em>more &amp; words_ _to be</em> <strong>&quot;emphasized&quot;</strong>!</p>"
       
       (compared using ==)
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:491: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)>'

  24) 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 <em>more words <strong>to be</em> emphasized</strong>!</p>"
       
       (compared using ==)
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/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)>'

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

  26) 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: "<p></p>\n\n<p>Some txt</p>"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,4 @@
       +<p></p>
       +
        <p>Some txt</p>
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:665:in `block in expect_transformations'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:664:in `each'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:664:in `expect_transformations'
     # /tmp/d20111129-16859-h2kp4b/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)>'

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

  28) Formatter mixed, complex input renders properly
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     NoMethodError:
       undefined method `type' for #<String:0x976e0e0>
     # /tmp/d20111129-16859-h2kp4b/solution.rb:121:in `push_block'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:88:in `block in generate_tags'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:84:in `each'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:84:in `generate_tags'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:80:in `initialize'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:7:in `new'
     # /tmp/d20111129-16859-h2kp4b/solution.rb:7:in `initialize'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `new'
     # /tmp/d20111129-16859-h2kp4b/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-h2kp4b/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.61116 seconds
57 examples, 28 failures

Failed examples:

rspec /tmp/d20111129-16859-h2kp4b/spec.rb:28 # Formatter paragraphs renders multiline paragraps
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:52 # Formatter paragraphs breaks paragraphs with block-level elements
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:99 # Formatter headers renders tricky ones
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:109 # Formatter headers skips the malformed ones
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:143 # Formatter code blocks renders multiple ones
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:159 # Formatter code blocks escapes special entities
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:163 # Formatter code blocks renders properly a longer example with tabs and Unicode
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:189 # Formatter code blocks renders properly with mixed content
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:223 # Formatter blockquotes renders multiline ones
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:239 # Formatter blockquotes renders multiple ones
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:255 # Formatter blockquotes renders multiline ones with multiple paragraphs
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:285 # Formatter links allows multiple links on a single line
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:315 # Formatter lists renders simple unordered lists
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:333 # Formatter lists renders simple ordered lists
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:351 # Formatter lists renders lists with a single item
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:377 # Formatter lists does not choke on malformed lists
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:397 # Formatter lists escapes special entities in the list elements
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:413 # Formatter lists allows links in list elements
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:437 # Formatter bold and italic text rendering allows multiple ones per line
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:463 # Formatter bold and italic text rendering works in links
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:468 # Formatter bold and italic text rendering works in list elements
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:479 # Formatter bold and italic text rendering works in links in list elements
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:490 # Formatter bold and italic text rendering does not brake HTML entities inside it
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:495 # Formatter bold and italic text rendering does not allow parial overlapping
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:521 # Formatter special entities escapes them in code blocks
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:541 # Formatter whitespace removes excess leading and trailing whitespace
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:550 # Formatter whitespace ignores leading and trailing whitespace of lines whenever possible
rspec /tmp/d20111129-16859-h2kp4b/spec.rb:586 # Formatter mixed, complex input renders properly

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

Димитър обнови решението на 20.11.2011 21:51 (преди над 12 години)

+class Formatter
+ attr_accessor :raw_text, :html
+
+ def initialize raw_text
+ @raw_text = raw_text
+ #trim_text = raw_text.gsub(/^\n|\n$/,'')
+ @html = HtmlConvertor.new raw_text
+
+ end
+
+ def to_html
+ @html.convert
+ end
+
+ def inspect
+ @raw_text
+ end
+
+ def render_to file_name
+ File.open(file_name,'w'){|file| file.write(to_html)}
+ end
+
+ def to_s
+ @html.convert
+ end
+end
+
+class Markdown
+ def txt_to_list txt
+ case txt
+ when /^[0-9]+./ then
+ HtmlObject.new txt.gsub(/[0-9]+. /,''), 'li', 'ul'
+ when /^* / then
+ HtmlObject.new txt.gsub(/\* /,''), 'li', 'ol'
+ end
+ end
+
+ def txt_to_heading txt
+ case txt
+ when /^# / then HtmlObject.new( txt.gsub(/^#( )+/,''),'h1')
+ when /^## / then HtmlObject.new( txt.gsub(/^##( )+/,''), 'h2')
+ when /^### / then HtmlObject.new( txt.gsub(/^###( )+/,''), 'h3')
+ when /^#### / then HtmlObject.new( txt.gsub(/^####( )+/,''), 'h4')
+ else HtmlObject.new(txt,'p')
+ end
+ end
+
+ def txt_to_code txt
+ HtmlObject.new txt, 'code'
+ end
+
+ def txt_to_bquotes txt
+ HtmlObject.new txt.gsub(/^> /,''), 'blockquote'
+ end
+
+ def to_tag line
+
+ case line
+ when /^(#){1,4} / then txt_to_heading line
+ when /^( ){4,}/ then txt_to_code line
+ when /^[0-9]. |^\* / then txt_to_list line
+ when /^> / then txt_to_bquotes line
+ else HtmlObject.new line, 'p'
+ end
+ end
+
+end
+
+class HtmlConvertor
+ attr_accessor :tags
+
+ def initialize trimmed_text
+ @tags = []
+ tmp_tags = []
+ mark = Markdown.new
+ trimmed_text.split(/\n+/).each do |line|
+ tmp_tags.push mark.to_tag line
+ end
+
+ generate_tags tmp_tags
+ end
+
+ def generate_tags tags_array
+ tags_array.each do |tag|
+ case tag.type
+ when 'ul' then push_ul_list tag
+ when 'ol' then push_ol_list tag
+ when 'blockquote' then puts push_block tag
+ when 'code' then push_code tag
+ when /h\d/ then @tags.push tag.escape
+ else @tags.push tag.escape
+ end
+ end
+ end
+
+ def push_ul_list tag
+ if @tags.size == 0 then
+ return @tags.push HtmlObject.new tag.escape, 'ul'
+ end
+ if @tags.class == String || @tags.last.type !='ul' then
+ @tags.push HtmlObject.new tag.escape, 'ul'
+ else
+ @tags.last.text << +"\n "+tag.escape
+ end
+ end
+ def push_ol_list tag
+ if @tags.size == 0 then
+ return @tags.push HtmlObject.new tag.escape, 'ol'
+ end
+ if @tags.class == String || @tags.last.type !='ol' then
+ @tags.push HtmlObject.new tag.escape, 'ol'
+ else
+ @tags.last.text << tag.escape
+ end
+ end
+
+ def push_block tag
+ if @tags.size == 0 then
+ return @tags.push HtmlObject.new tag.convert_to('p'), 'blockquote'
+ end
+ if @tags.class == HtmlObject || @tags.last.type != 'blockquote' then
+ @tags.push HtmlObject.new tag.convert_to('p'), 'blockquote'
+ else
+ @tags.last.text << (tag.convert_to 'p')
+ end
+ end
+
+ def push_code tag
+ if @tags.size == 0 || @tags.last.type != 'code' then
+ @tags.push HtmlObject.new tag.to_text, ['pre','code'], 'code'
+ else
+ @tags.last.text << "\n"+tag.to_text
+ end
+ end
+
+ def convert
+ #join-ваме с \n за да ни по красив и четим html-a :)
+ @tags.map(&:to_s).join("\n\n")
+ end
+
+end
+
+
+class HtmlObject
+ attr_accessor :tag_name, :text, :type
+
+ def initialize text, tag_name, tag_type = nil
+ @tag_name = tag_name
+ @text = text
+ @type = tag_type || tag_name
+ end
+
+ def wrap_tag tag_name
+ "<#{tag_name}>#{self.escape}</#{tag_name}>"
+ end
+
+ def to_s
+ if @tag_name.class == 'str'.class then
+ "<#{@tag_name}>#{@text}</#{@tag_name}>"
+ else
+ "<#{@tag_name.join('><')}>#{@text}</#{@tag_name.reverse.join('></')}>"
+ end
+ end
+
+ def to_text
+ @text.gsub(/^( ){4}/,'')
+ end
+
+ def convert_to new_tag
+ @tag_name = new_tag
+ escape
+ end
+
+
+ def escape
+ @text = @text.gsub(/\&/,'&amp;')
+ @text = @text.gsub(/</,'&lt;')
+ @text = @text.gsub(/>/,'&gt;')
+ @text = @text.gsub(/\"/,'&quot;')
+
+ wrap_inner_tags
+ end
+ def wrap_inner_tags
+ if @text.match(/\[(.*)\]\((.*)\)/) then
+ @text = @text.gsub(/(\[(.*)\])\((.*)\)/,"<a href=\"#{$2}\">#{$1}</a>")
+ end
+ if @text.match(/(\*\*)(.+)(\*\*)/) then
+ @text = @text.gsub(/(\*\*)(.+)(\*\*)/,"<strong>#{$2}</strong>")
+ end
+ if @text.match(/(_)(.+)(_)/) then
+ @text = @text.gsub(/(_)(.+)(_)/,"<em>#{$2}</em>")
+ end
+ to_s
+ end
+end

Аз си ходя на лекции. А това за идентацията няма да го коментирам...

Всъщност ще го коментирам...

Не че незнам как трябва да го идентирам Ходя на работа по 9 часа на ден и през цялото време си пиша така, като дойда събота сядам да си го направя това домашно, и като почна да спазвам тази Ruby идентация, която искате,кода ми става супер нечетими ми и НЕпрегледен и ми отнема 2-пъти повече време. Ако имам времем след като напиша домашното ще го форматирам, ако нямам - взимаите ми точки