Class: OpenLogCleaner::RpolDocument

Inherits:
Document
  • Object
show all
Defined in:
lib/openlogcleaner/rpol_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_io(io) ⇒ Object

/(?:Re:s*)?(w+ #1) by (.+)$/



10
11
12
13
14
15
16
# File 'lib/openlogcleaner/rpol_document.rb', line 10

def add_io(io)
  contents = io.read.split(/={10,}/)
  # remove top and tail
  contents.pop
  contents.shift
  add_messages(contents)
end

#add_messages(contents) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/openlogcleaner/rpol_document.rb', line 18

def add_messages(contents)
  contents.each do |message|
    title, body = message.split(/-{10,}/, 2)
    nick = extract_nick_and_set_title(title)
    messages << Say.new(nick, fix_body(body))
  end
end

#extra_cssObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/openlogcleaner/rpol_document.rb', line 61

def extra_css
<<-CSS
.red {
  color : #ff0000;
}
.orange {
  color : #ff6600;
}
.aqua {
  color : #339999;
}
.blue {
  color : #0000ff;
}
.darkblue {
  color : #000099;
}
.green {
  color : #009900;
}
.darkgreen {
  color : #006600;
}
.brown {
  color : #663300;
}
.purple {
  color : #990099;
}
.yellow {
  color : #cc9933;
}
.pink {
  color : #cc66cc;
}
.invis {
  color: #000000;
}
CSS
end

#extract_nick_and_set_title(title) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/openlogcleaner/rpol_document.rb', line 26

def extract_nick_and_set_title(title)
  title.gsub!('<br />','')
  title.strip!
  if title =~ /(?:Re:\s*)?(\w+ #1) by (.+)$/
    self.title = $1
    $2 # return the nick
  else
    raise ArgumentError, "Not a valid title string"
  end
end

#fix_body(body) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/openlogcleaner/rpol_document.rb', line 37

def fix_body(body)
  # avoid unicode issues by making &nbsp; a normal space
  body.gsub!('&nbsp;',' ')
  # make lots of dashes into a hr
  body.gsub!(/-{6,}/,'<hr />')
  doc = Nokogiri::HTML.parse(body)

  # remove edit info
  doc.css('p.messsage_information').remove

  # trim the first br
  doc.at('body').children.at('br').remove

  # fix font tags
  doc.search('font').each do |font|
    font.name = 'span'
  end
  # convert back to HTML
  text = doc.at('body').children.to_xhtml
  # fix trailing brs
  text.gsub!(/(\s*<br \/>)+\z/m, '')
  text
end