3 # This script converts API YAML files into markdown documentation
4 # and prints it to standard out.
9 def arg_signature(name, arg):
12 sig = sig + ": " + arg["type"]
14 sig = sig + " = " + arg["default"]
17 def get_signature(name, info):
19 return name + " : " + info["type"]
20 sig = name + " : func("
22 sig += ", ".join(arg_signature(name,arg) for name,arg in info["args"].items())
23 sig += " -> " + info["return"].get("type", "Void")
25 sig += "-> " + info["return"].get("type", "Void")
29 def print_method(name, info):
31 print(f"\n```tomo\n{get_signature(name, info)}\n```\n")
32 if "description" in info:
33 print(info["description"])
39 if "args" in info and info["args"]:
40 print("Argument | Type | Description | Default")
41 print("---------|------|-------------|---------")
42 for arg,arg_info in info["args"].items():
43 default = '`'+arg_info['default']+'`' if 'default' in arg_info else '-'
44 description = arg_info['description'].replace('\n', ' ')
45 print(f"{arg} | `{arg_info.get('type', '')}` | {description} | {default}")
46 #print(f"- **{arg}:** {arg_info['description']}")
49 print(f"\n**Return:** {info['return'].get('description', 'Nothing.')}")
52 print(f"\n**Example:**\n```tomo\n{info['example']}\n```")
54 def convert_to_markdown(yaml_doc:str)->str:
55 data = yaml.safe_load(yaml_doc)
58 for name in sorted([k for k in data.keys() if "." not in k]):
59 print_method(name, data[name])
62 for name in sorted([k for k in data.keys() if "." in k]):
63 if section is None or not name.startswith(section + "."):
64 match = re.match(r"(\w+)\.", name)
65 section = match.group(1)
66 print(f"\n# {section}")
68 print_method(name, data[name])
70 if __name__ == "__main__":
75 for filename in sys.argv[1:]:
76 with open(filename, "r") as f:
78 convert_to_markdown(all_files)
80 convert_to_markdown(sys.stdin.read())