#!/usr/bin/env python3 import sys import json import os import shutil import logging from rich.progress import track import jq # Set up logging logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) TWO_LEVEL_PREFIXES = [ "boot", "networking", "programs", "security", "services", "systemd", "users", "virtualisation", ] REMOVE_PREFIXES = [ "_module" ] OUT = "./nixos-docs" # Load file specified on command line j = json.load(open(sys.argv[1], 'r')) logger.info("JSON Loaded") # Make output directory logger.info("Creating output directory") if not os.path.exists(OUT): os.makedirs(OUT) else: shutil.rmtree(OUT) os.makedirs(OUT) ALL_PREFIXES = jq.compile("map(.loc[0]) | unique | .[]").input_value(j) for p in track(ALL_PREFIXES.all(), description="Creating filetree..."): if p in TWO_LEVEL_PREFIXES: os.makedirs(f"{OUT}/{p}") elif p in REMOVE_PREFIXES: pass else: f = open(f"{OUT}/{p}.md", "w") f.write(f"{p.title()}\n\n") f.close() for k, v in track(j.items(), description="Writing..."): opts = v['loc'] if (opts[0] in REMOVE_PREFIXES): pass desc: str = v["description"] default: str | None = None example: str | None = None optType: str = v["type"] if 'default' not in v: pass elif v['default']['_type'] == "literalExpression": default = f"`#!nix {v['default']['text']}`" elif v['default']['_type'] == "literalMD": default = v['default']['text'] if "example" not in v: pass elif v['example']['_type'] == "literalExpression": example = f"`#!nix {v['example']['text']}`" elif v['example']['_type'] == "literalMD": example = v['example']['text'] decl: str = f"https://github.com/nixos/nixpkgs/blob/master/{v["declarations"][0]}" if (opts[0] not in TWO_LEVEL_PREFIXES): f = open(f"{OUT}/{opts[0]}.md", "a") elif(opts[0] in TWO_LEVEL_PREFIXES): if len(opts) == 2: f = open(f"{OUT}/{opts[0]}/index.md", "a") elif len(opts) > 2: f = open(f"{OUT}/{opts[0]}/{opts[1]}.md", "a") heading: str = "-".join(opts) f.write(f"## `{k}` {{{heading}}}\n\n") f.write(f"{desc}\n") f.write(f"**Type:** `{optType}`\n\n") if default: f.write(f"### Default {{{heading}-default}}\n") f.write(default) f.write("\n\n") if example: f.write(f"### Example {{{heading}-example}}\n") f.write(example) f.write("\n\n") f.write(f"**Declared by:** <{decl}>\n\n") f.close()