Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Simplified markdown extensions setting #16

Open
wants to merge 2 commits into from

2 participants

@imiric

I think this is a simpler way of allowing Markdown extensions to be configured.

Basically a markdown_extensions attribute is added to the HTML renderer function, and the function itself checks for the attribute.

This avoids adding another argument, and the need for the _smart_html_renderer wrapper function.

Let me know what you think.

@playpauseandstop
Collaborator

Interesting addition, indeed! Will think about how to combine it with mine one. Cause I'm starting to use extended renderer function with pages instance in second argument and for me it works like magic :)

Will add more details here on weekend.

@playpauseandstop playpauseandstop added this to the 1.0 milestone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 11, 2013
  1. @imiric
  2. @imiric

    Updated tests

    imiric authored
This page is out of date. Refresh to see the latest.
Showing with 15 additions and 63 deletions.
  1. +13 −59 flask_flatpages/__init__.py
  2. +2 −4 flask_flatpages/tests.py
View
72 flask_flatpages/__init__.py
@@ -12,7 +12,6 @@
from __future__ import with_statement
-import inspect
import itertools
import os
@@ -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)
@@ -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)
@@ -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
View
6 flask_flatpages/tests.py
@@ -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
@@ -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):
@@ -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()
Something went wrong with that request. Please try again.