|
| 1 | +""" |
| 2 | +This script replaces mkdocs code and api placeholders with the actual code examples and |
| 3 | +API docstrings and writes the modified output to a file. |
| 4 | +
|
| 5 | +This code needs to import mesop, so your terminal needs to have mesop imported via pip. |
| 6 | +It does not seem to work when .cli.venv is loaded. |
| 7 | +""" |
| 8 | + |
| 9 | +import argparse |
| 10 | +import inspect |
| 11 | +import os |
| 12 | + |
| 13 | +import mesop as me |
| 14 | +import mesop.labs as mel |
| 15 | +from mesop.features.theme import ThemeVar |
| 16 | + |
| 17 | +DOCS_PATH = "../../docs/" |
| 18 | +MARKDOWN_DOCS_CONTEXT_FILE = "markdown_docs_context.txt" |
| 19 | +MARKDOWN_DOCS_BLOCKLIST_FILE = "markdown_docs_blocklist.txt" |
| 20 | +OUTPUT_DIR = "../gen/extracted_markdown/" |
| 21 | + |
| 22 | + |
| 23 | +def main(): |
| 24 | + parser = argparse.ArgumentParser( |
| 25 | + prog="Transform Markdown", |
| 26 | + description="Transform markdown documents for prompt context.", |
| 27 | + ) |
| 28 | + parser.add_argument("--use_cached_files_list", action="store_true") |
| 29 | + parser.add_argument("--cache_files_list", action="store_true") |
| 30 | + args = parser.parse_args() |
| 31 | + |
| 32 | + if args.use_cached_files_list and args.cache_files_list: |
| 33 | + print( |
| 34 | + "The flags --use_cached_files_list and --cache_files_list cannot be enabled at the same time" |
| 35 | + ) |
| 36 | + return |
| 37 | + |
| 38 | + if args.use_cached_files_list: |
| 39 | + file_paths = read_file_paths_from_file(MARKDOWN_DOCS_CONTEXT_FILE) |
| 40 | + else: |
| 41 | + file_paths = get_file_paths_recursively(DOCS_PATH) |
| 42 | + |
| 43 | + if args.cache_files_list: |
| 44 | + write_file_paths_to_file(MARKDOWN_DOCS_CONTEXT_FILE, file_paths) |
| 45 | + |
| 46 | + filtered_file_paths = filter_file_paths(file_paths) |
| 47 | + process_documentation(filtered_file_paths) |
| 48 | + |
| 49 | + |
| 50 | +def get_file_paths_recursively(directory): |
| 51 | + file_paths = [] |
| 52 | + |
| 53 | + for root, _, files in os.walk(directory): |
| 54 | + for file in files: |
| 55 | + if file.endswith(".md"): |
| 56 | + file_path = os.path.relpath(os.path.join(root, file), directory) |
| 57 | + file_paths.append(directory + file_path) |
| 58 | + |
| 59 | + return file_paths |
| 60 | + |
| 61 | + |
| 62 | +def write_file_paths_to_file(output_file, file_paths): |
| 63 | + with open(output_file, "w") as f: |
| 64 | + f.write("\n".join(file_paths).strip()) |
| 65 | + |
| 66 | + |
| 67 | +def read_file_paths_from_file(input_file): |
| 68 | + with open(input_file) as f: |
| 69 | + return list(filter(None, f.read().split("\n"))) |
| 70 | + |
| 71 | + |
| 72 | +def filter_file_paths(file_paths): |
| 73 | + blocklist = set(read_file_paths_from_file(MARKDOWN_DOCS_BLOCKLIST_FILE)) |
| 74 | + return [file_path for file_path in file_paths if file_path not in blocklist] |
| 75 | + |
| 76 | + |
| 77 | +def process_documentation(file_paths): |
| 78 | + if not os.path.exists(OUTPUT_DIR): |
| 79 | + os.makedirs(OUTPUT_DIR) |
| 80 | + |
| 81 | + total_char_count = 0 |
| 82 | + processed_files = 0 |
| 83 | + for file_path in file_paths: |
| 84 | + with open(file_path) as f: |
| 85 | + updated_lines = [] |
| 86 | + for line in f.readlines(): |
| 87 | + # Handle code snippet imports |
| 88 | + if line.startswith("--8<--"): |
| 89 | + with open( |
| 90 | + "../../" + line.removeprefix("--8<--").strip().replace('"', "") |
| 91 | + ) as f2: |
| 92 | + updated_lines.append(f2.read()) |
| 93 | + # Handle doc strings |
| 94 | + elif line.startswith("::: "): |
| 95 | + try: |
| 96 | + symbol_name = line.removeprefix(":::").strip().split(".")[-1] |
| 97 | + if symbol_name == "ThemeVar": |
| 98 | + updated_lines.append("### ThemeVar \n") |
| 99 | + updated_lines.append("attr ThemeVar = " + str(ThemeVar) + "\n") |
| 100 | + else: |
| 101 | + if "mesop.labs." in line: |
| 102 | + symbol = getattr(mel, symbol_name) |
| 103 | + else: |
| 104 | + symbol = getattr(me, symbol_name) |
| 105 | + |
| 106 | + signature = inspect.signature(symbol) |
| 107 | + updated_lines.append("### " + symbol.__name__ + "\n") |
| 108 | + type_keyword = ( |
| 109 | + "def" if "function" in str(symbol.__class__) else "class" |
| 110 | + ) |
| 111 | + updated_lines.append("```python") |
| 112 | + updated_lines.append( |
| 113 | + type_keyword + " " + symbol.__name__ + str(signature) + ":" |
| 114 | + ) |
| 115 | + updated_lines.append(" " + symbol.__doc__.strip()) |
| 116 | + updated_lines.append("```\n") |
| 117 | + except Exception: |
| 118 | + print("Failed to process: " + line) |
| 119 | + updated_lines.append(line.strip()) |
| 120 | + else: |
| 121 | + updated_lines.append(line.strip()) |
| 122 | + |
| 123 | + file_contents = "\n".join(updated_lines).strip() |
| 124 | + char_count = len(file_contents) |
| 125 | + total_char_count += char_count |
| 126 | + processed_files += 1 |
| 127 | + print(f"Processed {file_path}: {char_count:,} characters") |
| 128 | + |
| 129 | + with open( |
| 130 | + OUTPUT_DIR + file_path.replace("../../", "").replace("/", "_"), "w" |
| 131 | + ) as fw: |
| 132 | + fw.write(file_contents) |
| 133 | + |
| 134 | + print(f"Text extraction complete. Output written to {OUTPUT_DIR}") |
| 135 | + print(f"Total files processed: {processed_files}") |
| 136 | + print(f"Total characters extracted: {total_char_count:,}") |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == "__main__": |
| 140 | + main() |
0 commit comments