Skip to content

Use separate delimiter list for quotes - #667

Open
jgm wants to merge 1 commit into
masterfrom
twopass
Open

jgm wants to merge 1 commit into
masterfrom
twopass

Conversation

@jgm

@jgm jgm commented Nov 25, 2026

Copy link
Copy Markdown
Member

Match smart quotes in a separate pass after emphasis, and use a separate list to track possible quote matches. This allows us to fix the quadratic behavior noted in #521, and allows quote matching to be independent of emphasis matching, so that quotes can match across emphasis boundaries, e.g. in _a 'b_ c'.

Previously, smart quotes shared the delimiter stack with `*` and `_`
and were matched in the same pass as emphasis.  This had two problems:

1. Quadratic behavior (#521).  Matching a pair of quotes did not
   remove the delimiters between them, so with input like

       'x 'x 'x ... '"x'"x'"x

   or

       "x "x "x ... "_x"_x"_x

   each closer had to scan back over an ever-growing run of
   unrelated delimiters to find its opener.

2. Inconsistent interaction with emphasis.  Because delimiters are
   processed left to right and S_insert_emph removes delimiters
   between matched emphasis, whether a quote got matched depended
   on whether emphasis closed before it:

       _a 'b_ c'   =>  <em>a ’b</em> c’
       'a _b' c_   =>  ‘a <em>b’ c</em>

Now quotes go on their own stack (`last_quote`) and are matched by a
new function, process_smart_quotes, which runs after process_emphasis
(both at the end of inline parsing and when a link or image is
closed).  As a result:

- Quote pairs and emphasis are independent and may overlap:

      _a 'b_ c'   =>  <em>a ‘b</em> c’

- Quote pairs must nest properly with respect to each other.  When
  a pair matches, the quote delimiters between them are removed, so
  they can no longer match quotes outside the pair and fall back to
  their default rendering (a single quote becomes an apostrophe):

      "'Hi" there'  =>  “’Hi” there’

- Parsing is linear: each delimiter a closer scans past is removed
  once its opener is found, and when no opener is found,
  openers_bottom (tracked per quote character) keeps later closers
  from rescanning the same region.

process_emphasis now handles only `*` and `_`, so the openers_bottom
slots formerly used for quotes are gone.  remove_delimiter is
generalized as remove_from_stack, which takes the stack to remove
from.

Tests:

- smart_punct.txt: new examples for crossing quotes, properly nested
  quotes, and quotes overlapping emphasis.
- pathological_tests.py: new pathological_smart group, run with the
  smart option, containing the two reproducers from #521.  Both
  time out on the old code.  test/cmark.py gains to_html_smart to
  support this.

Closes #521.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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