| 1 |
# Legacy Em Extension for Python-Markdown |
| 2 |
# ======================================= |
| 3 |
|
| 4 |
# This extension provides legacy behavior for _connected_words_. |
| 5 |
|
| 6 |
# Copyright 2015-2018 The Python Markdown Project |
| 7 |
|
| 8 |
# License: [BSD](https://opensource.org/licenses/bsd-license.php) |
| 9 |
|
| 10 |
""" |
| 11 |
This extension provides legacy behavior for _connected_words_. |
| 12 |
""" |
| 13 |
|
| 14 |
from __future__ import annotations |
| 15 |
|
| 16 |
from . import Extension |
| 17 |
from ..inlinepatterns import UnderscoreProcessor, EmStrongItem, EM_STRONG2_RE, STRONG_EM2_RE |
| 18 |
import re |
| 19 |
|
| 20 |
# _emphasis_ |
| 21 |
EMPHASIS_RE = r'(_)([^_]+)\1' |
| 22 |
|
| 23 |
# __strong__ |
| 24 |
STRONG_RE = r'(_{2})(.+?)\1' |
| 25 |
|
| 26 |
# __strong_em___ |
| 27 |
STRONG_EM_RE = r'(_)\1(?!\1)([^_]+?)\1(?!\1)(.+?)\1{3}' |
| 28 |
|
| 29 |
|
| 30 |
class LegacyUnderscoreProcessor(UnderscoreProcessor): |
| 31 |
"""Emphasis processor for handling strong and em matches inside underscores.""" |
| 32 |
|
| 33 |
PATTERNS = [ |
| 34 |
EmStrongItem(re.compile(EM_STRONG2_RE, re.DOTALL | re.UNICODE), 'double', 'strong,em'), |
| 35 |
EmStrongItem(re.compile(STRONG_EM2_RE, re.DOTALL | re.UNICODE), 'double', 'em,strong'), |
| 36 |
EmStrongItem(re.compile(STRONG_EM_RE, re.DOTALL | re.UNICODE), 'double2', 'strong,em'), |
| 37 |
EmStrongItem(re.compile(STRONG_RE, re.DOTALL | re.UNICODE), 'single', 'strong'), |
| 38 |
EmStrongItem(re.compile(EMPHASIS_RE, re.DOTALL | re.UNICODE), 'single', 'em') |
| 39 |
] |
| 40 |
|
| 41 |
|
| 42 |
class LegacyEmExtension(Extension): |
| 43 |
""" Add legacy_em extension to Markdown class.""" |
| 44 |
|
| 45 |
def extendMarkdown(self, md): |
| 46 |
""" Register the processor. |
| 47 |
|
| 48 |
| Class Instance | Registry | Name | Priority | |
| 49 |
| ------------------------------------------------------------- | ---------------------------------------------------------------- | ------ | :------: | |
| 50 |
| [`LegacyUnderscoreProcessor`][markdown.extensions.legacy_em.LegacyUnderscoreProcessor] | [`inlinepatterns`][markdown.inlinepatterns.build_inlinepatterns] | `em_strong2` | `50` | |
| 51 |
|
| 52 |
""" |
| 53 |
# flake8: noqa: E501 48-50 |
| 54 |
md.inlinePatterns.register(LegacyUnderscoreProcessor(r'_'), 'em_strong2', 50) |
| 55 |
|
| 56 |
|
| 57 |
def makeExtension(**kwargs): # pragma: no cover |
| 58 |
""" Return an instance of the `LegacyEmExtension` """ |
| 59 |
return LegacyEmExtension(**kwargs) |