Skip to content

Commit 64bde6b

Browse files
hreedercharliermarsh
authored andcommitted
[docs] Add rule short code to mkdocs tags (astral-sh#14040)
## Summary <!-- What's the purpose of the change? What does it do, and why? --> This PR updates the metadata in the YAML frontmatter of the mkdocs documentation to include the rule short code as a tag, so it can be easily searched. Ref: astral-sh#13684 ## Test Plan <!-- How was it tested? --> This has been tested locally using the documentation provided [here](https://docs.astral.sh/ruff/contributing/#mkdocs) for generating docs. This generates docs that now have the tags section: ```markdown --- description: Checks for abstract classes without abstract methods. tags: - B024 --- # abstract-base-class-without-abstract-method (B024) ... trimmed ``` I've also verified that this gives the ability to get straight to the page via search when serving mkdocs locally. --------- Co-authored-by: Charlie Marsh <[email protected]>
1 parent 8e3a8d8 commit 64bde6b

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

scripts/generate_mkdocs.py

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,31 +104,68 @@ def clean_file_content(content: str, title: str) -> str:
104104
return f"# {title}\n\n" + content
105105

106106

107-
def add_meta_description(rule_doc: Path) -> str:
108-
"""Add a meta description to the rule doc."""
109-
# Read the rule doc into lines
107+
def generate_rule_metadata(rule_doc: Path) -> None:
108+
"""Add frontmatter metadata containing a rule's code and description.
109+
110+
For example:
111+
```yaml
112+
---
113+
description: Checks for abstract classes without abstract methods.
114+
tags:
115+
- B024
116+
---
117+
```
118+
"""
119+
# Read the rule doc into lines.
110120
with rule_doc.open("r", encoding="utf-8") as f:
111121
lines = f.readlines()
112122

113-
# Get the description from the rule doc lines
123+
# Get the description and rule code from the rule doc lines.
124+
rule_code = None
125+
description = None
114126
what_it_does_found = False
115127
for line in lines:
116128
if line == "\n":
117129
continue
118130

131+
# Assume that the only first-level heading is the rule title and code.
132+
#
133+
# For example: given `# abstract-base-class-without-abstract-method (B024)`,
134+
# extract the rule code (`B024`).
135+
if line.startswith("# "):
136+
rule_code = line.strip().rsplit("(", 1)
137+
rule_code = rule_code[1][:-1]
138+
119139
if line.startswith("## What it does"):
120140
what_it_does_found = True
121141
continue # Skip the '## What it does' line
122142

123-
if what_it_does_found:
143+
if what_it_does_found and not description:
124144
description = line.removesuffix("\n")
145+
146+
if all([rule_code, description]):
125147
break
126148
else:
149+
if not rule_code:
150+
raise ValueError("Missing title line")
151+
127152
if not what_it_does_found:
128153
raise ValueError(f"Missing '## What it does' in {rule_doc}")
129154

130155
with rule_doc.open("w", encoding="utf-8") as f:
131-
f.writelines("\n".join(["---", f"description: {description}", "---", "", ""]))
156+
f.writelines(
157+
"\n".join(
158+
[
159+
"---",
160+
f"description: {description}",
161+
"tags:",
162+
f"- {rule_code}",
163+
"---",
164+
"",
165+
"",
166+
]
167+
)
168+
)
132169
f.writelines(lines)
133170

134171

@@ -198,7 +235,7 @@ def main() -> None:
198235
# support.
199236
mdformat.file(rule_doc, extensions=["mkdocs", "admon", "no-escape-text"])
200237

201-
add_meta_description(rule_doc)
238+
generate_rule_metadata(rule_doc)
202239

203240
with Path("mkdocs.template.yml").open(encoding="utf8") as fp:
204241
config = yaml.safe_load(fp)

0 commit comments

Comments
 (0)