45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
|
|
"""`mkt` command entry point."""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import json
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
import click
|
||
|
|
import yaml
|
||
|
|
|
||
|
|
from markitect_tool.core import parse_markdown_file
|
||
|
|
|
||
|
|
|
||
|
|
@click.group()
|
||
|
|
@click.version_option()
|
||
|
|
def main() -> None:
|
||
|
|
"""Markdown-native toolkit for structured knowledge artifacts."""
|
||
|
|
|
||
|
|
|
||
|
|
@main.command()
|
||
|
|
@click.argument("file", type=click.Path(exists=True, dir_okay=False, path_type=Path))
|
||
|
|
@click.option(
|
||
|
|
"--format",
|
||
|
|
"output_format",
|
||
|
|
type=click.Choice(["json", "yaml", "tree"], case_sensitive=False),
|
||
|
|
default="json",
|
||
|
|
show_default=True,
|
||
|
|
)
|
||
|
|
def parse(file: Path, output_format: str) -> None:
|
||
|
|
"""Parse a Markdown file into a structured representation."""
|
||
|
|
|
||
|
|
document = parse_markdown_file(file)
|
||
|
|
data = document.to_dict()
|
||
|
|
if output_format == "yaml":
|
||
|
|
click.echo(yaml.safe_dump(data, sort_keys=False))
|
||
|
|
elif output_format == "tree":
|
||
|
|
for heading in document.headings:
|
||
|
|
click.echo(f"{'#' * heading.level} {heading.text}")
|
||
|
|
else:
|
||
|
|
click.echo(json.dumps(data, indent=2, ensure_ascii=False))
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|