From d8cddfbbd4f77a86cfd34b0768bef86fceecd572 Mon Sep 17 00:00:00 2001 From: SpookyDervish Date: Wed, 21 Jan 2026 20:53:24 +1100 Subject: [PATCH] dig docs command --- src/build.py | 60 +++++++++++++++++++++++++++++++++++++--------------- src/docs.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.py | 7 ++++++ 3 files changed, 107 insertions(+), 17 deletions(-) create mode 100644 src/docs.py diff --git a/src/build.py b/src/build.py index 3359e81..782117b 100644 --- a/src/build.py +++ b/src/build.py @@ -61,27 +61,53 @@ def build(args): with console.status("Packaging...", spinner="bouncingBall", spinner_style="green"): build_dir = f"{os.path.basename(args.folder_path)}_build" - if os.path.isdir(build_dir): - shutil.rmtree(build_dir) - os.mkdir(build_dir) + if not os.path.isdir(build_dir): + os.mkdir(build_dir) + + # generate a mineral.ini file + config_parser = configparser.ConfigParser() + config_parser["package"] = { + "description": "Your description here", + "version": "1.0.0", + "config_version": "1", + } + config_parser["dependencies"] = {} + + # write it to our new mineral + with open(os.path.join(build_dir, "mineral.ini"), "w") as f: + config_parser.write(f) + + # generate doc files + docs_folder = os.path.join(build_dir, "docs") + if not os.path.isdir(docs_folder): + os.mkdir(docs_folder) + + with open(os.path.join(docs_folder, "my_function.md"), "w") as f: + f.write("# mylib_MyFunction\n") + f.write("Describe your function briefly.\n\n") + f.write("## Arguments\n") + f.write("- myArgument (double): describe your arguments in a list format like this.\n\n") + f.write("## Returns\n") + f.write("result (int): then explain what the function returns\n\n") + f.write("## Example\n") + f.write("```python\n") + f.write("# then show how you use the function in an example\n") + f.write("call !mylib_MyFunction 123.0 &result\n") + f.write("```\n") + f.write("`Using \"python\" as the syntax highlighting language seems to work well with Ground.`") + + with open(os.path.join(build_dir, "SUMMARY.md"), "w") as f: + f.write("# mylib\n") + f.write("Introduce your module here!\n\nThis file will serve as an index page for all your docs.\n\n") + f.write("## Subtitle (use this for categories)\n") + f.write("- [mylib_MyFunction](docs/my_function.md)") + + console.print("[:white_check_mark:] Generated a new package for you!") shutil.move("main.so", os.path.join(build_dir, "main.so")) - # generate a mineral.ini file - config_parser = configparser.ConfigParser() - config_parser["package"] = { - "description": "Your description here", - "version": "1.0.0", - "config_version": "1" - } - config_parser["dependencies"] = {} + console.print("[:white_check_mark:] Put your library into your package!") - # write it to our new mineral - with open(os.path.join(build_dir, "mineral.ini"), "w") as f: - config_parser.write(f) - - console.print("[:white_check_mark:] Packaged!") - console.print(f"\n[b cyan]note:[/] You will need to edit the [i]mineral.ini[/] file to make sure the version number and dependencies are correct, and also rename your \"{os.path.basename(build_dir)}\" folder to the name of your mineral.") else: check_sudo() check_ground_libs_path() diff --git a/src/docs.py b/src/docs.py new file mode 100644 index 0000000..15b88b6 --- /dev/null +++ b/src/docs.py @@ -0,0 +1,57 @@ +from util import * +import os, sys +from rich.console import Console + +from textual.app import App, ComposeResult +from textual.widgets import MarkdownViewer, Header, Footer + + +console = Console() + + +class DocsApp(App): + TITLE = "Digpkg Docs" + SUB_TITLE = "made with ❤️ by SpookyDervish" + + def __init__(self, inital_markdown_path: str): + super().__init__() + self.inital_markdown_path = inital_markdown_path + + def compose(self) -> ComposeResult: + with open(self.inital_markdown_path, "r") as f: + markdown = f.read() + + yield Header() + yield MarkdownViewer(markdown) + yield Footer() + + +def docs(args): + check_ground_libs_path() + mineral_path = os.path.join(os.getenv("GROUND_LIBS"), args.mineral_name) + + if not os.path.isdir(mineral_path): + console.print(f"[b red]digpkg: can't read docs: the mineral [i]{args.mineral_name}[/] was not found!") + sys.exit(1) + + docs_path = os.path.join(mineral_path, "docs") + summary_md_file = os.path.join(mineral_path, "SUMMARY.md") + + if args.doc_file != None: + if not os.path.isfile(os.path.join(docs_path, f"{args.doc_file}.md")): + console.print(f"[b red]digpkg: can't read docs: the mineral [i]{args.mineral_name}[/] has no doc file named \"{args.doc_file}\".") + sys.exit(1) + + os.chdir(docs_path) + app = DocsApp(f"{args.doc_file}.md") + app.run() + sys.exit(0) + + if os.path.isfile(summary_md_file): + os.chdir(mineral_path) + app = DocsApp(summary_md_file) + app.run() + sys.exit(0) + else: + console.print(f"[b red]digpkg: can't read docs: the mineral [i]{args.mineral_name}[/] has no file named SUMMARY.md, please use the [i]--doc-file[/] argument to specify a specific doc to open instead.") + sys.exit(1) \ No newline at end of file diff --git a/src/main.py b/src/main.py index 7ecc51d..7c7f8c7 100644 --- a/src/main.py +++ b/src/main.py @@ -42,6 +42,10 @@ def parse_arguments(): build_command.add_argument("--gcc-args", nargs="*", help="any extra args you want to give to gcc") build_command.add_argument("--package", action="store_true", help="generate a folder with a mineral.ini and all the other files you need to publish the package") + # docs command + docs_command = sub_parsers.add_parser(name="docs", description="read the docs of a mineral") + docs_command.add_argument("mineral_name", help="name of the mineral you want to read the docs of") + docs_command.add_argument("-d", "--doc-file", help="load a specific doc file") # parse arguments are run the command we chose args = arg_parser.parse_args() @@ -62,6 +66,9 @@ def parse_arguments(): uninstall(args) elif args.command == "build": build(args) + elif args.command == "docs": + from docs import docs + docs(args) def main(): parse_arguments()