mirror of https://github.com/astral-sh/ruff
[docs] Add rule short code to mkdocs tags (#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: #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 <charlie.r.marsh@gmail.com>
This commit is contained in:
parent
8574751911
commit
099f077311
|
|
@ -104,31 +104,68 @@ def clean_file_content(content: str, title: str) -> str:
|
||||||
return f"# {title}\n\n" + content
|
return f"# {title}\n\n" + content
|
||||||
|
|
||||||
|
|
||||||
def add_meta_description(rule_doc: Path) -> str:
|
def generate_rule_metadata(rule_doc: Path) -> None:
|
||||||
"""Add a meta description to the rule doc."""
|
"""Add frontmatter metadata containing a rule's code and description.
|
||||||
# Read the rule doc into lines
|
|
||||||
|
For example:
|
||||||
|
```yaml
|
||||||
|
---
|
||||||
|
description: Checks for abstract classes without abstract methods.
|
||||||
|
tags:
|
||||||
|
- B024
|
||||||
|
---
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
# Read the rule doc into lines.
|
||||||
with rule_doc.open("r", encoding="utf-8") as f:
|
with rule_doc.open("r", encoding="utf-8") as f:
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
|
|
||||||
# Get the description from the rule doc lines
|
# Get the description and rule code from the rule doc lines.
|
||||||
|
rule_code = None
|
||||||
|
description = None
|
||||||
what_it_does_found = False
|
what_it_does_found = False
|
||||||
for line in lines:
|
for line in lines:
|
||||||
if line == "\n":
|
if line == "\n":
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# Assume that the only first-level heading is the rule title and code.
|
||||||
|
#
|
||||||
|
# For example: given `# abstract-base-class-without-abstract-method (B024)`,
|
||||||
|
# extract the rule code (`B024`).
|
||||||
|
if line.startswith("# "):
|
||||||
|
rule_code = line.strip().rsplit("(", 1)
|
||||||
|
rule_code = rule_code[1][:-1]
|
||||||
|
|
||||||
if line.startswith("## What it does"):
|
if line.startswith("## What it does"):
|
||||||
what_it_does_found = True
|
what_it_does_found = True
|
||||||
continue # Skip the '## What it does' line
|
continue # Skip the '## What it does' line
|
||||||
|
|
||||||
if what_it_does_found:
|
if what_it_does_found and not description:
|
||||||
description = line.removesuffix("\n")
|
description = line.removesuffix("\n")
|
||||||
|
|
||||||
|
if all([rule_code, description]):
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
if not rule_code:
|
||||||
|
raise ValueError("Missing title line")
|
||||||
|
|
||||||
if not what_it_does_found:
|
if not what_it_does_found:
|
||||||
raise ValueError(f"Missing '## What it does' in {rule_doc}")
|
raise ValueError(f"Missing '## What it does' in {rule_doc}")
|
||||||
|
|
||||||
with rule_doc.open("w", encoding="utf-8") as f:
|
with rule_doc.open("w", encoding="utf-8") as f:
|
||||||
f.writelines("\n".join(["---", f"description: {description}", "---", "", ""]))
|
f.writelines(
|
||||||
|
"\n".join(
|
||||||
|
[
|
||||||
|
"---",
|
||||||
|
f"description: {description}",
|
||||||
|
"tags:",
|
||||||
|
f"- {rule_code}",
|
||||||
|
"---",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
||||||
f.writelines(lines)
|
f.writelines(lines)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -198,7 +235,7 @@ def main() -> None:
|
||||||
# support.
|
# support.
|
||||||
mdformat.file(rule_doc, extensions=["mkdocs", "admon", "no-escape-text"])
|
mdformat.file(rule_doc, extensions=["mkdocs", "admon", "no-escape-text"])
|
||||||
|
|
||||||
add_meta_description(rule_doc)
|
generate_rule_metadata(rule_doc)
|
||||||
|
|
||||||
with Path("mkdocs.template.yml").open(encoding="utf8") as fp:
|
with Path("mkdocs.template.yml").open(encoding="utf8") as fp:
|
||||||
config = yaml.safe_load(fp)
|
config = yaml.safe_load(fp)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue