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

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

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

Резултати

  • 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

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

/tmp/d20111129-16859-u6iskh/solution.rb:87:in `initialize': No such file or directory - original.txt (Errno::ENOENT)
	from /tmp/d20111129-16859-u6iskh/solution.rb:87:in `new'
	from /tmp/d20111129-16859-u6iskh/solution.rb:87:in `<top (required)>'
	from /data/rails/evans/releases/20111128224832/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:256:in `require'
	from /data/rails/evans/releases/20111128224832/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:256:in `block in requires='
	from /data/rails/evans/releases/20111128224832/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:256:in `map'
	from /data/rails/evans/releases/20111128224832/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:256:in `requires='
	from /data/rails/evans/releases/20111128224832/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration_options.rb:19:in `block in configure'
	from /data/rails/evans/releases/20111128224832/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration_options.rb:18:in `each'
	from /data/rails/evans/releases/20111128224832/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration_options.rb:18:in `configure'
	from /data/rails/evans/releases/20111128224832/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/command_line.rb:17:in `run'
	from /data/rails/evans/releases/20111128224832/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:80:in `run_in_process'
	from /data/rails/evans/releases/20111128224832/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:69:in `run'
	from /data/rails/evans/releases/20111128224832/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:10:in `block in autorun'

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

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

+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