| 1 |
# -*- coding: utf-8 -*- |
| 2 |
""" |
| 3 |
Python Markdown |
| 4 |
|
| 5 |
A Python implementation of John Gruber's Markdown. |
| 6 |
|
| 7 |
Documentation: https://python-markdown.github.io/ |
| 8 |
GitHub: https://github.com/Python-Markdown/markdown/ |
| 9 |
PyPI: https://pypi.org/project/Markdown/ |
| 10 |
|
| 11 |
Started by Manfred Stienstra (http://www.dwerg.net/). |
| 12 |
Maintained for a few years by Yuri Takhteyev (http://www.freewisdom.org). |
| 13 |
Currently maintained by Waylan Limberg (https://github.com/waylan), |
| 14 |
Dmitry Shachnev (https://github.com/mitya57) and Isaac Muse (https://github.com/facelessuser). |
| 15 |
|
| 16 |
Copyright 2007-2022 The Python Markdown Project (v. 1.7 and later) |
| 17 |
Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b) |
| 18 |
Copyright 2004 Manfred Stienstra (the original version) |
| 19 |
|
| 20 |
License: BSD (see LICENSE.md for details). |
| 21 |
""" |
| 22 |
|
| 23 |
from markdown.test_tools import TestCase |
| 24 |
|
| 25 |
|
| 26 |
class TestSmarty(TestCase): |
| 27 |
|
| 28 |
default_kwargs = {'extensions': ['smarty']} |
| 29 |
|
| 30 |
def test_basic(self): |
| 31 |
self.assertMarkdownRenders( |
| 32 |
"It's fun. What's fun?", |
| 33 |
'<p>It’s fun. What’s fun?</p>' |
| 34 |
) |
| 35 |
self.assertMarkdownRenders( |
| 36 |
'"Isn\'t this fun"? --- she said...', |
| 37 |
'<p>“Isn’t this fun”? — she said…</p>' |
| 38 |
) |
| 39 |
self.assertMarkdownRenders( |
| 40 |
'"\'Quoted\' words in a larger quote."', |
| 41 |
'<p>“‘Quoted’ words in a larger quote.”</p>' |
| 42 |
) |
| 43 |
self.assertMarkdownRenders( |
| 44 |
'\'Quoted "words" in a larger quote.\'', |
| 45 |
'<p>‘Quoted “words” in a larger quote.’</p>' |
| 46 |
) |
| 47 |
self.assertMarkdownRenders( |
| 48 |
'"Quoted words at the \'end.\'"', |
| 49 |
'<p>“Quoted words at the ‘end.’”</p>' |
| 50 |
) |
| 51 |
self.assertMarkdownRenders( |
| 52 |
'\'Quoted words at the "end."\'', |
| 53 |
'<p>‘Quoted words at the “end.”’</p>' |
| 54 |
) |
| 55 |
self.assertMarkdownRenders( |
| 56 |
'(He replied, "She said \'Hello.\'")', |
| 57 |
'<p>(He replied, “She said ‘Hello.’”)</p>' |
| 58 |
) |
| 59 |
self.assertMarkdownRenders( |
| 60 |
'<span>He replied, "She said \'Hello.\'"</span>', |
| 61 |
'<p><span>He replied, “She said ‘Hello.’”</span></p>' |
| 62 |
) |
| 63 |
self.assertMarkdownRenders( |
| 64 |
'"quoted" text and **bold "quoted" text**', |
| 65 |
'<p>“quoted” text and <strong>bold “quoted” text</strong></p>' |
| 66 |
) |
| 67 |
self.assertMarkdownRenders( |
| 68 |
"'quoted' text and **bold 'quoted' text**", |
| 69 |
'<p>‘quoted’ text and <strong>bold ‘quoted’ text</strong></p>' |
| 70 |
) |
| 71 |
self.assertMarkdownRenders( |
| 72 |
'em-dashes (---) and ellipes (...)', |
| 73 |
'<p>em-dashes (—) and ellipes (…)</p>' |
| 74 |
) |
| 75 |
self.assertMarkdownRenders( |
| 76 |
'"[Link](http://example.com)" --- she said.', |
| 77 |
'<p>“<a href="http://example.com">Link</a>” — she said.</p>' |
| 78 |
) |
| 79 |
self.assertMarkdownRenders( |
| 80 |
'"Ellipsis within quotes..."', |
| 81 |
'<p>“Ellipsis within quotes…”</p>' |
| 82 |
) |
| 83 |
self.assertMarkdownRenders( |
| 84 |
"*Custer*'s Last Stand", |
| 85 |
"<p><em>Custer</em>’s Last Stand</p>" |
| 86 |
) |
| 87 |
|
| 88 |
def test_years(self): |
| 89 |
self.assertMarkdownRenders("1440--80's", '<p>1440–80’s</p>') |
| 90 |
self.assertMarkdownRenders("1440--'80s", '<p>1440–’80s</p>') |
| 91 |
self.assertMarkdownRenders("1440---'80s", '<p>1440—’80s</p>') |
| 92 |
self.assertMarkdownRenders("1960's", '<p>1960’s</p>') |
| 93 |
self.assertMarkdownRenders("one two '60s", '<p>one two ’60s</p>') |
| 94 |
self.assertMarkdownRenders("'60s", '<p>’60s</p>') |
| 95 |
|
| 96 |
def test_wrapping_line(self): |
| 97 |
text = ( |
| 98 |
"A line that 'wraps' with\n" |
| 99 |
"*emphasis* at the beginning of the next line." |
| 100 |
) |
| 101 |
html = ( |
| 102 |
'<p>A line that ‘wraps’ with\n' |
| 103 |
'<em>emphasis</em> at the beginning of the next line.</p>' |
| 104 |
) |
| 105 |
self.assertMarkdownRenders(text, html) |
| 106 |
|
| 107 |
def test_escaped(self): |
| 108 |
self.assertMarkdownRenders( |
| 109 |
'Escaped \\-- ndash', |
| 110 |
'<p>Escaped -- ndash</p>' |
| 111 |
) |
| 112 |
self.assertMarkdownRenders( |
| 113 |
'\\\'Escaped\\\' \\"quotes\\"', |
| 114 |
'<p>\'Escaped\' "quotes"</p>' |
| 115 |
) |
| 116 |
self.assertMarkdownRenders( |
| 117 |
'Escaped ellipsis\\...', |
| 118 |
'<p>Escaped ellipsis...</p>' |
| 119 |
) |
| 120 |
self.assertMarkdownRenders( |
| 121 |
'\'Escaped \\"quotes\\" in real ones\'', |
| 122 |
'<p>‘Escaped "quotes" in real ones’</p>' |
| 123 |
) |
| 124 |
self.assertMarkdownRenders( |
| 125 |
'\\\'"Real" quotes in escaped ones\\\'', |
| 126 |
"<p>'“Real” quotes in escaped ones'</p>" |
| 127 |
) |
| 128 |
|
| 129 |
def test_escaped_attr(self): |
| 130 |
self.assertMarkdownRenders( |
| 131 |
'', |
| 132 |
'<p><img alt="x"x" src="x" /></p>' |
| 133 |
) |
| 134 |
|
| 135 |
def test_code_spans(self): |
| 136 |
self.assertMarkdownRenders( |
| 137 |
'Skip `"code" -- --- \'spans\' ...`.', |
| 138 |
'<p>Skip <code>"code" -- --- \'spans\' ...</code>.</p>' |
| 139 |
) |
| 140 |
|
| 141 |
def test_code_blocks(self): |
| 142 |
text = ( |
| 143 |
' Also skip "code" \'blocks\'\n' |
| 144 |
' foo -- bar --- baz ...' |
| 145 |
) |
| 146 |
html = ( |
| 147 |
'<pre><code>Also skip "code" \'blocks\'\n' |
| 148 |
'foo -- bar --- baz ...\n' |
| 149 |
'</code></pre>' |
| 150 |
) |
| 151 |
self.assertMarkdownRenders(text, html) |
| 152 |
|
| 153 |
def test_horizontal_rule(self): |
| 154 |
self.assertMarkdownRenders('--- -- ---', '<hr />') |
| 155 |
|
| 156 |
|
| 157 |
class TestSmartyAngledQuotes(TestCase): |
| 158 |
|
| 159 |
default_kwargs = { |
| 160 |
'extensions': ['smarty'], |
| 161 |
'extension_configs': { |
| 162 |
'smarty': { |
| 163 |
'smart_angled_quotes': True, |
| 164 |
}, |
| 165 |
}, |
| 166 |
} |
| 167 |
|
| 168 |
def test_angled_quotes(self): |
| 169 |
self.assertMarkdownRenders( |
| 170 |
'<<hello>>', |
| 171 |
'<p>«hello»</p>' |
| 172 |
) |
| 173 |
self.assertMarkdownRenders( |
| 174 |
'Кавычки-<<ёлочки>>', |
| 175 |
'<p>Кавычки-«ёлочки»</p>' |
| 176 |
) |
| 177 |
self.assertMarkdownRenders( |
| 178 |
'Anführungszeichen->>Chevrons<<', |
| 179 |
'<p>Anführungszeichen-»Chevrons«</p>' |
| 180 |
) |
| 181 |
|
| 182 |
|
| 183 |
class TestSmartyCustomSubstitutions(TestCase): |
| 184 |
|
| 185 |
default_kwargs = { |
| 186 |
'extensions': ['smarty'], |
| 187 |
'extension_configs': { |
| 188 |
'smarty': { |
| 189 |
'smart_angled_quotes': True, |
| 190 |
'substitutions': { |
| 191 |
'ndash': '\u2013', |
| 192 |
'mdash': '\u2014', |
| 193 |
'ellipsis': '\u2026', |
| 194 |
'left-single-quote': '‚', # `sb` is not a typo! |
| 195 |
'right-single-quote': '‘', |
| 196 |
'left-double-quote': '„', |
| 197 |
'right-double-quote': '“', |
| 198 |
'left-angle-quote': '[', |
| 199 |
'right-angle-quote': ']', |
| 200 |
}, |
| 201 |
}, |
| 202 |
}, |
| 203 |
} |
| 204 |
|
| 205 |
def test_custom_substitutions(self): |
| 206 |
text = ( |
| 207 |
'<< The "Unicode char of the year 2014"\n' |
| 208 |
"is the 'mdash': ---\n" |
| 209 |
"Must not be confused with 'ndash' (--) ... >>" |
| 210 |
) |
| 211 |
html = ( |
| 212 |
'<p>[ The „Unicode char of the year 2014“\n' |
| 213 |
'is the ‚mdash‘: \u2014\n' |
| 214 |
'Must not be confused with ‚ndash‘ (\u2013) \u2026 ]</p>' |
| 215 |
) |
| 216 |
self.assertMarkdownRenders(text, html) |
| 217 |
|
| 218 |
|
| 219 |
class TestSmartyAndToc(TestCase): |
| 220 |
|
| 221 |
default_kwargs = { |
| 222 |
'extensions': ['smarty', 'toc'], |
| 223 |
} |
| 224 |
|
| 225 |
def test_smarty_and_toc(self): |
| 226 |
self.assertMarkdownRenders( |
| 227 |
'# *Foo* --- `bar`', |
| 228 |
'<h1 id="foo-bar"><em>Foo</em> — <code>bar</code></h1>', |
| 229 |
expected_attrs={ |
| 230 |
'toc_tokens': [ |
| 231 |
{ |
| 232 |
'level': 1, |
| 233 |
'id': 'foo-bar', |
| 234 |
'name': 'Foo — bar', |
| 235 |
'html': '<em>Foo</em> — <code>bar</code>', |
| 236 |
'data-toc-label': '', |
| 237 |
'children': [], |
| 238 |
}, |
| 239 |
], |
| 240 |
}, |
| 241 |
) |