This commit is contained in:
70
src/core/generator.py
Normal file
70
src/core/generator.py
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
from .parser import parse_openapi_spec, load_spec_file
|
||||||
|
from .models import Endpoint, APISpec
|
||||||
|
|
||||||
|
|
||||||
|
def generate_docs(spec_file: str, output: str = None, format: str = 'html', open_browser: bool = False):
|
||||||
|
"""Generate documentation in the specified format.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
spec_file: Path to the OpenAPI spec file
|
||||||
|
output: Output file path or directory
|
||||||
|
format: Output format (html, markdown, json, all)
|
||||||
|
open_browser: Whether to open the generated file in browser
|
||||||
|
"""
|
||||||
|
result = parse_openapi_spec(spec_file)
|
||||||
|
|
||||||
|
if not result.get('valid'):
|
||||||
|
raise ValueError(f"Invalid spec: {result.get('errors')}")
|
||||||
|
|
||||||
|
spec = result['spec']
|
||||||
|
|
||||||
|
if format == 'all':
|
||||||
|
for fmt in ['html', 'markdown', 'json']:
|
||||||
|
generate_docs(spec_file, output, fmt, open_browser and fmt == 'html')
|
||||||
|
return
|
||||||
|
|
||||||
|
if not output:
|
||||||
|
base_name = Path(spec_file).stem
|
||||||
|
if format == 'html':
|
||||||
|
output = f"{base_name}.html"
|
||||||
|
elif format == 'markdown':
|
||||||
|
output = f"{base_name}.md"
|
||||||
|
elif format == 'json':
|
||||||
|
output = f"{base_name}_docs.json"
|
||||||
|
|
||||||
|
if format == 'html':
|
||||||
|
from .templates.html_template import generate_html
|
||||||
|
generate_html(spec, output)
|
||||||
|
elif format == 'markdown':
|
||||||
|
from .templates.markdown_template import generate_markdown
|
||||||
|
generate_markdown(spec, output)
|
||||||
|
elif format == 'json':
|
||||||
|
generate_json_docs(spec, output)
|
||||||
|
|
||||||
|
print(f"Generated {format} documentation: {output}")
|
||||||
|
|
||||||
|
if open_browser and format == 'html':
|
||||||
|
import webbrowser
|
||||||
|
webbrowser.open(f'file://{os.path.abspath(output)}')
|
||||||
|
|
||||||
|
|
||||||
|
def generate_json_docs(spec: dict, output: str):
|
||||||
|
"""Generate JSON documentation."""
|
||||||
|
from .parser import extract_endpoints
|
||||||
|
|
||||||
|
endpoints = extract_endpoints(spec)
|
||||||
|
|
||||||
|
docs = {
|
||||||
|
'title': spec.get('info', {}).get('title', 'API Documentation'),
|
||||||
|
'version': spec.get('info', {}).get('version', '1.0.0'),
|
||||||
|
'description': spec.get('info', {}).get('description', ''),
|
||||||
|
'endpoints': endpoints,
|
||||||
|
'tags': spec.get('tags', []),
|
||||||
|
'servers': spec.get('servers', [])
|
||||||
|
}
|
||||||
|
|
||||||
|
with open(output, 'w') as f:
|
||||||
|
json.dump(docs, f, indent=2)
|
||||||
Reference in New Issue
Block a user