Skip to content

Commit

Permalink
Add a way to specify CSS files. Add default settings too.
Browse files Browse the repository at this point in the history
--HG--
rename : samples/themes/notmyidea/archives.html => samples/themes/notmyidea/templates/archives.html
rename : samples/themes/notmyidea/article.html => samples/themes/notmyidea/templates/article.html
rename : samples/themes/notmyidea/base.html => samples/themes/notmyidea/templates/base.html
rename : samples/themes/notmyidea/categories.html => samples/themes/notmyidea/templates/categories.html
rename : samples/themes/notmyidea/category.html => samples/themes/notmyidea/templates/category.html
rename : samples/themes/notmyidea/index.html => samples/themes/notmyidea/templates/index.html
rename : samples/themes/notmyidea/tag.html => samples/themes/notmyidea/templates/tag.html
rename : samples/themes/notmyidea/tags.html => samples/themes/notmyidea/templates/tags.html
  • Loading branch information
Alexis Metaireau committed Aug 18, 2010
1 parent 26da943 commit a114521
Show file tree
Hide file tree
Showing 14 changed files with 501 additions and 26 deletions.
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
* Add separated CSS files somewhere
* Fall back to settings + date of files when no metadata available
* Filter to generate only the files with .rst extension
* Add a licence
Expand Down
52 changes: 35 additions & 17 deletions pelican/generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import os
import re
import shutil
from codecs import open
from datetime import datetime
from docutils import core
Expand All @@ -14,29 +15,38 @@
_TEMPLATES = ('index', 'tag', 'tags', 'article', 'category', 'categories',
'archives')
_DIRECT_TEMPLATES = ('index', 'tags', 'categories', 'archives')
_DEFAULT_TEMPLATE_PATH =\
_DEFAULT_THEME =\
os.sep.join([os.path.dirname(os.path.abspath(__file__)), "themes"])
_DEFAULT_CONFIG = {'PATH': None,
'THEME': _DEFAULT_THEME,
'OUTPUT_PATH': 'output/',
'MARKUP': 'rst'}


def generate_output(files, templates_path=None, output_path=None, markup=None,
def generate_output(path=None, theme=None, output_path=None, markup=None,
settings=None):
"""Given a list of files, a template and a destination,
output the static files.
:param files: the list of files to parse
:param templates_path: where to search for templates
: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 markup: the markup language to use while parsing
:param settings: the settings file to use
"""
if not templates_path:
templates_path = _DEFAULT_TEMPLATE_PATH
if not output_path:
output_path = './output'
# get the settings
context = read_settings(settings)
path = path or context['PATH']
theme = theme or context['THEME']
output_path = output_path or context['OUTPUT_PATH']
output_path = os.path.realpath(output_path)
markup = markup or context['MARKUP']

# get the list of files to parse
files = []
for root, dirs, temp_files in os.walk(path, followlinks=True):
files.extend([os.sep.join((root, f)) for f in temp_files])

articles, dates, years, tags, categories = [], {}, {}, {}, {}

# for each file, get the informations.
for f in files:
f = os.path.abspath(f)
Expand All @@ -53,15 +63,14 @@ def generate_output(files, templates_path=None, output_path=None, markup=None,

# order the articles by date
articles.sort(key=attrgetter('date'), reverse=True)
templates = get_templates(templates_path)
context = {}
templates = get_templates(theme)
for item in ('articles', 'dates', 'years', 'tags', 'categories'):
value = locals()[item]
if hasattr(value, 'items'):
value = value.items()
context[item] = value
read_settings(context, settings)


# generate the output
generate = partial(generate_file, output_path)
for template in _DIRECT_TEMPLATES:
generate('%s.html' % template, templates[template], context)
Expand All @@ -74,6 +83,13 @@ def generate_output(files, templates_path=None, output_path=None, markup=None,
generate('%s' % article.url,
templates['article'], context, article=article)

# copy css path to output/css
try:
shutil.copytree(os.path.join(theme, 'css'),
os.path.join(output_path, 'css'))
except OSError:
pass


def generate_file(path, name, template, context, **kwargs):
context.update(kwargs)
Expand All @@ -89,6 +105,7 @@ def generate_file(path, name, template, context, **kwargs):


def get_templates(path=None):
path = os.path.join(path, 'templates')
env = Environment(loader=FileSystemLoader(path))
templates = {}
for template in _TEMPLATES:
Expand All @@ -102,9 +119,10 @@ def update_dict(mapping, key, value):
mapping[key].append(value)


def read_settings(context, filename):
def read_settings(filename):
"""Load a Python file into a dictionary.
"""
context = _DEFAULT_CONFIG.copy()
if filename:
tempdict = {}
execfile(filename, tempdict)
Expand Down Expand Up @@ -158,11 +176,11 @@ class Article(object):

def __init__(self, string, markup=None):
if markup == None:
markup = 'rest'
markup = 'rst'

for key, value in parse_metadata(string).items():
setattr(self, key, value)
if markup == 'rest':
if markup == 'rst':
extra_params = {'input_encoding': 'unicode',
'initial_header_level': '2'}
rendered_content = core.publish_parts(string, writer_name='html',
Expand Down
15 changes: 7 additions & 8 deletions pelican/pelican
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,24 @@ files to read and a template to use.
The main use case is to generate static-files-based blogs, to ease DVCSes as
storages, but it could be used with others goal in mind.""")
parser.add_argument('-p', '--path', default='content', dest='path',

parser.add_argument('-p', '--path', dest='path',
help='Path where to find the content files (default is "content").')
parser.add_argument('-t', '--templates-path', default=None, dest='templates',
parser.add_argument('-t', '--templates-path', dest='templates',
help='Path where to find the templates. If not specified, will uses the'
' ones included with pelican.')
parser.add_argument('-o', '--output', default=None, dest='output',
parser.add_argument('-o', '--output', dest='output',
help='Where to output the generated files. If not specified, a directory'
' will be created, named "output" in the current path.')
parser.add_argument('-m', '--markup', default='rest', dest='markup',
parser.add_argument('-m', '--markup', default='rst', dest='markup',
help='the markup language to use. Currently only ReSTreucturedtext is'
' available.')
parser.add_argument('-s', '--settings', default=None, dest='settings',
parser.add_argument('-s', '--settings', dest='settings',
help='the settings of the application. Default to None.')

if __name__ == '__main__':
args = parser.parse_args()
files = []
for root, dirs, temp_files in os.walk(args.path, followlinks=True):
files.extend([os.sep.join((root, f)) for f in temp_files])
generate_output(files, args.templates, args.output, args.markup,
generate_output(args.path, args.templates, args.output, args.markup,
args.settings)
print 'Done !'
2 changes: 2 additions & 0 deletions samples/test_settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
PATH = 'samples/content'
THEME = 'samples/themes/notmyidea'

BLOGNAME = 'NotMyIdea.org'
BLOGSUBTITLE = u"Alexis Métaireau's weblog"
Expand Down
Loading

0 comments on commit a114521

Please sign in to comment.