Решение на Пета задача от Васил Гочев

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

Към профила на Васил Гочев

Резултати

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

Код

# Наполовина работеща грозна чернова. Има доста какво да се желае относно оптимизация
module TagOperations
def open(tag)
"<#{tag}>"
end
def close(tag)
"</#{tag}>"
end
def encase(string, tag)
open(tag) + string + close(tag)
end
def escape(string)
unless string.match(/^[<>]/)
string.gsub(/[&<>"]/, '&' => '&amp;', '<' => '&lt;', '>' => '&gt;', '"' => '&quot;')
else
string
end
end
def get_heading(element)
element.match(/(^#+)/)
"h" + $1.length.to_s
end
def render_bold_italic(text)
if text.match(/[\*][\*](.+)[\*][\*]/)
text = text.gsub(/[\*][\*](.+)[\*][\*]/, "<strong>" + $1 + "</strong>")
end
if text.match(/_(.+)_/)
text = text.gsub(/_(.+)_/, "<em>" + $1 + "</em>")
end
text
end
end
class Line
attr_accessor :line, :type
def initialize(string)
@line = string
check_line
end
def check_line
if code? then @type = "code"
elsif empty? then @type = "empty"
elsif heading? then @type = "heading"
elsif blockquote? then @type = "blockquote"
elsif ordered_list? then @type = "ordered_list"
elsif unordered_list? then @type = "unordered list"
else @type = "paragraph"
end
end
def empty?
@line.match(/\A\s*\z/)
end
def heading?
@line.match(/(^\#{1,4})\s+\p{Alnum}+/)
end
def blockquote?
@line.match(/(^>\s+)/)
end
def ordered_list?
@line.match(/^[1-9][0-9]*\.\s+/)
end
def unordered_list?
@line.match(/^\*\s+/)
end
def code?
@line.match(/^\s{4,}/)
end
end
class Paragraph
include TagOperations
def initialize(prev_line, prev_element, next_line, next_element, full_text, delimiter)
@prev_line = prev_line
@prev_element = prev_element
@next_line = next_line
@next_element = next_element
@full_text = full_text
@delimiter = delimiter
end
def apply_paragraph
if @prev_line.type == "paragraph" and @prev_element != "start"
text = render_bold_italic(escape(@full_text.strip))
else
text = open("p") + render_bold_italic(escape(@full_text.strip))
end
if @next_element == "end" then @delimiter = "" end
if @next_line.type != "paragraph" or @next_element == "end"
text += close("p")
end
text + @delimiter
end
end
class Code
include TagOperations
def initialize(prev_line, prev_element, next_line, next_element, full_text, delimiter)
@prev_line = prev_line
@prev_element = prev_element
@next_line = next_line
@next_element = next_element
@full_text = full_text
@delimiter = delimiter
end
def apply_code
if @prev_line.type == "code" and @prev_element != "start"
text = escape(@full_text.strip)
else
text = open("pre") + open("code") + escape(@full_text.strip)
end
if @next_element == "end" then @delimiter = "" end
if @next_line.type != "code" or @next_element == "end"
text += close("code") + close("pre")
end
text + @delimiter
end
end
class Quote
include TagOperations
def initialize(prev_line, prev_element, next_line, next_element, full_text, delimiter)
@prev_line = prev_line
@prev_element = prev_element
@next_line = next_line
@next_element = next_element
@full_text = full_text
@delimiter = delimiter
end
def apply_quote
@full_text.match(/>\s+(.+)/)
if @prev_line.type == "blockquote" and @prev_element != "start"
if $1 then text = $1 else text = "" end
else
text = open("blockquote") + open("p") + $1
end
if @next_element == "end" then @delimiter = "" end
if @next_line.type != "blockquote" or @next_element == "end"
text += close("p") + close("blockquote")
end
text + @delimiter
end
end
class LineItem
include TagOperations
def initialize(array, index)
init_elements(array, index)
init_lines
end
def init_elements(array, index)
@prev_element = index == 0 ? "start" : array[index - 1]
@next_element = array[index + 1] ? array[index + 1] : "end"
@element = array[index]
end
def init_lines
@prev_line = Line.new(@prev_element)
@line = Line.new(@element)
@next_line = Line.new(@next_element)
end
def check_line
@element.match(/(.+)(\z|\n)/)
line_type($1, $2)
end
def line_type(full_text, delimiter)
case @line.type
when "heading" then heading(full_text, delimiter)
when "paragraph" then paragraph(full_text, delimiter)
when "empty" then "\n"
when "code" then code(full_text, delimiter)
when "blockquote" then quote(full_text, delimiter)
end
end
def quote(text, delim)
quote = Quote.new(@prev_line, @prev_element, @next_line, @next_element, text, delim)
quote.apply_quote
end
def code(text, delim)
code = Code.new(@prev_line, @prev_element, @next_line, @next_element, text, delim)
code.apply_code
end
def paragraph(text, delim)
p = Paragraph.new(@prev_line, @prev_element, @next_line, @next_element, text, delim)
p.apply_paragraph
end
def heading(full_text, delimiter)
full_text.match(/\s+(\p{Alnum}.*)/)
encase(render_bold_italic(escape($1)), get_heading(@element)) + delimiter
end
end
class Formatter
attr_accessor :raw
include TagOperations
def initialize(raw_string)
@raw = raw_string
end
def to_html
result = []
@raw.lines.each_with_index { |element, index| result[index] = element }
iterate_lines(result)
end
def iterate_lines(array)
result_string = ""
array.each_index do |index|
item = LineItem.new(array, index)
result_string += item.check_line
end
result_string
end
alias :to_s :to_html
def inspect
@raw
end
end

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

.......F......F....FFFFF..FFFFFFFF.FFF.FFFFF..F.FFFFF...F

Failures:

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

  2) 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 &lt;stdio.h&gt;\n\nint main(int, char**) {\n// 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.\nreturn 42;\n}</code></pre>"
       
       (compared using ==)
       
       Diff:
       @@ -3,7 +3,7 @@
        include &lt;stdio.h&gt;
        
        int main(int, char**) {
       -	// Whitespace след четирите задължителни интервала в началото, се запазва винаги.
       -	return 42;
       +// Whitespace след четирите задължителни интервала в началото, се запазва винаги.
       +return 42;
        }</code></pre>
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/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)>'

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

  4) Formatter blockquotes escapes special entities
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<blockquote><p>Cuttin &amp; Pastin, w/o Quotin (&quot;)</p></blockquote>"
            got: "<blockquote><p>Cuttin & Pastin, w/o Quotin (\")</p></blockquote>"
       
       (compared using ==)
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:272: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 links renders simple links
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p><a href=\"http://fmi.ruby.bg/\">Programming in Ruby</a></p>"
            got: "<p>[Programming in Ruby](http://fmi.ruby.bg/)</p>"
       
       (compared using ==)
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:278: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 links renders properly Unicode ones
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     Encoding::CompatibilityError:
       incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)
     # /tmp/d20111129-16859-1f1db6i/solution.rb:66:in `match'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:66:in `match'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:66:in `heading?'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:53:in `check_line'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:47:in `initialize'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:198:in `new'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:198:in `init_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:187:in `initialize'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:258:in `new'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:258:in `block in iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `each_index'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:251:in `to_html'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:282: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 first](some-url) and [Second](another-url).</p>"
       
       (compared using ==)
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/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 escapes special entities in the link description
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p>Also testing <a href=\"here\">special &amp; &quot;entities&quot; &lt;b&gt;</a>.</p>"
            got: "<p>Also testing [special &amp; &quot;entities&quot; &lt;b&gt;](here).</p>"
       
       (compared using ==)
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:304: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 links escapes special entities in the link URL
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p>Or <a href=\"special &amp; &quot;entities&quot; &lt;b&gt;\">what if</a> are in the URL, eh?</p>"
            got: "<p>Or [what if](special &amp; &quot;entities&quot; &lt;b&gt;) are in the URL, eh?</p>"
       
       (compared using ==)
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:309: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 lists renders simple unordered lists
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     Encoding::CompatibilityError:
       incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)
     # /tmp/d20111129-16859-1f1db6i/solution.rb:66:in `match'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:66:in `match'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:66:in `heading?'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:53:in `check_line'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:47:in `initialize'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:198:in `new'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:198:in `init_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:187:in `initialize'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:258:in `new'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:258:in `block in iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `each_index'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:251:in `to_html'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/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)>'

  11) Formatter lists renders simple ordered lists
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     Encoding::CompatibilityError:
       incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)
     # /tmp/d20111129-16859-1f1db6i/solution.rb:66:in `match'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:66:in `match'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:66:in `heading?'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:53:in `check_line'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:47:in `initialize'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:198:in `new'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:198:in `init_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:187:in `initialize'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:258:in `new'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:258:in `block in iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `each_index'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:251:in `to_html'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/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)>'

  12) Formatter lists renders lists with a single item
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     TypeError:
       can't convert nil into String
     # /tmp/d20111129-16859-1f1db6i/solution.rb:259:in `+'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:259:in `block in iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `each_index'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:251:in `to_html'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/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)>'

  13) Formatter lists does not choke on malformed lists
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     Encoding::CompatibilityError:
       incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)
     # /tmp/d20111129-16859-1f1db6i/solution.rb:66:in `match'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:66:in `match'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:66:in `heading?'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:53:in `check_line'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:47:in `initialize'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:198:in `new'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:198:in `init_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:187:in `initialize'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:258:in `new'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:258:in `block in iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `each_index'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:251:in `to_html'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/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)>'

  14) Formatter lists escapes special entities in the list elements
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     TypeError:
       can't convert nil into String
     # /tmp/d20111129-16859-1f1db6i/solution.rb:259:in `+'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:259:in `block in iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `each_index'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:251:in `to_html'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/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)>'

  15) Formatter lists allows links in list elements
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     TypeError:
       can't convert nil into String
     # /tmp/d20111129-16859-1f1db6i/solution.rb:259:in `+'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:259:in `block in iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `each_index'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:251:in `to_html'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/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)>'

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

  17) Formatter bold and italic text rendering works in headers
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<h1><em>Simplest</em> case</h1>"
            got: "<p># <em>Simplest</em> case</p>"
       
       (compared using ==)
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:665:in `block in expect_transformations'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:664:in `each'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:664:in `expect_transformations'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:447: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 bold and italic text rendering works in blockquotes
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<blockquote><p><em>Simplest</em> case</p></blockquote>"
            got: "<blockquote><p>_Simplest_ case</p></blockquote>"
       
       (compared using ==)
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:665:in `block in expect_transformations'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:664:in `each'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:664:in `expect_transformations'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:455: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 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 [<em>more words here_ _to be</em> <strong>emphasized</strong>](okay)?</p>"
       
       (compared using ==)
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/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)>'

  20) Formatter bold and italic text rendering works in list elements
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     TypeError:
       can't convert nil into String
     # /tmp/d20111129-16859-1f1db6i/solution.rb:259:in `+'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:259:in `block in iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `each_index'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:251:in `to_html'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/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)>'

  21) Formatter bold and italic text rendering works in links in list elements
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     TypeError:
       can't convert nil into String
     # /tmp/d20111129-16859-1f1db6i/solution.rb:259:in `+'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:259:in `block in iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `each_index'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:251:in `to_html'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/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)>'

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

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

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

  26) Formatter special entities escapes them in deeply-nested elements
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<h2><em>&quot;Black &amp; Decker&quot;</em>!</h2>"
            got: "<p>## <em>&quot;Black &amp; Decker&quot;</em>!</p>"
       
       (compared using ==)
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:665:in `block in expect_transformations'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:664:in `each'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:664:in `expect_transformations'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:536: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 removes excess leading and trailing whitespace
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<p>Some txt</p>"
            got: "\n\n<p>Some txt</p>"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,4 @@
       +
       +
        <p>Some txt</p>
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:665:in `block in expect_transformations'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:664:in `each'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:664:in `expect_transformations'
     # /tmp/d20111129-16859-1f1db6i/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)>'

  28) Formatter whitespace ignores leading and trailing whitespace of lines whenever possible
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
       
       expected: "<h1>Test with a header</h1>"
            got: "<p>#    Test with a header</p>\n\n"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -<h1>Test with a header</h1>
       +<p>#    Test with a header</p>
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:665:in `block in expect_transformations'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:664:in `each'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:664:in `expect_transformations'
     # /tmp/d20111129-16859-1f1db6i/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)>'

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

  30) Formatter mixed, complex input renders properly
     Failure/Error: Formatter.new(plain).to_html.should eq formatted.strip
     Encoding::CompatibilityError:
       incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)
     # /tmp/d20111129-16859-1f1db6i/solution.rb:66:in `match'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:66:in `match'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:66:in `heading?'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:53:in `check_line'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:47:in `initialize'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:198:in `new'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:198:in `init_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:187:in `initialize'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:258:in `new'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:258:in `block in iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `each_index'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:257:in `iterate_lines'
     # /tmp/d20111129-16859-1f1db6i/solution.rb:251:in `to_html'
     # /tmp/d20111129-16859-1f1db6i/spec.rb:660:in `expect_transformation'
     # /tmp/d20111129-16859-1f1db6i/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.61845 seconds
57 examples, 30 failures

Failed examples:

rspec /tmp/d20111129-16859-1f1db6i/spec.rb:99 # Formatter headers renders tricky ones
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:163 # Formatter code blocks renders properly a longer example with tabs and Unicode
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:255 # Formatter blockquotes renders multiline ones with multiple paragraphs
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:271 # Formatter blockquotes escapes special entities
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:277 # Formatter links renders simple links
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:281 # Formatter links renders properly Unicode ones
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:285 # Formatter links allows multiple links on a single line
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:303 # Formatter links escapes special entities in the link description
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:308 # Formatter links escapes special entities in the link URL
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:315 # Formatter lists renders simple unordered lists
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:333 # Formatter lists renders simple ordered lists
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:351 # Formatter lists renders lists with a single item
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:377 # Formatter lists does not choke on malformed lists
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:397 # Formatter lists escapes special entities in the list elements
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:413 # Formatter lists allows links in list elements
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:437 # Formatter bold and italic text rendering allows multiple ones per line
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:442 # Formatter bold and italic text rendering works in headers
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:450 # Formatter bold and italic text rendering works in blockquotes
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:463 # Formatter bold and italic text rendering works in links
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:468 # Formatter bold and italic text rendering works in list elements
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:479 # Formatter bold and italic text rendering works in links in list elements
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:490 # Formatter bold and italic text rendering does not brake HTML entities inside it
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:495 # Formatter bold and italic text rendering does not allow parial overlapping
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:515 # Formatter special entities escapes them in headers
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:527 # Formatter special entities escapes them in blockquotes
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:533 # Formatter special entities escapes them in deeply-nested elements
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:541 # Formatter whitespace removes excess leading and trailing whitespace
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:550 # Formatter whitespace ignores leading and trailing whitespace of lines whenever possible
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:558 # Formatter whitespace does not touch trailing whitespace in code blocks
rspec /tmp/d20111129-16859-1f1db6i/spec.rb:586 # Formatter mixed, complex input renders properly

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

Васил обнови решението на 23.11.2011 22:34 (преди почти 13 години)

+# Наполовина работеща грозна чернова. Има доста какво да се желае относно оптимизация
+
+module TagOperations
+ def open(tag)
+ "<#{tag}>"
+ end
+
+ def close(tag)
+ "</#{tag}>"
+ end
+
+ def encase(string, tag)
+ open(tag) + string + close(tag)
+ end
+
+ def escape(string)
+ unless string.match(/^[<>]/)
+ string.gsub(/[&<>"]/, '&' => '&amp;', '<' => '&lt;', '>' => '&gt;', '"' => '&quot;')
+ else
+ string
+ end
+ end
+
+ def get_heading(element)
+ element.match(/(^#+)/)
+ "h" + $1.length.to_s
+ end
+
+ def render_bold_italic(text)
+ if text.match(/[\*][\*](.+)[\*][\*]/)
+ text = text.gsub(/[\*][\*](.+)[\*][\*]/, "<strong>" + $1 + "</strong>")
+ end
+
+ if text.match(/_(.+)_/)
+ text = text.gsub(/_(.+)_/, "<em>" + $1 + "</em>")
+ end
+
+ text
+ end
+end
+
+class Line
+ attr_accessor :line, :type
+
+ def initialize(string)
+ @line = string
+ check_line
+ end
+
+ def check_line
+ if code? then @type = "code"
+ elsif empty? then @type = "empty"
+ elsif heading? then @type = "heading"
+ elsif blockquote? then @type = "blockquote"
+ elsif ordered_list? then @type = "ordered_list"
+ elsif unordered_list? then @type = "unordered list"
+ else @type = "paragraph"
+ end
+ end
+
+ def empty?
+ @line.match(/\A\s*\z/)
+ end
+
+ def heading?
+ @line.match(/(^\#{1,4})\s+\p{Alnum}+/)
+ end
+
+ def blockquote?
+ @line.match(/(^>\s+)/)
+ end
+
+ def ordered_list?
+ @line.match(/^[1-9][0-9]*\.\s+/)
+ end
+
+ def unordered_list?
+ @line.match(/^\*\s+/)
+ end
+
+ def code?
+ @line.match(/^\s{4,}/)
+ end
+
+end
+
+class Paragraph
+
+ include TagOperations
+
+ def initialize(prev_line, prev_element, next_line, next_element, full_text, delimiter)
+ @prev_line = prev_line
+ @prev_element = prev_element
+ @next_line = next_line
+ @next_element = next_element
+ @full_text = full_text
+ @delimiter = delimiter
+ end
+
+ def apply_paragraph
+ if @prev_line.type == "paragraph" and @prev_element != "start"
+ text = render_bold_italic(escape(@full_text.strip))
+ else
+ text = open("p") + render_bold_italic(escape(@full_text.strip))
+ end
+
+ if @next_element == "end" then @delimiter = "" end
+
+ if @next_line.type != "paragraph" or @next_element == "end"
+ text += close("p")
+ end
+
+ text + @delimiter
+ end
+
+end
+
+class Code
+
+ include TagOperations
+
+ def initialize(prev_line, prev_element, next_line, next_element, full_text, delimiter)
+ @prev_line = prev_line
+ @prev_element = prev_element
+ @next_line = next_line
+ @next_element = next_element
+ @full_text = full_text
+ @delimiter = delimiter
+ end
+
+ def apply_code
+ if @prev_line.type == "code" and @prev_element != "start"
+ text = escape(@full_text.strip)
+ else
+ text = open("pre") + open("code") + escape(@full_text.strip)
+ end
+
+ if @next_element == "end" then @delimiter = "" end
+
+ if @next_line.type != "code" or @next_element == "end"
+ text += close("code") + close("pre")
+ end
+
+ text + @delimiter
+ end
+
+end
+
+class Quote
+
+ include TagOperations
+
+ def initialize(prev_line, prev_element, next_line, next_element, full_text, delimiter)
+ @prev_line = prev_line
+ @prev_element = prev_element
+ @next_line = next_line
+ @next_element = next_element
+ @full_text = full_text
+ @delimiter = delimiter
+ end
+
+ def apply_quote
+ @full_text.match(/>\s+(.+)/)
+
+ if @prev_line.type == "blockquote" and @prev_element != "start"
+ if $1 then text = $1 else text = "" end
+ else
+ text = open("blockquote") + open("p") + $1
+ end
+
+ if @next_element == "end" then @delimiter = "" end
+
+ if @next_line.type != "blockquote" or @next_element == "end"
+ text += close("p") + close("blockquote")
+ end
+
+ text + @delimiter
+ end
+
+end
+
+class LineItem
+ include TagOperations
+
+ def initialize(array, index)
+ init_elements(array, index)
+ init_lines
+ end
+
+ def init_elements(array, index)
+ @prev_element = index == 0 ? "start" : array[index - 1]
+ @next_element = array[index + 1] ? array[index + 1] : "end"
+ @element = array[index]
+ end
+
+ def init_lines
+ @prev_line = Line.new(@prev_element)
+ @line = Line.new(@element)
+ @next_line = Line.new(@next_element)
+ end
+
+ def check_line
+ @element.match(/(.+)(\z|\n)/)
+ line_type($1, $2)
+ end
+
+ def line_type(full_text, delimiter)
+ case @line.type
+ when "heading" then heading(full_text, delimiter)
+ when "paragraph" then paragraph(full_text, delimiter)
+ when "empty" then "\n"
+ when "code" then code(full_text, delimiter)
+ when "blockquote" then quote(full_text, delimiter)
+ end
+ end
+
+ def quote(text, delim)
+ quote = Quote.new(@prev_line, @prev_element, @next_line, @next_element, text, delim)
+ quote.apply_quote
+ end
+
+ def code(text, delim)
+ code = Code.new(@prev_line, @prev_element, @next_line, @next_element, text, delim)
+ code.apply_code
+ end
+
+ def paragraph(text, delim)
+ p = Paragraph.new(@prev_line, @prev_element, @next_line, @next_element, text, delim)
+ p.apply_paragraph
+ end
+
+ def heading(full_text, delimiter)
+ full_text.match(/\s+(\p{Alnum}.*)/)
+ encase(render_bold_italic(escape($1)), get_heading(@element)) + delimiter
+ end
+end
+
+class Formatter
+ attr_accessor :raw
+
+ include TagOperations
+
+ def initialize(raw_string)
+ @raw = raw_string
+ end
+
+ def to_html
+ result = []
+ @raw.lines.each_with_index { |element, index| result[index] = element }
+
+ iterate_lines(result)
+ end
+
+ def iterate_lines(array)
+ result_string = ""
+
+ array.each_index do |index|
+ item = LineItem.new(array, index)
+ result_string += item.check_line
+ end
+ result_string
+ end
+
+ alias :to_s :to_html
+
+ def inspect
+ @raw
+ end
+end