Class: Qipowl::HtmlDoc

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/qipowl/bowlers/htmldoc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapping) ⇒ HtmlDoc

Returns a new instance of HtmlDoc.



13
14
15
16
17
18
19
20
# File 'lib/qipowl/bowlers/htmldoc.rb', line 13

def initialize mapping
  @mapping = mapping
  @inside = nil
  @collector = {}
  @tags = {:inplace => {}, :linewide => {}}
  @qp = ''
  @level = 0
end

Instance Attribute Details

#qpObject (readonly)

Returns the value of attribute qp.



12
13
14
# File 'lib/qipowl/bowlers/htmldoc.rb', line 12

def qp
  @qp
end

#tagsObject (readonly)

Returns the value of attribute tags.



12
13
14
# File 'lib/qipowl/bowlers/htmldoc.rb', line 12

def tags
  @tags
end

Instance Method Details

#characters(str) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/qipowl/bowlers/htmldoc.rb', line 89

def characters str
  case @inside 
  when :a, :dfn, :abbr
    @collector[:text] = str
  else
    @qp += str
  end
end

#end_element(name) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/qipowl/bowlers/htmldoc.rb', line 98

def end_element name
  @qp +=  case name.to_sym
          when :p, :div
            @collector.delete(name.to_sym)
            "\n\n"
          when :a
            @inside = nil
            (href= @collector.delete(:href)) ?
              " #{(@collector.delete(:text) || '').gsub(/\s+/, "\u{00A0}")}¹#{fix_href href} " :
              "☇ #{@collector.delete(:name)} #{@collector.delete(:text)}"
          when :dfn, :abbr, :cite
            @inside = nil
            result = " #{@collector.delete(:text).gsub(/\s+/, "\u{00A0}")}†#{@collector.delete(:title)}† " rescue ''
            result
          when :ul, :ol, :table, :dl
            @inside = nil
            "\n"
          when :li then "\n"
          when :pre then "\nΛ\n\n"
          when :b, :strong then "≡"
          when :i, :em, :nobr then "≈"
          when :u then "▁"
          when :dd then "\n"
          when :strike, :del, :s then "─"
          when :small then "↓"
          when :code, :tt then "λ"
          when :span, :sup
            "#{@collector.delete(name.to_sym)} "
          when :h1, :h2, :h3, :h4, :h5, :h6 then "\n\n"
          when :blockquote then "\n\n"
          when :figure
            @inside = nil
            "\n\n"
          else
            ''
          end
end

#fix_href(href, site = 'http://mudasobwa.ru/') ⇒ Object (private)



137
138
139
# File 'lib/qipowl/bowlers/htmldoc.rb', line 137

def fix_href href, site = 'http://mudasobwa.ru/'
  href.start_with?('http') ? href : href.gsub(/\A\/+/, '').prepend(site)
end

#start_element(name, attributes = []) ⇒ Object



22
23
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
55
56
57
58
59
60
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
# File 'lib/qipowl/bowlers/htmldoc.rb', line 22

def start_element name, attributes = []
  current_attrs = Hash[attributes]
  
  @qp +=  case name.to_sym
          when :p, :div
            if current_attrs['class']
              @collector[name.to_sym] = "✿_#{name.to_sym}_#{current_attrs['class'].gsub(/\s+/, '_')}".to_sym
              @tags[:linewide][@collector[name.to_sym]] = "#{name.to_sym}†#{current_attrs['class'].gsub(/\s+/, '†')}".to_sym
              "\n\n#{@collector[name.to_sym]} "
            else
              "\n\n"
            end
          when :ul, :ol, :table, :dl
            @inside = name.to_sym
            "\n"
          when :pre then "\n\nΛ\n"
          when :tr then " ┇ "
          when :td then " ┆ "
          when :a
            @inside = :a
            @collector[:href] = current_attrs['href']
            @collector[:name] = current_attrs['name']
            ''
          when :li then (@inside == :ol) ? "◦ " : "• "
          when :b, :strong then "≡"
          when :i, :em, :nobr then "≈"
          when :strike, :del, :s then "─"
          when :small then "↓"
          when :u then "▁"
          when :code, :tt then "λ"
          when :dfn, :abbr, :cite
            @inside = name.to_sym
            @collector[:title] = current_attrs['title']
          when :hr then "\n\n——\n\n"
          when :br then " ⏎\n"
          when :center then "\n— "
          when :dt then "▷ "
          when :dd then " — "
          when :h1 then "§1 "
          when :h2 then "§2 "
          when :h3 then "§3 "
          when :h4 then "§4 "
          when :h5 then "§5 "
          when :h6 then "§6 "
          when :blockquote then "\n\n〉 "
          when :figure
            @inside = :figure
            "\n\n"
          when :figcaption then " "
          when :img then fix_href(current_attrs['src'])
          when :span, :sup
            if current_attrs['class'].nil?
              ''
            else
              @collector[name.to_sym] = "✿_span_#{current_attrs['class'].gsub(/\s+/, '_')}".to_sym
              @tags[:inplace][@collector[name.to_sym]] = "span†#{current_attrs['class'].gsub(/\s+/, '†')}".to_sym
              " #{@collector[name.to_sym]}"
            end
          when :embed, :iframe then "\n\n#{current_attrs['src']}\n\n"
          when :html, :body, :object, :param, :thead, :tbody, :font, :'lj-embed', :'lj-cut'
            ''
          else
            raise "=== Unhandled: #{name} with attrs: [#{current_attrs}]"
            ''
          end
end