Class: Codeforces::Viewer::Render

Inherits:
Object
  • Object
show all
Defined in:
lib/codeforces/viewer/render.rb

Instance Method Summary collapse

Instance Method Details

#render(doc) ⇒ Object



77
78
79
80
81
82
83
84
# 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) ⇒ Object



68
69
70
71
72
73
74
75
# 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) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# 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) ⇒ Object



86
87
88
# File 'lib/codeforces/viewer/render.rb', line 86

def render_html(html)
  render(Nokogiri::HTML.fragment(html)).text
end

#render_input(input_id, doc) ⇒ Object



90
91
92
93
94
95
96
97
# 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) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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) ⇒ Object



99
100
101
102
103
104
105
106
# 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