Skip to content

Commit

Permalink
fix #4 using new rss template and jinja2
Browse files Browse the repository at this point in the history
not more dependent on PyRSS2Gen
added url back as optional param to _update_indexes
  • Loading branch information
yaph committed Sep 8, 2013
1 parent 2564d51 commit a8bda10
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 36 deletions.
50 changes: 16 additions & 34 deletions logya/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,13 @@ def update_index(self, doc, path):

self.indexes[path] = self.indexes.get(path, []) + [doc]

def _update_indexes(self, doc):
def _update_indexes(self, doc, url=None):
"""Add a doc to indexes determined from given url."""

dirs = self.get_dirs_from_path(doc['url'])
if url is None:
url = doc['url']

dirs = self.get_dirs_from_path(url)
last = 0
for d in dirs:
last += 1
Expand Down Expand Up @@ -177,38 +180,17 @@ def index_title(self, s):
def write_rss(self, feed_title, directory, docs):
"""Write RSS 2.0 XML file in target directory"""

items = []
for d in docs[0:self.feed_limit]:
# omit start page
if '/' == d['url']:
continue
url = self.base_url + os.path.join('/', directory, 'rss.xml')
self.template.add_var('url', url)
self.template.add_var('title', feed_title)
self.template.add_var('description', directory)
self.template.add_var('last_build', datetime.datetime.now())
self.template.add_var('items', docs[0:self.feed_limit])

url = self.base_url + d['url']
title = d['title']

description = title
# TODO make sure description is set in Parser class
if 'description' in d:
description = d['description']

items.append(PyRSS2Gen.RSSItem(
title=title,
link=url,
description=description,
guid=url,
pubDate=d['created']))

rss = PyRSS2Gen.RSS2(
title=feed_title,
link=self.base_url + os.path.join('/', directory, 'rss.xml'),
description=directory,
lastBuildDate=datetime.datetime.now(),
items=items)

rss_file_name = os.path.join(self.dir_dst, directory, 'rss.xml')
rss_file = open(rss_file_name, 'w', encoding='utf-8')
rss.write_xml(rss_file)
rss_file.close()
writer = FileWriter()
page = self.template.env.get_template('rss2.xml')
fh = writer.getfile(self.dir_dst, os.path.join(directory, 'rss.xml'))
writer.write(fh, page.render(self.template.get_vars()))

def write_index(self, filewriter, directory, template):
"""Write an auto-generated index.html file."""
Expand All @@ -226,7 +208,7 @@ def write_index(self, filewriter, directory, template):
self.template.add_var('title', self.index_title(directory))
self.template.add_var('directory', directory)

page = self.template.get_env().get_template(template)
page = self.template.env.get_template(template)
filewriter.write(filewriter.getfile(self.dir_dst, directory),
page.render(self.template.get_vars()))

Expand Down
18 changes: 18 additions & 0 deletions logya/sites/docs/templates/rss2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel><title>{{ title }}</title>
<link>{{ url }}</link>
<description>{{ description }}</description>
<lastBuildDate>{{ last_build.strftime("%a, %d %b %Y %H:%M:%S %z") }}</lastBuildDate>
<generator>Logya</generator>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
{% for item in items %}
<item>
<title>{{ item.title }}</title>
<link>{{ base_url }}{{ item.url }}</link>
<description>{{ item.description }}</description>
<guid>{{ base_url }}{{ item.url }}</guid>
<pubDate>{{ item.created.strftime("%a, %d %b %Y %H:%M:%S %z") }}</pubDate>
</item>
{% endfor %}
</channel></rss>
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Jinja2==2.7.1
Markdown==2.3.1
MarkupSafe==0.18
PyRSS2Gen==1.1
PyYAML==3.10
1 change: 0 additions & 1 deletion test_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Jinja2==2.7.1
Markdown==2.3.1
MarkupSafe==0.18
PyRSS2Gen==1.1
PyYAML==3.10
nose==1.3.0

0 comments on commit a8bda10

Please sign in to comment.