diff --git a/tools/generate_dynamic_docs.py b/tools/generate_dynamic_docs.py index 92e65cc..4b79315 100644 --- a/tools/generate_dynamic_docs.py +++ b/tools/generate_dynamic_docs.py @@ -12,6 +12,40 @@ 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. + """ + 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 + href = ref.replace('docs/', '').replace('.md', '.html') + return f'
See also: {text}
' + + elif format == 'web': + # Link to hosted docs + web_path = ref.replace('docs/', '').replace('.md', '') + return f'See also: {text}
' + + 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) + + return re.sub(link_pattern, replace_link, docstring) + # Must be run with McRogueFace as interpreter try: import mcrfpy @@ -304,8 +338,9 @@ def generate_html_docs(): html_content += f"""{func_name}{parsed['signature'] if parsed['signature'] else '(...)'}{html.escape(parsed['description'])}
""" + description = transform_doc_links(parsed['description'], format='html') + html_content += f"{description}
\n" if parsed['args']: html_content += "{method_name}{parsed['signature'] if parsed['signature'] else '(...)'}{html.escape(parsed['description'])}
\n" + description = transform_doc_links(parsed['description'], format='html') + html_content += f"{description}
\n" if parsed['args']: html_content += "See also: {text}
' + + elif format == 'web': + # Link to hosted docs + web_path = ref.replace('docs/', '').replace('.md', '') + return f'See also: {text}
' + + 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) + + return re.sub(link_pattern, replace_link, docstring) + +# Test cases +test_doc = "Description text.\n\nSee also: Tutorial Guide (docs/guide.md)\n\nMore text." + +html_result = transform_doc_links(test_doc, format='html') +print("HTML:", html_result) +assert 'Tutorial Guide' in html_result + +md_result = transform_doc_links(test_doc, format='markdown') +print("Markdown:", md_result) +assert '[Tutorial Guide](docs/guide.md)' in md_result + +plain_result = transform_doc_links(test_doc, format='python') +print("Python:", plain_result) +assert 'See also: Tutorial Guide (docs/guide.md)' in plain_result + +print("\nSUCCESS: All transformations work correctly")