Class: OpenLogCleaner::HtmlDocument

Inherits:
Document
  • Object
show all
Defined in:
lib/openlogcleaner/html_document.rb

Instance Attribute Summary

Attributes inherited from Document

#files, #messages, #title

Instance Method Summary collapse

Methods inherited from Document

#initialize

Constructor Details

This class inherits a constructor from OpenLogCleaner::Document

Instance Method Details

#add_count(msg) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/openlogcleaner/html_document.rb', line 97

def add_count(msg)
  if @count_id
    @id_counter = @id_counter + 1
    msg.count = @id_counter
    msg
  else
    msg
  end
end

#add_emote(msg) ⇒ Object



90
91
92
# File 'lib/openlogcleaner/html_document.rb', line 90

def add_emote(msg)
  add_message(Emote.from_html(msg))
end

#add_file(file) ⇒ Object



8
9
10
# File 'lib/openlogcleaner/html_document.rb', line 8

def add_file(file)
  File.open(file) { |f| add_io(f) }
end

#add_io(io) ⇒ Object



12
13
14
15
# File 'lib/openlogcleaner/html_document.rb', line 12

def add_io(io)
  doc = Nokogiri.parse(io)
  add_messages(doc)
end

#add_list_entry(dt, dd) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/openlogcleaner/html_document.rb', line 56

def add_list_entry(dt, dd)
  name = dt.inner_text
  if dd[:style] =~ /color: (#[a-f0-9]{6});/i
    color = $1
  else
    raise "No color"
  end
  content = dd.inner_html
  content.strip!

  case dt[:class]
  when /say/, /ooc/
    add_message Say.new(name, content, color)
  when /emote/
    add_message Emote.new(name, content, color)
  else
    raise "Unknown class #{dt[:class].inspect}"
  end
end

#add_message(msg) ⇒ Object



93
94
95
# File 'lib/openlogcleaner/html_document.rb', line 93

def add_message(msg)
  messages << add_count(msg)
end

#add_messages(doc) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/openlogcleaner/html_document.rb', line 24

def add_messages(doc)
  if (dl = doc.at('dl'))
    elements = dl.children.to_a
    elements.delete_if { |elem| Nokogiri::XML::Text === elem }
    until elements.empty?
      dt, dd = elements.shift, elements.shift
      add_list_entry(dt, dd)
    end
  else
    doc.css('div').each do |msg|
      begin
        case msg['class']
        when 'post'
          add_post(msg)
        when 'emote'
          add_emote(msg)
        when 'info', 'system'
          next
        else
          # warn about an unknown div, unless it's the container div, which is
          # element conversations get wrapped in by this code.
          # This allows multiple passes, should this code be updated.
          raise "Unknown div class '#{msg['class']}'" unless msg['id'] == 'container'
        end
      rescue Exception => e
        warn msg.inspect
        raise e
      end
    end
  end
end

#add_post(msg) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/openlogcleaner/html_document.rb', line 76

def add_post(msg)
  if msg.children.size == 1
    first_childs_first_node = msg.children.first.children.first
    if !first_childs_first_node.nil? and first_childs_first_node.text? and first_childs_first_node.text =~ /\A\*\*/
      add_message(Emote.from_html(msg))
    end
    return
  end
  return if msg.at('table') # deal with welcome messages

  # fix the nick
  add_message(Say.from_html(msg))
end

#replace_brs(doc) ⇒ Object



17
18
19
20
21
22
# File 'lib/openlogcleaner/html_document.rb', line 17

def replace_brs(doc)
  doc.css('br').each do |br|
    br.name = "<br />"
  end
  doc
end