Merge pull request #155 from borgar/configurable-readers

Allow overriding reader extensions.
This commit is contained in:
Alexis Metaireau 2011-08-11 13:42:38 -07:00
commit 0adb76d5d2
3 changed files with 12 additions and 4 deletions

View file

@ -46,6 +46,10 @@ Setting name (default value) what does it do?
`MARKUP` (``('rst', 'md')``) A list of available markup languages you want
to use. For the moment, only available values
are `rst` and `md`.
`MD_EXTENSIONS` (``('codehilite','extra')``) A list of the extensions that the markdown processor
will use. Refer to the extensions chapter in the
Python-Markdown documentation for a complete list of
supported extensions.
`OUTPUT_PATH` (``'output/'``) Where to output the generated files.
`PATH` (``None``) path to look at for input files.
`PDF_GENERATOR` (``False``) Set to True if you want to have PDF versions

View file

@ -211,7 +211,7 @@ class ArticlesGenerator(Generator):
files = self.get_files(self.path, exclude=['pages',])
all_articles = []
for f in files:
content, metadata = read_file(f)
content, metadata = read_file(f, settings=self.settings)
# if no category is set, use the name of the path as a category
if 'category' not in metadata.keys():

View file

@ -32,7 +32,7 @@ def _process_metadata(name, value):
class Reader(object):
enabled = True
extensions = None
class _FieldBodyTranslator(HTMLTranslator):
@ -99,11 +99,12 @@ class RstReader(Reader):
class MarkdownReader(Reader):
enabled = bool(Markdown)
extension = "md"
extensions = ['codehilite', 'extra']
def read(self, filename):
"""Parse content and metadata of markdown files"""
text = open(filename)
md = Markdown(extensions = ['meta', 'codehilite', 'extra'])
md = Markdown(extensions=set(self.extensions+['meta']))
content = md.convert(text)
metadata = {}
@ -133,13 +134,16 @@ class HtmlReader(Reader):
_EXTENSIONS = dict((cls.extension, cls) for cls in Reader.__subclasses__())
def read_file(filename, fmt=None):
def read_file(filename, fmt=None, settings=None):
"""Return a reader object using the given format."""
if not fmt:
fmt = filename.split('.')[-1]
if fmt not in _EXTENSIONS.keys():
raise TypeError('Pelican does not know how to parse %s' % filename)
reader = _EXTENSIONS[fmt]()
settings_key = '%s_EXTENSIONS' % fmt.upper()
if settings and settings_key in settings:
reader.extensions = settings[settings_key]
if not reader.enabled:
raise ValueError("Missing dependencies for %s" % fmt)
return reader.read(filename)