diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2023-09-18 12:57:57 +0200 |
|---|---|---|
| committer | Jeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com> | 2023-09-25 09:43:37 +0200 |
| commit | 836ea4654ff9bf389856d0030b28983c99286f0d (patch) | |
| tree | de84b61ec257d113d99d1451a2fb0b40e23ba8d8 /genexportsdoc.py | |
| parent | 9eb38cf98afae5e1a47216061fde0c42bc0f1bde (diff) | |
Docs: Add autogenerated exports doc
Diffstat (limited to 'genexportsdoc.py')
| -rw-r--r-- | genexportsdoc.py | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/genexportsdoc.py b/genexportsdoc.py new file mode 100644 index 0000000..ed538b2 --- /dev/null +++ b/genexportsdoc.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python3 + +import subprocess + +cmd=["go", "doc", "--all", "exports"] + +output=subprocess.check_output(cmd).decode("utf-8") + +section = [""] +package_doc = "" + +in_section = True + +num = 0 +for out in output.splitlines(): + line = out + "\n" + if line.startswith(" "): + line = line[4:] + if out.startswith("FUNCTIONS") or out.startswith("VARIABLES"): + in_section = False + if out.startswith("func"): + in_section = True + section.append(line) + num += 1 + continue + if in_section: + section[num] += line + +def func_name(signature: str) -> str: + idx = signature.index("(") + return signature[len("func "):idx] + +def gen_toc(title: str) -> str: + id = "-".join(title.lower().split(" ")) + return f"[{title}](#{id})" + +toc = "" +first = True + +gen_sections = [] +for sec in section: + if first: + gen_sections.append(("About the API", sec)) + first = False + continue + lines = sec.splitlines() + signature, doc = lines[0], "\n".join(lines[1:]) + body = f"Signature:\n ```go\n{signature}\n```\n{doc}" + gen_sections.append((func_name(signature), body)) + +first = True +toc = "# Table of contents\n" +for title, body in gen_sections: + if first: + toc += f"- {gen_toc(title)}\n" + func_toc = gen_toc("Functions") + toc += f"- {func_toc}\n" + first = False + continue + toc += f" * {gen_toc(title)}\n" + +data = "This document was automatically generated from the exports/exports.go file\n\n" +data += f"{toc}\n" +first = True +for title, body in gen_sections: + if first: + data += f"# {title}\n" + data += f"{body}\n" + data += "# Functions\n" + first = False + continue + data += f"## {title}\n" + data += f"{body}\n" + +with open("docs/src/api/functiondocs.md", "w+") as f: + f.write(data) |
