Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified markdown extensions setting #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 13 additions & 59 deletions flask_flatpages/__init__.py
Expand Up @@ -12,7 +12,6 @@

from __future__ import with_statement

import inspect
import itertools
import os

Expand All @@ -24,40 +23,26 @@
try:
from pygments.formatters import HtmlFormatter as PygmentsHtmlFormatter
except ImportError:
PygmentsHtmlFormatter = None
pass


VERSION = '0.5'


def pygmented_markdown(text, flatpages=None):
"""Render Markdown text to HTML.
def pygmented_markdown(text):
"""Render Markdown text to HTML. Uses the `Codehilite`_ extension if
`Pygments`_ is available.

Uses the `CodeHilite`_ extension only if `Pygments`_ is available. But if
`Pygments`_ no available removes "codehilite" from list of possible
extensions.
Other extensions can be added with the ``FLATPAGES_MARKDOWN_EXTENSIONS``
setting.

If you need other extensions to use setup them to
``FLATPAGES_MARKDOWN_EXTENSIONS`` list setting. Later whole
:class:`FlatPages` instance would be passed to your
``FLATPAGES_HTML_RENDERER`` function as second argument.

.. _CodeHilite:
http://www.freewisdom.org/projects/python-markdown/CodeHilite
.. _CodeHilite: http://www.freewisdom.org/projects/python-markdown/CodeHilite
.. _Pygments: http://pygments.org/
"""
extensions = flatpages.config('markdown_extensions') if flatpages else []

if PygmentsHtmlFormatter is None:
original_extensions = extensions
extensions = []
extensions = getattr(pygmented_markdown, 'markdown_extensions', [])

for extension in original_extensions:
if extension.startswith('codehilite'):
continue
extensions.append(extension)
elif not extensions:
extensions = ['codehilite']
if 'PygmentsHtmlFormatter' in globals():
extensions += ['codehilite']

return markdown.markdown(text, extensions)

Expand Down Expand Up @@ -186,6 +171,9 @@ def init_app(self, app):
config_key = 'FLATPAGES_%s' % key.upper()
app.config.setdefault(config_key, value)

app.config['FLATPAGES_HTML_RENDERER'].markdown_extensions = \
app.config.get('FLATPAGES_MARKDOWN_EXTENSIONS', [])

# Register function to forget all pages if necessary
app.before_request(self._conditional_auto_reset)

Expand Down Expand Up @@ -308,38 +296,4 @@ def _parse(self, string, path):
if not callable(html_renderer):
html_renderer = werkzeug.import_string(html_renderer)

html_renderer = self._smart_html_renderer(html_renderer)
return Page(path, meta, content, html_renderer)

def _smart_html_renderer(self, html_renderer):
"""As of 0.4 version we support passing :class:`FlatPages` instance to
HTML renderer function.

So we need to inspect function args spec and if it supports two
arguments, pass ``self`` instance there.

.. versionadded:: 0.4
"""
def wrapper(body):
"""Simple wrapper to inspect HTML renderer function and if it has
two arguments and second argument named ``extensions``, pass
``FLATPAGES_MARKDOWN_EXTENSIONS`` as second argument to function.
"""
try:
spec = inspect.getargspec(html_renderer)
except TypeError:
return html_renderer(body)

# Named tuple available only in Python 2.6+, before raw tuple used
spec_args = spec[0] if not hasattr(spec, 'args') else spec.args

if len(spec_args) == 1:
return html_renderer(body)
elif len(spec_args) == 2:
return html_renderer(body, self)

raise ValueError(
'HTML renderer function {!r} not supported by Flask-FlatPages,'
' wrong number of arguments.'.format(html_renderer)
)
return wrapper
6 changes: 2 additions & 4 deletions flask_flatpages/tests.py
Expand Up @@ -19,8 +19,6 @@

from contextlib import contextmanager

import jinja2

from flask import Flask
from flask_flatpages import FlatPages, pygments_style_defs
from werkzeug.exceptions import NotFound
Expand Down Expand Up @@ -154,7 +152,7 @@ def test_other_encoding(self):
self._unicode(pages)

def test_other_html_renderer(self):
def hello_renderer(body, pages):
def hello_renderer(body):
return pages.get('hello').body.upper()

for renderer in (unicode.upper, 'string.upper', hello_renderer):
Expand Down Expand Up @@ -184,7 +182,7 @@ def test_markdown_extensions(self):
u'<h1>Page Header</h1>\n<h2>Paragraph Header</h2>\n<p>Text</p>'
)

pages.app.config['FLATPAGES_MARKDOWN_EXTENSIONS'] = [
pages.app.config['FLATPAGES_HTML_RENDERER'].markdown_extensions = [
'codehilite', 'headerid'
]
pages.reload()
Expand Down