2010-12-02 03:22:24 +00:00
|
|
|
import argparse
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
from pelican.settings import read_settings
|
|
|
|
|
from pelican.utils import clean_output_dir
|
|
|
|
|
from pelican.writers import Writer
|
2010-12-18 20:16:48 +00:00
|
|
|
from pelican.generators import (ArticlesGenerator, PagesGenerator,
|
2010-12-02 03:22:24 +00:00
|
|
|
StaticGenerator, PdfGenerator)
|
|
|
|
|
|
|
|
|
|
|
2010-12-18 20:16:48 +00:00
|
|
|
def init_params(settings=None, path=None, theme=None, output_path=None,
|
2010-12-14 23:45:45 +01:00
|
|
|
markup=None, keep=False):
|
2010-12-02 03:22:24 +00:00
|
|
|
"""Read the settings, and performs some checks on the environment
|
|
|
|
|
before doing anything else.
|
|
|
|
|
"""
|
|
|
|
|
if settings is None:
|
|
|
|
|
settings = {}
|
|
|
|
|
settings = read_settings(settings)
|
|
|
|
|
path = path or settings['PATH']
|
|
|
|
|
if path.endswith('/'):
|
|
|
|
|
path = path[:-1]
|
|
|
|
|
|
|
|
|
|
# define the default settings
|
|
|
|
|
theme = theme or settings['THEME']
|
|
|
|
|
output_path = output_path or settings['OUTPUT_PATH']
|
|
|
|
|
output_path = os.path.realpath(output_path)
|
|
|
|
|
markup = markup or settings['MARKUP']
|
2010-12-14 23:45:45 +01:00
|
|
|
keep = keep or settings['KEEP_OUTPUT_DIRECTORY']
|
2010-12-02 03:22:24 +00:00
|
|
|
|
|
|
|
|
# find the theme in pelican.theme if the given one does not exists
|
|
|
|
|
if not os.path.exists(theme):
|
|
|
|
|
theme_path = os.sep.join([os.path.dirname(
|
|
|
|
|
os.path.abspath(__file__)), "themes/%s" % theme])
|
|
|
|
|
if os.path.exists(theme_path):
|
|
|
|
|
theme = theme_path
|
|
|
|
|
else:
|
|
|
|
|
raise Exception("Impossible to find the theme %s" % theme)
|
|
|
|
|
|
|
|
|
|
if 'SITEURL' not in settings:
|
|
|
|
|
settings['SITEURL'] = output_path
|
|
|
|
|
|
|
|
|
|
# get the list of files to parse
|
|
|
|
|
if not path:
|
|
|
|
|
raise Exception('you need to specify a path to search the docs on !')
|
|
|
|
|
|
2010-12-14 23:45:45 +01:00
|
|
|
return settings, path, theme, output_path, markup, keep
|
2010-12-02 03:22:24 +00:00
|
|
|
|
|
|
|
|
|
2010-12-14 23:45:45 +01:00
|
|
|
def run_generators(generators, settings, path, theme, output_path, markup, keep):
|
2010-12-02 03:22:24 +00:00
|
|
|
"""Run the generators and return"""
|
|
|
|
|
|
|
|
|
|
context = settings.copy()
|
2010-12-18 20:16:48 +00:00
|
|
|
generators = [p(context, settings, path, theme, output_path, markup, keep)
|
2010-12-02 03:22:24 +00:00
|
|
|
for p in generators]
|
|
|
|
|
|
|
|
|
|
for p in generators:
|
|
|
|
|
if hasattr(p, 'generate_context'):
|
|
|
|
|
p.generate_context()
|
|
|
|
|
|
|
|
|
|
# erase the directory if it is not the source
|
2010-12-14 23:45:45 +01:00
|
|
|
if output_path not in os.path.realpath(path) and not keep:
|
2010-12-02 03:22:24 +00:00
|
|
|
clean_output_dir(output_path)
|
|
|
|
|
|
2010-12-13 22:46:53 +00:00
|
|
|
writer = Writer(output_path)
|
|
|
|
|
|
2010-12-02 03:22:24 +00:00
|
|
|
for p in generators:
|
|
|
|
|
if hasattr(p, 'generate_output'):
|
|
|
|
|
p.generate_output(writer)
|
|
|
|
|
|
|
|
|
|
|
2010-12-14 23:45:45 +01:00
|
|
|
def run_pelican(settings, path, theme, output_path, markup, delete):
|
2010-12-02 03:22:24 +00:00
|
|
|
"""Run pelican with the given parameters"""
|
|
|
|
|
|
2010-12-14 23:45:45 +01:00
|
|
|
params = init_params(settings, path, theme, output_path, markup, delete)
|
2010-12-02 03:22:24 +00:00
|
|
|
generators = [ArticlesGenerator, PagesGenerator, StaticGenerator]
|
|
|
|
|
if params[0]['PDF_GENERATOR']: # param[0] is settings
|
|
|
|
|
processors.append(PdfGenerator)
|
|
|
|
|
run_generators(generators, *params)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
parser = argparse.ArgumentParser(description="""A tool to generate a
|
|
|
|
|
static blog, with restructured text input files.""")
|
|
|
|
|
|
|
|
|
|
parser.add_argument(dest='path',
|
|
|
|
|
help='Path where to find the content files')
|
|
|
|
|
parser.add_argument('-t', '--theme-path', dest='theme',
|
|
|
|
|
help='Path where to find the theme templates. If not specified, it will'
|
|
|
|
|
'use the default one included with pelican.')
|
|
|
|
|
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.')
|
2010-12-16 20:44:42 +01:00
|
|
|
parser.add_argument('-m', '--markup', default='', dest='markup',
|
|
|
|
|
help='the markup language to use (rst or md).')
|
2010-12-02 03:22:24 +00:00
|
|
|
parser.add_argument('-s', '--settings', dest='settings',
|
|
|
|
|
help='the settings of the application. Default to None.')
|
2010-12-18 20:17:32 +00:00
|
|
|
parser.add_argument('-k', '--keep-output-directory', dest='keep',
|
|
|
|
|
action='store_true',
|
|
|
|
|
help='Keep the output directory and just update all the generated files.'
|
|
|
|
|
'Default is to delete the output directory.')
|
2010-12-02 03:22:24 +00:00
|
|
|
args = parser.parse_args()
|
2010-12-16 20:44:42 +01:00
|
|
|
markup = [a.strip().lower() for a in args.markup.split(',')]
|
2010-12-02 03:22:24 +00:00
|
|
|
|
2010-12-14 23:45:45 +01:00
|
|
|
run_pelican(args.settings, args.path, args.theme, args.output, markup, args.keep)
|
2010-12-02 03:22:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|