From 43e931baa4b8140fe700b23af6758572a8835ce7 Mon Sep 17 00:00:00 2001 From: James Rowe Date: Thu, 24 Feb 2011 05:15:04 +0000 Subject: [PATCH 1/3] Make readers with external dependencies optional. --- pelican/readers.py | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/pelican/readers.py b/pelican/readers.py index 35681ac1..4e1d7b2e 100644 --- a/pelican/readers.py +++ b/pelican/readers.py @@ -1,12 +1,18 @@ # -*- coding: utf-8 -*- -from docutils import core -from markdown import Markdown +try: + from docutils import core + + # import the directives to have pygments support + import rstdirectives +except ImportError: + core = False +try: + from markdown import Markdown +except ImportError: + Markdown = False import re import string -# import the directives to have pygments support -import rstdirectives - from pelican.utils import get_date, open @@ -17,7 +23,12 @@ _METADATAS_PROCESSORS = { } -class RstReader(object): +class Reader(object): + enabled = True + +class RstReader(Reader): + enabled = bool(core) + extension = "rst" def _parse_metadata(self, content): """Return the dict containing metadatas""" @@ -43,7 +54,9 @@ class RstReader(object): metadatas['title'] = title return content, metadatas -class MarkdownReader(object): +class MarkdownReader(Reader): + enabled = bool(Markdown) + extension = "md" def read(self, filename): """Parse content and metadata of markdown files""" @@ -60,7 +73,8 @@ class MarkdownReader(object): return content, metadatas -class HtmlReader(object): +class HtmlReader(Reader): + extension = "html" _re = re.compile('\<\!\-\-\#\s?[A-z0-9_-]*\s?\:s?[A-z0-9\s_-]*\s?\-\-\>') def read(self, filename): @@ -76,8 +90,7 @@ class HtmlReader(object): -_EXTENSIONS = {'rst': RstReader, 'md': MarkdownReader, 'html': HtmlReader} # supported formats - +_EXTENSIONS = dict((cls.extension, cls) for cls in Reader.__subclasses__()) def read_file(filename, fmt=None): """Return a reader object using the given format.""" @@ -86,4 +99,6 @@ def read_file(filename, fmt=None): if fmt not in _EXTENSIONS.keys(): raise TypeError('Pelican does not know how to parse %s' % filename) reader = _EXTENSIONS[fmt]() + if not reader.enabled: + raise ValueError("Missing dependencies for %s" % fmt) return reader.read(filename) From 6fc6a4dac9f0941c99e29636d3ac83cf11f2575d Mon Sep 17 00:00:00 2001 From: James Rowe Date: Thu, 24 Feb 2011 05:17:05 +0000 Subject: [PATCH 2/3] Document Reader.enabled attribute usage. --- docs/internals.rst | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/internals.rst b/docs/internals.rst index 80fc8661..4ff8c91c 100644 --- a/docs/internals.rst +++ b/docs/internals.rst @@ -40,7 +40,8 @@ method, that is returning an HTML content and some metadata. Take a look to the Markdown reader:: - class MarkdownReader(object): + class MarkdownReader(Reader): + enabled = bool(Markdown) def read(self, filename): """Parse content and metadata of markdown files""" @@ -59,6 +60,12 @@ Take a look to the Markdown reader:: Simple isn't it ? +If your new reader requires additional Python dependencies then you should wrap +their `imports` statements in `try...except`. Then inside the reader's class +set the `enabled` class attribute to mark import success or failure. This makes +it possible for users to continue using their favourite markup method without +needing to install modules for all the additional formats they don't use. + How to implement a new generator ? ================================== From 09f964ffcbeda804f52133e5825a39577ae27f9d Mon Sep 17 00:00:00 2001 From: James Rowe Date: Sat, 26 Feb 2011 16:17:20 +0000 Subject: [PATCH 3/3] Split hard and soft dependencies in getting_started doc. --- docs/getting_started.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/getting_started.rst b/docs/getting_started.rst index 442732be..d90e1fe8 100644 --- a/docs/getting_started.rst +++ b/docs/getting_started.rst @@ -23,13 +23,14 @@ At this time, pelican is dependent of the following python packages: * feedgenerator, to generate the ATOM feeds. * jinja2, for templating support. -* pygments, to have syntactic colorization -* docutils and Markdown If you're not using python 2.7, you will also need `argparse`. -All those dependencies will be processed automatically if you install pelican -using setuptools/distribute or pip. +Optionally: + +* docutils, for reST support +* pygments, to have syntactic colorization with resT input +* Markdown, for Markdown as an input format Writing articles using pelican ==============================