| 1 |
"""Test cases for PathConverter.""" |
| 2 |
from .. import util |
| 3 |
import os |
| 4 |
|
| 5 |
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 6 |
PARENT_DIR = os.path.dirname(CURRENT_DIR) |
| 7 |
|
| 8 |
|
| 9 |
class TestRelative(util.MdCase): |
| 10 |
"""Test relative paths.""" |
| 11 |
|
| 12 |
extension = ["pymdownx.pathconverter"] |
| 13 |
extension_configs = { |
| 14 |
"pymdownx.pathconverter": { |
| 15 |
"base_path": CURRENT_DIR, |
| 16 |
"relative_path": PARENT_DIR |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
def test_in_script(self): |
| 21 |
"""Test that we do not parse image in script.""" |
| 22 |
|
| 23 |
self.check_markdown( |
| 24 |
r''' |
| 25 |
<script> |
| 26 |
var str = '<img alt="picture" src="../test_extensions/_assets/bg.png" />' |
| 27 |
</script> |
| 28 |
''', |
| 29 |
r''' |
| 30 |
<script> |
| 31 |
var str = '<img alt="picture" src="../test_extensions/_assets/bg.png" />' |
| 32 |
</script> |
| 33 |
''', |
| 34 |
True |
| 35 |
) |
| 36 |
|
| 37 |
def test_comment(self): |
| 38 |
"""Test comment.""" |
| 39 |
|
| 40 |
self.check_markdown( |
| 41 |
r'<!--  -->', |
| 42 |
r'<!--  -->' |
| 43 |
) |
| 44 |
|
| 45 |
def test_relative_path(self): |
| 46 |
"""Test relative path.""" |
| 47 |
|
| 48 |
self.check_markdown( |
| 49 |
r'', |
| 50 |
r'<p><img alt="picture" src="test_extensions/_assets/bg.png" /></p>' |
| 51 |
) |
| 52 |
|
| 53 |
def test_file_win_file_path_root(self): |
| 54 |
"""Test windows file:// path with root slash.""" |
| 55 |
|
| 56 |
self.check_markdown( |
| 57 |
r'[file link windows abs](file:///c:/path/file.html)', |
| 58 |
r'<p><a href="file:///c:/path/file.html">file link windows abs</a></p>' |
| 59 |
) |
| 60 |
|
| 61 |
def test_win_file_path(self): |
| 62 |
"""Test windows file:// path.""" |
| 63 |
|
| 64 |
self.check_markdown( |
| 65 |
r'[file link windows abs2](file://c:/path/file.html)', |
| 66 |
r'<p><a href="file://c:/path/file.html">file link windows abs2</a></p>' |
| 67 |
) |
| 68 |
|
| 69 |
def test_file_root(self): |
| 70 |
"""Test Linux/Unix style root file:// path.""" |
| 71 |
|
| 72 |
self.check_markdown( |
| 73 |
r'[file link abs](file:///path/file.html)', |
| 74 |
r'<p><a href="file:///path/file.html">file link abs</a></p>' |
| 75 |
) |
| 76 |
|
| 77 |
def test_root(self): |
| 78 |
"""Test /root path.""" |
| 79 |
|
| 80 |
self.check_markdown( |
| 81 |
r'[absolute](/absolute)', |
| 82 |
r'<p><a href="/absolute">absolute</a></p>' |
| 83 |
) |
| 84 |
|
| 85 |
def test_url(self): |
| 86 |
"""Test normal URL.""" |
| 87 |
|
| 88 |
self.check_markdown( |
| 89 |
r'[link](http://www.google.com)', |
| 90 |
r'<p><a href="http://www.google.com">link</a></p>' |
| 91 |
) |
| 92 |
|
| 93 |
def test_fragment(self): |
| 94 |
"""Test HTML fragment.""" |
| 95 |
|
| 96 |
self.check_markdown( |
| 97 |
r'[fragment](#fragment)', |
| 98 |
r'<p><a href="#fragment">fragment</a></p>' |
| 99 |
) |
| 100 |
|
| 101 |
def test_windows(self): |
| 102 |
"""Test Windows file path.""" |
| 103 |
|
| 104 |
self.check_markdown( |
| 105 |
r'[windows path abs](c:/path/file.html)', |
| 106 |
r'<p><a href="c:/path/file.html">windows path abs</a></p>' |
| 107 |
) |
| 108 |
|
| 109 |
def test_network_path(self): |
| 110 |
"""Test network path.""" |
| 111 |
|
| 112 |
self.check_markdown( |
| 113 |
r'[windows network path](//network/path/file.html)', |
| 114 |
r'<p><a href="//network/path/file.html">windows network path</a></p>' |
| 115 |
) |
| 116 |
|
| 117 |
def test_strange_url(self): |
| 118 |
"""Test strange URL.""" |
| 119 |
|
| 120 |
self.check_markdown( |
| 121 |
r'[strange link](strange://odd/link/file.html)', |
| 122 |
r'<p><a href="strange://odd/link/file.html">strange link</a></p>' |
| 123 |
) |
| 124 |
|
| 125 |
def test_strange_url2(self): |
| 126 |
"""Test additional strange URL.""" |
| 127 |
|
| 128 |
self.check_markdown( |
| 129 |
r'[strange link 2](strange://www.odd.com/link/file.html)', |
| 130 |
r'<p><a href="strange://www.odd.com/link/file.html">strange link 2</a></p>' |
| 131 |
) |
| 132 |
|
| 133 |
def test_mail(self): |
| 134 |
"""Test mail link.""" |
| 135 |
|
| 136 |
self.check_markdown( |
| 137 |
r'<[email protected]>', |
| 138 |
r'<p><a href="mailto:mail@mail' |
| 139 |
r'.com">mail@mail.com</a></p>' |
| 140 |
) |
| 141 |
|
| 142 |
|
| 143 |
class TestAbsolute(util.MdCase): |
| 144 |
"""Test absolute paths.""" |
| 145 |
|
| 146 |
extension = ["pymdownx.pathconverter"] |
| 147 |
extension_configs = { |
| 148 |
"pymdownx.pathconverter": { |
| 149 |
"base_path": "/Some/fake/path", |
| 150 |
"absolute": True |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
def test_in_script(self): |
| 155 |
"""Test that we do not parse image in script.""" |
| 156 |
|
| 157 |
self.check_markdown( |
| 158 |
r''' |
| 159 |
<script> |
| 160 |
var str = '<img alt="picture" src="../test_extensions/_assets/bg.png" />' |
| 161 |
</script> |
| 162 |
''', |
| 163 |
r''' |
| 164 |
<script> |
| 165 |
var str = '<img alt="picture" src="../test_extensions/_assets/bg.png" />' |
| 166 |
</script> |
| 167 |
''', |
| 168 |
True |
| 169 |
) |
| 170 |
|
| 171 |
def test_comment(self): |
| 172 |
"""Test comment.""" |
| 173 |
|
| 174 |
self.check_markdown( |
| 175 |
r'<!--  -->', |
| 176 |
r'<!--  -->' |
| 177 |
) |
| 178 |
|
| 179 |
def test_relative_path(self): |
| 180 |
"""Test relative path.""" |
| 181 |
|
| 182 |
self.check_markdown( |
| 183 |
r'', |
| 184 |
r'<p><img alt="picture" src="/Some/fake/path/test_extensions/_assets/bg.png" /></p>' |
| 185 |
) |
| 186 |
|
| 187 |
def test_file_win_file_path_root(self): |
| 188 |
"""Test windows file:// path with root slash.""" |
| 189 |
|
| 190 |
self.check_markdown( |
| 191 |
r'[file link windows abs](file:///c:/path/file.html)', |
| 192 |
r'<p><a href="file:///c:/path/file.html">file link windows abs</a></p>' |
| 193 |
) |
| 194 |
|
| 195 |
def test_win_file_path(self): |
| 196 |
"""Test windows file:// path.""" |
| 197 |
|
| 198 |
self.check_markdown( |
| 199 |
r'[file link windows abs2](file://c:/path/file.html)', |
| 200 |
r'<p><a href="file://c:/path/file.html">file link windows abs2</a></p>' |
| 201 |
) |
| 202 |
|
| 203 |
def test_file_root(self): |
| 204 |
"""Test Linux/Unix style root file:// path.""" |
| 205 |
|
| 206 |
self.check_markdown( |
| 207 |
r'[file link abs](file:///path/file.html)', |
| 208 |
r'<p><a href="file:///path/file.html">file link abs</a></p>' |
| 209 |
) |
| 210 |
|
| 211 |
def test_root(self): |
| 212 |
"""Test /root path.""" |
| 213 |
|
| 214 |
self.check_markdown( |
| 215 |
r'[absolute](/absolute)', |
| 216 |
r'<p><a href="/absolute">absolute</a></p>' |
| 217 |
) |
| 218 |
|
| 219 |
def test_url(self): |
| 220 |
"""Test normal URL.""" |
| 221 |
|
| 222 |
self.check_markdown( |
| 223 |
r'[link](http://www.google.com)', |
| 224 |
r'<p><a href="http://www.google.com">link</a></p>' |
| 225 |
) |
| 226 |
|
| 227 |
def test_fragment(self): |
| 228 |
"""Test HTML fragment.""" |
| 229 |
|
| 230 |
self.check_markdown( |
| 231 |
r'[fragment](#fragment)', |
| 232 |
r'<p><a href="#fragment">fragment</a></p>' |
| 233 |
) |
| 234 |
|
| 235 |
def test_windows(self): |
| 236 |
"""Test Windows file path.""" |
| 237 |
|
| 238 |
self.check_markdown( |
| 239 |
r'[windows path abs](c:/path/file.html)', |
| 240 |
r'<p><a href="c:/path/file.html">windows path abs</a></p>' |
| 241 |
) |
| 242 |
|
| 243 |
def test_network_path(self): |
| 244 |
"""Test network path.""" |
| 245 |
|
| 246 |
self.check_markdown( |
| 247 |
r'[windows network path](//network/path/file.html)', |
| 248 |
r'<p><a href="//network/path/file.html">windows network path</a></p>' |
| 249 |
) |
| 250 |
|
| 251 |
def test_strange_url(self): |
| 252 |
"""Test strange URL.""" |
| 253 |
|
| 254 |
self.check_markdown( |
| 255 |
r'[strange link](strange://odd/link/file.html)', |
| 256 |
r'<p><a href="strange://odd/link/file.html">strange link</a></p>' |
| 257 |
) |
| 258 |
|
| 259 |
def test_strange_url2(self): |
| 260 |
"""Test additional strange URL.""" |
| 261 |
|
| 262 |
self.check_markdown( |
| 263 |
r'[strange link 2](strange://www.odd.com/link/file.html)', |
| 264 |
r'<p><a href="strange://www.odd.com/link/file.html">strange link 2</a></p>' |
| 265 |
) |
| 266 |
|
| 267 |
def test_mail(self): |
| 268 |
"""Test mail link.""" |
| 269 |
|
| 270 |
self.check_markdown( |
| 271 |
r'<[email protected]>', |
| 272 |
r'<p><a href="mailto:mail@mail' |
| 273 |
r'.com">mail@mail.com</a></p>' |
| 274 |
) |
| 275 |
|
| 276 |
|
| 277 |
class TestAbsoluteFileScheme(TestAbsolute): |
| 278 |
"""Test absolute paths with file:// scheme.""" |
| 279 |
|
| 280 |
extension = ["pymdownx.pathconverter"] |
| 281 |
extension_configs = { |
| 282 |
"pymdownx.pathconverter": { |
| 283 |
"base_path": "/Some/fake/path", |
| 284 |
"absolute": True, |
| 285 |
"file_scheme": True, |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
def test_relative_path(self): |
| 290 |
"""Test relative path.""" |
| 291 |
|
| 292 |
self.check_markdown( |
| 293 |
r'', |
| 294 |
r'<p><img alt="picture" src="file:///Some/fake/path/test_extensions/_assets/bg.png" /></p>' |
| 295 |
) |
| 296 |
|
| 297 |
|
| 298 |
class TestWindowsAbs(util.MdCase): |
| 299 |
"""Test windows specific cases for absolute.""" |
| 300 |
|
| 301 |
extension = ["pymdownx.pathconverter"] |
| 302 |
extension_configs = { |
| 303 |
"pymdownx.pathconverter": { |
| 304 |
"base_path": "C:/Some/fake/path", |
| 305 |
"absolute": True |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
def test_windows_root_conversion(self): |
| 310 |
"""Test Windows c:/ Conversion.""" |
| 311 |
|
| 312 |
if util.is_win(): |
| 313 |
self.check_markdown( |
| 314 |
r'', |
| 315 |
r'<p><img alt="picture" src="/C:/Some/fake/path/extensions/_assets/bg.png" /></p>' |
| 316 |
) |
| 317 |
else: |
| 318 |
self.check_markdown( |
| 319 |
r'', |
| 320 |
r'<p><img alt="picture" src="/C%3A/Some/fake/path/extensions/_assets/bg.png" /></p>' |
| 321 |
) |
| 322 |
|
| 323 |
|
| 324 |
class TestWindowsAbsFileScheme(util.MdCase): |
| 325 |
"""Test windows specific cases for absolute with file:// scheme.""" |
| 326 |
|
| 327 |
extension = ["pymdownx.pathconverter"] |
| 328 |
extension_configs = { |
| 329 |
"pymdownx.pathconverter": { |
| 330 |
"base_path": "C:/Some/fake/path", |
| 331 |
"absolute": True, |
| 332 |
"file_scheme": True, |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
def test_windows_root_conversion(self): |
| 337 |
"""Test Windows c:/ Conversion.""" |
| 338 |
if util.is_win(): |
| 339 |
self.check_markdown( |
| 340 |
r'', |
| 341 |
r'<p><img alt="picture" src="file:///C:/Some/fake/path/extensions/_assets/bg.png" /></p>' |
| 342 |
) |
| 343 |
else: |
| 344 |
self.check_markdown( |
| 345 |
r'', |
| 346 |
r'<p><img alt="picture" src="file:///C%3A/Some/fake/path/extensions/_assets/bg.png" /></p>' |
| 347 |
) |
| 348 |
|
| 349 |
|
| 350 |
class TestWindowsRel(util.MdCase): |
| 351 |
"""Test windows specific cases for relative.""" |
| 352 |
|
| 353 |
extension = ["pymdownx.pathconverter"] |
| 354 |
extension_configs = { |
| 355 |
"pymdownx.pathconverter": { |
| 356 |
"base_path": "C:/Some/fake/path", |
| 357 |
"relative_path": "C:/Some/other/path" |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
def test_windows_root_conversion(self): |
| 362 |
"""Test Windows c:/ Conversion.""" |
| 363 |
|
| 364 |
if util.is_win(): |
| 365 |
self.check_markdown( |
| 366 |
r'', |
| 367 |
r'<p><img alt="picture" src="../../fake/path/extensions/_assets/bg.png" /></p>' |
| 368 |
) |
| 369 |
else: |
| 370 |
self.check_markdown( |
| 371 |
r'', |
| 372 |
r'<p><img alt="picture" src="../../fake/path/extensions/_assets/bg.png" /></p>' |
| 373 |
) |