From 0a1a868b378d16800719be4b9663a78021815f26 Mon Sep 17 00:00:00 2001 From: Nico Di Rocco Date: Fri, 7 Sep 2012 08:46:38 +0200 Subject: [PATCH 1/3] Added sourcefile generator that generates .text files --- docs/plugins.rst | 10 +++------- docs/settings.rst | 3 +++ pelican/__init__.py | 6 +++--- pelican/generators.py | 10 ++++++++++ pelican/settings.py | 1 + pelican/signals.py | 1 - 6 files changed, 20 insertions(+), 11 deletions(-) diff --git a/docs/plugins.rst b/docs/plugins.rst index 654b18f7..0808ce04 100644 --- a/docs/plugins.rst +++ b/docs/plugins.rst @@ -53,14 +53,10 @@ List of signals Here is the list of currently implemented signals: -========================= ============================ =========================================================================== +========================= ============================ ========================================= Signal Arguments Description -========================= ============================ =========================================================================== +========================= ============================ ========================================= initialized pelican object -finalized pelican object invoked after all the generators are executed and just before pelican exits - usefull for custom post processing actions, such as: - - minifying js/css assets. - - notify/ping search engines with an updated sitemap. article_generate_context article_generator, metadata article_generator_init article_generator invoked in the ArticlesGenerator.__init__ get_generators generators invoked in Pelican.get_generator_classes, @@ -68,7 +64,7 @@ get_generators generators invoked in Pelican.ge generator in a tuple or in a list. pages_generate_context pages_generator, metadata pages_generator_init pages_generator invoked in the PagesGenerator.__init__ -========================= ============================ =========================================================================== +========================= ============================ ========================================= The list is currently small, don't hesitate to add signals and make a pull request if you need them! diff --git a/docs/settings.rst b/docs/settings.rst index 2db98259..a0411e0b 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -69,6 +69,9 @@ Setting name (default value) What doe `PDF_GENERATOR` (``False``) Set to True if you want to have PDF versions of your documents. You will need to install `rst2pdf`. +`OUTPUT_SOURCES` (``False``) Set to True if you want to copy the articles and pages in their + original format (e.g. Markdown or ReStructeredText) to the + specified OUTPUT_PATH. `RELATIVE_URLS` (``True``) Defines whether Pelican should use document-relative URLs or not. If set to ``False``, Pelican will use the SITEURL setting to construct absolute URLs. diff --git a/pelican/__init__.py b/pelican/__init__.py index 0af52c44..620f8406 100644 --- a/pelican/__init__.py +++ b/pelican/__init__.py @@ -9,7 +9,7 @@ import argparse from pelican import signals from pelican.generators import (Generator, ArticlesGenerator, PagesGenerator, - StaticGenerator, PdfGenerator, LessCSSGenerator) + StaticGenerator, PdfGenerator, LessCSSGenerator, SourceFileGenerator) from pelican.log import init from pelican.settings import read_settings, _DEFAULT_CONFIG from pelican.utils import clean_output_dir, files_changed, file_changed, NoFilesError @@ -179,14 +179,14 @@ class Pelican(object): if hasattr(p, 'generate_output'): p.generate_output(writer) - signals.finalized.send(self) - def get_generator_classes(self): generators = [StaticGenerator, ArticlesGenerator, PagesGenerator] if self.settings['PDF_GENERATOR']: generators.append(PdfGenerator) if self.settings['LESS_GENERATOR']: # can be True or PATH to lessc generators.append(LessCSSGenerator) + if self.settings['OUTPUT_SOURCES']: + generators.append(SourceFileGenerator) for pair in signals.get_generators.send(self): (funct, value) = pair diff --git a/pelican/generators.py b/pelican/generators.py index 94edb3b2..b28e5c6d 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -491,6 +491,16 @@ class PdfGenerator(Generator): for page in self.context['pages']: self._create_pdf(page, pdf_path) +class SourceFileGenerator(Generator): + def _create_source(self, obj, output_path): + filename = os.path.splitext(obj.save_as)[0] + dest = os.path.join(output_path, filename + '.text') + copy('', obj.filename, dest) + + def generate_output(self, writer=None): + logger.info(u' Generating source files...') + for object in chain(self.context['articles'], self.context['pages']): + self._create_source(object, self.output_path) class LessCSSGenerator(Generator): """Compile less css files.""" diff --git a/pelican/settings.py b/pelican/settings.py index 92c68ddc..df105673 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -32,6 +32,7 @@ _DEFAULT_CONFIG = {'PATH': '.', 'SITENAME': 'A Pelican Blog', 'DISPLAY_PAGES_ON_MENU': True, 'PDF_GENERATOR': False, + 'OUTPUT_SOURCES': False, 'DEFAULT_CATEGORY': 'misc', 'DEFAULT_DATE': 'fs', 'WITH_FUTURE_DATES': True, diff --git a/pelican/signals.py b/pelican/signals.py index 408d84c9..7ee88a0a 100644 --- a/pelican/signals.py +++ b/pelican/signals.py @@ -1,7 +1,6 @@ from blinker import signal initialized = signal('pelican_initialized') -finalized = signal('pelican_finalized') article_generate_context = signal('article_generate_context') article_generator_init = signal('article_generator_init') get_generators = signal('get_generators') From a7dd21ffafb399b88183e6e71fd022d9f947f439 Mon Sep 17 00:00:00 2001 From: Nico Di Rocco Date: Fri, 28 Sep 2012 14:59:05 +0200 Subject: [PATCH 2/3] Added a new setting OUTPUT_SOURCES_EXTENSION Using this configurable setting users can control what extension will be appended to filenames by the SourcesGenerator. The default is to use the ``.text`` extension. --- docs/settings.rst | 3 +++ pelican/generators.py | 5 ++++- pelican/settings.py | 13 +++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/docs/settings.rst b/docs/settings.rst index a0411e0b..9997f474 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -72,6 +72,9 @@ Setting name (default value) What doe `OUTPUT_SOURCES` (``False``) Set to True if you want to copy the articles and pages in their original format (e.g. Markdown or ReStructeredText) to the specified OUTPUT_PATH. +`OUTPUT_SOURCES_EXTENSION` (``.text``) Controls the extension that will be used by the SourcesGenerator. + Defaults to .text. If the first character is not a `.` the + dot character will be prepended to the extension. `RELATIVE_URLS` (``True``) Defines whether Pelican should use document-relative URLs or not. If set to ``False``, Pelican will use the SITEURL setting to construct absolute URLs. diff --git a/pelican/generators.py b/pelican/generators.py index b28e5c6d..e9646825 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -492,9 +492,12 @@ class PdfGenerator(Generator): self._create_pdf(page, pdf_path) class SourceFileGenerator(Generator): + def generate_context(self): + self.output_extension = self.settings['OUTPUT_SOURCES_EXTENSION'] + def _create_source(self, obj, output_path): filename = os.path.splitext(obj.save_as)[0] - dest = os.path.join(output_path, filename + '.text') + dest = os.path.join(output_path, filename + self.output_extension) copy('', obj.filename, dest) def generate_output(self, writer=None): diff --git a/pelican/settings.py b/pelican/settings.py index df105673..dce1d8ac 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -33,6 +33,7 @@ _DEFAULT_CONFIG = {'PATH': '.', 'DISPLAY_PAGES_ON_MENU': True, 'PDF_GENERATOR': False, 'OUTPUT_SOURCES': False, + 'OUTPUT_SOURCES_EXTENSION': '.text', 'DEFAULT_CATEGORY': 'misc', 'DEFAULT_DATE': 'fs', 'WITH_FUTURE_DATES': True, @@ -175,4 +176,16 @@ def configure_settings(settings, default_settings=None, filename=None): logger.warn("You must install the webassets module to use WEBASSETS.") settings['WEBASSETS'] = False + if 'OUTPUT_SOURCES_EXTENSION' in settings: + try: + if not isinstance(settings['OUTPUT_SOURCES_EXTENSION'], str): + raise ValueError + elif '.' is not settings['OUTPUT_SOURCES_EXTENSION'][0]: + settings['OUTPUT_SOURCES_EXTENSION'] = '.' + settings['OUTPUT_SOURCES_EXTENSION'] + except(ValueError, IndexError): + logger.warn("Detected misconfiguration with OUTPUT_SOURCES_EXTENSION." + " falling back to the default extension " + + _DEFAULT_CONFIG['OUTPUT_SOURCES_EXTENSION']) + settings['OUTPUT_SOURCES_EXTENSION'] = _DEFAULT_CONFIG['OUTPUT_SOURCES_EXTENSION'] + return settings From c53a06a5d5183ce54bd94262b3af30611a59ee86 Mon Sep 17 00:00:00 2001 From: Nico Di Rocco Date: Wed, 3 Oct 2012 22:06:45 +0200 Subject: [PATCH 3/3] Simplified configuration option to be more flexible As @ametaireau suggested: instead of having logic that prepends the OUTPUT_SOURCES_EXTENSION with a '.' we allow the user more flexibility to control the extension that can be used. --- docs/settings.rst | 4 ++-- pelican/settings.py | 9 ++------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/docs/settings.rst b/docs/settings.rst index 9997f474..e9bf54ff 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -73,8 +73,8 @@ Setting name (default value) What doe original format (e.g. Markdown or ReStructeredText) to the specified OUTPUT_PATH. `OUTPUT_SOURCES_EXTENSION` (``.text``) Controls the extension that will be used by the SourcesGenerator. - Defaults to .text. If the first character is not a `.` the - dot character will be prepended to the extension. + Defaults to ``.text``. If not a valid string the default value + will be used. `RELATIVE_URLS` (``True``) Defines whether Pelican should use document-relative URLs or not. If set to ``False``, Pelican will use the SITEURL setting to construct absolute URLs. diff --git a/pelican/settings.py b/pelican/settings.py index dce1d8ac..cd76aa22 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -177,15 +177,10 @@ def configure_settings(settings, default_settings=None, filename=None): settings['WEBASSETS'] = False if 'OUTPUT_SOURCES_EXTENSION' in settings: - try: - if not isinstance(settings['OUTPUT_SOURCES_EXTENSION'], str): - raise ValueError - elif '.' is not settings['OUTPUT_SOURCES_EXTENSION'][0]: - settings['OUTPUT_SOURCES_EXTENSION'] = '.' + settings['OUTPUT_SOURCES_EXTENSION'] - except(ValueError, IndexError): + if not isinstance(settings['OUTPUT_SOURCES_EXTENSION'], str): + settings['OUTPUT_SOURCES_EXTENSION'] = _DEFAULT_CONFIG['OUTPUT_SOURCES_EXTENSION'] logger.warn("Detected misconfiguration with OUTPUT_SOURCES_EXTENSION." " falling back to the default extension " + _DEFAULT_CONFIG['OUTPUT_SOURCES_EXTENSION']) - settings['OUTPUT_SOURCES_EXTENSION'] = _DEFAULT_CONFIG['OUTPUT_SOURCES_EXTENSION'] return settings