Initial upload: Doc2Man CLI tool with parsers, generators, and tests
This commit is contained in:
177
doc2man/preview.py
Normal file
177
doc2man/preview.py
Normal file
@@ -0,0 +1,177 @@
|
||||
"""Terminal preview module for Doc2Man."""
|
||||
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from rich.console import Console
|
||||
from rich.markdown import Markdown
|
||||
from rich.panel import Panel
|
||||
from rich.pretty import Pretty
|
||||
from rich.syntax import Syntax
|
||||
from rich.table import Table
|
||||
|
||||
|
||||
def preview_output(parsed_data: Dict[str, Any], format: str, console: Console) -> None:
|
||||
"""Preview parsed documentation in the terminal."""
|
||||
if format == "markdown":
|
||||
preview_markdown(parsed_data, console)
|
||||
elif format == "man":
|
||||
preview_man(parsed_data, console)
|
||||
elif format == "html":
|
||||
preview_html(parsed_data, console)
|
||||
else:
|
||||
console.print(Pretty(parsed_data))
|
||||
|
||||
|
||||
def preview_markdown(parsed_data: Dict[str, Any], console: Console) -> None:
|
||||
"""Preview documentation as markdown."""
|
||||
lines = []
|
||||
|
||||
if "title" in parsed_data:
|
||||
lines.append(f"# {parsed_data['title']}")
|
||||
lines.append("")
|
||||
|
||||
if "description" in parsed_data:
|
||||
lines.append(parsed_data["description"])
|
||||
lines.append("")
|
||||
|
||||
if "functions" in parsed_data:
|
||||
for func in parsed_data["functions"]:
|
||||
lines.append(f"## {func.get('name', 'Unknown')}")
|
||||
lines.append("")
|
||||
|
||||
if "description" in func:
|
||||
lines.append(func["description"])
|
||||
lines.append("")
|
||||
|
||||
if "args" in func.get("params", []):
|
||||
lines.append("### Parameters")
|
||||
lines.append("")
|
||||
for param in func["params"]:
|
||||
name = param.get("name", "Unknown")
|
||||
param_type = param.get("type", "")
|
||||
desc = param.get("description", "")
|
||||
lines.append(f"- `{name}`" + (f": `{param_type}`" if param_type else "") + f" — {desc}")
|
||||
lines.append("")
|
||||
|
||||
if "returns" in func:
|
||||
ret = func["returns"]
|
||||
ret_type = ret.get("type", "")
|
||||
ret_desc = ret.get("description", "")
|
||||
lines.append("### Returns")
|
||||
lines.append("")
|
||||
lines.append(f"{ret_type}: {ret_desc}" if ret_type else ret_desc)
|
||||
lines.append("")
|
||||
|
||||
if "examples" in func.get("examples", []):
|
||||
lines.append("### Examples")
|
||||
lines.append("")
|
||||
for example in func["examples"]:
|
||||
lines.append("```python")
|
||||
lines.append(example)
|
||||
lines.append("```")
|
||||
lines.append("")
|
||||
|
||||
markdown_text = "\n".join(lines)
|
||||
md = Markdown(markdown_text)
|
||||
console.print(md)
|
||||
|
||||
|
||||
def preview_man(parsed_data: Dict[str, Any], console: Console) -> None:
|
||||
"""Preview documentation in man page format."""
|
||||
lines = []
|
||||
|
||||
if "title" in parsed_data:
|
||||
lines.append(f".TH {parsed_data['title'].upper().replace(' ', '_')} 1")
|
||||
lines.append("")
|
||||
|
||||
if "functions" in parsed_data:
|
||||
for func in parsed_data["functions"]:
|
||||
name = func.get("name", "Unknown")
|
||||
lines.append(f".SH NAME")
|
||||
lines.append(f"{name}")
|
||||
lines.append("")
|
||||
|
||||
if "description" in func:
|
||||
lines.append(".SH DESCRIPTION")
|
||||
lines.append(func["description"])
|
||||
lines.append("")
|
||||
|
||||
if func.get("params", []):
|
||||
lines.append(".SH OPTIONS")
|
||||
for param in func["params"]:
|
||||
name = param.get("name", "")
|
||||
desc = param.get("description", "")
|
||||
lines.append(f".TP")
|
||||
lines.append(f".B {name}")
|
||||
lines.append(desc)
|
||||
lines.append("")
|
||||
|
||||
if "examples" in func.get("examples", []):
|
||||
lines.append(".SH EXAMPLES")
|
||||
for example in func["examples"]:
|
||||
lines.append(".IP")
|
||||
lines.append(example)
|
||||
lines.append("")
|
||||
|
||||
man_text = "\n".join(lines)
|
||||
syntax = Syntax(man_text, "groff", theme="monokai", line_numbers=False)
|
||||
console.print(Panel(syntax, title="Man Page Preview"))
|
||||
|
||||
|
||||
def preview_html(parsed_data: Dict[str, Any], console: Console) -> None:
|
||||
"""Preview documentation as HTML (rendered or source)."""
|
||||
lines = ["<html>", "<body>"]
|
||||
|
||||
if "title" in parsed_data:
|
||||
lines.append(f"<h1>{parsed_data['title']}</h1>")
|
||||
|
||||
if "description" in parsed_data:
|
||||
lines.append(f"<p>{parsed_data['description']}</p>")
|
||||
|
||||
if "functions" in parsed_data:
|
||||
for func in parsed_data["functions"]:
|
||||
name = func.get("name", "Unknown")
|
||||
lines.append(f"<h2>{name}</h2>")
|
||||
|
||||
if "description" in func:
|
||||
lines.append(f"<p>{func['description']}</p>")
|
||||
|
||||
if func.get("params", []):
|
||||
lines.append("<h3>Parameters</h3>")
|
||||
lines.append("<ul>")
|
||||
for param in func["params"]:
|
||||
name = param.get("name", "Unknown")
|
||||
param_type = param.get("type", "")
|
||||
desc = param.get("description", "")
|
||||
lines.append(f"<li><code>{name}</code>" + (f" ({param_type})" if param_type else "") + f": {desc}</li>")
|
||||
lines.append("</ul>")
|
||||
|
||||
if "examples" in func.get("examples", []):
|
||||
lines.append("<h3>Examples</h3>")
|
||||
lines.append("<pre><code>")
|
||||
for example in func["examples"]:
|
||||
lines.append(example)
|
||||
lines.append("</code></pre>")
|
||||
|
||||
lines.append("</body>")
|
||||
lines.append("</html>")
|
||||
|
||||
html_text = "\n".join(lines)
|
||||
syntax = Syntax(html_text, "html", theme="monokai", line_numbers=False)
|
||||
console.print(Panel(syntax, title="HTML Preview"))
|
||||
|
||||
|
||||
def display_metadata(metadata: Dict[str, Any], console: Console) -> None:
|
||||
"""Display parsed metadata for debugging."""
|
||||
table = Table(title="Parsed Metadata")
|
||||
table.add_column("Key", style="cyan")
|
||||
table.add_column("Value", style="magenta")
|
||||
|
||||
for key, value in metadata.items():
|
||||
if isinstance(value, (list, dict)):
|
||||
value_str = str(value)[:100] + "..." if len(str(value)) > 100 else str(value)
|
||||
else:
|
||||
value_str = str(value)
|
||||
table.add_row(key, value_str)
|
||||
|
||||
console.print(table)
|
||||
Reference in New Issue
Block a user