Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions docs/user_guide/06-api-documentation.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,33 @@ exactly. A misspelled name, a name that was renamed or removed, or a name that a
expected after a build, check that the name in your `reference` config matches what your package
exports.

### Grouped Reference Pages

Use a `kind: page` entry to document related objects on one page. Set `path` to the page's
path within the reference directory, omitting `.qmd` or `.html`. List the objects in `contents`,
using the same names and member options as for entries directly in a section.

```{.yaml filename="docs/great-docs.yml"}
reference:
- title: Authentication
contents:
- kind: page
path: sessions
summary:
name: Login sessions
desc: Create and end login sessions.
contents:
- login
- logout
```

This publishes `reference/sessions.html` with a heading and link target for each object.
`summary.name` labels the page in the title, sidebar and reference index. If omitted, it
defaults to the filename stem. `summary.desc` adds a description to the reference index.

Historical versions that use `api_snapshot` or `git_ref` reconstruct their reference from
individual symbols. Each symbol gets its own page in those versions.

### Custom Title and Description

You can customize the heading and introductory text of the API reference page using the `title` and
Expand Down
28 changes: 13 additions & 15 deletions great_docs/_apiref/_rendering/api_pages.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""
API pages, with their front matter, objects and summary rows

A page with one object renders it at the page's heading level; a page with several
renders each one level deeper.
A page with its own title renders object headings one level deeper.
"""

from __future__ import annotations
Expand Down Expand Up @@ -46,10 +45,9 @@ class APIPageRenderer(PageRendererMixin, BaseRenderer):
"""
An API page that documents one or more prepared Python objects

The page title in the front matter comes from the first object. A page with one
object omits that object's heading, which would repeat the title; a page with
several keeps each object's heading, one level deeper. The page's summary rows
come from its authored summary or from its objects' own rows.
The page title comes from the authored summary or the first object. When an
object supplies the title of its own page, its heading is omitted. On other
pages each object has a separate heading.
"""

toc_depth: int = _DEFAULT_SITE_TOC_DEPTH
Expand All @@ -69,23 +67,22 @@ def __post_init__(self) -> None:
"""The page's path relative to the output directory, which its objects' `context` reports"""

@property
def _has_one_object(self) -> bool:
"""Whether the page contains exactly one documented object"""
return len(self.page.contents) == 1
def _has_object_title(self) -> bool:
"""Whether the page title replaces its only object's heading"""
return len(self.page.contents) == 1 and self.page.summary is None

@cached_property
def object_renderers(self) -> list[ObjectRenderer]:
"""The renderers of the page's objects, which share the page's context"""
level = self.level if self._has_one_object else self.level + 1
level = self.level if self._has_object_title else self.level + 1
return [
self.make_renderer(c, level, page_path=self.path, _purpose=_RenderingPurpose.API_PAGE)
for c in self.page.contents
]

def render_metadata(self) -> BlockContent:
# Derive the title of the page from the first (top-level) object
"""
Render the page front matter from the first object's title and the page summary
Render the page front matter with its title and description

The description comes from the authored summary, or else the first object's
subject. The page raises `toc-depth` only when the site setting would hide
Expand All @@ -97,7 +94,8 @@ def render_metadata(self) -> BlockContent:
The front matter.
"""
obj = self.object_renderers[0]
title = obj.title_content
summary = self.page.summary
title = summary.name if summary else obj.title_content
metadata: dict[str, object] = {
"title": f"{title}",
"body-classes": "doc-api-page doc-py-api-page",
Expand All @@ -109,8 +107,8 @@ def render_metadata(self) -> BlockContent:
# Add a page override only when the site depth excludes members.
if self.toc_depth < _MIN_MEMBER_TOC_DEPTH:
metadata["toc-depth"] = _MIN_MEMBER_TOC_DEPTH
summary = self.page.summary
if description := metadata_value(summary.desc if summary else obj.docstring_subject):
subject = summary.desc if summary and summary.desc else obj.docstring_subject
if description := metadata_value(subject):
metadata["description-meta"] = description
return Meta(metadata)

Expand Down
8 changes: 6 additions & 2 deletions great_docs/_apiref/_rendering/members.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from __future__ import annotations

import posixpath
from dataclasses import dataclass
from functools import cached_property
from typing import TYPE_CHECKING, cast
Expand Down Expand Up @@ -352,9 +353,12 @@ def _render_member_pages_group(
}[group]
member_renderers = [
self.make_renderer(
Doc.from_griffe(page.obj.name, page.obj, anchor=""),
Doc.from_griffe(page.obj.name, page.obj, anchor=page.target_anchor),
contained=False,
page_path=f"{page.path}.qmd",
page_path=posixpath.relpath(
f"{page.path}{self.settings.out_page_suffix}",
start=posixpath.dirname(self.page_path or "") or ".",
),
)
for page in pages
]
Expand Down
4 changes: 3 additions & 1 deletion great_docs/_apiref/api_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ def __init__(

# Pages, the index, the sidebar and the text export share the same page paths.
self.sections = unique_page_paths(
[SpecSection(**section) for section in spec.sections], self._entry_label
[SpecSection(**section) for section in spec.sections],
self._entry_label,
index_path=self.settings.out_index.removesuffix(self.settings.out_page_suffix),
)

@property
Expand Down
35 changes: 26 additions & 9 deletions great_docs/_apiref/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,12 @@ def remove_package_prefix(sections: list[Section], package: str) -> list[Section
class _MemberPagePathClaimer(NodeTransformer):
"""A tree whose member links and generated pages use their published paths"""

def __init__(self, claimed: dict[str, str], authored: dict[tuple[str, str], str]) -> None:
def __init__(
self,
claimed: dict[str, str],
authored: dict[tuple[str, str], tuple[str, str]],
grouped_paths: set[str],
) -> None:
"""
Claim member page paths that do not clash with the paths already claimed

Expand All @@ -277,10 +282,13 @@ def __init__(self, claimed: dict[str, str], authored: dict[tuple[str, str], str]
The paths already claimed, keyed by their casefolded form. Each claimed member
page path is added to it.
authored
The published page path for each authored lookup path and canonical object.
The page path and fragment for each authored lookup path and canonical object.
grouped_paths
Authored page paths reserved for groups of objects.
"""
self.claimed = claimed
self.authored = authored
self.grouped_paths = grouped_paths

def enter(self, el: object) -> object:
"""
Expand All @@ -291,7 +299,8 @@ def enter(self, el: object) -> object:
if doc.lookup_path is not None:
target = self.authored.get((doc.lookup_path, doc.obj.canonical_path))
if target is not None:
return el.replace(path=target, link_only=True)
path, anchor = target
return el.replace(path=path, target_anchor=anchor, link_only=True)
return super().enter(el)

def exit(self, el: object) -> object:
Expand All @@ -309,7 +318,12 @@ def exit(self, el: object) -> object:
The node, or a copy of a member page with a new path.
"""
if isinstance(el, MemberPage) and not el.link_only:
path = claim_page_path(el.path, self.claimed, partial(get_label, el.obj))
path = claim_page_path(
el.path,
self.claimed,
partial(get_label, el.obj),
allow_same=el.path.casefold() not in self.grouped_paths,
)
return el if path == el.path else el.replace(path=path)
return super().exit(el)

Expand All @@ -334,17 +348,20 @@ def unique_member_page_paths(sections: list[Section]) -> list[Section]:
The sections with member links and generated page paths resolved.
"""
claimed: dict[str, str] = {}
authored: dict[tuple[str, str], str] = {}
authored: dict[tuple[str, str], tuple[str, str]] = {}
grouped_paths: set[str] = set()
for section in sections:
for entry in section.contents:
if isinstance(entry, Page) and not isinstance(entry, MemberPage):
_ = claim_page_path(entry.path, claimed)
if len(entry.contents) == 1:
doc = entry.contents[0]
if entry.summary is not None:
grouped_paths.add(entry.path.casefold())
for doc in entry.contents:
if doc.lookup_path is not None:
_ = authored.setdefault(
(doc.lookup_path, doc.obj.canonical_path), entry.path
(doc.lookup_path, doc.obj.canonical_path),
(entry.path, doc.anchor if entry.summary is not None else ""),
)

claimer = _MemberPagePathClaimer(claimed, authored)
claimer = _MemberPagePathClaimer(claimed, authored, grouped_paths)
return claimer.visit(sections) # type: ignore[return-value]
2 changes: 2 additions & 0 deletions great_docs/_apiref/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ class MemberPage(Page):
contents: list[Any] = field(default_factory=list[Any])
link_only: bool = False
"""Whether an authored page at this path already documents the member"""
target_anchor: str = ""
"""The member's fragment when its authored page documents several objects"""


@dataclass
Expand Down
13 changes: 9 additions & 4 deletions great_docs/_apiref/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from ._type_checks import is_enum
from ._walkable import MISSING, MissingType, Walkable
from .content import Doc, DocAttribute, MemberPage, Page, Section, Text
from .spec import SpecObject, SpecOptions, SpecSection, SpecText
from .spec import SpecObject, SpecOptions, SpecPage, SpecSection, SpecText

# Dunder members inherited from `object`/`type` (or PyO3 metaclasses) that carry docstrings but are
# never useful API documentation. Filtered out unconditionally so they do not pollute reference
Expand Down Expand Up @@ -527,8 +527,8 @@ def _resolve_entry(self, el: SpecEntry) -> Page | Text | None:
"""
Rebuild a single section entry as its resolved `content` counterpart

A documented object becomes its own single-object `Page`; a free-text
block stays inline as a `Text`.
A named page retains its selected objects and summary. A standalone
object gets its own page, and a free-text block stays inline.

Parameters
----------
Expand All @@ -543,13 +543,18 @@ def _resolve_entry(self, el: SpecEntry) -> Page | Text | None:
Raises
------
TypeError
If `el` is neither an object nor a text block.
If the entry has no resolved content type.
"""
if isinstance(el, SpecObject):
doc = self._resolve_object(el)
if doc is None:
return None
return Page(contents=cast("list[Any]", [doc]), path=el.page_path)
if isinstance(el, SpecPage):
documents = [doc for entry in el.contents if (doc := self._resolve_object(entry))]
if not documents:
return None
return Page(path=el.path, contents=cast("list[Any]", documents), summary=el.summary)
if isinstance(el, SpecText):
return self._resolve_text(el)
raise TypeError(f"Cannot resolve section entry of type: {type(el)}")
Expand Down
Loading
Loading