Repository navigation
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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'.