#!/usr/bin/env python3 """ Dynamic documentation generator for McRogueFace. Extracts all documentation directly from the compiled module using introspection. """ import os import sys import inspect import datetime import html import re from pathlib import Path def transform_doc_links(docstring, format='html', base_url=''): """Transform MCRF_LINK patterns based on output format. Detects pattern: "See also: TEXT (docs/path.md)" Transforms to appropriate format for output type. For HTML/web formats, properly escapes content before inserting HTML tags. """ if not docstring: return docstring link_pattern = r'See also: ([^(]+) \(([^)]+)\)' def replace_link(match): text, ref = match.group(1).strip(), match.group(2).strip() if format == 'html': # Convert docs/foo.md → foo.html and escape for safe HTML href = html.escape(ref.replace('docs/', '').replace('.md', '.html'), quote=True) text_escaped = html.escape(text) return f'
See also: {text_escaped}
' elif format == 'web': # Link to hosted docs and escape for safe HTML web_path = ref.replace('docs/', '').replace('.md', '') href = html.escape(f"{base_url}/{web_path}", quote=True) text_escaped = html.escape(text) return f'See also: {text_escaped}
' elif format == 'markdown': # Markdown link return f'\n**See also:** [{text}]({ref})' else: # 'python' or default # Keep as plain text for Python docstrings return match.group(0) # For HTML formats, escape the entire docstring first, then process links if format in ('html', 'web'): # Split by the link pattern, escape non-link parts, then reassemble parts = [] last_end = 0 for match in re.finditer(link_pattern, docstring): # Escape the text before this match if match.start() > last_end: parts.append(html.escape(docstring[last_end:match.start()])) # Process the link (replace_link handles escaping internally) parts.append(replace_link(match)) last_end = match.end() # Escape any remaining text after the last match if last_end < len(docstring): parts.append(html.escape(docstring[last_end:])) return ''.join(parts) else: # For non-HTML formats, just do simple replacement return re.sub(link_pattern, replace_link, docstring) # Must be run with McRogueFace as interpreter try: import mcrfpy except ImportError: print("Error: This script must be run with McRogueFace as the interpreter") print("Usage: ./build/mcrogueface --exec generate_dynamic_docs.py") sys.exit(1) def parse_docstring(docstring): """Parse a docstring to extract signature, description, args, and returns.""" if not docstring: return {"signature": "", "description": "", "args": [], "returns": "", "example": ""} lines = docstring.strip().split('\n') result = { "signature": "", "description": "", "args": [], "returns": "", "example": "" } # First line often contains the signature if lines and '(' in lines[0] and ')' in lines[0]: result["signature"] = lines[0].strip() lines = lines[1:] if len(lines) > 1 else [] # Parse the rest current_section = "description" description_lines = [] example_lines = [] in_example = False for line in lines: line_lower = line.strip().lower() if line_lower.startswith("args:") or line_lower.startswith("arguments:"): current_section = "args" continue elif line_lower.startswith("returns:") or line_lower.startswith("return:"): current_section = "returns" result["returns"] = line[line.find(':')+1:].strip() continue elif line_lower.startswith("example:") or line_lower.startswith("examples:"): in_example = True continue elif line_lower.startswith("note:"): if description_lines: description_lines.append("") description_lines.append(line) continue if in_example: example_lines.append(line) elif current_section == "description" and not line.startswith(" "): description_lines.append(line) elif current_section == "args" and line.strip(): # Parse argument lines like " x: X coordinate" match = re.match(r'\s+(\w+):\s*(.+)', line) if match: result["args"].append({ "name": match.group(1), "description": match.group(2).strip() }) elif current_section == "returns" and line.strip() and line.startswith(" "): result["returns"] += " " + line.strip() result["description"] = '\n'.join(description_lines).strip() result["example"] = '\n'.join(example_lines).strip() return result def get_all_functions(): """Get all module-level functions.""" functions = {} for name in dir(mcrfpy): if name.startswith('_'): continue obj = getattr(mcrfpy, name) if inspect.isbuiltin(obj) or inspect.isfunction(obj): doc_info = parse_docstring(obj.__doc__) functions[name] = { "name": name, "doc": obj.__doc__ or "", "parsed": doc_info } return functions def get_all_classes(): """Get all classes and their methods/properties.""" classes = {} for name in dir(mcrfpy): if name.startswith('_'): continue obj = getattr(mcrfpy, name) if inspect.isclass(obj): class_info = { "name": name, "doc": obj.__doc__ or "", "methods": {}, "properties": {}, "bases": [base.__name__ for base in obj.__bases__ if base.__name__ != 'object'] } # Get methods and properties for attr_name in dir(obj): if attr_name.startswith('__') and attr_name != '__init__': continue try: attr = getattr(obj, attr_name) if callable(attr): method_doc = attr.__doc__ or "" class_info["methods"][attr_name] = { "doc": method_doc, "parsed": parse_docstring(method_doc) } elif isinstance(attr, property): prop_doc = (attr.fget.__doc__ if attr.fget else "") or "" class_info["properties"][attr_name] = { "doc": prop_doc, "readonly": attr.fset is None } except: pass classes[name] = class_info return classes def get_constants(): """Get module constants.""" constants = {} for name in dir(mcrfpy): if name.startswith('_') or name[0].islower(): continue obj = getattr(mcrfpy, name) if not (inspect.isclass(obj) or callable(obj)): constants[name] = { "name": name, "value": repr(obj) if not name.startswith('default_') else f"<{name}>", "type": type(obj).__name__ } return constants def generate_html_docs(): """Generate HTML documentation.""" functions = get_all_functions() classes = get_all_classes() constants = get_constants() html_content = f"""Generated on {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
This documentation was dynamically generated from the compiled module.
{func_name}{parsed['signature'] if parsed['signature'] else '(...)'}{description}
\n" if parsed['args']: html_content += "Returns: {html.escape(parsed['returns'])}
\n" if parsed['example']: html_content += f"{html.escape(parsed['example'])}\n"
html_content += " Inherits from: {', '.join(class_info['bases'])}
\n" if class_info['doc']: html_content += f"{html.escape(class_info['doc'])}
\n" # Properties if class_info['properties']: html_content += "{method_name}{parsed['signature'] if parsed['signature'] else '(...)'}{description}
\n" if parsed['args']: html_content += "Returns: {html.escape(parsed['returns'])}
\n" html_content += "{const_name} ({const_info['type']}): {const_info['value']}