fix(robustness): improve Markdown link parsing, diagnostics, and thread safety - #66
Merged
Conversation
…read safety
1. Fragile Markdown link parsing: replace the split("](") heuristic in
_build_attribution_display with a re.fullmatch() call using the pattern
\[([^\]]+)\]\(([^)]+)\). The old approach broke silently when the URL
itself contained the two-character sequence "](".
2. Misleading diagnostic messages for imageless figures: capture the
original figure identifier (_figure_id) before the "dummy.png"
placeholder is appended to self.arguments. All warning/error messages
that referenced self.arguments[0] now use _figure_id so they report
"<no image>" instead of "dummy.png" for caption-only figures.
3. Thread-unsafe global cache: _untranslate_map_cache is now initialised
under a threading.Lock using the double-checked locking pattern so that
parallel Sphinx read workers cannot race to load the JSON file at the
same time.
https://claude.ai/code/session_01ApsmWPWsKBesMV1rq2VenA
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.
Summary
Three robustness improvements that prevent silent misbehaviour in edge cases.
1. Fragile Markdown link parsing in
_build_attribution_display(__init__.py:951)The source-value parser used
split("](")to detect the[text](url)format. This breaks silently when the URL itself contains the two-character sequence]((e.g. some documentation links with anchor fragments). The checklen(...split("](")) == 2also passed spuriously for strings with a single](anywhere.Fix: replace the split heuristic with
re.fullmatch(r"\[([^\]]+)\]\(([^)]+)\)", ...)which correctly matches only well-formed Markdown inline links and is unambiguous regardless of URL content.2. Misleading diagnostic messages for imageless figures (
__init__.py:510)When a figure has no image argument, the code appended
"dummy.png"toself.argumentsas a placeholder, and all subsequent warning and error messages usedself.arguments[0]. Every diagnostic for such a figure therefore reported"dummy.png"instead of a meaningful identifier, making it impossible to locate the offending directive.Fix: capture
_figure_id = self.arguments[0] if self.arguments else "<no image>"before the placeholder is appended, and use_figure_idin all diagnostic messages (unrecognized BibTeX key, missing/invalid license, invalid date, missing source).3. Thread-unsafe lazy initialisation of
_untranslate_map_cache(__init__.py:1672)parallel_read_safe = Trueallows Sphinx to fork worker processes that calluntranslate_license()concurrently. The previous check-and-set pattern (if _untranslate_map_cache is None: _untranslate_map_cache = ...) is not atomic; two workers can both observeNoneand both call_load_untranslate_map()simultaneously.Fix: protect the initialisation block with a
threading.Lockusing the double-checked locking pattern so that the JSON file is read at most once per process regardless of concurrency.Test plan
:source:to a URL containing]((e.g.[docs](https://example.com/path#section)) and confirm the link renders correctly.<no image>rather thandummy.png.sphinx-build -j auto) with a non-English locale and confirm no exception from concurrentuntranslate_license()calls.https://claude.ai/code/session_01ApsmWPWsKBesMV1rq2VenA