Skip to content

Commit

Permalink
Merge pull request #881 from Mortal/extension-config
Browse files Browse the repository at this point in the history
Specify extensions as module names rather than Extension instances
  • Loading branch information
benjaoming committed Jul 1, 2018
2 parents 928e0cb + db19dc1 commit 32a2d54
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 315 deletions.
1 change: 1 addition & 0 deletions docs/release_notes.rst
Expand Up @@ -43,6 +43,7 @@ Fixed
* Use ``simple_tag`` for assignment tag :url-issue:`791` (Raffaele Salmaso)
* Direct invocation of ``pytest`` fixed (removing ``runtests.py``) :url-issue:`781` (Branko Majic)
* Line breaks in help texts for macros :url-issue:`851` (Mathias Dannesbo)
* Table of contents now has a header by default, and several built-in django-wiki extensions can be configured using ``WIKI_MARKDOWN_KWARGS`` :url-issue:`881` (Mathias Rav)

Deprecated/Removed
~~~~~~~~~~~~~~~~~~
Expand Down
29 changes: 22 additions & 7 deletions src/wiki/conf/settings.py
Expand Up @@ -26,13 +26,28 @@
'WIKI_MARKDOWN_SANITIZE_HTML',
True)

#: Arguments for the Markdown instance, for instance a list of extensions to
#: use.
#: See: https://pythonhosted.org/Markdown/extensions/index.html
#: Arguments for the Markdown instance, as a dictionary. The "extensions" key
#: should be a list of extra extensions to use besides the built-in django-wiki
#: extensions, and the "extension_configs" should be a dictionary, specifying
#: the keyword-arguments to pass to each extension.
#:
#: To set a custom title for TOC's::
#: For a list of extensions officially supported by Python-Markdown, see:
#: https://python-markdown.github.io/extensions/
#:
#: WIKI_MARKDOWN_KWARGS = {'extension_configs': {'toc': _('Contents of this article')}}
#: To set a custom title for table of contents, specify the following in your
#: Django project settings::
#:
#: WIKI_MARKDOWN_KWARGS = {
#: 'extension_configs': {
#: 'wiki.plugins.macros.mdx.toc': {'title': 'Contents of this article'},
#: },
#: }
#:
#: Besides the extensions enabled by the "extensions" key, the following
#: built-in django-wiki extensions can be configured with "extension_configs":
#: "wiki.core.markdown.mdx.codehilite", "wiki.core.markdown.mdx.previewlinks",
#: "wiki.core.markdown.mdx.responsivetable", "wiki.plugins.macros.mdx.macro",
#: "wiki.plugins.macros.mdx.toc", "wiki.plugins.macros.mdx.wikilinks".
MARKDOWN_KWARGS = {
'extensions': [
'markdown.extensions.footnotes',
Expand All @@ -46,8 +61,8 @@
'markdown.extensions.sane_lists',
],
'extension_configs': {
'toc': {
'title': _('Table of Contents')}},
'wiki.plugins.macros.mdx.toc': {'title': _('Contents')},
},
}
MARKDOWN_KWARGS.update(getattr(django_settings, 'WIKI_MARKDOWN_KWARGS', {}))

Expand Down
9 changes: 3 additions & 6 deletions src/wiki/core/markdown/__init__.py
@@ -1,9 +1,6 @@
import bleach
import markdown
from wiki.conf import settings
from wiki.core.markdown.mdx.codehilite import WikiCodeHiliteExtension
from wiki.core.markdown.mdx.previewlinks import PreviewLinksExtension
from wiki.core.markdown.mdx.responsivetable import ResponsiveTableExtension
from wiki.core.plugins import registry as plugin_registry


Expand All @@ -20,9 +17,9 @@ def __init__(self, article, preview=False, user=None, *args, **kwargs):
def core_extensions(self):
"""List of core extensions found in the mdx folder"""
return [
PreviewLinksExtension(),
ResponsiveTableExtension(),
WikiCodeHiliteExtension(),
'wiki.core.markdown.mdx.codehilite',
'wiki.core.markdown.mdx.previewlinks',
'wiki.core.markdown.mdx.responsivetable',
]

def get_markdown_extensions(self):
Expand Down
5 changes: 5 additions & 0 deletions src/wiki/core/markdown/mdx/codehilite.py
Expand Up @@ -117,3 +117,8 @@ def extendMarkdown(self, md, md_globals):
">normalize_whitespace")

md.registerExtension(self)


def makeExtension(*args, **kwargs):
"""Return an instance of the extension."""
return WikiCodeHiliteExtension(*args, **kwargs)
5 changes: 5 additions & 0 deletions src/wiki/core/markdown/mdx/previewlinks.py
Expand Up @@ -19,3 +19,8 @@ def run(self, root):
if not a.get('href').startswith('#'):
a.set('target', '_blank')
return root


def makeExtension(*args, **kwargs):
"""Return an instance of the extension."""
return PreviewLinksExtension(*args, **kwargs)
5 changes: 5 additions & 0 deletions src/wiki/core/markdown/mdx/responsivetable.py
Expand Up @@ -37,3 +37,8 @@ def move_children(self, element1, element2):
def convert_to_wrapper(self, element):
element.tag = 'div'
element.set('class', 'table-responsive')


def makeExtension(*args, **kwargs):
"""Return an instance of the extension."""
return ResponsiveTableExtension(*args, **kwargs)
5 changes: 5 additions & 0 deletions src/wiki/plugins/macros/mdx/macro.py
Expand Up @@ -99,3 +99,8 @@ def wikilink(self):
'Insert a link to another wiki page with a short notation.'),
example_code='[[WikiLink]]',
args={})


def makeExtension(*args, **kwargs):
"""Return an instance of the extension."""
return MacroExtension(*args, **kwargs)

0 comments on commit 32a2d54

Please sign in to comment.