Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ See the [Contributing Guide](contributing.md) for details.
performance for repeated inline patterns (#1619).
* Officially support Python 3.15 and drop support for Python 3.10
* Walk backtick runs in `BacktickInlineProcessor` without a regex (#1620).
* Complete rework of emphasis handling to imporove nested emphasis handling better (#1632).

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion docs/extensions/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ Here are some convenience functions and other examples:

| Class | Kind | Description |
| -------------------------------------------------------------------------------------|-----------|---------------------------------------------------------------|
| [`AsteriskProcessor`][markdown.inlinepatterns.AsteriskProcessor] | built-in | Emphasis processor for handling strong and em matches inside asterisks |
| [`DelimiterProcessor`][markdown.inlinepatterns.DelimiterProcessor] | built-in | Emphasis processor for handling strong and em matches |
| [`WikiLinksInlineProcessor`][markdown.extensions.wikilinks.WikiLinksInlineProcessor] | extension | Link `[[article names]]` to wiki given in metadata |
| [`FootnoteInlineProcessor`][markdown.extensions.footnotes.FootnoteInlineProcessor] | extension | Replaces footnote in text with link to footnote div at bottom |

Expand Down
3 changes: 3 additions & 0 deletions markdown/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from __future__ import annotations

import codecs
import time
import sys
import logging
import importlib
Expand Down Expand Up @@ -106,6 +107,7 @@ def __init__(self, **kwargs):

"""

self.last_run: float = 0.0
self.tab_length: int = kwargs.get('tab_length', 4)

self.ESCAPED_CHARS: list[str] = [
Expand Down Expand Up @@ -267,6 +269,7 @@ def reset(self) -> Markdown:
Called once upon creation of a class instance. Should be called manually between calls
to [`Markdown.convert`][markdown.Markdown.convert].
"""
self.last_run = time.time()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me why we need this. If I understand correctly, this initiates a reset of the extension once for each run of md.convert. Why not just have md.reset() accomplish that (by registering the extension)?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an inline processor, I wasn't sure the best way to handle it as it isn't wrapped in an extension, though I guess we could...I guess we could send the inline processor through register directly, but that didn't seem proper 🤷🏻

self.htmlStash.reset()
self.references.clear()

Expand Down
34 changes: 6 additions & 28 deletions markdown/extensions/legacy_em.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,7 @@
from __future__ import annotations

from . import Extension
from ..inlinepatterns import UnderscoreProcessor, EmStrongItem, EM_STRONG2_RE, STRONG_EM2_RE
import re

# _emphasis_
EMPHASIS_RE = r'(_)([^_]+)\1'

# __strong__
STRONG_RE = r'(_{2})(.+?)\1'

# __strong_em___
STRONG_EM_RE = r'(_)\1(?!\1)([^_]+?)\1(?!\1)(.+?)\1{3}'


class LegacyUnderscoreProcessor(UnderscoreProcessor):
"""Emphasis processor for handling strong and em matches inside underscores."""

PATTERNS = [
EmStrongItem(re.compile(EM_STRONG2_RE, re.DOTALL | re.UNICODE), 'double', 'strong,em'),
EmStrongItem(re.compile(STRONG_EM2_RE, re.DOTALL | re.UNICODE), 'double', 'em,strong'),
EmStrongItem(re.compile(STRONG_EM_RE, re.DOTALL | re.UNICODE), 'double2', 'strong,em'),
EmStrongItem(re.compile(STRONG_RE, re.DOTALL | re.UNICODE), 'single', 'strong'),
EmStrongItem(re.compile(EMPHASIS_RE, re.DOTALL | re.UNICODE), 'single', 'em')
]
from ..inlinepatterns import DelimiterProcessor


class LegacyEmExtension(Extension):
Expand All @@ -45,13 +23,13 @@ class LegacyEmExtension(Extension):
def extendMarkdown(self, md):
""" Register the processor.

| Class Instance | Registry | Name | Priority |
| ------------------------------------------------------------- | ---------------------------------------------------------------- | ------ | :------: |
| [`LegacyUnderscoreProcessor`][markdown.extensions.legacy_em.LegacyUnderscoreProcessor] | [`inlinepatterns`][markdown.inlinepatterns.build_inlinepatterns] | `em_strong2` | `50` |
| Class Instance | Registry | Name | Priority |
| ------------------------------------------------------------------ | ---------------------------------------------------------------- | ------------ | :------: |
| [`DelimiterProcessor`][markdown.inlinepatterns.DelimiterProcessor] | [`inlinepatterns`][markdown.inlinepatterns.build_inlinepatterns] | `em_strong2` | `50` |

"""
# flake8: noqa: E501 48-50
md.inlinePatterns.register(LegacyUnderscoreProcessor(r'_'), 'em_strong2', 50)
# flake8: noqa: E501 27-29
md.inlinePatterns.register(DelimiterProcessor(r'_', 'strong,em', md), 'em_strong2', 50)


def makeExtension(**kwargs): # pragma: no cover
Expand Down
Loading
Loading