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