class Codeforces::Viewer::Render

Public Instance Methods

render(doc) click to toggle source
# File lib/codeforces/viewer/render.rb, line 77
def render(doc)
  if doc.children.length > 0
    doc.children.map {|item| render item }
  end
  doc = render_normal_tag(doc)
  doc = render_header_class(doc)
  doc
end
render_header(doc) click to toggle source
# File lib/codeforces/viewer/render.rb, line 68
def render_header(doc)
  doc = render_header_class(doc)
  if doc.children.length > 0
    doc.children.map {|item| render_header item }
  end
  doc = render_normal_tag(doc)
  doc
end
render_header_class(doc) click to toggle source
# File lib/codeforces/viewer/render.rb, line 56
def render_header_class(doc)
  case doc["class"]
  when "title", "section-title"
    doc.content = "# #{doc.text.strip}".colorize(:mode => :bold)
  when "tex-span", "tex-font-style-tt"
    doc.content = doc.text.strip.colorize(:mode => :bold)
  when "property-title"
    doc.content = doc.text.strip.colorize(:mode => :underline)
  end
  doc
end
render_html(html) click to toggle source
# File lib/codeforces/viewer/render.rb, line 86
def render_html(html)
  render(Nokogiri::HTML.fragment(html)).text
end
render_input(input_id, doc) click to toggle source
# File lib/codeforces/viewer/render.rb, line 90
def render_input(input_id, doc)
  input_id = input_id.to_i
  if input_id == 0
    doc.xpath('./div[@class="input"]/pre').map {|item| render item }
  else
    [render(doc.xpath('./div[@class="input"]/pre')[input_id - 1])]
  end
end
render_list(depth, doc) click to toggle source
# File lib/codeforces/viewer/render.rb, line 25
def render_list(depth, doc)
  case doc.name
  when "ol"
    doc = render_list_item(depth + 2, doc, true)
  when "ul"
    doc = render_list_item(depth + 2, doc)
  end
  doc.content = "#{doc.text}\n"
  doc
end
render_list_item(depth, doc, number = false) click to toggle source
# File lib/codeforces/viewer/render.rb, line 7
def render_list_item(depth, doc, number = false)
  cnt = 0
  doc.children.each do |item|
    case item.name
    when "ol", "ul"
      item = render_list(depth, item)
    when "li"
      if number
        cnt += 1
        item.content = "#{" " * depth}#{cnt}. #{item.text}\n"
      else
        item.content = "#{" " * depth}* #{item.text}\n"
      end
    end
  end
  doc
end
render_normal_tag(doc) click to toggle source
# File lib/codeforces/viewer/render.rb, line 36
def render_normal_tag(doc)
  case doc.name
  when "div", "pre"
    doc.content = "\n#{doc.content}\n"
  when "p"
    doc.content = "\n\n#{doc.content}\n\n"
  when "ol"
    doc = render_list(0, doc)
  when "br"
    doc.content = "\n"
  when "sup"
    doc.content = "^#{doc.content}"
  when "sub"
    doc.content = "[#{doc.content}]"
  when "img"
    doc.content = "\n!! Image File: #{doc["src"]}\n\n"
  end
  doc
end
render_output(output_id, doc) click to toggle source
# File lib/codeforces/viewer/render.rb, line 99
def render_output(output_id, doc)
  output_id = output_id.to_i
  if output_id == 0
    doc.xpath('./div[@class="output"]/pre').map {|item| render item }
  else
    [render(doc.xpath('./div[@class="output"]/pre')[output_id - 1])]
  end
end