Skip to content

Commit 234ac59

Browse files
committed
Update AutoNumbering transform.
a) Ensure that enumerable elements (tables, figures and literal_blocks) have an ID. (In Docutils > 0.22, `nodes.document.note_implicit_target()` does not genereate an ID if `legacy_ids`_ is False.) b) Title-derived IDs for enumerable elements: "Enumerable elements" get an auto-ID in the `AutoNumbering` SphinxTransform and a self-link (¶) after the caption/title in the HTML document. Currently, the ID is made of the "auto-id-prefix" + a running number (id1, id2, ...). The generated IDs are not stable -- inserting/removing an enuerable element, footnote, or section with non-Lating heading will change the running number! Base the generated IDs on the caption or title text instead (similar to sections). To avoid changed IDs when documents are re-compiled, this only happens if `legacy_ids`_ is False. .. _legacy_ids: https://docutils.sf.net/docs/user/config.html#legacy-ids
1 parent cc7c6f4 commit 234ac59

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

sphinx/transforms/__init__.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,18 @@ def apply(self, **kwargs: Any) -> None:
206206
domain: StandardDomain = self.env.domains.standard_domain
207207

208208
for node in self.document.findall(nodes.Element):
209-
if (
210-
domain.is_enumerable_node(node)
211-
and domain.get_numfig_title(node) is not None
212-
and node['ids'] == []
213-
):
209+
if not domain.is_enumerable_node(node):
210+
continue
211+
refname = domain.get_numfig_title(node)
212+
if refname and node['ids'] == []:
213+
if not node['names'] and getattr(self.document.settings, 'legacy_ids', True):
214+
# cf. https://docutils.sf.net/docs/user/config.html#legacy-ids
215+
node['names'].append(nodes.fully_normalize_name(refname))
214216
self.document.note_implicit_target(node)
217+
# Since Docutils 0.23, implicit targets don't get an ID
218+
# at registration if "legacy_ids" is False:
219+
if not getattr(self.document.settings, 'legacy_ids', True):
220+
self.document.set_id(node)
215221

216222

217223
class SortIds(SphinxTransform):

sphinx/writers/html5.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ def append_fignumber(figtype: str, figure_id: str) -> None:
452452

453453
figtype = self._domains.standard_domain.get_enumerable_node_type(node)
454454
if figtype:
455-
if len(node['ids']) == 0:
455+
if not node['ids']:
456456
msg = __('Any IDs not assigned for %s node') % node.tagname
457457
logger.warning(msg, location=node)
458458
else:

0 commit comments

Comments
 (0)