Skip to content

Commit

Permalink
Merge pull request #522 from Crozzers/middle-word-em
Browse files Browse the repository at this point in the history
Add `middle-word-em` extra
  • Loading branch information
nicholasserra committed Jul 23, 2023
2 parents 863a3af + 0615627 commit 6c5fb43
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -3,6 +3,7 @@
## python-markdown2 2.4.10 (not yet released)

- [pull #520] Allow more relative links in safe mode (issue #517)
- [pull #522] Add `middle-word-em` extra


## python-markdown2 2.4.9
Expand Down
18 changes: 14 additions & 4 deletions lib/markdown2.py
Expand Up @@ -66,6 +66,9 @@
some limitations.
* metadata: Extract metadata from a leading '---'-fenced block.
See <https://github.com/trentm/python-markdown2/issues/77> for details.
* middle-word-em: Allows or disallows emphasis syntax in the middle of words,
defaulting to allow. Disabling this means that `this_text_here` will not be
converted to `this<em>text</em>here`.
* nofollow: Add `rel="nofollow"` to add `<a>` tags with an href. See
<http://en.wikipedia.org/wiki/Nofollow>.
* numbering: Support of generic counters. Non standard extension to
Expand Down Expand Up @@ -2299,17 +2302,24 @@ def _do_tg_spoiler(self, text):
return text

_strong_re = re.compile(r"(\*\*|__)(?=\S)(.+?[*_]*)(?<=\S)\1", re.S)
_em_re = re.compile(r"(\*|_)(?=\S)(.+?)(?<=\S)\1", re.S)
_em_re = r"(\*|_)(?=\S)(.+?)(?<=\S)\1"
_code_friendly_strong_re = re.compile(r"\*\*(?=\S)(.+?[*_]*)(?<=\S)\*\*", re.S)
_code_friendly_em_re = re.compile(r"\*(?=\S)(.+?)(?<=\S)\*", re.S)
_code_friendly_em_re = r"\*(?=\S)(.+?)(?<=\S)\*"
def _do_italics_and_bold(self, text):
if self.extras.get('middle-word-em', True) is False:
code_friendly_em_re = r'(?<=\b)%s(?=\b)' % self._code_friendly_em_re
em_re = r'(?<=\b)%s(?=\b)' % self._em_re
else:
code_friendly_em_re = self._code_friendly_em_re
em_re = self._em_re

# <strong> must go first:
if "code-friendly" in self.extras:
text = self._code_friendly_strong_re.sub(r"<strong>\1</strong>", text)
text = self._code_friendly_em_re.sub(r"<em>\1</em>", text)
text = re.sub(code_friendly_em_re, r"<em>\1</em>", text, flags=re.S)
else:
text = self._strong_re.sub(r"<strong>\2</strong>", text)
text = self._em_re.sub(r"<em>\2</em>", text)
text = re.sub(em_re, r"<em>\2</em>", text, flags=re.S)
return text

# "smarty-pants" extra: Very liberal in interpreting a single prime as an
Expand Down
5 changes: 5 additions & 0 deletions test/tm-cases/middle_word_em.html
@@ -0,0 +1,5 @@
<p>When middle word emphasis is disabled strings like 'self.this_long_attr' should not
become <code>self.this&lt;em&gt;long&lt;/em&gt;attr</code>.</p>

<p>Emphasis will <em>only</em> occur when the word is surrounded with whitespace.
This should still work with <em>my_filename</em>.</p>
1 change: 1 addition & 0 deletions test/tm-cases/middle_word_em.opts
@@ -0,0 +1 @@
{'extras': {'middle-word-em': False}}
5 changes: 5 additions & 0 deletions test/tm-cases/middle_word_em.text
@@ -0,0 +1,5 @@
When middle word emphasis is disabled strings like 'self.this_long_attr' should not
become `self.this<em>long</em>attr`.

Emphasis will _only_ occur when the word is surrounded with whitespace.
This should still work with _my_filename_.

0 comments on commit 6c5fb43

Please sign in to comment.