Skip to content

Commit

Permalink
tools/features_yaml2mx: Adjust help creation
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Feb 21, 2024
1 parent 1e6d8d3 commit 047bd53
Showing 1 changed file with 16 additions and 30 deletions.
46 changes: 16 additions & 30 deletions dist/tools/features_yaml2mx/features_yaml2mx.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,49 +55,40 @@ def write_makefile(outfile, yaml_path, parsed):
outfile.flush()


def write_md_section(outfile, section, content, level):
def write_md_section(outfile, group, level):
"""
Write a section documenting certain features to the given file in markdown
format.
:param outfile: The file to write the section to
:type outfile: file
:param section: Name of the section (a.k.a. section title)
:type section: str
:param content: The section content (e.g. a subtree from the parsed YAML)
:type content: dict
:param group: The group content (e.g. a subtree from the parsed YAML)
:type group: dict
:param level: The current section level (e.g. 1=section, 2=subsection)
:type level: int
"""
outfile.write("#" * level + f" {section}\n")
if "help" in content:
title = group.get("title")
outfile.write("#" * level + f" {title}" if title else "" + "\n")
if "help" in group:
outfile.write("\n")
outfile.write(content["help"])
outfile.write(group["help"])
outfile.write("\n")

if "features" in content:
if "features" in group:
outfile.write("\n")
outfile.write("""\
| Feature | Description |
|:--------------------------------- |:----------------------------------------------------------------------------- |
""")

for feature, description in content["features"].items():
if not isinstance(description, str):
sys.exit(f"expected description for feature \"{feature}\" " +
f"to be a string, but is {description}")
feature = f"`{feature}`"
description = description.strip().replace("\n", " ")
outfile.write(f"| {feature:<33} | {description:<77} |\n")
for feature in group["features"]:
name = f"`{feature['name']}`"
description = feature['help'].strip().replace("\n", " ")
outfile.write(f"| {name:<33} | {description:<77} |\n")

if not isinstance(content, dict):
sys.exit(f"Expected \"{content}\" to be a dict. " +
"(Wrong indent? Missing `features:` before feature dict?)")

for subsection, subcontent in content.items():
if subsection not in ("help", "features"):
outfile.write("\n")
write_md_section(outfile, subsection, subcontent, level + 1)
for group in group.get('groups', []):
outfile.write("\n")
write_md_section(outfile, group, level + 1)


def write_mdfile(outfile, yaml_path, parsed):
Expand All @@ -123,12 +114,7 @@ def write_mdfile(outfile, yaml_path, parsed):
[TOC]
""")
first_section = True
for section, content in parsed.items():
if not first_section:
outfile.write("\n")
write_md_section(outfile, section, content, 1)
first_section = False
write_md_section(outfile, parsed, 0)


def convert_features(yaml_file, mk_file, md_file):
Expand Down

0 comments on commit 047bd53

Please sign in to comment.