generated from coulomb/repo-seed
Parse Markdown files into a structured Python object
This commit is contained in:
5
src/markitect_tool/cli/__init__.py
Normal file
5
src/markitect_tool/cli/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
"""Command-line interface for markitect-tool."""
|
||||
|
||||
from markitect_tool.cli.main import main
|
||||
|
||||
__all__ = ["main"]
|
||||
44
src/markitect_tool/cli/main.py
Normal file
44
src/markitect_tool/cli/main.py
Normal file
@@ -0,0 +1,44 @@
|
||||
"""`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()
|
||||
Reference in New Issue
Block a user