Skip to content

Commit 5cf1e3d

Browse files
authored
👌 Add autodoc2_no_index option (#3)
1 parent 37a5e40 commit 5cf1e3d

File tree

7 files changed

+75
-7
lines changed

7 files changed

+75
-7
lines changed

src/autodoc2/config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class RenderConfig:
3030
hidden_objects: set[t.Literal["undoc", "dunder", "private", "inherited"]]
3131
hidden_regexes: list[re.Pattern[str]]
3232
deprecated_module_regexes: list[re.Pattern[str]]
33+
no_index: bool
3334
module_summary: bool
3435
class_docstring: t.Literal["merge", "both"]
3536
class_inheritance: bool
@@ -386,6 +387,15 @@ class Config:
386387
},
387388
)
388389

390+
no_index: bool = dc.field(
391+
default=False,
392+
metadata={
393+
"help": "Do not generate a cross-reference index.",
394+
"sphinx_type": (bool,),
395+
"category": "render",
396+
},
397+
)
398+
389399
deprecated_module_regexes: list[t.Pattern[str]] = dc.field(
390400
default_factory=list,
391401
metadata={
@@ -508,6 +518,7 @@ def to_render_config(self, pkg_index: int | None) -> RenderConfig:
508518
skip_module_regexes=self.skip_module_regexes,
509519
hidden_objects=self.hidden_objects,
510520
hidden_regexes=self.hidden_regexes,
521+
no_index=self.no_index,
511522
deprecated_module_regexes=self.deprecated_module_regexes,
512523
module_summary=self.module_summary,
513524
class_docstring=self.class_docstring,
@@ -529,6 +540,7 @@ def to_render_config(self, pkg_index: int | None) -> RenderConfig:
529540
hidden_regexes=self.hidden_regexes
530541
if pkg.hidden_regexes is None
531542
else pkg.hidden_regexes,
543+
no_index=self.no_index,
532544
deprecated_module_regexes=self.deprecated_module_regexes
533545
if pkg.deprecated_module_regexes is None
534546
else pkg.deprecated_module_regexes,

src/autodoc2/render/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ def is_module_deprecated(self, item: ItemData) -> bool:
213213
for p in self.config.deprecated_module_regexes
214214
)
215215

216-
# TODO allow for adding :noindex:
216+
def no_index(self, item: ItemData) -> bool:
217+
"""Whether this item should be excluded from search engines."""
218+
return self.config.no_index
217219

218220
def show_module_summary(self, item: ItemData) -> bool:
219221
"""Whether to show a summary for this module/package."""

src/autodoc2/render/myst_.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ def render_package(self, item: ItemData) -> t.Iterable[str]:
7979
yield ""
8080

8181
yield f"```{{py:module}} {full_name}"
82+
if self.no_index(item):
83+
yield ":noindex:"
8284
if self.is_module_deprecated(item):
8385
yield ":deprecated:"
8486
yield from ["```", ""]
@@ -184,6 +186,8 @@ def render_function(self, item: ItemData) -> t.Iterable[str]:
184186

185187
yield f"{backticks}{{py:function}} {sig}"
186188
yield f":canonical: {item['full_name']}"
189+
if self.no_index(item):
190+
yield ":noindex:"
187191
# TODO overloads
188192
if "async" in item.get("properties", []):
189193
yield ":async:"
@@ -217,12 +221,10 @@ def render_class(self, item: ItemData) -> t.Iterable[str]:
217221
# note, here we can cannot yield by line,
218222
# because we need to look ahead to know the length of the backticks
219223

220-
lines: list[str] = []
221-
222-
lines += [
223-
f":canonical: {item['full_name']}",
224-
"",
225-
]
224+
lines: list[str] = [f":canonical: {item['full_name']}"]
225+
if self.no_index(item):
226+
lines += [":noindex:"]
227+
lines += [""]
226228

227229
# TODO overloads
228230

@@ -276,6 +278,8 @@ def render_property(self, item: ItemData) -> t.Iterable[str]:
276278
short_name = item["full_name"].split(".")[-1]
277279
yield f"{backticks}{{py:property}} {short_name}"
278280
yield f":canonical: {item['full_name']}"
281+
if self.no_index(item):
282+
yield ":noindex:"
279283
for prop in ("abstractmethod", "classmethod"):
280284
if prop in item.get("properties", []):
281285
yield f":{prop}:"
@@ -305,6 +309,8 @@ def render_method(self, item: ItemData) -> t.Iterable[str]:
305309

306310
yield f"{backticks}{{py:method}} {sig}"
307311
yield f":canonical: {item['full_name']}"
312+
if self.no_index(item):
313+
yield ":noindex:"
308314
# TODO overloads
309315
# TODO collect final decorated in analysis
310316
for prop in ("abstractmethod", "async", "classmethod", "final", "staticmethod"):
@@ -333,6 +339,8 @@ def render_data(self, item: ItemData) -> t.Iterable[str]:
333339

334340
yield f"{backticks}{{py:{item['type']}}} {short_name}"
335341
yield f":canonical: {item['full_name']}"
342+
if self.no_index(item):
343+
yield ":noindex:"
336344
for prop in ("abstractmethod", "classmethod"):
337345
if prop in item.get("properties", []):
338346
yield f":{prop}:"

src/autodoc2/render/rst_.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def render_package(self, item: ItemData) -> t.Iterable[str]:
7474
yield from [line, "=" * len(line), ""]
7575

7676
yield f".. py:module:: {full_name}"
77+
if self.no_index(item):
78+
yield " :noindex:"
7779
if self.is_module_deprecated(item):
7880
yield " :deprecated:"
7981
yield from ["", ""]
@@ -171,6 +173,8 @@ def render_function(self, item: ItemData) -> t.Iterable[str]:
171173

172174
yield f".. py:function:: {sig}"
173175
yield f" :canonical: {item['full_name']}"
176+
if self.no_index(item):
177+
yield " :noindex:"
174178
# TODO overloads
175179
if "async" in item.get("properties", []):
176180
yield " :async:"
@@ -205,6 +209,8 @@ def render_class(self, item: ItemData) -> t.Iterable[str]:
205209

206210
yield f".. py:{item['type']}:: {sig}"
207211
yield f" :canonical: {item['full_name']}"
212+
if self.no_index(item):
213+
yield " :noindex:"
208214
yield ""
209215

210216
# TODO overloads
@@ -255,6 +261,8 @@ def render_property(self, item: ItemData) -> t.Iterable[str]:
255261
short_name = item["full_name"].split(".")[-1]
256262
yield f".. py:property:: {short_name}"
257263
yield f" :canonical: {item['full_name']}"
264+
if self.no_index(item):
265+
yield " :noindex:"
258266
for prop in ("abstractmethod", "classmethod"):
259267
if prop in item.get("properties", []):
260268
yield f" :{prop}:"
@@ -283,6 +291,8 @@ def render_method(self, item: ItemData) -> t.Iterable[str]:
283291

284292
yield f".. py:method:: {sig}"
285293
yield f" :canonical: {item['full_name']}"
294+
if self.no_index(item):
295+
yield " :noindex:"
286296
# TODO overloads
287297
# TODO collect final decorated in analysis
288298
for prop in ("abstractmethod", "async", "classmethod", "final", "staticmethod"):
@@ -310,6 +320,8 @@ def render_data(self, item: ItemData) -> t.Iterable[str]:
310320
short_name = item["full_name"].split(".")[-1]
311321
yield f".. py:{item['type']}:: {short_name}"
312322
yield f" :canonical: {item['full_name']}"
323+
if self.no_index(item):
324+
yield " :noindex:"
313325
for prop in ("abstractmethod", "classmethod"):
314326
if prop in item.get("properties", []):
315327
yield f" :{prop}:"

tests/test_render.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,28 @@ def test_basic(renderer: RendererBase, extension: str, tmp_path: Path, file_regr
3535
file_regression.check(content, extension=extension)
3636

3737

38+
@pytest.mark.parametrize(
39+
"renderer,extension",
40+
[
41+
(RstRenderer, ".rst"),
42+
(MystRenderer, ".md"),
43+
],
44+
ids=["rst", "myst"],
45+
)
46+
def test_config_options(
47+
renderer: RendererBase, extension: str, tmp_path: Path, file_regression
48+
):
49+
"""Test basic rendering."""
50+
package = build_package(tmp_path)
51+
db = InMemoryDb()
52+
for path, modname in yield_modules(package):
53+
for item in analyse_module(path, modname):
54+
db.add(item)
55+
config = Config(no_index=True)
56+
content = "\n".join(renderer(db, config).render_item(package.name + ".func"))
57+
file_regression.check(content, extension=extension)
58+
59+
3860
@pytest.mark.parametrize(
3961
"with_rebuild",
4062
[True, False],
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```{py:function} func(a: str, b: int) -> package.a.c.ac1
2+
:canonical: package.func
3+
:noindex:
4+
5+
This is a function.
6+
7+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.. py:function:: func(a: str, b: int) -> package.a.c.ac1
2+
:canonical: package.func
3+
:noindex:
4+
5+
This is a function.

0 commit comments

Comments
 (0)