1
2 """TurboFeed widgets for RSS / Atom feeds handling."""
3 __docformat__ = 'restructuredtext'
4
5 __all__ = [
6 'FeedButton',
7 'FeedHeadLinks',
8 'FeedLinks',
9 'FeedLinksDesc',
10 'static_dir',
11 ]
12
13 import logging
14
15 import pkg_resources
16
17 from turbogears import url
18 from turbogears.widgets import CSSLink, Widget, WidgetDescription, \
19 register_static_directory
20
21
22 log = logging.getLogger('turbofeeds.widgets')
23
24 static_dir = pkg_resources.resource_filename("turbofeeds", "static")
25 register_static_directory("turbofeeds", static_dir)
26
27
29 """A list of links to feeds for all supported formats.
30
31 The value passed to the widget contructor or ``display`` method will be the
32 text for the link. The value may use the "%(type)s" placeholder for the
33 feed format name.
34
35 """
36 params = ['base_url', 'controller', 'feed_types', 'mimetypes', 'title',
37 'url_params']
38 template = """\
39 <div xmlns:py="http://purl.org/kid/ns#" py:strip="">
40 <ul class="feedlinklist">
41 <li py:for="type, name in feed_types">
42 <a py:attrs="dict(title=title % dict(type=name))" class="feedlink"
43 href="${feed_url(type)}">${value % dict(type=name)}</a>
44 </li>
45 </ul>
46 </div>
47 """
48 css = [CSSLink("turbofeeds", "css/feeds.css", media="screen")]
49 base_url = None
50 controller = None
51 feed_types = [
52 ('rss2_0', 'RSS 2.0'),
53 ('atom0_3', 'Atom 0.3'),
54 ('atom1_0', 'Atom 1.0')
55 ]
56 title = '%(type)s'
57 url_params = {}
58 mimetypes = {
59 'atom0_3': 'application/atom+xml',
60 'atom1_0': 'application/rss+xml',
61 'mrss1_1_1': 'application/rss+xml',
62 'rss2_0': 'application/rss+xml',
63 }
64
65 params_doc = {
66 'base_url': 'The base_url of the feed. The feed format will be '
67 'appended to this. Can be determined from "controller", if given.',
68 'controller': 'The FeedController instance serving the feeds the '
69 'links will point to.',
70 'feed_types': 'A list of 2-item tuples matching format identifier to '
71 'format name. A link will be generated for each format.',
72 'title': 'String to use for "title" attribute of feed links. May use '
73 '"%(type)s" placeholder for the feed format name.',
74 'url_params' : 'Dictionary containing extra URL parameters appended to'
75 ' the feed URL',
76 }
77
93
94 - def feed_url(self, base_url, type, params):
95 """Returns feed URL by combining base_url, feed type and params."""
96
97 return url([base_url, type], params)
98
100 """One or more LINK elements to advertise feeds in the HTML header.
101
102 The value passed to the widget contructor or ``display`` method will be the
103 text in the LINK element's title attribute. The value may use the "%(type)s"
104 placeholder for the feed format name.
105
106 """
107 template = """\
108 <div xmlns:py="http://purl.org/kid/ns#" py:strip="">
109 <link rel="alternate" type="${mimetypes[type]}" href="${feed_url(type)}"
110 py:attrs="dict(title=value % dict(type=name))" py:for="type, name in feed_types" />
111 </div>
112 """
113
115 name = "Feed link list"
116 for_widget = FeedLinks(base_url = '/feed')
117 template = """\
118 <div>
119 ${for_widget("Subscribe to %(type)s feed",
120 title="Click link to access the feed in %(type)s format")}
121 </div>
122 """
123
174
175
176 if __name__ == '__main__':
177 from turbogears.view import load_engines
178 load_engines()
179 fl = FeedLinks(base_url='/feed')
180 print fl.render('View %(type)s feed')
181 print
182 print fl.render('View %(type)s feed', title='Click to view feed in browser',
183 url_params=dict(compat=True))
184 fl = FeedLinks(base_url='/myfeeds')
185 print fl.render('View my %(type)s feed')
186