Skip to content

Commit

Permalink
REFACTOR: both data format and stack handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinBlues committed Jun 29, 2011
1 parent 9673dd0 commit f980428
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 65 deletions.
36 changes: 22 additions & 14 deletions lib/opml_speed_reader.rb
Expand Up @@ -79,9 +79,9 @@ def self.parse_body(reader, stack)
case path
when %r|opml/body(/outline)+|
libxml['title'] = (!!reader['title']) ? reader['title'].strip : reader['text'].strip
libxml['url'] = reader['xmlUrl'].strip if reader['xmlUrl']
libxml['feed_url'] = reader['xmlUrl'].strip if reader['xmlUrl']
OpmlSpeedReader.trace(:essential_elements) do
"BEGIN(#{path}): '#{libxml['title']}' '#{libxml['url']}' #{stack.size-3}."
"BEGIN(#{path}): '#{libxml['title']}' '#{libxml['feed_url']}' #{stack.size-3}."
end
yield(libxml.dup, stack.size - 3) unless libxml.empty?
end
Expand All @@ -107,28 +107,36 @@ def self.parse(io)

parser_stack.pop

feed_stack = [[]]
feed_stack = [[title]]
OpmlSpeedReader.parse_body(reader, parser_stack) do |feed, depth|
case (depth+1) <=> (feed_stack.size)
when +1:
feed_stack << [feed]
when 0:
feed_stack[-1] << feed
when -1:
tmp = feed_stack.pop
feed_stack[-1][0]['feeds'] = tmp
# puts "STACK: #{feed_stack.inspect}."
# puts "F(#{(depth+1) <=> (feed_stack.size)}): #{depth} #{feed.inspect}."
if feed.size > 1
raise if ((depth+1) <=> (feed_stack.size)) == -1
feed_stack[-1] << feed
else
raise
case ((depth+1) <=> (feed_stack.size))
when +1: raise
when 0: feed_stack << [feed['title']]
when -1:
tmp = feed_stack.pop
feed_stack[-1] << tmp
feed_stack << [feed['title']]
else
raise
end
end
end

# flatten stack
while feed_stack.size > 1
# puts "STACK: #{feed_stack.inspect}."
tmp = feed_stack.pop
feed_stack[-1][-1]['feeds'] = tmp
feed_stack[-1] << tmp
end

{:title => title, :feeds => feed_stack[0]}
# puts "STACK: #{feed_stack.inspect}."

feed_stack[0]
end
end
2 changes: 1 addition & 1 deletion lib/opml_speed_reader/version.rb
@@ -1,3 +1,3 @@
module OpmlSpeedReader
VERSION = "0.0.1"
VERSION = "0.1.0"
end
33 changes: 15 additions & 18 deletions test/opml/Google.yml
@@ -1,27 +1,24 @@
---
:title: Lars subscriptions in Google Reader
:feeds:
- title: @Bibliotek
feeds:
- Lars subscriptions in Google Reader
- - "@Bibliotek"
- title: 24 Ways
url: 24ways.xml
feed_url: 24ways.xml
- title: Jeff's Austin Blues
url: austinblues.xml
feed_url: austinblues.xml
- title: Christian Century Blogs
url: CCBlog1.xml
- title: El País
url: ElPais.xml
- title: 'Geeknet :: Blog'
url: GeekNet.xml
feed_url: CCBlog1.xml
- title: "El Pa\xC3\xADs"
feed_url: ElPais.xml
- title: "Geeknet :: Blog"
feed_url: GeekNet.xml
- title: The Guardian World News
url: Guardian.xml
feed_url: Guardian.xml
- title: Haaretz.com - News
url: Haaretz.xml
- title: @Politics
feeds:
feed_url: Haaretz.xml
- - "@Politics"
- title: Jeff Patton's Holistic Product Design & Development
url: JeffPatton.xml
feed_url: JeffPatton.xml
- title: Jon Udell
url: JonsNewRadio.xml
feed_url: JonsNewRadio.xml
- title: Linux Magazine Full Feed
url: LinuxMag.xml
feed_url: LinuxMag.xml
13 changes: 6 additions & 7 deletions test/opml/Thunderbird.yml
@@ -1,9 +1,8 @@
---
:title: Forumzilla OPML Export
:feeds:
---
- Forumzilla OPML Export
- title: Distributing the Future
url: "test/opml/DistributingTheFuture.xml"
- title: 'Forums: Message List - Developer Tools'
url: "test/opml/AmazonDeveloperTools.xml"
feed_url: test/opml/DistributingTheFuture.xml
- title: "Forums: Message List - Developer Tools"
feed_url: test/opml/AmazonDeveloperTools.xml
- title: The Roadhouse
url: "test/opml/TheRoadhouse.xml"
feed_url: test/opml/TheRoadhouse.xml
11 changes: 5 additions & 6 deletions test/opml/UserLandFrontier.yml
@@ -1,9 +1,8 @@
---
title: ourFavoriteFeedsData.top100
:feeds:
---
- ourFavoriteFeedsData.top100
- title: Scripting News
url: "test/opml/ScriptingNews.xml"
feed_url: test/opml/ScriptingNews.xml
- title: Wired News
url: "test/opml/WiredNews.xml"
feed_url: test/opml/WiredNews.xml
- title: Boing Boing Blog
url: "test/opml/BoingBoing.xml"
feed_url: test/opml/BoingBoing.xml
21 changes: 2 additions & 19 deletions test/test_suite.rb
Expand Up @@ -14,25 +14,8 @@ def test_parsing
io = open(filename.gsub(/\.yml\Z/, '.xml'))
opml = OpmlSpeedReader.parse(io)
# puts "ACTUAL: #{opml.inspect}."
# puts "ACTUAL: #{opml[:feeds].map{|a| "'#{a.title}'"}.join(", ")}."
# puts "DETAILS: #{opml[:details].inspect}."
# expected[:items].each do |item|
expected[:feeds].each do |feed|
# puts "FEED: |#{feed.inspect}|"
feed['title'].gsub!(/&#x([0-9a-f]+);/i){ [$1.hex].pack("U*")}
a = opml[:feeds].detect{|i| feed['title'] == i['title']}
if a.nil?
puts "MISSING EXPECTED: '#{feed['title']}'."
else
assert feed['title'] == a['title'], "FEED TITLE: '#{a['title']}' vs. '#{feed['title']}'."
assert a['url'] == feed['url'], "FEED URL: '#{a['url']}' vs. '#{feed['url']}'."

opml[:feeds].delete(a)
end
end
# puts "OPML: |#{opml[:feeds].inspect}|"
assert(opml[:feeds].empty?,
"#{opml[:feeds].length} channels expected in #{filename.gsub(%r{.*/}, '')} not found.")
# puts "ACTUAL: #{opml.to_yaml}."
assert opml == expected
end
end
end

0 comments on commit f980428

Please sign in to comment.