summaryrefslogtreecommitdiff
path: root/test/ruby/sidebarize.in.rb
blob: 6cb9e225714dbdcb79575275ea0d94edc86f4747 (plain)
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
#!/usr/bin/env ruby
# Sidebarize is a quite simple converter, like a lot others.
# But in one way Sidebarize is special: it converts
# feeds into HTML-pages suitable for sidebars in Gecko-browsers.

# import support for all kinds of feeds
require 'rss/0.9'
require 'rss/1.0'
require 'rss/2.0'
# import support for encodings
require 'iconv'
# import support for CGI
require 'cgi'
# import support for getting files from URLs
require 'net/http'
require 'uri'

Version = '0.0.9'

Header = %q(<!DOCTYPE html PUBLIC 
	"-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>%s Sidebar</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <link rel="stylesheet" type="text/css" href="sidebarize.css" />
    </head>

    <body>
        <p>%s</p>)

Footer = %q(        <p>
            <img src="fourpiece32.png" width="32" height="32" class="logo" />
            generated by Sidebarize %s
        </p>
    </body>
</html>)

Entry = %q(        <div>
            <img src="arrow.png" width="6" height="11" />
            <a href="%s">%s</a>
        </div>)

class FeedConverter
    def initialize
        @xml_source = ''
        @feed_data = []
        @feed_version = ''
        @feed_description = ''
    end
    
    def from_file(filename)
        f = File::open(filename)
        @xml_source = f.read
        f.close
    end
    
    def from_url(url)
        @xml_source = Net::HTTP.get(URI.parse(url))
    end
    
    def parse
        feed = RSS::Parser.parse(@xml_source)
        @feed_description = feed.channel.description
        @feed_name = feed.channel.title
        
    
        feed.items.each do |item|
            item_data = {}
            item_data['title'] = item.title
            item_data['link'] = item.link
            @feed_data << item_data
        end
    end
    
    # Output HTML from the internal data structure
    def to_html
        # header
        puts Header % [convert_entity(@feed_name), convert_entity(@feed_description)]
        
        # the entries
        @feed_data.each do |item|
            puts Entry % [item['link'], convert_entity(item['title'])]
        end
    
        # footer
        print Footer % Version
    end
end

# Converts entities
# uses code by murphy extended with iconv conversion
def convert_entity(text)
    text = Iconv.new('iso-8859-15', 'utf-8').iconv(text) 
    
    feed = text.inspect[1...-1]

    feed.gsub!(/\\([0-7]+)|(.)/m) do
        if $2
            $&
        else
            '&#%s;' % $1.to_i(8).to_s(10)
        end
    end
    
    feed.gsub!('\"', '&quot;')

    return feed
end

# Starter
def main
    fc = FeedConverter.new
    cgi = CGI.new
    if cgi.has_key? 'url'
        # yeah, the user pointed us to an URL
        fc.from_url(cgi['url'])
        #fc.from_file('sd.xml')
        #fc.from_file('sbarize_design.html')
        
        begin
            # try to parse it and to generate HTML
            fc.parse
            
            puts 'Content-Type: text/html'
            puts
            fc.to_html
        rescue
            # parsing failed so show an error message
            puts 'Content-Type: text/html'
            puts
            puts Header % ['No', 'The specified feed is not valid.']
            puts Footer % Version
        end
    else
        # no, we've got no URL, generate error message
        puts 'Content-Type: text/plain'
        puts
        puts Header % ['No', 'You have to set the url=http://domain.tld/path/feed.xml to your feed.']
        puts Footer % Version
    end
end

main if __FILE__ == $0