Skip to content

Commit 141b9b0

Browse files
authored
Allow to specify toctree elements that are not strings. (#45)
1 parent 1a8a47d commit 141b9b0

File tree

3 files changed

+57
-13
lines changed

3 files changed

+57
-13
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- "Allow the ``toctree`` entries for in a collection's ``docs/docsite/extra-docs.yml`` to be a dictionary with ``ref`` and ``title`` keys instead of just a reference as a string (https://github.com/ansible-community/antsibull-docs/pull/45)."

src/antsibull_docs/data/docsite/plugins_by_collection.rst.j2

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ Communication
9494
:maxdepth: 1
9595

9696
{% for toctree_entry in section.toctree %}
97-
@{toctree_entry}@
97+
{% if toctree_entry.title %}
98+
@{ toctree_entry.title | rst_escape }@ <@{ toctree_entry.ref }@>
99+
{% else %}
100+
@{ toctree_entry.ref }@
101+
{% endif %}
98102
{% endfor %}
99103
{% endif %}
100104

src/antsibull_docs/extra_docs.py

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,20 @@ class ExtraDocsIndexError(Exception):
2828
pass
2929

3030

31+
class TocTreeEntry:
32+
ref: str
33+
title: t.Optional[str]
34+
35+
def __init__(self, ref: str, title: t.Optional[str] = None):
36+
self.ref = ref
37+
self.title = title
38+
39+
3140
class Section:
3241
title: str
33-
toctree: t.List[str]
42+
toctree: t.List[TocTreeEntry]
3443

35-
def __init__(self, title: str, toctree: t.List[str]):
44+
def __init__(self, title: str, toctree: t.List[TocTreeEntry]):
3645
self.title = title
3746
self.toctree = toctree
3847

@@ -97,23 +106,52 @@ def lint_required_conditions(content: str, collection_name: str
97106
return sorted(labels), errors
98107

99108

109+
def _parse_toctree_entry(entry: t.Dict[t.Any, t.Any],
110+
toctree_index: int,
111+
section_index: int
112+
) -> t.Tuple[t.Optional[TocTreeEntry], t.List[str]]:
113+
errors: t.List[str] = []
114+
toctree_entry: t.Optional[TocTreeEntry] = None
115+
for key in ('ref', ):
116+
if key not in entry:
117+
errors.append(
118+
f'Toctree entry #{toctree_index} in section #{section_index}'
119+
f' does not have a "{key}" entry')
120+
for key, value in entry.items():
121+
if not isinstance(key, str) or not isinstance(value, str):
122+
errors.append(
123+
f'Toctree entry #{toctree_index} in section #{section_index}'
124+
f' must have strings for keys and values for all entries')
125+
if not errors:
126+
toctree_entry = TocTreeEntry(entry['ref'], title=entry.get('title'))
127+
return toctree_entry, errors
128+
129+
100130
def load_toctree(yaml_section: t.Dict[str, t.Any], section_index: int = 0
101-
) -> t.Tuple[t.List[str], t.List[str]]:
131+
) -> t.Tuple[t.List[TocTreeEntry], t.List[str]]:
102132
errors: t.List[str] = []
103-
toctree: t.List[str] = []
133+
toctree: t.List[TocTreeEntry] = []
104134
if 'toctree' in yaml_section:
105135
if not isinstance(yaml_section['toctree'], list):
106136
errors.append(
107137
f'Toctree entry in section #{section_index} is not a list')
108138
return toctree, errors
109139

110-
for toctree_index, toctree_name in enumerate(yaml_section['toctree']):
111-
if not isinstance(toctree_name, str):
112-
errors.append(
113-
f'Toctree entry #{toctree_index} in section #{section_index}'
114-
f' is not a string')
140+
for toctree_index, toctree_entry in enumerate(yaml_section['toctree']):
141+
if isinstance(toctree_entry, str):
142+
toctree.append(TocTreeEntry(toctree_entry))
115143
continue
116-
toctree.append(toctree_name)
144+
if isinstance(toctree_entry, dict):
145+
toctree_entry_obj, toctree_entry_errors = _parse_toctree_entry(
146+
toctree_entry, toctree_index, section_index)
147+
errors.extend(toctree_entry_errors)
148+
if toctree_entry_obj:
149+
toctree.append(toctree_entry_obj)
150+
continue
151+
errors.append(
152+
f'Toctree entry #{toctree_index} in section #{section_index}'
153+
f' is neither a string nor a dictionary')
154+
continue
117155
return toctree, errors
118156

119157

@@ -189,8 +227,8 @@ async def load_collection_extra_docs(collection_name: str,
189227
sections = []
190228

191229
for section in sections:
192-
for i, toctree in enumerate(section.toctree):
193-
section.toctree[i] = f"{path_prefix}/{toctree}"
230+
for toctree in section.toctree:
231+
toctree.ref = f"{path_prefix}/{toctree.ref}"
194232
documents = []
195233
for abs_path, rel_path in find_extra_docs(collection_path):
196234
try:

0 commit comments

Comments
 (0)