Skip to content

Commit 403e771

Browse files
authored
Merge pull request #96 from lightpanda-io/honour-docformat
Honour the package's __docformat__ when rendering docstrings
2 parents 0cff4cf + 22236b0 commit 403e771

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

scripts/generate-python-reference.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@
7777
CODE_SPAN_RE = re.compile(r"(`+)(.+?)\1", re.DOTALL)
7878
MODULE_PREFIX_RE = re.compile(r"\blightpanda\.\w+\.")
7979
LINK_RE = re.compile(r"\]\(#([a-z0-9-]+)\)")
80+
SECTION_RE = re.compile(r"^#{2,6} (\w+):$", re.MULTILINE)
81+
LIST_ITEM_RE = re.compile(r"^\s*[-*] ")
82+
DOCFORMAT = getattr(lightpanda, "__docformat__", "restructuredtext")
8083

8184

8285
def slug(*parts: str) -> str:
@@ -132,6 +135,7 @@ def fence_indented_blocks(text: str) -> str:
132135
out: list[str] = []
133136
block: list[str] = []
134137
in_fence = False
138+
in_list = False # indented lines inside a list are item continuations
135139

136140
def flush() -> None:
137141
if block:
@@ -149,11 +153,16 @@ def flush() -> None:
149153
flush()
150154
in_fence = not in_fence
151155
out.append(line)
152-
elif not in_fence and line.startswith(" ") and line.strip():
156+
elif not in_fence and line.startswith(" ") and line.strip() and not in_list:
153157
block.append(line.rstrip())
154158
else:
155159
flush()
156160
out.append(line)
161+
if not in_fence:
162+
if LIST_ITEM_RE.match(line):
163+
in_list = True
164+
elif not line.strip():
165+
in_list = False
157166
flush()
158167
return "\n".join(out)
159168

@@ -180,7 +189,10 @@ def render_docstring(doc: pdoc.doc.Doc, links: dict[str, str]) -> str:
180189
raw = doc.docstring
181190
if not raw.strip():
182191
return ""
183-
text = pdoc.docstrings.convert(raw, "restructuredtext", doc.source_file)
192+
text = pdoc.docstrings.convert(raw, DOCFORMAT, doc.source_file)
193+
# pdoc renders Google-style sections (Args, Returns, ...) as headings;
194+
# keep them out of the page's outline.
195+
text = SECTION_RE.sub(r"**\1:**", text)
184196
text = fence_indented_blocks(text)
185197
out: list[str] = []
186198
prose: list[str] = []

0 commit comments

Comments
 (0)