Skip to content

Commit

Permalink
Use relative URLS instead of static ones.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Metaireau committed Nov 20, 2010
1 parent 56fb482 commit 0e95084
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions pelican/generators.py
Expand Up @@ -64,7 +64,8 @@ def run(self, processors):
for p in processors:
p.process(context, self)

def generate_feed(self, elements, context, filename=None):
def generate_feed(self, elements, context, filename=None,
relative_urls=True):
"""Generate a feed with the list of articles provided
Return the feed. If no output_path or filename is specified, just return
Expand All @@ -74,16 +75,22 @@ def generate_feed(self, elements, context, filename=None):
:param context: the context to get the feed metadatas.
:param output_path: where to output the file.
:param filename: the filename to output.
:param relative_urls: use relative urls or absolutes ones
"""
if relative_urls:
site_url = self._get_relative_siteurl(filename)
else:
site_url = context['SITEURL']

feed = Atom1Feed(
title=context['SITENAME'],
link=context['SITEURL'],
feed_url='%s/%s' % (context['SITEURL'], filename),
link=site_url,
feed_url= filename,
description=context.get('SITESUBTITLE', ''))
for element in elements:
feed.add_item(
title=element.title,
link='%s/%s' % (context['SITEURL'], element.url),
link= element.url,
description=element.content,
author_name=getattr(element, 'author', 'John Doe'),
pubdate=element.date)
Expand All @@ -101,15 +108,20 @@ def generate_feed(self, elements, context, filename=None):
fp.close()
return feed

def generate_file(self, name, template, context, **kwargs):
def generate_file(self, name, template, context, relative_urls=True,
**kwargs):
"""Write the file with the given informations
:param name: name of the file to output
:param template: template to use to generate the content
:param context: dict to pass to the templates.
:param relative_urls: use relative urls or absolutes ones
:param **kwargs: additional variables to pass to the templates
"""
context = context.copy()
if relative_urls:
context['SITEURL'] = self._get_relative_siteurl(name)

context.update(kwargs)
output = template.render(context)
filename = os.sep.join((self.output_path, name))
Expand Down Expand Up @@ -154,3 +166,7 @@ def get_files(self, path, exclude=[], extensions=None):
files.extend([os.sep.join((root, f)) for f in temp_files
if True in [f.endswith(ext) for ext in extensions]])
return files

def _get_relative_siteurl(self, filename):
"""Return the siteurl relative to the given filename"""
return '../' * filename.count('/') + '.'

0 comments on commit 0e95084

Please sign in to comment.