| 1 |
""" |
| 2 |
============================== |
| 3 |
tests.test_markdown_extensions |
| 4 |
============================== |
| 5 |
|
| 6 |
Test proper work of various Markdown extensions. |
| 7 |
|
| 8 |
""" |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
import sys |
| 12 |
|
| 13 |
|
| 14 |
import markdown |
| 15 |
import pytest |
| 16 |
from flask_flatpages import FlatPages |
| 17 |
from flask_flatpages.imports import PygmentsHtmlFormatter |
| 18 |
from markdown.extensions.toc import TocExtension |
| 19 |
|
| 20 |
|
| 21 |
def check_toc_page(pages: FlatPages): |
| 22 |
toc = pages.get("toc") |
| 23 |
fixture = markdown.markdown( |
| 24 |
toc.body, |
| 25 |
extensions=[TocExtension()], |
| 26 |
) |
| 27 |
assert fixture == toc.html |
| 28 |
|
| 29 |
|
| 30 |
def check_default_codehilite_page(pages: FlatPages): |
| 31 |
codehilite = pages.get("codehilite") |
| 32 |
body = codehilite.body |
| 33 |
fixture = markdown.markdown( |
| 34 |
body, |
| 35 |
extensions=["codehilite"], |
| 36 |
) |
| 37 |
assert codehilite.html == fixture |
| 38 |
|
| 39 |
|
| 40 |
def check_codehilite_with_linenums(pages: FlatPages): |
| 41 |
codehilite = pages.get("codehilite") |
| 42 |
body = codehilite.body |
| 43 |
fixture = markdown.markdown( |
| 44 |
body, |
| 45 |
extensions=["codehilite"], |
| 46 |
extension_configs={"codehilite": {"linenums": True}}, |
| 47 |
) |
| 48 |
assert codehilite.html == fixture |
| 49 |
|
| 50 |
|
| 51 |
def check_extra(pages: FlatPages): |
| 52 |
extra_sep = "\n" if sys.version_info[:2] > (2, 6) else "\n\n" |
| 53 |
fixture = ( |
| 54 |
"<p>This is <em>true</em> markdown text.</p>\n" |
| 55 |
"<div>{0}" |
| 56 |
"<p>This is <em>true</em> markdown text.</p>\n" |
| 57 |
"</div>".format(extra_sep) |
| 58 |
) |
| 59 |
extra = pages.get("extra") |
| 60 |
assert extra.html == fixture |
| 61 |
|
| 62 |
|
| 63 |
HELLO_HTML = "<h1>Page Header</h1>\n<h2>Paragraph Header</h2>\n<p>Text</p>" |
| 64 |
HELLO_ID_HTML = ( |
| 65 |
"""<h1 id="page-header">Page Header</h1>\n""" |
| 66 |
"""<h2 id="paragraph-header">Paragraph Header</h2>\n""" |
| 67 |
"""<p>Text</p>""" |
| 68 |
) |
| 69 |
|
| 70 |
|
| 71 |
def test_default_markdown(app_with_context, flatpages_factory): |
| 72 |
pages = flatpages_factory(app_with_context) |
| 73 |
hello = pages.get("headerid") |
| 74 |
assert hello.html == HELLO_HTML |
| 75 |
app_with_context.config["FLATPAGES_MARKDOWN_EXTENSIONS"] = [] |
| 76 |
pages.reload() |
| 77 |
pages._file_cache = {} |
| 78 |
|
| 79 |
hello = pages.get("headerid") |
| 80 |
assert hello.html == HELLO_HTML |
| 81 |
if PygmentsHtmlFormatter is not None: |
| 82 |
check_default_codehilite_page(pages) |
| 83 |
|
| 84 |
|
| 85 |
@pytest.mark.skipif( |
| 86 |
PygmentsHtmlFormatter is None, reason="Pygments not installed" |
| 87 |
) |
| 88 |
def test_codehilite_linenums_disabled(app_with_context, flatpages_factory): |
| 89 |
pages = flatpages_factory(app_with_context) |
| 90 |
# Test explicity disabled |
| 91 |
app_with_context.config["FLATPAGES_MARKDOWN_EXTENSIONS"] = ["codehilite"] |
| 92 |
check_default_codehilite_page(pages) |
| 93 |
# Test explicity disabled |
| 94 |
app_with_context.config["FLATPAGES_EXTENSION_CONFIGS"] = { |
| 95 |
"codehilite": {"linenums": "False"} |
| 96 |
} |
| 97 |
pages.reload() |
| 98 |
pages._file_cache = {} |
| 99 |
check_default_codehilite_page(pages) |
| 100 |
|
| 101 |
|
| 102 |
@pytest.mark.skipif( |
| 103 |
PygmentsHtmlFormatter is None, reason="Pygments not installed" |
| 104 |
) |
| 105 |
def test_codehilite_linenums_enabled(app_with_context, flatpages_factory): |
| 106 |
pages = flatpages_factory(app_with_context) |
| 107 |
app_with_context.config["FLATPAGES_MARKDOWN_EXTENSIONS"] = ["codehilite"] |
| 108 |
app_with_context.config["FLATPAGES_EXTENSION_CONFIGS"] = { |
| 109 |
"codehilite": {"linenums": "True"} |
| 110 |
} |
| 111 |
|
| 112 |
check_codehilite_with_linenums(pages) |
| 113 |
|
| 114 |
|
| 115 |
def test_extra(app_with_context, flatpages_factory): |
| 116 |
pages = flatpages_factory(app_with_context) |
| 117 |
app_with_context.config["FLATPAGES_MARKDOWN_EXTENSIONS"] = ["extra"] |
| 118 |
check_extra(pages) |
| 119 |
|
| 120 |
|
| 121 |
def test_toc(app_with_context, flatpages_factory): |
| 122 |
pages = flatpages_factory(app_with_context) |
| 123 |
app_with_context.config["FLATPAGES_MARKDOWN_EXTENSIONS"] = ["toc"] |
| 124 |
check_toc_page(pages) |
| 125 |
|
| 126 |
|
| 127 |
def test_headerid_with_toc(app_with_context, flatpages_factory): |
| 128 |
pages = flatpages_factory(app_with_context) |
| 129 |
app_with_context.config["FLATPAGES_MARKDOWN_EXTENSIONS"] = [ |
| 130 |
"codehilite", |
| 131 |
"toc", # headerid is deprecated in Markdown 3.0 |
| 132 |
] |
| 133 |
pages.reload() |
| 134 |
pages._file_cache = {} |
| 135 |
|
| 136 |
hello = pages.get("headerid") |
| 137 |
assert hello.html == HELLO_ID_HTML |
| 138 |
if PygmentsHtmlFormatter is not None: |
| 139 |
check_default_codehilite_page(pages) # test codehilite also loaded |
| 140 |
|
| 141 |
|
| 142 |
@pytest.mark.skipif( |
| 143 |
PygmentsHtmlFormatter is None, reason="Pygments not installed" |
| 144 |
) |
| 145 |
def test_extension_importpath(app_with_context, flatpages_factory): |
| 146 |
pages = flatpages_factory(app_with_context) |
| 147 |
app_with_context.config["FLATPAGES_MARKDOWN_EXTENSIONS"] = [ |
| 148 |
"markdown.extensions.codehilite:CodeHiliteExtension" |
| 149 |
] |
| 150 |
check_default_codehilite_page(pages) |
| 151 |
app_with_context.config[ |
| 152 |
"FLATPAGES_EXTENSION_CONFIGS" |
| 153 |
] = { # Markdown 3 style config |
| 154 |
"markdown.extensions.codehilite:CodeHiliteExtension": { |
| 155 |
"linenums": True |
| 156 |
} |
| 157 |
} |
| 158 |
pages.reload() |
| 159 |
pages._file_cache = {} |
| 160 |
check_codehilite_with_linenums(pages) |
| 161 |
|
| 162 |
|
| 163 |
@pytest.mark.skipif( |
| 164 |
PygmentsHtmlFormatter is None, reason="Pygments not installed" |
| 165 |
) |
| 166 |
def test_extension_object(app_with_context, flatpages_factory): |
| 167 |
pages = flatpages_factory(app_with_context) |
| 168 |
from markdown.extensions.codehilite import CodeHiliteExtension |
| 169 |
|
| 170 |
codehilite = CodeHiliteExtension() |
| 171 |
app_with_context.config["FLATPAGES_MARKDOWN_EXTENSIONS"] = [codehilite] |
| 172 |
check_default_codehilite_page(pages) |
| 173 |
codehilite = CodeHiliteExtension(linenums="True") # Check config applies |
| 174 |
app_with_context.config["FLATPAGES_MARKDOWN_EXTENSIONS"] = [codehilite] |
| 175 |
pages.reload() |
| 176 |
pages._file_cache = {} |
| 177 |
check_codehilite_with_linenums(pages) |
| 178 |
|
| 179 |
|
| 180 |
@pytest.mark.skipif( |
| 181 |
PygmentsHtmlFormatter is None, reason="Pygments not installed" |
| 182 |
) |
| 183 |
def test_mixed_extension_types(app_with_context, flatpages_factory): |
| 184 |
from markdown.extensions.toc import TocExtension |
| 185 |
|
| 186 |
pages = flatpages_factory(app_with_context) |
| 187 |
|
| 188 |
toc = TocExtension() |
| 189 |
app_with_context.config["FLATPAGES_MARKDOWN_EXTENSIONS"] = [ |
| 190 |
toc, |
| 191 |
"codehilite", |
| 192 |
"markdown.extensions.extra:ExtraExtension", |
| 193 |
] |
| 194 |
check_toc_page(pages) |
| 195 |
check_default_codehilite_page(pages) |
| 196 |
check_extra(pages) |
| 197 |
app_with_context.config["FLATPAGES_EXTENSION_CONFIGS"] = { |
| 198 |
"codehilite": {"linenums": "True"} |
| 199 |
} |
| 200 |
pages.reload() |
| 201 |
pages._file_cache = {} |
| 202 |
check_toc_page(pages) |
| 203 |
check_extra(pages) |
| 204 |
check_codehilite_with_linenums(pages) |