Решение на Пета задача от Антоний Цветков

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

Към профила на Антоний Цветков

Резултати

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

Код

class HTML
def specialsymbols(text)
text.gsub!(/&/,"&")
text.gsub!(/</,"&lt;")
text.gsub!(/(.)>/,"#{$1}&gt;")
text.gsub!(/"/,"&quot;")
end
def textstyle(text)
while text.match /\*\*(.*?)\*\*/
text.sub!(/\*\*(.*)\*\*/,"<strong>#{$1}</strong>")
end
while text.match /_([^_]*)_/
text.sub!(/_([^_]*)_/,"<em>#{$1}</em>")
end
end
def links(text)
while text.match /
([^
]*)\]([^]*)\)/
text.sub!(/
[^
]*\][^]*\)/
,"<a href=\"#{$2}\">#{$1}</a>")
end
end
def headers(text)
while text.match /^#### (.*)/
text.sub!(/^#### (.*)/,"<h4>#{$1}</h4>")
end
while text.match /^### (.*)/
text.sub!(/^### (.*)/,"<h3>#{$1}</h3>")
end
while text.match /^## (.*)/
text.sub!(/^## (.*)/,"<h2>#{$1}</h2>")
end
while text.match /^# (.*)/
text.sub!(/^# (.*)/,"<h1>#{$1}</h1>")
end
end
def lists(text)
while text.match /^\d+\. (.*)$/
text.sub!(/^\d+\. (.*)$/," <li>#{$1}</li>")
end
while text.match /^\* (.*)$/
text.sub!(/^\* (.*)$/," <li>#{$1}</li>")
end
end
def code(text)
text.match /^ (.*[\n\r]+\g<1>*)/
text.sub!(/^ (.*[\n\r]+\g<1>*)/,"<pre><code>#{$1}</code></pre>")
end
def paragraph(text)
while text.match /(?!<(h[1-4])>)(.*)<\/\2>/
text.sub!(/(?!<(h[1-4])>)(.*)<\/\2>/, "<p>#{$3}</p>")
end
end
end
class Formater
def initialize(text)
@markdown=text
@html=HTML.new
end
def to_html
@html.specialsymbols @markdown
@html.textstyle @markdown
@html.links @markdown
@html.headers @markdown
@html.lists @markdown
#@html.blockquote @markdown
@html.code @markdown
@html.paragraph @markdown
end
def to_s
puts self.to_html
end
def inspect
puts @markdown
end
end
file = File.new("original.txt", "r")
while (line = file.gets) do
text="#{text}"+"#{line}"
end
file.close
test=Formater.new(text)
test.to_html
test.inspect

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

▸ Покажи лога

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

Антоний обнови решението на 23.11.2011 23:48 (преди над 13 години)

▸ Покажи разликите
+class HTML
+ def specialsymbols(text)
+ text.gsub!(/&/,"&amp;")
+ text.gsub!(/</,"&lt;")
+ text.gsub!(/(.)>/,"#{$1}&gt;")
+ text.gsub!(/"/,"&quot;")
+ end
+
+ def textstyle(text)
+ while text.match /\*\*(.*?)\*\*/
+ text.sub!(/\*\*(.*)\*\*/,"<strong>#{$1}</strong>")
+ end
+ while text.match /_([^_]*)_/
+ text.sub!(/_([^_]*)_/,"<em>#{$1}</em>")
+ end
+ end
+
+ def links(text)
+ while text.match /
([^
]*)\]([^]*)\)/
+ text.sub!(/
[^
]*\][^]*\)/
,"<a href=\"#{$2}\">#{$1}</a>")
+ end
+ end
+
+ def headers(text)
+ while text.match /^#### (.*)/
+ text.sub!(/^#### (.*)/,"<h4>#{$1}</h4>")
+ end
+ while text.match /^### (.*)/
+ text.sub!(/^### (.*)/,"<h3>#{$1}</h3>")
+ end
+ while text.match /^## (.*)/
+ text.sub!(/^## (.*)/,"<h2>#{$1}</h2>")
+ end
+ while text.match /^# (.*)/
+ text.sub!(/^# (.*)/,"<h1>#{$1}</h1>")
+ end
+ end
+
+ def lists(text)
+ while text.match /^\d+\. (.*)$/
+ text.sub!(/^\d+\. (.*)$/," <li>#{$1}</li>")
+ end
+ while text.match /^\* (.*)$/
+ text.sub!(/^\* (.*)$/," <li>#{$1}</li>")
+ end
+ end
+
+ def code(text)
+ text.match /^ (.*[\n\r]+\g<1>*)/
+ text.sub!(/^ (.*[\n\r]+\g<1>*)/,"<pre><code>#{$1}</code></pre>")
+ end
+
+ def paragraph(text)
+ while text.match /(?!<(h[1-4])>)(.*)<\/\2>/
+ text.sub!(/(?!<(h[1-4])>)(.*)<\/\2>/, "<p>#{$3}</p>")
+ end
+ end
+end
+
+class Formater
+ def initialize(text)
+ @markdown=text
+ @html=HTML.new
+ end
+
+ def to_html
+ @html.specialsymbols @markdown
+ @html.textstyle @markdown
+ @html.links @markdown
+ @html.headers @markdown
+ @html.lists @markdown
+ #@html.blockquote @markdown
+ @html.code @markdown
+ @html.paragraph @markdown
+ end
+
+
+ def to_s
+ puts self.to_html
+ end
+
+ def inspect
+ puts @markdown
+ end
+end
+
+file = File.new("original.txt", "r")
+while (line = file.gets) do
+ text="#{text}"+"#{line}"
+end
+file.close
+
+test=Formater.new(text)
+test.to_html
+test.inspect