|
| 1 | +# Script for update documentation used by mkdocs |
| 2 | + |
| 3 | + |
| 4 | +from os import path, listdir, mkdir |
| 5 | +import subprocess |
| 6 | + |
| 7 | +# TODO: 0. Get project documentation path |
| 8 | +# TODO: Load it up from a config file |
| 9 | + |
| 10 | +DOCS_PATH = "doc/" |
| 11 | +MKDOCS_PATH = "mkdocs.yml" |
| 12 | +STATIC_DOCS_PATH = "static_documentation" |
| 13 | + |
| 14 | +## Ensure there is a folder for static documentation. |
| 15 | +## NOTE: This is not being used when mkdocs runs as server |
| 16 | +if not path.exists(STATIC_DOCS_PATH): |
| 17 | + mkdir(STATIC_DOCS_PATH) |
| 18 | + print(f"Directory '{STATIC_DOCS_PATH}' created successfully") |
| 19 | +else: |
| 20 | + print(f"Directory '{STATIC_DOCS_PATH}' already exists, using it") |
| 21 | + |
| 22 | +# TODO: 1. Get mkdocs.yml template |
| 23 | + |
| 24 | +docs_title = str() |
| 25 | +nav_sections = dict() |
| 26 | + |
| 27 | +DOCS_CONFIG = { |
| 28 | + "name": "material", |
| 29 | + "palette": { |
| 30 | + "primary": "indigo", |
| 31 | + "accent": "pink", |
| 32 | + "toggle": { |
| 33 | + "icon": "material/weather-sunny", |
| 34 | + "name": "Switch to dark mode" |
| 35 | + }, |
| 36 | + "scheme": "default" |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +# TODO: 2. Render a document with actual state for that template |
| 41 | +# - Read subdir of doc as categories |
| 42 | +# TODO: Obtain current repo version (to let reader to know what version the doc is aiming to). This could be done using git tags. |
| 43 | + |
| 44 | +docs_title = "Puter v2.5.1 development documentation" |
| 45 | + |
| 46 | +categories = [category for category in listdir(DOCS_PATH) if path.isdir(path.join(DOCS_PATH, category))] |
| 47 | + |
| 48 | +print(f"DOC: Updating MKDOCS documentation for the following categories: {', '.join(categories)}") |
| 49 | +# - Create navbar using categories (assuming each one has a README) |
| 50 | +for category in categories: |
| 51 | + category_path = f"{category}/README.md" |
| 52 | + nav_sections[" - "+category] = category_path |
| 53 | + |
| 54 | +DOCUMENT_TEMPLATE = { |
| 55 | + "docs_dir" : DOCS_PATH, |
| 56 | + "site_dir" : STATIC_DOCS_PATH, |
| 57 | + "site_name" : docs_title, |
| 58 | + "nav" : nav_sections, |
| 59 | + "theme" : DOCS_CONFIG, |
| 60 | +} |
| 61 | +print(DOCUMENT_TEMPLATE) |
| 62 | + |
| 63 | + |
| 64 | +# TODO: 3. Replace mkdocs document if exists, else create one |
| 65 | +def dict_to_yaml(data, indent=0): |
| 66 | + yaml_str = "" |
| 67 | + spaces = " " * indent # Defining indentation. |
| 68 | + |
| 69 | + if isinstance(data, dict): |
| 70 | + for key, value in data.items(): |
| 71 | + yaml_str += f"{spaces}{key}:" |
| 72 | + if isinstance(value, (dict, list)): # If value is nested, use recursion |
| 73 | + yaml_str += "\n" + dict_to_yaml(value, indent + 1) |
| 74 | + else: |
| 75 | + yaml_str += f" {value}\n" |
| 76 | + |
| 77 | + elif isinstance(data, list): |
| 78 | + for item in data: |
| 79 | + yaml_str += f"{spaces}- " |
| 80 | + if isinstance(item, (dict, list)): |
| 81 | + yaml_str += "\n" + dict_to_yaml(item, indent + 1) |
| 82 | + else: |
| 83 | + yaml_str += f"{item}\n" |
| 84 | + |
| 85 | + return yaml_str |
| 86 | + |
| 87 | +document_string = dict_to_yaml(DOCUMENT_TEMPLATE) |
| 88 | +print(document_string) |
| 89 | + |
| 90 | +def create_mkdocs_file(content): |
| 91 | + |
| 92 | + with open(MKDOCS_PATH, "w") as file: # NOTE: this is overriding mkdoc file if exists, this avoids a lot of issues. |
| 93 | + file.write(content) |
| 94 | + |
| 95 | + print(f"File '{MKDOCS_PATH}' created successfully.") |
| 96 | + |
| 97 | +create_mkdocs_file(document_string) |
0 commit comments