ShyyTAUI/generate_docs.py

130 lines
4.7 KiB
Python

#!/usr/bin/env python3
import re
import os
from pathlib import Path
from collections import defaultdict
# Read the index.d.ts file
with open('/Users/serverbp/Documents/Luna/Development/ShyyTAUI/node_modules/moons-ta-client/dist/index.d.ts', 'r') as f:
content = f.read()
# Define the output directory
output_dir = Path('/Users/serverbp/Documents/Luna/Development/ShyyTAUI/src/lib/taDocs')
# Extract all enum definitions
enum_pattern = r'declare enum (\w+) \{([^}]+)\}'
enum_matches = re.finditer(enum_pattern, content, re.DOTALL)
enums = {}
for match in enum_matches:
enum_name = match.group(1)
enum_body = match.group(2)
enums[enum_name] = enum_body.strip()
# Extract all interface definitions
interface_pattern = r'interface (\w+) \{([^}]*(?:\{[^}]*\}[^}]*)*)\}'
interface_matches = re.finditer(interface_pattern, content, re.DOTALL)
interfaces = {}
for match in interface_matches:
interface_name = match.group(1)
interface_body = match.group(2)
# Skip $Type classes
if interface_name.endswith('$Type'):
continue
interfaces[interface_name] = interface_body.strip()
# Extract all types referenced in interfaces
referenced_types = defaultdict(set)
def extract_types_from_interface(interface_name, body):
# Find all type references in format: type?: SomeType or type: SomeType or property: Type[] etc
# This regex captures type names (capitalized words, can have underscores)
type_pattern = r'(?::\s*|(?:Promise<))(\w+(?:_\w+)*)'
for match in re.finditer(type_pattern, body):
type_name = match.group(1)
# Skip built-in types
if type_name not in ['string', 'number', 'boolean', 'any', 'void', 'never', 'unknown', 'object', 'Promise', 'IBinaryReader', 'IBinaryWriter', 'BinaryReadOptions', 'BinaryWriteOptions', 'PartialMessage', 'MessageType', 'Extract', 'Record', 'K']:
referenced_types[interface_name].add(type_name)
for name, body in interfaces.items():
extract_types_from_interface(name, body)
# Function to extract the full definition from the file (with comments)
def get_full_definition(name, is_enum=False):
if is_enum:
pattern = f'((?://.*\n)*declare enum {re.escape(name)} \\{{.*?\\}})'
else:
pattern = f'((?://.*\n)*interface {re.escape(name)} \\{{(?:.*?)*?\\}})'
match = re.search(pattern, content, re.DOTALL)
if match:
return match.group(1).strip()
return None
# Create enum documentation files
for enum_name in sorted(enums.keys()):
full_def = get_full_definition(enum_name, is_enum=True)
if not full_def:
full_def = f"declare enum {enum_name} {{{enums[enum_name]}}}"
# Extract referenced types from enum
refs = []
for match in re.finditer(r'(\w+(?:_\w+)*)', full_def):
type_name = match.group(1)
if type_name in enums or type_name in interfaces:
refs.append(type_name)
refs = list(set(refs))
refs = [r for r in refs if r != enum_name]
refs.sort()
# Create markdown content
md_content = f"# {enum_name}\n\n```ts\n{full_def}\n```\n"
# Add references
if refs:
md_content += "\n## References\n"
for ref in refs:
if ref in enums:
md_content += f"- For `{ref}` please find documentation at [Enums -> {ref}](/documentation#Enums/5-{ref}).\n"
elif ref in interfaces:
md_content += f"- For `{ref}` please find documentation at [Models -> {ref}](/documentation#Models/4-{ref}).\n"
# Write file
file_path = output_dir / f"5-{enum_name}.md"
with open(file_path, 'w') as f:
f.write(md_content)
print(f"✓ Created {file_path.name}")
# Create interface documentation files
for interface_name in sorted(interfaces.keys()):
full_def = get_full_definition(interface_name, is_enum=False)
if not full_def:
full_def = f"interface {interface_name} {{{interfaces[interface_name]}}}"
# Create markdown content
md_content = f"# {interface_name}\n\n```ts\n{full_def}\n```\n"
# Add references
refs = list(referenced_types.get(interface_name, set()))
refs.sort()
if refs:
md_content += "\n## References\n"
for ref in refs:
if ref in enums:
md_content += f"- For `{ref}` please find documentation at [Enums -> {ref}](/documentation#Enums/5-{ref}).\n"
elif ref in interfaces:
md_content += f"- For `{ref}` please find documentation at [Models -> {ref}](/documentation#Models/4-{ref}).\n"
# Write file
file_path = output_dir / f"4-{interface_name}.md"
with open(file_path, 'w') as f:
f.write(md_content)
print(f"✓ Created {file_path.name}")
print(f"\nProcessed {len(enums)} enums and {len(interfaces)} interfaces")