forked from opted-eu/wp3inventory
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#47 markdown extension incompatible with flask v3, quick patch to be …
…removed later
- Loading branch information
1 parent
8ca3b2a
commit 09bf01d
Showing
2 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
|
||
# -*- coding: utf-8 -*- | ||
""" | ||
flaskext.markdown | ||
~~~~~~~~~~~~~~~~~ | ||
Markdown filter class for Flask | ||
To use:: | ||
from flaskext.markdown import Markdown | ||
md = Markdown(app) | ||
Then in your template:: | ||
{% filter markdown %} | ||
Your Markdown | ||
============= | ||
{% endfilter %} | ||
You can also do:: | ||
{{ mymarkdown|markdown}} | ||
Optionally, you can keep a reference to the Markdown instance and use that | ||
to register custom extensions by calling :func:`register_extension` or | ||
decorating the extension class with :func:`extend` | ||
:copyright: (c) 2010 by Dan Colish. | ||
:license: BSD, MIT see LICENSE for more details. | ||
""" | ||
from __future__ import absolute_import | ||
try: | ||
from flask import Markup | ||
except: | ||
try: | ||
from markupsafe import Markup | ||
except: | ||
raise Exception('Markup module not found') | ||
import markdown as md | ||
from markdown import ( | ||
blockprocessors, | ||
Extension, | ||
preprocessors, | ||
) | ||
|
||
|
||
__all__ = ['blockprocessors', 'Extension', 'Markdown', 'preprocessors'] | ||
|
||
|
||
class Markdown(object): | ||
""" | ||
Simple wrapper class for Markdown objects, any options that are available | ||
for markdown may be passed as keyword arguments like so:: | ||
md = Markdown(app, | ||
extensions=['footnotes'], | ||
extension_configs={'footnotes': ('PLACE_MARKER','~~~~~~~~')}, | ||
safe_mode=True, | ||
output_format='html4', | ||
) | ||
You can then call :func:`register_extension` to load custom extensions into | ||
the Markdown instance or use the :func:`extend` decorator | ||
:param app: Your Flask app instance | ||
:param markdown_options: Keyword args for the Markdown instance | ||
""" | ||
|
||
def __init__(self, app, **markdown_options): | ||
"""Markdown uses old style classes""" | ||
self._instance = md.Markdown(**markdown_options) | ||
app.jinja_env.filters.setdefault('markdown', self) | ||
|
||
def __call__(self, stream): | ||
return Markup(self._instance.convert(stream)) | ||
|
||
def extend(self, configs=None): | ||
""" | ||
Decorator for registering macros | ||
You must either force the decorated class to be imported | ||
or define it in the same file you instantiate Markdown. | ||
To register a simple extension you could do:: | ||
from flaskext.markdown import Extension, Markdown | ||
from preprocessors import SimplePreprocessor | ||
markdown_instance = Markdown(app) | ||
@markdown_instance.make_extension() | ||
class SimpleExtension(Extension): | ||
def extendMarkdown(self, md, md_globals): | ||
md.preprocessors.add('prover_block', | ||
SimplePreprocessor(md), | ||
'_begin') | ||
md.registerExtension(self) | ||
:param configs: A dictionary of options for the extension being registered | ||
""" | ||
|
||
def decorator(ext_cls): | ||
return self.register_extension(ext_cls, configs) | ||
return decorator | ||
|
||
def register_extension(self, ext_cls, configs=None): | ||
""" | ||
This will register an extension class with self._instance. You may pass | ||
any additional configs required for your extension | ||
It is best to call this when starting your Flask app, ie.:: | ||
from .mdx_simpl import SimpleExtension | ||
md = Markdown(app) | ||
md.register_extension(SimpleExtension) | ||
Any additional configuration arguments can be added to configs and will | ||
be passed through to the extension you are registering | ||
:param configs: A dictionary of options for the extension being regsitered | ||
:param ext_cls: The class name of your extension | ||
""" | ||
instance = ext_cls() | ||
self._instance.registerExtensions([instance], configs) | ||
return ext_cls |