Skip to content

Commit 4014a52

Browse files
authored
Merge pull request #1965 from sebix/1957-markdown-extensions
Configuration option for markdown extensions
2 parents 999421b + 1a1aad9 commit 4014a52

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

docs/user/markdown.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,3 +753,15 @@ The following types are supported:
753753

754754
.. note::
755755
You should note that the title will be automatically capitalized.
756+
757+
Instance-specific extensions
758+
----------------------------
759+
760+
You can specify a list of additional extensions to the markdown parser per instance.
761+
A list of potentially interesting extensions can be access in `Python-markdown's wiki <https://github.com/Python-Markdown/markdown/wiki/Third-Party-Extensions>`_.
762+
763+
For example, to automatically link URLs: ::
764+
765+
class Config(DefaultConfig):
766+
...
767+
markdown_extensions = ['pymdownx.magiclink']

src/moin/config/default.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class for the benefit of the WikiConfig macro.
5454
auth_have_login = None
5555
auth_login_inputs = None
5656
_site_plugin_lists = None
57+
markdown_extensions = []
5758

5859
def __init__(self):
5960
"""Init Config instance"""

src/moin/converters/_tests/test_markdown_in.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
MoinMoin - Tests for moin.converters.markdown_in
77
"""
88

9+
from collections import namedtuple
910
import pytest
11+
from flask import Flask
1012

1113
from . import serialize, XMLNS_RE
1214

@@ -15,13 +17,24 @@
1517
from ..markdown_in import Converter
1618

1719

20+
DefaultConfig = namedtuple("DefaultConfig", ("markdown_extensions",))
21+
config = DefaultConfig(markdown_extensions=[])
22+
23+
1824
class TestConverter:
1925
namespaces = {moin_page: "", xlink: "xlink", xinclude: "xinclude", html: "html"}
2026

2127
output_re = XMLNS_RE
2228

2329
def setup_class(self):
24-
self.conv = Converter()
30+
# mock patching flask.current_app.cfg does not work here as for speccing the original object is called and that causes a "RuntimeError: working outside of application context"
31+
app = Flask(__name__)
32+
# DefaultConfig doesn't work here as it does not provide all the defaults required to be initialized
33+
app.cfg = config
34+
ctx = app.app_context()
35+
ctx.push()
36+
with ctx:
37+
self.conv = Converter()
2538

2639
data = [
2740
("Text", "<p>Text</p>"),

src/moin/converters/_tests/test_markdown_in_out.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
MoinMoin - Tests for markdown->DOM->markdown using markdown_in and markdown_out converters
88
"""
99

10+
from collections import namedtuple
1011
import pytest
12+
from flask import Flask
1113

1214
from emeraldtree import ElementTree as ET
1315

@@ -18,6 +20,10 @@
1820
from moin.converters.markdown_out import Converter as conv_out
1921

2022

23+
DefaultConfig = namedtuple("DefaultConfig", ("markdown_extensions",))
24+
config = DefaultConfig(markdown_extensions=[])
25+
26+
2127
class TestConverter:
2228

2329
input_namespaces = 'xmlns="{}" xmlns:page="{}" xmlns:xlink="{}" xmlns:xinclude="{}" xmlns:html="{}"'.format(
@@ -36,7 +42,14 @@ class TestConverter:
3642
output_re = XMLNS_RE
3743

3844
def setup_class(self):
39-
self.conv_in = conv_in()
45+
# mock patching flask.current_app.cfg does not work here as for speccing the original object is called and that causes a "RuntimeError: working outside of application context"
46+
app = Flask(__name__)
47+
# DefaultConfig doesn't work here as it does not provide all the defaults required to be initialized
48+
app.cfg = config
49+
ctx = app.app_context()
50+
ctx.push()
51+
with ctx:
52+
self.conv_in = conv_in()
4053
self.conv_out = conv_out()
4154

4255
data = [

src/moin/converters/markdown_in.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from ._util import decode_data
1818
from moin.utils.iri import Iri
1919
from moin.converters.html_in import Converter as HTML_IN_Converter
20+
from flask import current_app
2021

2122
from emeraldtree import ElementTree as ET
2223

@@ -558,8 +559,12 @@ def convert_invalid_p_nodes(self, node):
558559
self.convert_invalid_p_nodes(child)
559560

560561
def __init__(self):
562+
# The Moin configuration
563+
self.app_configuration = current_app.cfg
564+
561565
self.markdown = Markdown(
562-
extensions=[ExtraExtension(), CodeHiliteExtension(guess_lang=False), "mdx_wikilink_plus", "admonition"],
566+
extensions=[ExtraExtension(), CodeHiliteExtension(guess_lang=False), "mdx_wikilink_plus", "admonition"]
567+
+ self.app_configuration.markdown_extensions,
563568
extension_configs={
564569
"mdx_wikilink_plus": {
565570
"html_class": None,

0 commit comments

Comments
 (0)