| 1 |
""" |
| 2 |
Python Markdown |
| 3 |
|
| 4 |
A Python implementation of John Gruber's Markdown. |
| 5 |
|
| 6 |
Documentation: https://python-markdown.github.io/ |
| 7 |
GitHub: https://github.com/Python-Markdown/markdown/ |
| 8 |
PyPI: https://pypi.org/project/Markdown/ |
| 9 |
|
| 10 |
Started by Manfred Stienstra (http://www.dwerg.net/). |
| 11 |
Maintained for a few years by Yuri Takhteyev (http://www.freewisdom.org). |
| 12 |
Currently maintained by Waylan Limberg (https://github.com/waylan), |
| 13 |
Dmitry Shachnev (https://github.com/mitya57) and Isaac Muse (https://github.com/facelessuser). |
| 14 |
|
| 15 |
Copyright 2007-2023 The Python Markdown Project (v. 1.7 and later) |
| 16 |
Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b) |
| 17 |
Copyright 2004 Manfred Stienstra (the original version) |
| 18 |
|
| 19 |
License: BSD (see LICENSE.md for details). |
| 20 |
|
| 21 |
Python-Markdown Extension Regression Tests |
| 22 |
========================================== |
| 23 |
|
| 24 |
A collection of regression tests to confirm that the included extensions |
| 25 |
continue to work as advertised. This used to be accomplished by `doctests`. |
| 26 |
""" |
| 27 |
|
| 28 |
import unittest |
| 29 |
import markdown |
| 30 |
|
| 31 |
|
| 32 |
class TestExtensionClass(unittest.TestCase): |
| 33 |
""" Test markdown.extensions.Extension. """ |
| 34 |
|
| 35 |
def setUp(self): |
| 36 |
class TestExtension(markdown.extensions.Extension): |
| 37 |
config = { |
| 38 |
'foo': ['bar', 'Description of foo'], |
| 39 |
'bar': ['baz', 'Description of bar'] |
| 40 |
} |
| 41 |
|
| 42 |
self.ext = TestExtension() |
| 43 |
self.ExtKlass = TestExtension |
| 44 |
|
| 45 |
def testGetConfig(self): |
| 46 |
self.assertEqual(self.ext.getConfig('foo'), 'bar') |
| 47 |
|
| 48 |
def testGetConfigDefault(self): |
| 49 |
self.assertEqual(self.ext.getConfig('baz'), '') |
| 50 |
self.assertEqual(self.ext.getConfig('baz', default='missing'), 'missing') |
| 51 |
|
| 52 |
def testGetConfigs(self): |
| 53 |
self.assertEqual(self.ext.getConfigs(), {'foo': 'bar', 'bar': 'baz'}) |
| 54 |
|
| 55 |
def testGetConfigInfo(self): |
| 56 |
self.assertEqual( |
| 57 |
dict(self.ext.getConfigInfo()), |
| 58 |
dict([ |
| 59 |
('foo', 'Description of foo'), |
| 60 |
('bar', 'Description of bar') |
| 61 |
]) |
| 62 |
) |
| 63 |
|
| 64 |
def testSetConfig(self): |
| 65 |
self.ext.setConfig('foo', 'baz') |
| 66 |
self.assertEqual(self.ext.getConfigs(), {'foo': 'baz', 'bar': 'baz'}) |
| 67 |
|
| 68 |
def testSetConfigWithBadKey(self): |
| 69 |
# `self.ext.setConfig('bad', 'baz)` => `KeyError` |
| 70 |
self.assertRaises(KeyError, self.ext.setConfig, 'bad', 'baz') |
| 71 |
|
| 72 |
def testConfigAsKwargsOnInit(self): |
| 73 |
ext = self.ExtKlass(foo='baz', bar='blah') |
| 74 |
self.assertEqual(ext.getConfigs(), {'foo': 'baz', 'bar': 'blah'}) |
| 75 |
|
| 76 |
|
| 77 |
class TestMetaData(unittest.TestCase): |
| 78 |
""" Test `MetaData` extension. """ |
| 79 |
|
| 80 |
def setUp(self): |
| 81 |
self.md = markdown.Markdown(extensions=['meta']) |
| 82 |
|
| 83 |
def testBasicMetaData(self): |
| 84 |
""" Test basic metadata. """ |
| 85 |
|
| 86 |
text = '''Title: A Test Doc. |
| 87 |
Author: Waylan Limberg |
| 88 |
John Doe |
| 89 |
Blank_Data: |
| 90 |
|
| 91 |
The body. This is paragraph one.''' |
| 92 |
self.assertEqual( |
| 93 |
self.md.convert(text), |
| 94 |
'<p>The body. This is paragraph one.</p>' |
| 95 |
) |
| 96 |
self.assertEqual( |
| 97 |
self.md.Meta, { |
| 98 |
'author': ['Waylan Limberg', 'John Doe'], |
| 99 |
'blank_data': [''], |
| 100 |
'title': ['A Test Doc.'] |
| 101 |
} |
| 102 |
) |
| 103 |
|
| 104 |
def testYamlMetaData(self): |
| 105 |
""" Test metadata specified as simple YAML. """ |
| 106 |
|
| 107 |
text = '''--- |
| 108 |
Title: A Test Doc. |
| 109 |
Author: [Waylan Limberg, John Doe] |
| 110 |
Blank_Data: |
| 111 |
--- |
| 112 |
|
| 113 |
The body. This is paragraph one.''' |
| 114 |
self.assertEqual( |
| 115 |
self.md.convert(text), |
| 116 |
'<p>The body. This is paragraph one.</p>' |
| 117 |
) |
| 118 |
self.assertEqual( |
| 119 |
self.md.Meta, { |
| 120 |
'author': ['[Waylan Limberg, John Doe]'], |
| 121 |
'blank_data': [''], |
| 122 |
'title': ['A Test Doc.'] |
| 123 |
} |
| 124 |
) |
| 125 |
|
| 126 |
def testMissingMetaData(self): |
| 127 |
""" Test document without Meta Data. """ |
| 128 |
|
| 129 |
text = ' Some Code - not extra lines of meta data.' |
| 130 |
self.assertEqual( |
| 131 |
self.md.convert(text), |
| 132 |
'<pre><code>Some Code - not extra lines of meta data.\n' |
| 133 |
'</code></pre>' |
| 134 |
) |
| 135 |
self.assertEqual(self.md.Meta, {}) |
| 136 |
|
| 137 |
def testMetaDataWithoutNewline(self): |
| 138 |
""" Test document with only metadata and no newline at end.""" |
| 139 |
text = 'title: No newline' |
| 140 |
self.assertEqual(self.md.convert(text), '') |
| 141 |
self.assertEqual(self.md.Meta, {'title': ['No newline']}) |
| 142 |
|
| 143 |
def testMetaDataReset(self): |
| 144 |
""" Test that reset call remove Meta entirely """ |
| 145 |
|
| 146 |
text = '''Title: A Test Doc. |
| 147 |
Author: Waylan Limberg |
| 148 |
John Doe |
| 149 |
Blank_Data: |
| 150 |
|
| 151 |
The body. This is paragraph one.''' |
| 152 |
self.md.convert(text) |
| 153 |
|
| 154 |
self.md.reset() |
| 155 |
self.assertEqual(self.md.Meta, {}) |
| 156 |
|
| 157 |
|
| 158 |
class TestWikiLinks(unittest.TestCase): |
| 159 |
""" Test `Wikilinks` Extension. """ |
| 160 |
|
| 161 |
def setUp(self): |
| 162 |
self.md = markdown.Markdown(extensions=['wikilinks']) |
| 163 |
self.text = "Some text with a [[WikiLink]]." |
| 164 |
|
| 165 |
def testBasicWikilinks(self): |
| 166 |
""" Test `[[wikilinks]]`. """ |
| 167 |
|
| 168 |
self.assertEqual( |
| 169 |
self.md.convert(self.text), |
| 170 |
'<p>Some text with a ' |
| 171 |
'<a class="wikilink" href="/WikiLink/">WikiLink</a>.</p>' |
| 172 |
) |
| 173 |
|
| 174 |
def testWikilinkWhitespace(self): |
| 175 |
""" Test whitespace in `wikilinks`. """ |
| 176 |
self.assertEqual( |
| 177 |
self.md.convert('[[ foo bar_baz ]]'), |
| 178 |
'<p><a class="wikilink" href="/foo_bar_baz/">foo bar_baz</a></p>' |
| 179 |
) |
| 180 |
self.assertEqual( |
| 181 |
self.md.convert('foo [[ ]] bar'), |
| 182 |
'<p>foo bar</p>' |
| 183 |
) |
| 184 |
|
| 185 |
def testSimpleSettings(self): |
| 186 |
""" Test Simple Settings. """ |
| 187 |
|
| 188 |
self.assertEqual(markdown.markdown( |
| 189 |
self.text, extensions=[ |
| 190 |
markdown.extensions.wikilinks.WikiLinkExtension( |
| 191 |
base_url='/wiki/', |
| 192 |
end_url='.html', |
| 193 |
html_class='foo') |
| 194 |
] |
| 195 |
), |
| 196 |
'<p>Some text with a ' |
| 197 |
'<a class="foo" href="/wiki/WikiLink.html">WikiLink</a>.</p>') |
| 198 |
|
| 199 |
def testComplexSettings(self): |
| 200 |
""" Test Complex Settings. """ |
| 201 |
|
| 202 |
md = markdown.Markdown( |
| 203 |
extensions=['wikilinks'], |
| 204 |
extension_configs={ |
| 205 |
'wikilinks': [ |
| 206 |
('base_url', 'http://example.com/'), |
| 207 |
('end_url', '.html'), |
| 208 |
('html_class', '') |
| 209 |
] |
| 210 |
}, |
| 211 |
safe_mode=True |
| 212 |
) |
| 213 |
self.assertEqual( |
| 214 |
md.convert(self.text), |
| 215 |
'<p>Some text with a ' |
| 216 |
'<a href="http://example.com/WikiLink.html">WikiLink</a>.</p>' |
| 217 |
) |
| 218 |
|
| 219 |
def testWikilinksMetaData(self): |
| 220 |
""" test `MetaData` with `Wikilinks` Extension. """ |
| 221 |
|
| 222 |
text = """wiki_base_url: http://example.com/ |
| 223 |
wiki_end_url: .html |
| 224 |
wiki_html_class: |
| 225 |
|
| 226 |
Some text with a [[WikiLink]].""" |
| 227 |
md = markdown.Markdown(extensions=['meta', 'wikilinks']) |
| 228 |
self.assertEqual( |
| 229 |
md.convert(text), |
| 230 |
'<p>Some text with a ' |
| 231 |
'<a href="http://example.com/WikiLink.html">WikiLink</a>.</p>' |
| 232 |
) |
| 233 |
|
| 234 |
# `MetaData` should not carry over to next document: |
| 235 |
self.assertEqual( |
| 236 |
md.convert("No [[MetaData]] here."), |
| 237 |
'<p>No <a class="wikilink" href="/MetaData/">MetaData</a> ' |
| 238 |
'here.</p>' |
| 239 |
) |
| 240 |
|
| 241 |
def testURLCallback(self): |
| 242 |
""" Test used of a custom URL builder. """ |
| 243 |
|
| 244 |
from markdown.extensions.wikilinks import WikiLinkExtension |
| 245 |
|
| 246 |
def my_url_builder(label, base, end): |
| 247 |
return '/bar/' |
| 248 |
|
| 249 |
md = markdown.Markdown(extensions=[WikiLinkExtension(build_url=my_url_builder)]) |
| 250 |
self.assertEqual( |
| 251 |
md.convert('[[foo]]'), |
| 252 |
'<p><a class="wikilink" href="/bar/">foo</a></p>' |
| 253 |
) |
| 254 |
|
| 255 |
|
| 256 |
class TestAdmonition(unittest.TestCase): |
| 257 |
""" Test Admonition Extension. """ |
| 258 |
|
| 259 |
def setUp(self): |
| 260 |
self.md = markdown.Markdown(extensions=['admonition']) |
| 261 |
|
| 262 |
def testRE(self): |
| 263 |
RE = self.md.parser.blockprocessors['admonition'].RE |
| 264 |
tests = [ |
| 265 |
('!!! note', ('note', None)), |
| 266 |
('!!! note "Please Note"', ('note', 'Please Note')), |
| 267 |
('!!! note ""', ('note', '')), |
| 268 |
] |
| 269 |
for test, expected in tests: |
| 270 |
self.assertEqual(RE.match(test).groups(), expected) |
| 271 |
|
| 272 |
|
| 273 |
class TestSmarty(unittest.TestCase): |
| 274 |
def setUp(self): |
| 275 |
config = { |
| 276 |
'smarty': [ |
| 277 |
('smart_angled_quotes', True), |
| 278 |
('substitutions', { |
| 279 |
'ndash': '\u2013', |
| 280 |
'mdash': '\u2014', |
| 281 |
'ellipsis': '\u2026', |
| 282 |
'left-single-quote': '‚', # `sb` is not a typo! |
| 283 |
'right-single-quote': '‘', |
| 284 |
'left-double-quote': '„', |
| 285 |
'right-double-quote': '“', |
| 286 |
'left-angle-quote': '[', |
| 287 |
'right-angle-quote': ']', |
| 288 |
}), |
| 289 |
] |
| 290 |
} |
| 291 |
self.md = markdown.Markdown( |
| 292 |
extensions=['smarty'], |
| 293 |
extension_configs=config |
| 294 |
) |
| 295 |
|
| 296 |
def testCustomSubstitutions(self): |
| 297 |
text = """<< The "Unicode char of the year 2014" |
| 298 |
is the 'mdash': --- |
| 299 |
Must not be confused with 'ndash' (--) ... >> |
| 300 |
""" |
| 301 |
correct = """<p>[ The „Unicode char of the year 2014“ |
| 302 |
is the ‚mdash‘: \u2014 |
| 303 |
Must not be confused with ‚ndash‘ (\u2013) \u2026 ]</p>""" |
| 304 |
self.assertEqual(self.md.convert(text), correct) |