commit 0bff1b3be8b33bc7f19d0035050ea28b82bf5eba
parent 078b8decc4b19d55a723cf5553ba11a9434766dc
Author: Sam Kingston <sam@sjkwi.com.au>
Date: Sun, 10 Oct 2010 17:52:24 +1000
removed hardcoded website url and replaced it with the --website argument to constance.py
Diffstat:
11 files changed, 54 insertions(+), 36 deletions(-)
diff --git a/blog.py b/blog.py
@@ -49,8 +49,9 @@ class BlogEntry(object):
self.guid = self.metadata['guid']
self.language = self.metadata.get('language', None)
- def generate_atom(self):
- return template_loader.load('entry.atom').generate(item=self)
+ def generate_atom(self, website):
+ return template_loader.load('entry.atom').generate(item=self,
+ website=website)
class BlogEntrySet(object):
@@ -65,21 +66,24 @@ class BlogEntrySet(object):
def __iter__(self):
return iter(self.entries)
-def generate(dir, xslt):
+def generate(dir, xslt, website):
entries = BlogEntrySet(dir)
for entry in entries:
- rendered = template_loader.load('entry.html').generate(item=entry).render('xhtml')
+ rendered = template_loader.load('entry.html').generate(item=entry,
+ website=website).render('xhtml')
transformed = str(xslt(lxml.etree.fromstring(rendered)))
constance.output(os.path.join(dir, entry.id.encode('utf8') + '.html'), transformed)
# index
- rendered = template_loader.load('index.html').generate(items=entries).render('xhtml')
+ rendered = template_loader.load('index.html').generate(items=entries,
+ website=website).render('xhtml')
transformed = str(xslt(lxml.etree.fromstring(rendered)))
constance.output(os.path.join(dir, 'index.html'), transformed)
# feed
- rendered = template_loader.load('index.atom').generate(items=entries).render('xml')
+ rendered = template_loader.load('index.atom').generate(items=entries,
+ website=website).render('xml')
constance.output(os.path.join(dir, 'index.atom'), rendered)
return entries
diff --git a/constance.py b/constance.py
@@ -35,25 +35,33 @@ def main():
parser.add_option('--tags-dir', metavar='DIR')
parser.add_option('--root-dir', metavar='DIR')
parser.add_option('--xslt', metavar='FILENAME')
+ parser.add_option('--website', metavar='URL')
parser.set_defaults(blog_dir='./blog/',
reading_log=None,
tags_dir='./tags/',
root_dir='./',
- xslt='./style.xsl')
+ xslt='./style.xsl',
+ website='http://localhost')
options, args = parser.parse_args()
+ # strip trailing slash if it was given
+ website = options.website
+ if website[-1] == '/':
+ website = website[:-1]
+
xslt = lxml.etree.XSLT(lxml.etree.parse(options.xslt))
- blog_entries = blog.generate(options.blog_dir, xslt)
+ blog_entries = blog.generate(options.blog_dir, xslt, website)
if options.reading_log is not None:
- reading_entries = reading.generate(options.reading_log, xslt)
+ reading_entries = reading.generate(options.reading_log, xslt, website)
else:
reading_entries = []
- tags.generate(options.tags_dir, xslt, blog_entries)
+ tags.generate(options.tags_dir, xslt, blog_entries, website)
for filename in os.listdir(options.root_dir):
if filename.endswith('.html.in'):
transformed = str(xslt(lxml.etree.parse(filename)))
output(filename[:-3], transformed)
- homepage.generate(options.root_dir, xslt, blog_entries, reading_entries)
+ homepage.generate(options.root_dir, xslt, blog_entries, reading_entries,
+ website)
if __name__ == '__main__':
main()
diff --git a/homepage.py b/homepage.py
@@ -13,13 +13,16 @@ template_loader = genshi.template.TemplateLoader(
os.path.join(os.path.dirname(__file__), 'templates', 'homepage'),
variable_lookup='strict')
-def generate(dir, xslt, blog_entries, reading_entries):
+def generate(dir, xslt, blog_entries, reading_entries, website):
# index
template = template_loader.load('index.html')
- rendered = template.generate(blog_entries=blog_entries, reading_entries=reading_entries).render('xhtml')
+ rendered = template.generate(blog_entries=blog_entries,
+ reading_entries=reading_entries, website=website).render('xhtml')
transformed = str(xslt(lxml.etree.fromstring(rendered)))
constance.output(os.path.join(dir, 'index.html'), transformed)
# firehose
- rendered = template_loader.load('firehose.atom').generate(items=chain(blog_entries, reading_entries)).render('xml')
+ rendered = template_loader.load('firehose.atom').generate(
+ items=chain(blog_entries, reading_entries),
+ website=website).render('xml')
constance.output(os.path.join(dir, 'firehose.atom'), rendered)
diff --git a/reading.py b/reading.py
@@ -39,15 +39,17 @@ class ReadingLogEntrySet(object):
def __iter__(self):
return iter(self.entries)
-def generate(filename, xslt):
+def generate(filename, xslt, website):
entries = ReadingLogEntrySet(filename)
- rendered = template_loader.load('reading.html').generate(items=entries).render('xhtml')
+ rendered = template_loader.load('reading.html').generate(items=entries,
+ website=website).render('xhtml')
transformed = str(xslt(lxml.etree.fromstring(rendered)))
constance.output(os.path.join(os.path.dirname(filename), 'reading.html'), transformed)
# feed
- rendered = template_loader.load('reading.atom').generate(items=entries).render('xml')
+ rendered = template_loader.load('reading.atom').generate(items=entries,
+ website=website).render('xml')
constance.output(os.path.join(os.path.dirname(filename), 'reading.atom'), rendered)
return entries
diff --git a/tags.py b/tags.py
@@ -12,7 +12,7 @@ template_loader = genshi.template.TemplateLoader(
os.path.join(os.path.dirname(__file__), 'templates', 'tags'),
variable_lookup='strict')
-def generate(dir, xslt, blog_entries):
+def generate(dir, xslt, blog_entries, website):
tag_freqs = {}
for entry in blog_entries:
for tag in entry.tags:
@@ -24,6 +24,7 @@ def generate(dir, xslt, blog_entries):
transformed = str(xslt(lxml.etree.fromstring(rendered)))
constance.output(os.path.join(dir, tag.encode('utf8') + '.html'), transformed)
- rendered = template_loader.load('index.html').generate(tag_freqs=tag_freqs).render('xhtml')
+ rendered = template_loader.load('index.html').generate(tag_freqs=tag_freqs,
+ website=website).render('xhtml')
transformed = str(xslt(lxml.etree.fromstring(rendered)))
constance.output(os.path.join(dir, 'index.html'), transformed)
diff --git a/templates/blog/entry.atom b/templates/blog/entry.atom
@@ -15,10 +15,10 @@ from viewutils import ATOM_TIME_FORMAT
<name>Dan C</name>
<email>djc@djc.id.au</email>
</author>
-<category py:for="tag in item.tags" scheme="http://www.djc.id.au/tags/" term="${tag}" />
-<link rel="alternate" href="http://www.djc.id.au/blog/${urllib.quote(item.id.encode('utf8'), '')}" />
+<category py:for="tag in item.tags" scheme="${website}/tags/" term="${tag}" />
+<link rel="alternate" href="${website}/blog/${urllib.quote(item.id.encode('utf8'), '')}" />
<title type="text">${item.title.striptags()}</title>
-<content type="xhtml" xml:base="http://www.djc.id.au/blog/${urllib.quote(item.id.encode('utf8'), '')}">
+<content type="xhtml" xml:base="${website}/blog/${urllib.quote(item.id.encode('utf8'), '')}">
<div xmlns="http://www.w3.org/1999/xhtml">
${item.body}
</div>
diff --git a/templates/blog/entry.html b/templates/blog/entry.html
@@ -13,7 +13,7 @@ from viewutils import tag_list
<body>
<div class="item blog-entry" py:attrs="(item.language is not None) and {'lang': item.language} or {}">
- <h1 class="entry-title"><a href="http://www.djc.id.au/blog/${item.id}" rel="bookmark">${item.title}</a></h1>
+ <h1 class="entry-title"><a href="${website}/blog/${item.id}" rel="bookmark">${item.title}</a></h1>
<div class="date published">${item.publication_date.strftime(str('%-1d %b %Y'))}</div>
diff --git a/templates/blog/index.atom b/templates/blog/index.atom
@@ -7,15 +7,15 @@ from viewutils import ATOM_TIME_FORMAT
sorted_items = sorted(items, key=lambda item: item.publication_date, reverse=True)
?>
-<id>http://www.djc.id.au/blog/index.atom</id>
+<id>${website}/blog/index.atom</id>
<title type="text">Dan’s blog</title>
-<link rel="self" type="application/atom+xml" href="http://www.djc.id.au/blog/index.atom" />
-<link rel="alternate" href="http://www.djc.id.au/blog/" />
+<link rel="self" type="application/atom+xml" href="${website}/blog/index.atom" />
+<link rel="alternate" href="${website}/blog/" />
<generator>constance</generator>
<updated py:if="sorted_items">${max(item.modified_date for item in sorted_items).strftime(ATOM_TIME_FORMAT)}</updated>
<py:for each="item in sorted_items">
- ${item.generate_atom()}
+ ${item.generate_atom(website)}
</py:for>
</feed>
diff --git a/templates/homepage/firehose.atom b/templates/homepage/firehose.atom
@@ -7,15 +7,15 @@ from viewutils import ATOM_TIME_FORMAT
sorted_items = sorted(items, key=lambda item: item.publication_date, reverse=True)
?>
-<id>http://www.djc.id.au/firehose.atom</id>
+<id>${website}/firehose.atom</id>
<title type="text">Dan’s firehose</title>
-<link rel="self" type="application/atom+xml" href="http://www.djc.id.au/firehose.atom" />
-<link rel="alternate" href="http://www.djc.id.au/" />
+<link rel="self" type="application/atom+xml" href="${website}/firehose.atom" />
+<link rel="alternate" href="${website}/" />
<generator>constance</generator>
<updated py:if="sorted_items">${max(item.modified_date for item in sorted_items).strftime(ATOM_TIME_FORMAT)}</updated>
<py:for each="item in sorted_items">
- ${item.generate_atom()}
+ ${item.generate_atom(website)}
</py:for>
</feed>
diff --git a/templates/reading/entry.atom b/templates/reading/entry.atom
@@ -13,7 +13,7 @@ from viewutils import ATOM_TIME_FORMAT
<name>Dan C</name>
<email>djc@djc.id.au</email>
</author>
-<category py:for="tag in item.tags" scheme="http://www.djc.id.au/tags/" term="${tag}" />
+<category py:for="tag in item.tags" scheme="${website}/tags/" term="${tag}" />
<title type="text">${item.title.striptags()} by ${item.author}</title>
<summary py:if="item.rating" type="text">${item.rating} stars</summary>
<content type="xhtml">
diff --git a/templates/reading/reading.atom b/templates/reading/reading.atom
@@ -7,15 +7,15 @@ from viewutils import ATOM_TIME_FORMAT
sorted_items = sorted(items, key=lambda item: item.publication_date, reverse=True)
?>
-<id>http://www.djc.id.au/reading.atom</id>
+<id>${website}/reading.atom</id>
<title type="text">Dan’s reading log</title>
-<link rel="self" type="application/atom+xml" href="http://www.djc.id.au/reading.atom" />
-<link rel="alternate" href="http://www.djc.id.au/reading" />
+<link rel="self" type="application/atom+xml" href="${website}/reading.atom" />
+<link rel="alternate" href="${website}/reading" />
<generator>constance</generator>
<updated py:if="sorted_items">${max(item.modified_date for item in sorted_items).strftime(ATOM_TIME_FORMAT)}</updated>
<py:for each="item in sorted_items">
- ${item.generate_atom()}
+ ${item.generate_atom(website)}
</py:for>
</feed>