Class: FeedYamlizer::HtmlListener

Inherits:
Object
  • Object
show all
Includes:
REXML::StreamListener
Defined in:
lib/feed_yamlizer/html_listener.rb

Constant Summary collapse

STRIP_TAGS =
%w[ body font ]
BLOCK_TAGS =
%w[ p div ]
HEADER_TAGS =
%w[ h1 h2 h3 h4 h5 h6 ]
UNIFORM_HEADER_TAG =
"h4"

Instance Method Summary collapse

Constructor Details

#initializeHtmlListener

Returns a new instance of HtmlListener.



15
16
17
18
19
# File 'lib/feed_yamlizer/html_listener.rb', line 15

def initialize
  @nested_tags = []
  @content = [""]
  @links = []
end

Instance Method Details

#pathObject



148
149
150
# File 'lib/feed_yamlizer/html_listener.rb', line 148

def path
  @nested_tags.join('/')
end

#resultObject



21
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
# File 'lib/feed_yamlizer/html_listener.rb', line 21

def result
  # we call strip_empty_tags twice to catch empty tags nested in a tag like <p>
  # not full-proof but good enough for now
  x = @content.map {|line| strip_empty_tags( strip_empty_tags( line ).strip ) }.
    select {|line| line.strip != ""}.
    compact.
    join("\n\n")

  digits = @links.size.to_s.size 

  x = Nokogiri::HTML.parse(x).inner_text
  # wrap the text
  x = FeedYamlizer.format(x)

  # format the blockquotes
  line_buffer = []
  blockquote_buffer = []
  inblock = false
  x.split(/\n/).each do |line|
    if line == '[blockquote]'
      inblock = true
    elsif line == '[/blockquote]'
      inblock = false
      block = blockquote_buffer.join("\n")
      line_buffer << (FeedYamlizer.format(block, 4))
      blockquote_buffer = []
    else
      if inblock 
        blockquote_buffer << " " * 4 + line.to_s
      else
        line_buffer << line
      end
    end
  end
  x = line_buffer.join("\n")
  

  res = x + "\n\n" + @links.map {|x| 
    gutter = x[:index].to_s.rjust(digits)
    if x[:content] && x[:content].strip.length > 0
      %Q|#{gutter}. "#{x[:content].gsub(/[\r\n]+/, ' ').strip}"\n#{' ' * (digits + 2)}#{x[:href]}|
    else
      "#{gutter}. #{x[:href]}"
    end
  }.join("\n")
  res 
end

#start_of_block?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/feed_yamlizer/html_listener.rb', line 144

def start_of_block?
  BLOCK_TAGS.include? @nested_tags[-1] 
end

#strip_empty_tags(line) ⇒ Object



69
70
71
# File 'lib/feed_yamlizer/html_listener.rb', line 69

def strip_empty_tags(line)
  line.gsub(%r{<(\w+)[^>]*>\s*</\1>}, '')
end

#tag_end(name) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/feed_yamlizer/html_listener.rb', line 105

def tag_end(name)
  @nested_tags.pop
  case name
  when 'a'
    @links[-1][:index] = @links.size
    @in_link = false
    @content[-1] << "#{(@links[-1][:content] || '').strip.gsub(/[\r\n]+/, ' ')}[#{@links.size}]"
  when *HEADER_TAGS
    @content[-1] << "</#{UNIFORM_HEADER_TAG}>" 
  when 'blockquote'
    @content += ["","[/blockquote]", '']
  when 'ul', 'ol', 'dl'
    @content[-1] << "</#{name}>"
  when 'li', 'dt', 'dd'
    @content += ["", "[/blockquote]", '']
  when 'strong', 'em'
    @content[-1] << "</#{name}>"
  when *BLOCK_TAGS
    @content[-1] << "</p>"
  when 'pre'
    @pre = false
    @content[-1] << "\n[/pre]"
  end
end

#tag_start(name, attrs) ⇒ Object



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
101
102
103
# File 'lib/feed_yamlizer/html_listener.rb', line 73

def tag_start(name, attrs)
  @nested_tags.push name
  case name 
  when 'a'
    @links << {:href => attrs['href']}
    @in_link = true
  when 'img'
    text = attrs['alt'] || attrs['title']
    chunk = "[img:#{text}] "
    @content[-1] << chunk
  when *HEADER_TAGS
    @content << "<#{UNIFORM_HEADER_TAG}>" 
  when 'br' #skip
    #@content << "<br/>"
    # @content << ""
    @content[-1] += " " 
  when 'blockquote'
    @content += ["[blockquote]", ""]
  when 'ul', 'ol', 'dl'
    @content << "<#{name}>\n"
  when 'li', 'dt', 'dd'
    @content += ["[blockquote]", "", "* "]
  when 'strong', 'em'
    @content[-1] << "<#{name}>"
  when *BLOCK_TAGS
    @content << "<p>"
  when 'pre'
    @pre = true
    @content << "[pre]\n"
  end
end

#text(text) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/feed_yamlizer/html_listener.rb', line 130

def text(text)
  return if text =~ /\a\s*\Z/
  if @in_link 
    (@links[-1][:content] ||= "") << text
    return
  end

  if @pre 
    @content[-1] << text.gsub("\n", NEWLINE_PLACEHOLDER).gsub(/\t/, TAB_PLACEHOLDER).gsub(" ", SPACE_PLACEHOLDER)
  else
    @content[-1] << text
  end
end