Решение на Пета задача от Недислав Стойчев

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

Към профила на Недислав Стойчев

Резултати

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

Код

class Formatter
attr_accessor :input_text
attr_accessor :raw_text
attr_accessor :html
attr_reader :major_split
attr_reader :symbols
def initialize(text)
@major_split = MajorSplit.new
@symbols = Symbols.new
@input_text = text
@raw_text = text.strip.split("\n\n")
@html = []
end
def to_html
raw_text.each do |x|
if x.include? "#"
major_split.headers_split x, html
else
major_split.blockquotes x.strip, html
major_split.paragraph x.strip,html
end
end
return to_html_concat(html)
end
def to_s
to_html
end
def inspect
input_text
end
def to_html_concat(html)
output = ""
html.each_index do |x|
if x >= 1
output << "\n\n"+ html[x]
else
output << html[x]
end
end
output
end
end
class MajorSplit
attr_reader :symbols
def initialize
@symbols = Symbols.new
end
def paragraph(line, html)
if match = /^.?.?(([A-Z](.)+.+)\n?)+/.match(line)
s = symbols.special_sym match
html << "<p>#{s.strip}</p>"
elsif match = /^\#{1,4}\s{1,4}.+[A-Za-z].+/.match(line)
headers line, html
else
html << "<p>#{line.strip!}</p>"
end
end
def headers(line, html)
if match = /^\#{,4}\s{1,4}.+[A-Za-z].+/.match(line)
count = match.to_s.count "#"
s = symbols.special_sym match
html << "<h#{count}>#{s.delete("#").lstrip}</h#{count}>"
else
html << "<p>#{line.strip!}</p>"
end
end
def headers_split(line, html)
line.split("\n").each do |y|
paragraph y,html
end
end
def precode(line, html)
p line
if match = /(^\s{4}(.+)).?/m.match(line)
html << "<pre><code>#{match.to_s.delete(/^\s{4}/)}</pre></code>"
end
end
def blockquotes(line, html)
if match = /^> (\w+.+)\n?/m.match(line)
html << "<blockquote>#{match}</blockquote>"
end
end
def lists(line, html)
end
end
class Symbols
def links(line)
if /(.+)
(\w+.+)/
.match(line.to_s)
p line.gsub!(/\w+.+
\w+.+/
, "<a href=\"#{$2}\">#{$1}</a>")
end
return line
end
def fonts(line)
if /\*\*(.+)\*\*/.match(line.to_s)
line.gsub!(/\*\*.+\*\*/, "<strong>#{$1}</strong>")
end
if /_(.+)_/.match(line.to_s) then line.gsub!(/_.+_/, "<em>#{$1}</em>") end
links line
end
def special_sym(line)
match = line.to_s
if match.include? "&" then match.gsub!("&", "&amp;") end
if match.include? "<" then match.gsub!("<", "&lt;") end
if match.include? ">" then match.gsub!(">", "&gt;") end
if match.include? "\"" then match.gsub!("\"", "&quot;") end
fonts match
end
def to_symbols(line)
special_sym line
end
end

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

▸ Покажи лога

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

Недислав обнови решението на 23.11.2011 17:54 (преди над 13 години)

▸ Покажи разликите
+class Formatter
+ attr_accessor :input_text
+ attr_accessor :raw_text
+ attr_accessor :html
+ attr_reader :major_split
+ attr_reader :symbols
+
+ def initialize(text)
+ @major_split = MajorSplit.new
+ @symbols = Symbols.new
+ @input_text = text
+ @raw_text = text.strip.split("\n\n")
+ @html = []
+ end
+
+ def to_html
+ raw_text.each do |x|
+ if x.include? "#"
+ major_split.headers_split x, html
+ else
+ major_split.blockquotes x.strip, html
+ major_split.paragraph x.strip,html
+ end
+ end
+ return to_html_concat(html)
+ end
+
+
+ def to_s
+ to_html
+ end
+
+ def inspect
+ input_text
+ end
+
+ def to_html_concat(html)
+ output = ""
+ html.each_index do |x|
+ if x >= 1
+ output << "\n\n"+ html[x]
+ else
+ output << html[x]
+ end
+ end
+ output
+ end
+end
+
+
+class MajorSplit
+ attr_reader :symbols
+
+ def initialize
+ @symbols = Symbols.new
+ end
+
+ def paragraph(line, html)
+ if match = /^.?.?(([A-Z](.)+.+)\n?)+/.match(line)
+ s = symbols.special_sym match
+ html << "<p>#{s.strip}</p>"
+ elsif match = /^\#{1,4}\s{1,4}.+[A-Za-z].+/.match(line)
+ headers line, html
+ else
+ html << "<p>#{line.strip!}</p>"
+ end
+ end
+
+ def headers(line, html)
+ if match = /^\#{,4}\s{1,4}.+[A-Za-z].+/.match(line)
+ count = match.to_s.count "#"
+ s = symbols.special_sym match
+ html << "<h#{count}>#{s.delete("#").lstrip}</h#{count}>"
+ else
+ html << "<p>#{line.strip!}</p>"
+ end
+ end
+
+ def headers_split(line, html)
+ line.split("\n").each do |y|
+ paragraph y,html
+ end
+ end
+
+ def precode(line, html)
+ p line
+ if match = /(^\s{4}(.+)).?/m.match(line)
+ html << "<pre><code>#{match.to_s.delete(/^\s{4}/)}</pre></code>"
+ end
+ end
+
+ def blockquotes(line, html)
+ if match = /^> (\w+.+)\n?/m.match(line)
+ html << "<blockquote>#{match}</blockquote>"
+ end
+ end
+
+ def lists(line, html)
+ end
+
+end
+
+class Symbols
+
+ def links(line)
+ if /(.+)(\w+.+)/.match(line.to_s)
+ p line.gsub!(/\w+.+\w+.+/, "<a href=\"#{$2}\">#{$1}</a>")
+ end
+ return line
+ end
+
+ def fonts(line)
+ if /\*\*(.+)\*\*/.match(line.to_s)
+ line.gsub!(/\*\*.+\*\*/, "<strong>#{$1}</strong>")
+ end
+ if /_(.+)_/.match(line.to_s) then line.gsub!(/_.+_/, "<em>#{$1}</em>") end
+ links line
+ end
+
+ def special_sym(line)
+ match = line.to_s
+ if match.include? "&" then match.gsub!("&", "&amp;") end
+ if match.include? "<" then match.gsub!("<", "&lt;") end
+ if match.include? ">" then match.gsub!(">", "&gt;") end
+ if match.include? "\"" then match.gsub!("\"", "&quot;") end
+ fonts match
+ end
+
+ def to_symbols(line)
+ special_sym line
+ end
+end