public
Fork of lorenjohnson/radiant-rss-reader
Description: Feed reader for Radiant CMS
Homepage: http://www.hellovenado.com
Clone URL: git://github.com/jamesmacaulay/radiant-rss-reader.git
Search Repo:
reorganize dir structure; update README
jamesmacaulay (author)
Mon May 12 09:17:25 -0700 2008
commit  680f0cc36888548c9738c81e71fe9216185909bb
tree    c12e238d3a5ac30b180619efb8656a30fa350b07
parent  92a7110d055edf9942bc7d069acd6a71337373b9
...
 
...
1
0
@@ -0,0 +1 @@
0
+.DS_Store
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
0
@@ -0,0 +1,51 @@
0
+# ABOUT
0
+This is a RadiantCMS extension (originally a behavior by Alessandro Preite Martinez) that
0
+adds some tags to fetch and display RSS feeds. It uses the
0
+'ruby-feedparser' module, and it is able to cache the raw feed data
0
+and to only fetch the new feed if it has been modified (using the
0
+If-Modified-Since HTTP header).
0
+
0
+
0
+# INSTALLATION
0
+
0
+## via Git
0
+
0
+ cd RADIANT_APP_ROOT
0
+ git clone git://github.com/lorenjohnson/radiant-rss-reader.git vendor/extensions/rss_reader
0
+
0
+## via Git (as submodule)
0
+
0
+ cd RADIANT_APP_ROOT
0
+ git submodule add git://github.com/lorenjohnson/radiant-rss-reader.git vendor/extensions/rss_reader
0
+
0
+...then `git submodule init` and `git submodule update` as necessary.
0
+
0
+## via tarball
0
+
0
+Download the tarball from http://github.com/lorenjohnson/radiant-rss-reader/tarball/master into `RADIANT_APP_ROOT/vendor/extentions`, then:
0
+
0
+ cd RADIANT_APP_ROOT/vendor/extentions
0
+ tar xvzf lorenjohnson-radiant-rss-reader.tgz
0
+ mv lorenjohnson-radiant-rss-reader rss_reader
0
+
0
+# USAGE EXAMPLE
0
+Use it in your page like this (just an example):
0
+
0
+ <dl>
0
+ <r:feed:items url="http://www.somefeed.com/rss limit="5">
0
+ <dt><r:feed:link /> - by <r:feed:creator />, <r:feed:date format="%b %d"/></dt>
0
+ <dd><r:feed:content /></dd>
0
+ </r:feed:items>
0
+ </dl>
0
+
0
+# CONTRIBUTORS
0
+Port to Extension:
0
+BJ Clark (bjclark@scidept.com, http://www.scidept.com/) & Loren Johnson (loren@fn-group.com, http://www.fn-group.com)
0
+
0
+Modifications:
0
+James MacAulay (jmacaulay@gmail.com, http://jmacaulay.net/)
0
+
0
+Original Author:
0
+Alessandro Preite Martinez (ale@incal.net)
0
+
0
+License - Creative Commons Attribution-Share Alike 2.5 License
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,35 +0,0 @@
0
-* ABOUT
0
-This is a RadiantCMS extension (originally a behavior by Alessandro Preite Martinez) that
0
-adds some tags to fetch and display RSS feeds. It uses the
0
-'ruby-feedparser' module, and it is able to cache the raw feed data
0
-and to only fetch the new feed if it has been modified (using the
0
-If-Modified-Since HTTP header).
0
-
0
-
0
-* INSTALLATION
0
-1. Download the .tgz file into the root of your radiant installation.
0
-2. Expand the .tgz file, should put "feedparser.rb and a folder called 'feedparser'" into your /lib/ folder and a folder called rss_reader in /vendors/extensions/
0
-
0
-
0
-* USAGE EXAMPLE
0
-Use it in your page like this (just an example):
0
-
0
-<dl>
0
- <r:feed:items url="http://www.somefeed.com/rss limit="5">
0
- <dt><r:feed:link /> - by <r:feed:creator />, <r:feed:date format="%b %d"/></dt>
0
- <dd><r:feed:content /></dd>
0
- </r:feed:items>
0
- </dl>
0
-
0
-
0
-* CONTRIBUTORS
0
-Port to Extension:
0
-BJ Clark (bjclark@scidept.com, http://www.scidept.com/) & Loren Johnson (loren@fn-group.com, http://www.fn-group.com)
0
-
0
-Modifications:
0
-James MacAulay (jmacaulay@gmail.com, http://jmacaulay.net/)
0
-
0
-Original Author:
0
-Alessandro Preite Martinez (ale@incal.net)
0
-
0
-License - Creative Commons Attribution-Share Alike 2.5 License
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
0
@@ -0,0 +1,294 @@
0
+require 'rexml/document'
0
+require 'time'
0
+require 'feedparser/textconverters'
0
+require 'feedparser/rexml_patch'
0
+require 'base64'
0
+
0
+module FeedParser
0
+
0
+ class UnknownFeedTypeException < RuntimeError
0
+ end
0
+
0
+ # an RSS/Atom feed
0
+ class Feed
0
+ attr_reader :type, :title, :link, :description, :creator, :encoding, :items
0
+
0
+ # REXML::Element for this feed.
0
+ attr_reader :xml
0
+
0
+ # parse str to build a Feed
0
+ def initialize(str = nil)
0
+ parse(str) if str
0
+ end
0
+
0
+ # Determines all the fields using a string containing an
0
+ # XML document
0
+ def parse(str)
0
+ # Dirty hack: some feeds contain the & char. It must be changed to &amp;
0
+ str.gsub!(/&(\s+)/, '&amp;\1')
0
+ doc = REXML::Document.new(str)
0
+ @xml = doc.root
0
+ # get feed info
0
+ @encoding = doc.encoding
0
+ @title,@link,@description,@creator = nil
0
+ @items = []
0
+ if doc.root.elements['channel'] || doc.root.elements['rss:channel']
0
+ @type = "rss"
0
+ # We have a RSS feed!
0
+ # Title
0
+ if (e = doc.root.elements['channel/title'] ||
0
+ doc.root.elements['rss:channel/rss:title']) && e.text
0
+ @title = e.text.toUTF8(@encoding).rmWhiteSpace!
0
+ end
0
+ # Link
0
+ if (e = doc.root.elements['channel/link'] ||
0
+ doc.root.elements['rss:channel/rss:link']) && e.text
0
+ @link = e.text.rmWhiteSpace!
0
+ end
0
+ # Description
0
+ if (e = doc.root.elements['channel/description'] ||
0
+ doc.root.elements['rss:channel/rss:description']) && e.text
0
+ @description = e.text.toUTF8(@encoding).rmWhiteSpace!
0
+ end
0
+ # Creator
0
+ if ((e = doc.root.elements['channel/dc:creator']) && e.text) ||
0
+ ((e = doc.root.elements['channel/author'] ||
0
+ doc.root.elements['rss:channel/rss:author']) && e.text)
0
+ @creator = e.text.toUTF8(@encoding).rmWhiteSpace!
0
+ end
0
+ # Items
0
+ if doc.root.elements['channel/item']
0
+ query = 'channel/item'
0
+ elsif doc.root.elements['item']
0
+ query = 'item'
0
+ elsif doc.root.elements['rss:channel/rss:item']
0
+ query = 'rss:channel/rss:item'
0
+ else
0
+ query = 'rss:item'
0
+ end
0
+ doc.root.each_element(query) { |e| @items << RSSItem::new(e, self) }
0
+
0
+ elsif doc.root.elements['/feed']
0
+ # We have an ATOM feed!
0
+ @type = "atom"
0
+ # Title
0
+ if (e = doc.root.elements['/feed/title']) && e.text
0
+ @title = e.text.toUTF8(@encoding).rmWhiteSpace!
0
+ end
0
+ # Link
0
+ doc.root.each_element('/feed/link') do |e|
0
+ if e.attribute('type') and (
0
+ e.attribute('type').value == 'text/html' or
0
+ e.attribute('type').value == 'application/xhtml' or
0
+ e.attribute('type').value == 'application/xhtml+xml')
0
+ if (h = e.attribute('href')) && h
0
+ @link = h.value.rmWhiteSpace!
0
+ end
0
+ end
0
+ end
0
+ # Description
0
+ if e = doc.root.elements['/feed/info']
0
+ e = e.elements['div'] || e
0
+ @description = e.to_s.toUTF8(@encoding).rmWhiteSpace!
0
+ end
0
+ # Items
0
+ doc.root.each_element('/feed/entry') do |e|
0
+ @items << AtomItem::new(e, self)
0
+ end
0
+ else
0
+ raise UnknownFeedTypeException::new
0
+ end
0
+ end
0
+
0
+ def to_s
0
+ s = ''
0
+ s += "Type: #{@type}\n"
0
+ s += "Encoding: #{@encoding}\n"
0
+ s += "Title: #{@title}\n"
0
+ s += "Link: #{@link}\n"
0
+ s += "Description: #{@description}\n"
0
+ s += "Creator: #{@creator}\n"
0
+ s += "\n"
0
+ @items.each { |i| s += i.to_s }
0
+ s
0
+ end
0
+ end
0
+
0
+ # an Item from a feed
0
+ class FeedItem
0
+ attr_accessor :title, :link, :content, :date, :creator, :subject,
0
+ :category, :cacheditem
0
+
0
+ attr_reader :feed
0
+
0
+ # REXML::Element for this item
0
+ attr_reader :xml
0
+
0
+ def initialize(item = nil, feed = nil)
0
+ @xml = item
0
+ @feed = feed
0
+ @title, @link, @content, @date, @creator, @subject, @category = nil
0
+ parse(item) if item
0
+ end
0
+
0
+ def parse(item)
0
+ raise "parse() should be implemented by subclasses!"
0
+ end
0
+
0
+ def to_s
0
+ s = "--------------------------------\n" +
0
+ "Title: #{@title}\nLink: #{@link}\n" +
0
+ "Date: #{@date.to_s}\nCreator: #{@creator}\n" +
0
+ "Subject: #{@subject}\nCategory: #{@category}\nContent:\n#{content}\n"
0
+ if defined?(@enclosures) and @enclosures.length > 0
0
+ s2 = "Enclosures:\n"
0
+ @enclosures.each do |e|
0
+ s2 += e.join(' ') + "\n"
0
+ end
0
+ s += s2
0
+ end
0
+ return s
0
+ end
0
+ end
0
+
0
+ class RSSItem < FeedItem
0
+
0
+ # The item's enclosures childs. An array of (url, length, type) triplets.
0
+ attr_accessor :enclosures
0
+
0
+ def parse(item)
0
+ # Title. If no title, use the pubDate as fallback.
0
+ if ((e = item.elements['title'] || item.elements['rss:title']) &&
0
+ e.text) ||
0
+ ((e = item.elements['pubDate'] || item.elements['rss:pubDate']) &&
0
+ e.text)
0
+ @title = e.text.toUTF8(@feed.encoding).rmWhiteSpace!
0
+ end
0
+ # Link
0
+ if ((e = item.elements['link'] || item.elements['rss:link']) && e.text)||
0
+ (e = item.elements['guid'] || item.elements['rss:guid'] and
0
+ not (e.attribute('isPermaLink') and
0
+ e.attribute('isPermaLink').value == 'false'))
0
+ @link = e.text.rmWhiteSpace!
0
+ end
0
+ # Content
0
+ if (e = item.elements['content:encoded']) ||
0
+ (e = item.elements['description'] || item.elements['rss:description'])
0
+ @content = FeedParser::getcontent(e, @feed)
0
+ end
0
+ # Date
0
+ if e = item.elements['dc:date'] || item.elements['pubDate'] ||
0
+ item.elements['rss:pubDate']
0
+ begin
0
+ @date = Time::xmlschema(e.text)
0
+ rescue
0
+ begin
0
+ @date = Time::rfc2822(e.text)
0
+ rescue
0
+ begin
0
+ @date = Time::parse(e.text)
0
+ rescue
0
+ @date = nil
0
+ end
0
+ end
0
+ end
0
+ end
0
+ # Creator
0
+ @creator = @feed.creator
0
+ if (e = item.elements['dc:creator'] || item.elements['author'] ||
0
+ item.elements['rss:author']) && e.text
0
+ @creator = e.text.toUTF8(@feed.encoding).rmWhiteSpace!
0
+ end
0
+ # Subject
0
+ if (e = item.elements['dc:subject']) && e.text
0
+ @subject = e.text.toUTF8(@feed.encoding).rmWhiteSpace!
0
+ end
0
+ # Category
0
+ if (e = item.elements['dc:category'] || item.elements['category'] ||
0
+ item.elements['rss:category']) && e.text
0
+ @category = e.text.toUTF8(@feed.encoding).rmWhiteSpace!
0
+ end
0
+ # Enclosures
0
+ @enclosures = []
0
+ item.each_element('enclosure') do |e|
0
+ url = e.attribute('url').value if e.attribute('url')
0
+ length = e.attribute('length').value if e.attribute('length')
0
+ type = e.attribute('type').value if e.attribute('type')
0
+ @enclosures << [ url, length, type ]
0
+ end
0
+ end
0
+ end
0
+
0
+ class AtomItem < FeedItem
0
+ def parse(item)
0
+ # Title
0
+ if (e = item.elements['title']) && e.text
0
+ @title = e.text.toUTF8(@feed.encoding).rmWhiteSpace!
0
+ end
0
+ # Link
0
+ item.each_element('link') do |e|
0
+ if e.attribute('type') and (
0
+ e.attribute('type').value == 'text/html' or
0
+ e.attribute('type').value == 'application/xhtml' or
0
+ e.attribute('type').value == 'application/xhtml+xml')
0
+ if (h = e.attribute('href')) && h.value
0
+ @link = h.value
0
+ end
0
+ end
0
+ end
0
+ # Content
0
+ if e = item.elements['content'] || item.elements['summary']
0
+ if (e.attribute('mode') and e.attribute('mode').value == 'escaped') &&
0
+ e.text
0
+ @content = e.text.toUTF8(@feed.encoding).rmWhiteSpace!
0
+ else
0
+ @content = FeedParser::getcontent(e, @feed)
0
+ end
0
+ end
0
+ # Date
0
+ if (e = item.elements['issued'] || e = item.elements['created']) && e.text
0
+ begin
0
+ @date = Time::xmlschema(e.text)
0
+ rescue
0
+ begin
0
+ @date = Time::rfc2822(e.text)
0
+ rescue
0
+ begin
0
+ @date = Time::parse(e.text)
0
+ rescue
0
+ @date = nil
0
+ end
0
+ end
0
+ end
0
+ end
0
+ # Creator
0
+ @creator = @feed.creator
0
+ if (e = item.elements['author/name']) && e.text
0
+ @creator = e.text.toUTF8(@feed.encoding).rmWhiteSpace!
0
+ end
0
+ end
0
+ end
0
+
0
+ def FeedParser::getcontent(e, feed = nil)
0
+ encoding = feed ? feed.encoding : 'utf-8'
0
+ children = e.children.reject do |i|
0
+ i.class == REXML::Text and i.to_s.chomp == ''
0
+ end
0
+ if children.length > 1
0
+ s = ''
0
+ children.each { |c| s += c.to_s }
0
+ return s.toUTF8(encoding).rmWhiteSpace!.text2html
0
+ elsif children.length == 1
0
+ c = children[0]
0
+ if c.class == REXML::Text
0
+ return e.text.toUTF8(encoding).rmWhiteSpace!.text2html
0
+ else
0
+ if c.class == REXML::CData
0
+ return c.to_s.toUTF8(encoding).rmWhiteSpace!.text2html
0
+ elsif c.text
0
+ return c.text.toUTF8(encoding).text2html
0
+ end
0
+ end
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
0
@@ -0,0 +1,47 @@
0
+require 'feedparser'
0
+
0
+module FeedParser
0
+ class Feed
0
+ def to_html
0
+ s = ''
0
+ s += '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'
0
+ s += "\n"
0
+ s += "<html>\n"
0
+ s += "<body>\n"
0
+ s += "<p>Type: #{@type}<br>\n"
0
+ s += "Encoding: #{@encoding}<br>\n"
0
+ s += "Title: #{@title}<br>\n"
0
+ s += "Link: #{@link}<br>\n"
0
+ s += "Description: #{@description}<br>\n"
0
+ s += "Creator: #{@creator}</p>\n"
0
+ s += "\n"
0
+ @items.each do |i|
0
+ s += "\n<hr/><!-- *********************************** -->\n"
0
+ s += i.to_html
0
+ end
0
+ s
0
+ end
0
+ end
0
+
0
+ class FeedItem
0
+ def to_html
0
+ s = "<p>Feed: "
0
+ s += "<a href=\"#{@feed.link}\">\n" if @feed.link
0
+ s += "#{@feed.title}\n" if @feed.title
0
+ s += "</a>\n" if @feed.link
0
+ s += "<br/>\nItem: "
0
+ s += "<a href=\"#{@link}\">\n" if @link
0
+ s += "#{@title}\n" if @title
0
+ s += "</a>\n" if @link
0
+ s += "\n"
0
+ s += "<br/>Date: #{@date.to_s}\n" if @date # TODO improve date rendering ?
0
+ s += "<br/>Author: #{@creator}\n" if @creator
0
+ s += "<br/>Subject: #{@subject}\n" if @subject
0
+ s += "<br/>Category: #{@category}\n" if @category
0
+ s += "</p>\n"
0
+ s += "#{@content}" if @content
0
+ s += '</body></html>'
0
+ s
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
0
@@ -0,0 +1,368 @@
0
+require 'feedparser/sgml-parser'
0
+
0
+module FeedParser
0
+# this class provides a simple SGML parser that removes HTML tags
0
+ class HTML2TextParser < SGMLParser
0
+
0
+ attr_reader :savedata
0
+
0
+ def initialize(verbose = false)
0
+ @savedata = ''
0
+ @pre = false
0
+ @href = nil
0
+ @links = []
0
+ super(verbose)
0
+ end
0
+
0
+ def handle_data(data)
0
+ # let's remove all CR
0
+ data.gsub!(/\n/, '') if not @pre
0
+
0
+ @savedata << data
0
+ end
0
+
0
+ def unknown_starttag(tag, attrs)
0
+ case tag
0
+ when 'p'
0
+ @savedata << "\n\n"
0
+ when 'br'
0
+ @savedata << "\n"
0
+ when 'ul'
0
+ @savedata << "\n"
0
+ when 'li'
0
+ @savedata << "\n - "
0
+ when 'b'
0
+ @savedata << '*'
0
+ when 'strong'
0
+ @savedata << '*'
0
+ when 'em'
0
+ @savedata << '*'
0
+ when 'u'
0
+ @savedata << '_'
0
+ when 'i'
0
+ @savedata << '/'
0
+ when 'pre'
0
+ @savedata << "\n\n"
0
+ @pre = true
0
+ when 'a'
0
+ # find href in args
0
+ @href = nil
0
+ attrs.each do |a|
0
+ if a[0] == 'href'
0
+ @href = a[1]
0
+ end
0
+ end
0
+ if @href
0
+ @links << @href.gsub(/^("|'|)(.*)("|')$/,'\2')
0
+ end
0
+ else
0
+# puts "unknown tag: #{tag}"
0
+ end
0
+ end
0
+
0
+ def close
0
+ super
0
+ if @links.length > 0
0
+ @savedata << "\n\n"
0
+ @links.each_index do |i|
0
+ @savedata << "[#{i+1}] #{@links[i]}\n"
0
+ end
0
+ end
0
+ end
0
+
0
+ def unknown_endtag(tag)
0
+ case tag
0
+ when 'ul'
0
+ @savedata << "\n"
0
+ when 'b'
0
+ @savedata << '*'
0
+ when 'strong'
0
+ @savedata << '*'
0
+ when 'em'
0
+ @savedata << '*'
0
+ when 'u'
0
+ @savedata << '_'
0
+ when 'i'
0
+ @savedata << '/'
0
+ when 'pre'
0
+ @savedata << "\n\n"
0
+ @pre = false
0
+ when 'a'
0
+ if @href
0
+ @savedata << "[#{@links.length}]"
0
+ @href = nil
0
+ end
0
+ end
0
+ end
0
+
0
+ def unknown_charref(ref)
0
+ handle_data([ref.to_i].pack('U*'))
0
+ end
0
+ HTML_ENTITIES = {
0
+ "quot" => 34,
0
+ "amp" => 38,
0
+ "lt" => 60,
0
+ "gt" => 62,
0
+ "apos" => 39,
0
+
0
+ "nbsp" => 160,
0
+ "iexcl" => 161,
0
+ "cent" => 162,
0
+ "pound" => 163,
0
+ "curren" => 164,
0
+ "yen" => 165,
0
+ "brvbar" => 166,
0
+ "sect" => 167,
0
+ "uml" => 168,
0
+ "copy" => 169,
0
+ "ordf" => 170,
0
+ "laquo" => 171,
0
+ "not" => 172,
0
+ "shy" => 173,
0
+ "reg" => 174,
0
+ "macr" => 175,
0
+ "deg" => 176,
0
+ "plusmn" => 177,
0
+ "sup2" => 178,
0
+ "sup3" => 179,
0
+ "acute" => 180,
0
+ "micro" => 181,
0
+ "para" => 182,
0
+ "middot" => 183,
0
+ "cedil" => 184,
0
+ "sup1" => 185,
0
+ "ordm" => 186,
0
+ "raquo" => 187,
0
+ "frac14" => 188,
0
+ "frac12" => 189,
0
+ "frac34" => 190,
0
+ "iquest" => 191,
0
+ "Agrave" => 192,
0
+ "Aacute" => 193,
0
+ "Acirc" => 194,
0
+ "Atilde" => 195,
0
+ "Auml" => 196,
0
+ "Aring" => 197,
0
+ "AElig" => 198,
0
+ "Ccedil" => 199,
0
+ "Egrave" => 200,
0
+ "Eacute" => 201,
0
+ "Ecirc" => 202,
0
+ "Euml" => 203,
0
+ "Igrave" => 204,
0
+ "Iacute" => 205,
0
+ "Icirc" => 206,
0
+ "Iuml" => 207,
0
+ "ETH" => 208,
0
+ "Ntilde" => 209,
0
+ "Ograve" => 210,
0
+ "Oacute" => 211,
0
+ "Ocirc" => 212,
0
+ "Otilde" => 213,
0
+ "Ouml" => 214,
0
+ "times" => 215,
0
+ "Oslash" => 216,
0
+ "Ugrave" => 217,
0
+ "Uacute" => 218,
0
+ "Ucirc" => 219,
0
+ "Uuml" => 220,
0
+ "Yacute" => 221,
0
+ "THORN" => 222,
0
+ "szlig" => 223,
0
+ "agrave" => 224,
0
+ "aacute" => 225,
0
+ "acirc" => 226,
0
+ "atilde" => 227,
0
+ "auml" => 228,
0
+ "aring" => 229,
0
+ "aelig" => 230,
0
+ "ccedil" => 231,
0
+ "egrave" => 232,
0
+ "eacute" => 233,
0
+ "ecirc" => 234,
0
+ "euml" => 235,
0
+ "igrave" => 236,
0
+ "iacute" => 237,
0
+ "icirc" => 238,
0
+ "iuml" => 239,
0
+ "eth" => 240,
0
+ "ntilde" => 241,
0
+ "ograve" => 242,
0
+ "oacute" => 243,
0
+ "ocirc" => 244,
0
+ "otilde" => 245,
0
+ "ouml" => 246,
0
+ "divide" => 247,
0
+ "oslash" => 248,
0
+ "ugrave" => 249,
0
+ "uacute" => 250,
0
+ "ucirc" => 251,
0
+ "uuml" => 252,
0
+ "yacute" => 253,
0
+ "thorn" => 254,
0
+ "yuml" => 255,
0
+
0
+
0
+ "fnof" => 402,
0
+ "Alpha" => 913,
0
+ "Beta" => 914,
0
+ "Gamma" => 915,
0
+ "Delta" => 916,
0
+ "Epsilon" => 917,
0
+ "Zeta" => 918,
0
+ "Eta" => 919,
0
+ "Theta" => 920,
0
+ "Iota" => 921,
0
+ "Kappa" => 922,
0
+ "Lambda" => 923,
0
+ "Mu" => 924,
0
+ "Nu" => 925,
0
+ "Xi" => 926,
0
+ "Omicron" => 927,
0
+ "Pi" => 928,
0
+ "Rho" => 929,
0
+ "Sigma" => 931,
0
+ "Tau" => 932,
0
+ "Upsilon" => 933,
0
+ "Phi" => 934,
0
+ "Chi" => 935,
0
+ "Psi" => 936,
0
+ "Omega" => 937,
0
+ "alpha" => 945,
0
+ "beta" => 946,
0
+ "gamma" => 947,
0
+ "delta" => 948,
0
+ "epsilon" => 949,
0
+ "zeta" => 950,
0
+ "eta" => 951,
0
+ "theta" => 952,
0
+ "iota" => 953,