Add a way to use Typogrify to enhance the generated HTML.

This commit is contained in:
Alexis Metaireau 2012-03-11 02:48:36 +01:00
commit d43bd1dcb8
5 changed files with 41 additions and 2 deletions

View file

@ -69,6 +69,11 @@ Setting name (default value) What does it do?
`TIMEZONE` The timezone used in the date information, to `TIMEZONE` The timezone used in the date information, to
generate Atom and RSS feeds. See the "timezone" generate Atom and RSS feeds. See the "timezone"
section below for more info. section below for more info.
`TYPOGRIFY` (``False``) If set to true, some
additional transformations will be done on the
generated HTML, using the `Typogrify
<http://static.mintchaos.com/projects/typogrify/>`_
library
================================================ ===================================================== ================================================ =====================================================
.. [#] Default is the system locale. .. [#] Default is the system locale.

View file

@ -143,12 +143,24 @@ def read_file(filename, fmt=None, settings=None):
"""Return a reader object using the given format.""" """Return a reader object using the given format."""
if not fmt: if not fmt:
fmt = filename.split('.')[-1] fmt = filename.split('.')[-1]
if fmt not in _EXTENSIONS.keys(): if fmt not in _EXTENSIONS.keys():
raise TypeError('Pelican does not know how to parse %s' % filename) raise TypeError('Pelican does not know how to parse %s' % filename)
reader = _EXTENSIONS[fmt](settings) reader = _EXTENSIONS[fmt](settings)
settings_key = '%s_EXTENSIONS' % fmt.upper() settings_key = '%s_EXTENSIONS' % fmt.upper()
if settings and settings_key in settings: if settings and settings_key in settings:
reader.extensions = settings[settings_key] reader.extensions = settings[settings_key]
if not reader.enabled: if not reader.enabled:
raise ValueError("Missing dependencies for %s" % fmt) raise ValueError("Missing dependencies for %s" % fmt)
return reader.read(filename)
content, metadata = reader.read(filename)
# eventually filter the content with typogrify if asked so
if settings and settings['TYPOGRIFY']:
from typogrify import Typogrify
content = Typogrify.typogrify(content)
return content, metadata

View file

@ -57,7 +57,8 @@ _DEFAULT_CONFIG = {'PATH': None,
'DEFAULT_METADATA': (), 'DEFAULT_METADATA': (),
'FILES_TO_COPY': (), 'FILES_TO_COPY': (),
'DEFAULT_STATUS': 'published', 'DEFAULT_STATUS': 'published',
'ARTICLE_PERMALINK_STRUCTURE': '' 'ARTICLE_PERMALINK_STRUCTURE': '',
'TYPOGRIFY': False,
} }

View file

@ -0,0 +1,4 @@
Article title
#############
This is some content. With some stuff to "typogrify".

View file

@ -33,3 +33,20 @@ class RstReaderTest(unittest.TestCase):
for key, value in expected.items(): for key, value in expected.items():
self.assertEquals(value, metadata[key], key) self.assertEquals(value, metadata[key], key)
def test_typogrify(self):
# if nothing is specified in the settings, the content should be
# unmodified
content, _ = readers.read_file(_filename('article.rst'))
expected = "<p>This is some content. With some stuff to "\
"&quot;typogrify&quot;.</p>\n"
self.assertEqual(content, expected)
# otherwise, typogrify should be applied
content, _ = readers.read_file(_filename('article.rst'),
settings={'TYPOGRIFY': True})
expected = "<p>This is some content. With some stuff to&nbsp;"\
"&#8220;typogrify&#8221;.</p>\n"
self.assertEqual(content, expected)