Add a way to output pages as well.

This commit is contained in:
Alexis Metaireau 2010-10-30 20:17:23 +01:00
commit 340ddf4aa8
5 changed files with 109 additions and 45 deletions

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
import argparse import argparse
from pelican.generators import ArticlesGenerator from pelican.generators import ArticlesGenerator, PagesGenerator
parser = argparse.ArgumentParser(description="""A tool to generate a parser = argparse.ArgumentParser(description="""A tool to generate a
static blog, with restructured text input files.""") static blog, with restructured text input files.""")
@ -23,6 +23,15 @@ parser.add_argument('-s', '--settings', dest='settings',
if __name__ == '__main__': if __name__ == '__main__':
args = parser.parse_args() args = parser.parse_args()
gen = ArticlesGenerator(args.settings)
gen.generate(args.path, args.theme, args.output, args.markup) articles = ArticlesGenerator(args.settings)
pages = PagesGenerator(args.settings)
context = {}
for gen in articles, pages:
context.update(gen.create_context(args.path, args.theme, args.output,
args.markup))
for gen in articles, pages:
gen.generate(context)
print "Enjoy !" print "Enjoy !"

View file

@ -8,7 +8,7 @@ class Page(object):
:param string: the string to parse, containing the original content. :param string: the string to parse, containing the original content.
:param markup: the markup language to use while parsing. :param markup: the markup language to use while parsing.
""" """
mandatory_properties = ('title') mandatory_properties = ('title',)
def __init__(self, content, metadatas={}, settings={}): def __init__(self, content, metadatas={}, settings={}):
self.content = content self.content = content

View file

@ -10,13 +10,13 @@ from feedgenerator import Atom1Feed
from pelican.utils import update_dict from pelican.utils import update_dict
from pelican.settings import read_settings from pelican.settings import read_settings
from pelican.contents import Article from pelican.contents import Article, Page
from pelican.readers import read_file from pelican.readers import read_file
## Constants ########################################################## ## Constants ##########################################################
_TEMPLATES = ('index', 'tag', 'tags', 'article', 'category', 'categories', _TEMPLATES = ('index', 'tag', 'tags', 'article', 'category', 'categories',
'archives') 'archives', 'page')
_DIRECT_TEMPLATES = ('index', 'tags', 'categories', 'archives') _DIRECT_TEMPLATES = ('index', 'tags', 'categories', 'archives')
@ -92,6 +92,7 @@ class Generator(object):
:param context: dict to pass to the templates. :param context: dict to pass to the templates.
:param **kwargs: additional variables to pass to the templates :param **kwargs: additional variables to pass to the templates
""" """
context = context.copy()
context.update(kwargs) context.update(kwargs)
output = template.render(context) output = template.render(context)
filename = os.sep.join((self.output_path, name)) filename = os.sep.join((self.output_path, name))
@ -128,7 +129,47 @@ class Generator(object):
except: except:
pass pass
def _get_context(self, items):
"""Return the context to be used in templates"""
# create a new context only if none currently exists.
if not hasattr(self, "context"):
context = self.settings.copy()
else:
context = self.context
# put all we need in the context, to generate the output
for item in items:
value = getattr(self, item)
if hasattr(value, 'items'):
value = value.items()
context[item] = value
return context
def get_files(self, path, exclude=[]):
"""Return the files to use to use in this generator
:param path: the path to search the file on
:param exclude: the list of path to exclude
"""
files = []
for root, dirs, temp_files in os.walk(path, followlinks=True):
for e in exclude:
if e in dirs:
dirs.remove(e)
files.extend([os.sep.join((root, f)) for f in temp_files
if f.endswith(self.format)])
return files
def is_valid_content(self, content, f):
try:
content.check_properties()
return True
except NameError as e:
print u" [info] Skipping %s: impossible to find informations about '%s'" % (f, e)
return False
class ArticlesGenerator(Generator): class ArticlesGenerator(Generator):
def __init__(self, settings=None): def __init__(self, settings=None):
@ -139,14 +180,6 @@ class ArticlesGenerator(Generator):
self.tags = {} self.tags = {}
self.categories = {} self.categories = {}
def get_files(self, path):
"""Return the files to use to use in this generator"""
files = []
for root, dirs, temp_files in os.walk(path, followlinks=True):
files.extend([os.sep.join((root, f)) for f in temp_files
if f.endswith(self.format)])
return files
def process_files(self, files): def process_files(self, files):
"""Process all the files and build the lists and dicts of """Process all the files and build the lists and dicts of
articles/categories/etc. articles/categories/etc.
@ -161,10 +194,7 @@ class ArticlesGenerator(Generator):
metadatas['category'] = unicode(category) metadatas['category'] = unicode(category)
article = Article(content, metadatas, settings=self.settings) article = Article(content, metadatas, settings=self.settings)
try: if not self.is_valid_content(article, f):
article.check_properties()
except NameError as e:
print u"[info] Skipping %s: impossible to find informations about '%s'" % (f, e)
continue continue
update_dict(self.dates, article.date.strftime('%Y-%m-%d'), article) update_dict(self.dates, article.date.strftime('%Y-%m-%d'), article)
@ -175,18 +205,6 @@ class ArticlesGenerator(Generator):
update_dict(self.tags, tag, article) update_dict(self.tags, tag, article)
self.articles.append(article) self.articles.append(article)
def _get_context(self):
"""Return the context to be used in templates"""
context = self.settings.copy()
# put all we need in the context, to generate the output
for item in ('articles', 'dates', 'years', 'tags', 'categories'):
value = getattr(self, item)
if hasattr(value, 'items'):
value = value.items()
context[item] = value
return context
def generate_feeds(self, context): def generate_feeds(self, context):
"""Generate the feeds from the current context, and output files.""" """Generate the feeds from the current context, and output files."""
@ -231,26 +249,52 @@ class ArticlesGenerator(Generator):
except OSError: except OSError:
pass pass
def generate(self, path=None, theme=None, output_path=None, fmt=None): def create_context(self, path=None, theme=None, output_path=None, fmt=None):
"""Search the given path for files, and generate a static blog in output,
using the given theme.
:param path: the path where to find the files to parse
:param theme: where to search for templates
:param output_path: where to output the generated files
:param settings: the settings file to use
:param fmt: the format of the files to read. It's a list.
"""
self._init_params(path, theme, output_path, fmt) self._init_params(path, theme, output_path, fmt)
# build the list of articles / categories / etc. # build the list of articles / categories / etc.
self.process_files(self.get_files(path)) self.process_files(self.get_files(path, ['pages',]))
# sort the articles by date # sort the articles by date
self.articles.sort(key=attrgetter('date'), reverse=True) self.articles.sort(key=attrgetter('date'), reverse=True)
# and generate the output :) # and generate the output :)
context = self._get_context() return self._get_context(('articles', 'dates', 'years', 'tags',
'categories'))
def generate(self, context):
self.generate_feeds(context) self.generate_feeds(context)
self.generate_pages(context) self.generate_pages(context)
self.generate_static_content() self.generate_static_content()
class PagesGenerator(Generator):
"""Generate pages"""
def __init__(self, settings=None):
super(PagesGenerator, self).__init__(settings)
self.pages = []
def process_files(self, files):
"""Process all the files and build the lists and dicts of
articles/categories/etc.
"""
for f in files:
content, metadatas = read_file(f)
page = Page(content, metadatas, settings=self.settings)
if not self.is_valid_content(page, f):
continue
self.pages.append(page)
def generate_pages(self, context):
templates = self.get_templates(self.theme)
for page in self.pages:
self.generate_file('pages/%s' % page.url,
templates['page'], context, page=page)
def create_context(self, path=None, theme=None, output_path=None, fmt=None):
self._init_params(path, theme, output_path, fmt)
self.process_files(self.get_files(os.sep.join((path, 'pages'))))
return self._get_context(('pages',))
def generate(self, context):
self.generate_pages(context)

View file

@ -30,6 +30,9 @@
{% for title, link in MENUITEMS %} {% for title, link in MENUITEMS %}
<li><a href="{{ link }}">{{ title }}</a></li> <li><a href="{{ link }}">{{ title }}</a></li>
{% endfor %} {% endfor %}
{% for title, link in PAGES %}
<li><a href="{{ SITEURL }}/{{ link }}">{{ title }}</a></li>
{% endfor %}
{% for cat, null in categories %} {% for cat, null in categories %}
<li {% if cat == category %}class="active"{% endif %}><a href="{{ SITEURL }}/category/{{ cat }}.html">{{ cat }}</a></li> <li {% if cat == category %}class="active"{% endif %}><a href="{{ SITEURL }}/category/{{ cat }}.html">{{ cat }}</a></li>
{% endfor %} {% endfor %}
@ -38,11 +41,11 @@
{% block content %} {% block content %}
{% endblock %} {% endblock %}
<section id="extras" class="body"> <section id="extras" class="body">
{% if SITEROLL %} {% if LINKS %}
<div class="blogroll"> <div class="blogroll">
<h2>blogroll</h2> <h2>blogroll</h2>
<ul> <ul>
{% for name, link in SITEROLL %} {% for name, link in LINKS %}
<li><a href="{{ link }}">{{ name }}</a></li> <li><a href="{{ link }}">{{ name }}</a></li>
{% endfor %} {% endfor %}
</ul> </ul>

View file

@ -0,0 +1,8 @@
{% extends "base.html" %}
{% block title %}{{ page.title }}{% endblock %}
{% block content %}
<section id="content" class="body">
<h2>{{ page.title }}</h2>
{{ page.content }}
</section>
{% endblock %}