"""`mkt` command entry point.""" from __future__ import annotations import json from pathlib import Path import click import yaml from markitect_tool.cache import ( build_cache, cache_path_for, detect_changes, fingerprint_file, load_cache, save_cache, ) from markitect_tool.core import parse_markdown_file from markitect_tool.contract import ( ContractLoaderError, check_markdown_file, collect_metrics, load_contract_file, validate_contract, ) from markitect_tool.generation import ( GenerationPlanError, generate_stub_from_contract, load_data_file, load_generation_plan_file, run_generation_plan, ) from markitect_tool.ops import IncludeError, compose_files, resolve_includes, transform_markdown from markitect_tool.query import InvalidQueryError, extract_document, query_document from markitect_tool.schema import load_schema_file, validate_markdown_file, validate_schema from markitect_tool.template import ( MissingTemplateVariable, TemplateError, analyze_template, render_template, ) @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)) @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", "text"], case_sensitive=False), default="text", show_default=True, ) def metrics(file: Path, output_format: str) -> None: """Report practical size and complexity metrics for a Markdown file.""" document = parse_markdown_file(file) data = collect_metrics(document).to_dict() | {"document_path": str(file)} _emit_metrics(data, output_format) @main.command() @click.argument("file", type=click.Path(exists=True, dir_okay=False, path_type=Path)) @click.argument("selector") @click.option( "--format", "output_format", type=click.Choice(["json", "yaml", "text"], case_sensitive=False), default="json", show_default=True, ) def query(file: Path, selector: str, output_format: str) -> None: """Query structured Markdown content with a small selector.""" document = parse_markdown_file(file) try: matches = query_document(document, selector) except InvalidQueryError as exc: raise click.ClickException(str(exc)) from exc data = { "selector": selector, "document_path": str(file), "count": len(matches), "matches": [match.to_dict() for match in matches], } _emit_query(data, output_format) @main.command() @click.argument("file", type=click.Path(exists=True, dir_okay=False, path_type=Path)) @click.argument("selector") @click.option( "--format", "output_format", type=click.Choice(["text", "json", "yaml"], case_sensitive=False), default="text", show_default=True, ) def extract(file: Path, selector: str, output_format: str) -> None: """Extract text or Markdown content from structured Markdown.""" document = parse_markdown_file(file) try: items = extract_document(document, selector) except InvalidQueryError as exc: raise click.ClickException(str(exc)) from exc data = { "selector": selector, "document_path": str(file), "count": len(items), "items": items, } _emit_extract(data, output_format) @main.command() @click.argument("file", type=click.Path(exists=True, dir_okay=False, path_type=Path)) @click.option("--strip-frontmatter", is_flag=True, help="Remove YAML frontmatter.") @click.option( "--set", "set_values", multiple=True, metavar="KEY=VALUE", help="Set a frontmatter value. Dot paths create nested mappings.", ) @click.option( "--heading-delta", type=int, default=0, show_default=True, help="Shift ATX heading levels, clamped to 1..6.", ) @click.option("--extract", "extract_selector", help="Replace content with selector output.") @click.option( "--output", type=click.Path(dir_okay=False, path_type=Path), help="Write transformed Markdown to a file.", ) @click.option( "--format", "output_format", type=click.Choice(["markdown", "json", "yaml"], case_sensitive=False), default="markdown", show_default=True, ) def transform( file: Path, strip_frontmatter: bool, set_values: tuple[str, ...], heading_delta: int, extract_selector: str | None, output: Path | None, output_format: str, ) -> None: """Apply deterministic transforms to a Markdown file.""" try: frontmatter_updates = _parse_key_value_options(set_values) result = transform_markdown( file.read_text(encoding="utf-8"), strip_frontmatter=strip_frontmatter, set_frontmatter=frontmatter_updates, heading_delta=heading_delta, extract_selector=extract_selector, source_path=str(file), ) except (InvalidQueryError, ValueError) as exc: raise click.ClickException(str(exc)) from exc _emit_markdown_result(result.to_dict(), output_format, output) @main.command() @click.argument( "files", nargs=-1, required=True, type=click.Path(exists=True, dir_okay=False, path_type=Path), ) @click.option("--title", help="Add a top-level title before composed files.") @click.option( "--heading-delta", type=int, default=0, show_default=True, help="Shift heading levels in each input before composing.", ) @click.option( "--include-frontmatter", is_flag=True, help="Keep each input file's frontmatter in the composed body.", ) @click.option( "--output", type=click.Path(dir_okay=False, path_type=Path), help="Write composed Markdown to a file.", ) @click.option( "--format", "output_format", type=click.Choice(["markdown", "json", "yaml"], case_sensitive=False), default="markdown", show_default=True, ) def compose( files: tuple[Path, ...], title: str | None, heading_delta: int, include_frontmatter: bool, output: Path | None, output_format: str, ) -> None: """Compose multiple Markdown files into one document.""" result = compose_files( list(files), title=title, heading_delta=heading_delta, include_frontmatter=include_frontmatter, ) _emit_markdown_result(result.to_dict(), output_format, output) @main.command() @click.argument("file", type=click.Path(exists=True, dir_okay=False, path_type=Path)) @click.option( "--base-dir", type=click.Path(exists=True, file_okay=False, path_type=Path), help="Directory includes must stay within. Defaults to the input file directory.", ) @click.option( "--max-depth", type=int, default=10, show_default=True, help="Maximum recursive include depth.", ) @click.option( "--output", type=click.Path(dir_okay=False, path_type=Path), help="Write resolved Markdown to a file.", ) @click.option( "--format", "output_format", type=click.Choice(["markdown", "json", "yaml"], case_sensitive=False), default="markdown", show_default=True, ) def include( file: Path, base_dir: Path | None, max_depth: int, output: Path | None, output_format: str, ) -> None: """Resolve Markdown include markers in a document.""" try: result = resolve_includes( file.read_text(encoding="utf-8"), base_dir=base_dir or file.parent, current_path=file, max_depth=max_depth, ) except IncludeError as exc: raise click.ClickException(str(exc)) from exc _emit_markdown_result(result.to_dict(), output_format, output) @main.group() def cache() -> None: """Fingerprint Markdown files and detect changed inputs.""" @cache.command("fingerprint") @click.argument("file", type=click.Path(exists=True, dir_okay=False, path_type=Path)) @click.option( "--root", type=click.Path(exists=True, file_okay=False, path_type=Path), default=Path("."), show_default=True, help="Root used for relative cache paths.", ) @click.option( "--format", "output_format", type=click.Choice(["json", "yaml", "text"], case_sensitive=False), default="json", show_default=True, ) def cache_fingerprint(file: Path, root: Path, output_format: str) -> None: """Fingerprint one Markdown file.""" entry = fingerprint_file(file, root=root) _emit_cache_data(entry.to_dict(), output_format) @cache.command("build") @click.argument("paths", nargs=-1, required=True, type=click.Path(exists=True, path_type=Path)) @click.option( "--root", type=click.Path(exists=True, file_okay=False, path_type=Path), default=Path("."), show_default=True, help="Root used for relative cache paths.", ) @click.option( "--cache-path", type=click.Path(dir_okay=False, path_type=Path), help="Cache manifest path. Defaults to .markitect/cache/manifest.json under root.", ) @click.option("--no-recursive", is_flag=True, help="Do not recurse into directories.") @click.option("--dry-run", is_flag=True, help="Report manifest without writing it.") @click.option( "--format", "output_format", type=click.Choice(["json", "yaml", "text"], case_sensitive=False), default="text", show_default=True, ) def cache_build( paths: tuple[Path, ...], root: Path, cache_path: Path | None, no_recursive: bool, dry_run: bool, output_format: str, ) -> None: """Build or refresh a lightweight Markdown cache manifest.""" manifest = build_cache(list(paths), root=root, recursive=not no_recursive) manifest_path = cache_path_for(root, cache_path) if not dry_run: save_cache(manifest, manifest_path) data = manifest.to_dict() | { "cache_path": str(manifest_path), "written": not dry_run, "count": len(manifest.entries), } _emit_cache_data(data, output_format) @cache.command("status") @click.argument("paths", nargs=-1, required=True, type=click.Path(exists=True, path_type=Path)) @click.option( "--root", type=click.Path(exists=True, file_okay=False, path_type=Path), default=Path("."), show_default=True, help="Root used for relative cache paths.", ) @click.option( "--cache-path", type=click.Path(dir_okay=False, path_type=Path), help="Cache manifest path. Defaults to .markitect/cache/manifest.json under root.", ) @click.option("--no-recursive", is_flag=True, help="Do not recurse into directories.") @click.option( "--format", "output_format", type=click.Choice(["json", "yaml", "text"], case_sensitive=False), default="text", show_default=True, ) def cache_status( paths: tuple[Path, ...], root: Path, cache_path: Path | None, no_recursive: bool, output_format: str, ) -> None: """Report changed, new, unchanged, and deleted Markdown files.""" manifest_path = cache_path_for(root, cache_path) manifest = load_cache(manifest_path) status = detect_changes(manifest, list(paths), root=root, recursive=not no_recursive) data = status.to_dict() | {"cache_path": str(manifest_path)} _emit_cache_data(data, output_format) raise click.exceptions.Exit(1 if status.dirty else 0) @main.group() def template() -> None: """Render and inspect deterministic Markdown templates.""" @template.command("inspect") @click.argument("template_file", type=click.Path(exists=True, dir_okay=False, path_type=Path)) @click.option( "--format", "output_format", type=click.Choice(["json", "yaml", "text"], case_sensitive=False), default="text", show_default=True, ) def template_inspect(template_file: Path, output_format: str) -> None: """Inspect variables required by a template.""" data = analyze_template(template_file.read_text(encoding="utf-8")).to_dict() | { "template_path": str(template_file) } _emit_template_analysis(data, output_format) raise click.exceptions.Exit(0 if data["valid"] else 1) @template.command("render") @click.argument("template_file", type=click.Path(exists=True, dir_okay=False, path_type=Path)) @click.option( "--data", "data_file", type=click.Path(exists=True, dir_okay=False, path_type=Path), help="JSON, YAML, or CSV data file. CSV must contain one record for render.", ) @click.option( "--set", "set_values", multiple=True, metavar="KEY=VALUE", help="Set a template data value. Dot paths create nested mappings.", ) @click.option("--lenient", is_flag=True, help="Keep unresolved placeholders instead of failing.") @click.option( "--output", type=click.Path(dir_okay=False, path_type=Path), help="Write rendered Markdown to a file.", ) @click.option( "--format", "output_format", type=click.Choice(["markdown", "json", "yaml"], case_sensitive=False), default="markdown", show_default=True, ) def template_render( template_file: Path, data_file: Path | None, set_values: tuple[str, ...], lenient: bool, output: Path | None, output_format: str, ) -> None: """Render a Markdown template with structured data.""" try: data = _load_template_data(data_file) data = _deep_merge_cli(data, _parse_key_value_options(set_values)) result = render_template( template_file.read_text(encoding="utf-8"), data, strict=not lenient, ) except (MissingTemplateVariable, TemplateError, ValueError, TypeError) as exc: raise click.ClickException(str(exc)) from exc _emit_markdown_result(result.to_dict(), output_format, output) @main.group() def generate() -> None: """Generate Markdown from contracts, rules, or external hooks.""" @generate.command("stub") @click.option( "--contract", "contract_file", required=True, type=click.Path(exists=True, dir_okay=False, path_type=Path), help="Markdown document contract to generate from.", ) @click.option( "--data", "data_file", type=click.Path(exists=True, dir_okay=False, path_type=Path), help="Optional JSON/YAML data for frontmatter values.", ) @click.option( "--set", "set_values", multiple=True, metavar="KEY=VALUE", help="Set generation data. Dot paths create nested mappings.", ) @click.option("--include-optional", is_flag=True, help="Include optional contract sections.") @click.option( "--output", type=click.Path(dir_okay=False, path_type=Path), help="Write generated Markdown to a file.", ) @click.option( "--format", "output_format", type=click.Choice(["markdown", "json", "yaml"], case_sensitive=False), default="markdown", show_default=True, ) def generate_stub( contract_file: Path, data_file: Path | None, set_values: tuple[str, ...], include_optional: bool, output: Path | None, output_format: str, ) -> None: """Generate a Markdown stub from a document contract.""" try: data = _load_template_data(data_file) data = _deep_merge_cli(data, _parse_key_value_options(set_values)) result = generate_stub_from_contract( load_contract_file(contract_file), data=data, include_optional=include_optional, ) except (ContractLoaderError, ValueError, TypeError) as exc: raise click.ClickException(str(exc)) from exc _emit_markdown_result(result.to_dict(), output_format, output) @generate.command("rules") @click.argument("rules_file", type=click.Path(exists=True, dir_okay=False, path_type=Path)) @click.option( "--output-dir", type=click.Path(file_okay=False, path_type=Path), help="Directory used for relative output paths in the plan.", ) @click.option("--dry-run", is_flag=True, help="Render without writing output files.") @click.option( "--format", "output_format", type=click.Choice(["json", "yaml"], case_sensitive=False), default="json", show_default=True, ) def generate_rules( rules_file: Path, output_dir: Path | None, dry_run: bool, output_format: str, ) -> None: """Run a Markdown/YAML generation plan.""" try: plan = load_generation_plan_file(rules_file) result = run_generation_plan( plan, base_dir=rules_file.parent, output_dir=output_dir, dry_run=dry_run, ) except (GenerationPlanError, TemplateError, MissingTemplateVariable) as exc: raise click.ClickException(str(exc)) from exc _emit_jsonish(result.to_dict(), output_format) @main.command() @click.argument("file", type=click.Path(exists=True, dir_okay=False, path_type=Path)) @click.option( "--schema", "schema_file", required=True, type=click.Path(exists=True, dir_okay=False, path_type=Path), ) @click.option( "--format", "output_format", type=click.Choice(["json", "yaml", "text"], case_sensitive=False), default="text", show_default=True, ) def validate(file: Path, schema_file: Path, output_format: str) -> None: """Validate a Markdown file against a Markdown schema file.""" result = validate_markdown_file(file, schema_file) _emit_result(result.to_dict(), output_format) raise click.exceptions.Exit(0 if result.valid else 1) @main.group() def schema() -> None: """Work with Markdown schema files.""" @schema.command("validate") @click.argument("schema_file", type=click.Path(exists=True, dir_okay=False, path_type=Path)) @click.option( "--format", "output_format", type=click.Choice(["json", "yaml", "text"], case_sensitive=False), default="text", show_default=True, ) def schema_validate(schema_file: Path, output_format: str) -> None: """Validate that a Markdown schema contains a well-formed JSON Schema.""" loaded = load_schema_file(schema_file) result = validate_schema(loaded.schema) data = result.to_dict() | {"schema_path": str(schema_file)} _emit_result(data, output_format) raise click.exceptions.Exit(0 if result.valid else 1) @main.group() def contract() -> None: """Work with Markdown document contracts.""" @contract.command("validate") @click.argument("contract_file", type=click.Path(exists=True, dir_okay=False, path_type=Path)) @click.option( "--format", "output_format", type=click.Choice(["json", "yaml", "text"], case_sensitive=False), default="text", show_default=True, ) def contract_validate(contract_file: Path, output_format: str) -> None: """Validate that a Markdown contract file is well formed.""" result = validate_contract(load_contract_file(contract_file)) _emit_diagnostic_result(result.to_dict(), output_format) raise click.exceptions.Exit(0 if result.valid else 1) @contract.command("check") @click.argument("file", type=click.Path(exists=True, dir_okay=False, path_type=Path)) @click.option( "--contract", "contract_file", required=True, type=click.Path(exists=True, dir_okay=False, path_type=Path), ) @click.option( "--format", "output_format", type=click.Choice(["json", "yaml", "text"], case_sensitive=False), default="text", show_default=True, ) def contract_check(file: Path, contract_file: Path, output_format: str) -> None: """Check a Markdown file against a Markdown document contract.""" try: result = check_markdown_file(file, contract_file) except ContractLoaderError as exc: raise click.ClickException(str(exc)) from exc _emit_diagnostic_result(result.to_dict(), output_format) raise click.exceptions.Exit(0 if result.valid else 1) def _emit_result(data: dict, output_format: str) -> None: if output_format == "json": click.echo(json.dumps(data, indent=2, ensure_ascii=False)) elif output_format == "yaml": click.echo(yaml.safe_dump(data, sort_keys=False)) else: if data.get("valid"): click.echo("valid") else: click.echo("invalid") for violation in data.get("violations", []): click.echo(f"- {violation['path']}: {violation['message']}") def _emit_diagnostic_result(data: dict, output_format: str) -> None: if output_format == "json": click.echo(json.dumps(data, indent=2, ensure_ascii=False)) elif output_format == "yaml": click.echo(yaml.safe_dump(data, sort_keys=False)) else: click.echo("valid" if data.get("valid") else "invalid") for diagnostic in data.get("diagnostics", []): click.echo( f"- [{diagnostic['severity']}] {diagnostic['code']}: " f"{diagnostic['message']}" ) if diagnostic.get("source"): source = diagnostic["source"] suffix = f":{source['line']}" if source.get("line") else "" click.echo(f" source: {source.get('path', '')}{suffix}") if diagnostic.get("guidance"): click.echo(f" guidance: {diagnostic['guidance']}") def _emit_metrics(data: dict, output_format: str) -> None: if output_format == "json": click.echo(json.dumps(data, indent=2, ensure_ascii=False)) elif output_format == "yaml": click.echo(yaml.safe_dump(data, sort_keys=False)) else: doc = data["document"] click.echo("document") for metric, value in doc.items(): click.echo(f"- {metric}: {value}") sections = data.get("sections", []) if sections: click.echo("sections") for section in sections: click.echo( f"- {section['heading']}: words={section['words']}, " f"paragraphs={section['paragraphs']}, line={section['line']}" ) def _emit_query(data: dict, output_format: str) -> None: if output_format == "json": click.echo(json.dumps(data, indent=2, ensure_ascii=False)) elif output_format == "yaml": click.echo(yaml.safe_dump(data, sort_keys=False)) else: click.echo(f"{data['count']} match(es)") for match in data["matches"]: location = f":{match['line']}" if match.get("line") else "" click.echo(f"- {match['kind']} {match['path']}{location}") if match.get("text"): click.echo(f" {match['text'].splitlines()[0]}") def _emit_extract(data: dict, output_format: str) -> None: if output_format == "json": click.echo(json.dumps(data, indent=2, ensure_ascii=False)) elif output_format == "yaml": click.echo(yaml.safe_dump(data, sort_keys=False)) else: click.echo("\n\n".join(data["items"])) def _emit_markdown_result(data: dict, output_format: str, output: Path | None) -> None: if output_format == "json": click.echo(json.dumps(data, indent=2, ensure_ascii=False)) return if output_format == "yaml": click.echo(yaml.safe_dump(data, sort_keys=False)) return markdown = data["markdown"] if output: output.write_text(markdown, encoding="utf-8") else: click.echo(markdown, nl=False) def _emit_cache_data(data: dict, output_format: str) -> None: if output_format == "json": click.echo(json.dumps(data, indent=2, ensure_ascii=False)) elif output_format == "yaml": click.echo(yaml.safe_dump(data, sort_keys=False)) else: if "dirty" in data: click.echo("dirty" if data["dirty"] else "clean") for key in ["new", "changed", "deleted", "unchanged"]: values = data.get(key, []) if values: click.echo(f"{key}: {len(values)}") for value in values: click.echo(f"- {value}") else: click.echo(f"cache_path: {data.get('cache_path', '')}") click.echo(f"count: {data.get('count', len(data.get('entries', [])))}") if data.get("written") is not None: click.echo(f"written: {data['written']}") def _emit_jsonish(data: dict, output_format: str) -> None: if output_format == "yaml": click.echo(yaml.safe_dump(data, sort_keys=False)) else: click.echo(json.dumps(data, indent=2, ensure_ascii=False)) def _emit_template_analysis(data: dict, output_format: str) -> None: if output_format == "json": click.echo(json.dumps(data, indent=2, ensure_ascii=False)) elif output_format == "yaml": click.echo(yaml.safe_dump(data, sort_keys=False)) else: click.echo("valid" if data["valid"] else "invalid") click.echo(f"variables: {data['unique_variables']}") for variable in data["variables"]: click.echo(f"- {variable}") for error in data["syntax_errors"]: click.echo(f"! {error}") def _parse_key_value_options(items: tuple[str, ...]) -> dict[str, object]: values: dict[str, object] = {} for item in items: if "=" not in item: raise ValueError(f"Expected KEY=VALUE, got `{item}`") key, raw_value = item.split("=", 1) key = key.strip() if not key: raise ValueError(f"Expected non-empty key in `{item}`") _set_path(values, key.split("."), yaml.safe_load(raw_value)) return values def _set_path(mapping: dict[str, object], path: list[str], value: object) -> None: current = mapping for part in path[:-1]: next_value = current.setdefault(part, {}) if not isinstance(next_value, dict): raise ValueError(f"Cannot set nested frontmatter path through scalar `{part}`") current = next_value current[path[-1]] = value def _load_template_data(data_file: Path | None) -> dict[str, object]: if data_file is None: return {} data = load_data_file(data_file) if isinstance(data, list): if len(data) != 1: raise ValueError("Template render expects exactly one CSV record") data = data[0] if not isinstance(data, dict): raise ValueError("Template data must be a mapping") return data def _deep_merge_cli(left: dict[str, object], right: dict[str, object]) -> dict[str, object]: merged = dict(left) for key, value in right.items(): if isinstance(merged.get(key), dict) and isinstance(value, dict): merged[key] = _deep_merge_cli(merged[key], value) else: merged[key] = value return merged if __name__ == "__main__": main()