Describe the bug
context
When a Sphinx project using sphinx-external-toc includes subtrees without a caption in _toc.yml, and the gettext builder is used to generate .pot files.
expectation
No empty msgid entries should appear in the generated .pot files.
bug
The insert_toctrees() function in events.py unconditionally sets rawcaption on every toctree node via toctree.caption or "":
|
# TODO this wasn't in the original code, |
|
# but alabaster theme intermittently raised `KeyError('rawcaption')` |
|
subnode["rawcaption"] = toctree.caption or "" |
When no caption is present, this assigns an empty string "", which Sphinx's extract_original_messages() then extracts as an empty msgid:
#: path/to/file.md:1
msgid ""
msgstr ""
These empty entries cause msgcat and other gettext tools to report "duplicate message definition" errors.
problem
Sphinx's toctree.extract_original_messages() checks 'rawcaption' in self — it only cares whether the key exists, not whether its value is non-empty.
def extract_original_messages(self) -> list[str]:
messages: list[str] = []
# toctree entries
messages.extend(self.get('rawentries', []))
# :caption: option
if 'rawcaption' in self:
messages.append(self['rawcaption'])
return messages
The workaround toctree.caption or "" (originally added for alabaster theme compatibility) forces the key to always exist, causing gettext to extract empty strings.
Reproduce the bug
-
Create a _toc.yml with nested subtrees that lack a caption:
root: intro
subtrees:
- entries:
- file: doc1
subtrees:
- entries:
- file: subfolder/page
-
Run the gettext builder:
sphinx-build -b gettext . _build/gettext
-
Inspect the generated .pot files — they will contain empty msgid entries.
List your environment
sphinx-external-toc==1.1.0
Additional context
This issue was originally reported as sphinx-doc/sphinx#13168
Describe the bug
context
When a Sphinx project using
sphinx-external-tocincludes subtrees without acaptionin_toc.yml, and the gettext builder is used to generate.potfiles.expectation
No empty
msgidentries should appear in the generated.potfiles.bug
The
insert_toctrees()function inevents.pyunconditionally setsrawcaptionon every toctree node viatoctree.caption or "":sphinx-external-toc/sphinx_external_toc/events.py
Lines 233 to 235 in ba976db
When no caption is present, this assigns an empty string
"", which Sphinx'sextract_original_messages()then extracts as an emptymsgid:These empty entries cause
msgcatand other gettext tools to report "duplicate message definition" errors.problem
Sphinx's
toctree.extract_original_messages()checks'rawcaption' in self— it only cares whether the key exists, not whether its value is non-empty.The workaround
toctree.caption or ""(originally added for alabaster theme compatibility) forces the key to always exist, causing gettext to extract empty strings.Reproduce the bug
Create a
_toc.ymlwith nested subtrees that lack acaption:Run the gettext builder:
sphinx-build -b gettext . _build/gettextInspect the generated
.potfiles — they will contain emptymsgidentries.List your environment
sphinx-external-toc==1.1.0Additional context
This issue was originally reported as sphinx-doc/sphinx#13168