diff --git a/morss/feeds.py b/morss/feeds.py index 86d63a8..d086a2d 100644 --- a/morss/feeds.py +++ b/morss/feeds.py @@ -10,6 +10,13 @@ from StringIO import StringIO import json import csv +try: + from wheezy.template.engine import Engine + from wheezy.template.loader import DictLoader + from wheezy.template.ext.core import CoreExtension +except ImportError: + Engine = DictLoader = CoreExtension = None + json.encoder.c_make_encoder = None try: @@ -353,6 +360,15 @@ class FeedParser(FeedBase): out.seek(0) return out.read() + def tohtml(self): + if DictLoader is None: + log('dep wheezy.template needed') + + loader = DictLoader({'reader': open('reader.html.template').read()}) + engine = Engine(loader=loader, extensions=[CoreExtension()]) + template = engine.get_template('reader') + return template.render({'feed':self}).encode('utf-8') + class FeedParserRSS(FeedParser): """ RSS Parser diff --git a/morss/morss.py b/morss/morss.py index 9014a2f..d2bd7b9 100644 --- a/morss/morss.py +++ b/morss/morss.py @@ -644,6 +644,8 @@ def After(rss, options): return rss.tojson() elif options.csv: return rss.tocsv() + elif options.reader: + return rss.tohtml() else: return rss.tostring(xml_declaration=True, encoding='UTF-8') @@ -692,7 +694,7 @@ def cgi_app(environ, start_response): headers['status'] = '200 OK' headers['etag'] = '"%s"' % int(time.time()) - if options.html: + if options.html or options.reader: headers['content-type'] = 'text/html' elif options.debug or options.txt: headers['content-type'] = 'text/plain' diff --git a/morss/reader.html.template b/morss/reader.html.template new file mode 100644 index 0000000..4dfdea7 --- /dev/null +++ b/morss/reader.html.template @@ -0,0 +1,41 @@ +@require(feed) + + + + @feed.title – via morss + + + + + + + + + +
+ @for item in feed.items: +
+
+ @item.title +
@item.content
+
+
+ @end +
+ + + + diff --git a/setup.py b/setup.py index 85f9fbd..55e3168 100644 --- a/setup.py +++ b/setup.py @@ -13,6 +13,7 @@ setup( name='morss', 'python-dateutil <= 1.5', 'lxml', 'html2text', - 'ordereddict' + 'ordereddict', + 'wheezy.template' ] )