Skip to content

Limit abbreviation definitions to one line - #1728

Open
naaz234 wants to merge 1 commit into
Python-Markdown:masterfrom
naaz234:abbr-definition-single-line
Open

naaz234 wants to merge 1 commit into
Python-Markdown:masterfrom
naaz234:abbr-definition-single-line

Conversation

@naaz234

@naaz234 naaz234 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Description

AbbrBlockprocessor.RE matches the abbreviation with [^\\]*?, which also matches newlines, and test accepts any block. So with abbr (or extra) enabled, a line which starts with *[ but is not a definition is read on to the next ]: further down the same block:

>>> markdown.markdown('*[sic]* marks a quoted error.\n*[HTML]: Hyper Text Markup Language\n\nHTML', extensions=['abbr'])
'<p>HTML</p>'

The first line is dropped and HTML is not defined. The abbreviation recorded instead is 'sic]* marks a quoted error.\n*[HTML'. A link reference definition on the later line is lost the same way. When no ]: follows, every line starting with *[ is scanned to the end of the block, so the time is quadratic in the number of such lines:

'*[a]\n' * n, extensions=['extra'], CPython 3.11

           master   with this change
 39 KiB     1.7 s       0.08 s
 78 KiB     6.6 s       0.08 s
156 KiB    28.6 s       0.17 s

Before 9edba85 the class excluded ], so a match stopped at the first ] and a line such as *[sic]* could not run on into the next one. Excluding newlines instead keeps ] and ^ allowed in an abbreviation and limits each attempt to its own line, as the line-based preprocessor which the block processor replaced did. A title on the following line (test_abbr_line_break) still works. The one change in behavior is that an abbreviation whose name is split over two lines is no longer a definition.

The old and new regex give the same search and finditer matches on every string up to length 9 over *, [, ], :, space, a, newline and backslash, plus 300,000 random strings, except where the old match's abbreviation spans lines. Rendered HTML and recorded abbreviations match master on 200,000 random documents (with abbr, extra, and extra plus toc, admonition, sane_lists, smarty and nl2br) except the 3,649 in which master matched a multi-line abbreviation.

This is separate from #1702, which takes consecutive definitions in one pass. The scan above happens inside a single search, and finditer has the same problem. #1702's code change applies cleanly on top of this one (only the changelog entry conflicts), its tests pass, and the 156 KiB input takes 0.3 s with both.

The first new test fails on master. The second takes 13.4 s on master and 0.09 s with this change.

AI Assistance Disclosure

  • No AI tools were used in preparing this PR.

  • AI tools were used and I have disclosed which ones, and have fully reviewed and verified their output.

    AI tools were use to assist with the following:

    • Code
    • Code Comments
    • Documentation
    • Changelog Entry
    • Commit Message
    • The Description Above
    • Other: finding the issue, the tests, the timings and the comparisons against master

Checklist

The `[^\\]*?` group for the abbreviation in `AbbrBlockprocessor.RE` also
matches newlines, so a line starting with `*[` which is not a definition
is read on to the next `]:` further down the block. The lines in between
become part of the abbreviation and are dropped from the output, and the
definition (or link reference) on the later line is lost. When no `]:`
follows, every line starting with `*[` is scanned to the end of the
block, so n of them take O(n^2) time.

Before 9edba85 the group was `[^\]]*`, which stopped at the first `]`.
Excluding newlines instead keeps `]` and `^` allowed in an abbreviation
and limits each attempt to its own line, as the line-based preprocessor
this replaced did. A title on the following line is still matched by
the rest of the pattern.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant