""" Markdown commands plugin for MarkiTect. This plugin provides the core markdown file operations with md- prefixes, replacing the legacy unprefixed commands for better namespace consistency. """ import click from pathlib import Path from typing import Dict, Any from markitect.plugins.base import CommandPlugin, PluginMetadata, PluginType from markitect.plugins.decorators import register_plugin from markitect.document_manager import DocumentManager from markitect.serializer import ASTSerializer # Simple helper function - avoiding circular imports def get_default_format(available_formats=['table', 'json', 'yaml', 'simple'], fallback='simple'): """Get the default output format - simplified version for plugin.""" return fallback @register_plugin("markdown_commands") class MarkdownCommandsPlugin(CommandPlugin): """Plugin providing core markdown file operations.""" @property def metadata(self) -> PluginMetadata: return PluginMetadata( name="markdown_commands", version="1.0.0", description="Core markdown file operations (ingest, get, list) with md- prefixes", author="MarkiTect Core Team", plugin_type=PluginType.COMMAND, markitect_version=">=0.1.0" ) def get_commands(self) -> Dict[str, Any]: """Return the markdown commands with md- prefixes.""" return { 'md-ingest': md_ingest_command, 'md-get': md_get_command, 'md-list': md_list_command } # Define commands as standalone functions @click.command() @click.argument('file_path', type=click.Path(exists=True)) @click.pass_context def md_ingest_command(ctx, file_path): """ Process and store a markdown file. Ingests a markdown file into the MarkiTect system, parsing its content, extracting front matter, generating AST cache, and storing metadata in the database. FILE_PATH: Path to the markdown file to process Examples: markitect md-ingest README.md markitect md-ingest docs/guide.md """ config = ctx.obj or {} try: if config.get('verbose', False): click.echo(f"Processing file: {file_path}") # Initialize document manager with database manager doc_manager = DocumentManager(config.get('db_manager')) # Process the file result = doc_manager.ingest_file(file_path) if config.get('verbose', False): click.echo(f"Processing results:") click.echo(f" File: {result['metadata']['filename']}") click.echo(f" AST nodes: {len(result['ast'])} nodes") click.echo(f" Cache file: {result['ast_cache_path']}") click.echo(f" Parse time: {result['parse_time']:.2f}s") click.echo(f" Cache time: {result['cache_time']:.2f}s") click.echo(f"✓ Successfully ingested: {Path(file_path).name}") except Exception as e: click.echo(f"Error processing file: {e}", err=True) raise click.Abort() @click.command() @click.argument('file_path', type=str) @click.option('--output', '-o', type=click.Path(), help='Output file path (default: stdout)') @click.pass_context def md_get_command(ctx, file_path, output): """ Retrieve and output a processed markdown file. Loads the file from the database and AST cache, then serializes it back to markdown format. Supports outputting to file or stdout. FILE_PATH: Name of the file to retrieve Examples: markitect md-get README.md markitect md-get docs/guide.md --output modified_guide.md """ config = ctx.obj or {} try: if config.get('verbose', False): click.echo(f"Retrieving file: {file_path}") db_manager = config.get('db_manager') # Get file information from database file_info = db_manager.get_markdown_file(file_path) if not file_info: click.echo(f"File not found in database: {file_path}", err=True) click.echo("Use 'markitect md-ingest' to process the file first.", err=True) raise click.Abort() # Load AST from cache cache_filename = f"{file_path}.ast.json" cache_path = Path('.ast_cache') / cache_filename if not cache_path.exists(): click.echo(f"AST cache not found: {cache_path}", err=True) click.echo("Try re-ingesting the file to regenerate cache.", err=True) raise click.Abort() # Read AST from cache import json with open(cache_path, 'r', encoding='utf-8') as f: ast = json.load(f) # Parse front matter from database front_matter = None if file_info.get('front_matter'): try: front_matter = eval(file_info['front_matter']) except (ValueError, TypeError, SyntaxError): if config.get('verbose', False): click.echo("Warning: Could not parse front matter", err=True) # Serialize AST back to markdown serializer = ASTSerializer() markdown_content = serializer.serialize_to_markdown(ast, front_matter) # Output to file or stdout if output: output_path = Path(output) output_path.parent.mkdir(parents=True, exist_ok=True) with open(output_path, 'w', encoding='utf-8') as f: f.write(markdown_content) click.echo(f"✓ File written to: {output_path}") else: click.echo(markdown_content) if config.get('verbose', False): click.echo(f"Retrieved {len(ast)} AST tokens", err=True) except Exception as e: click.echo(f"Error retrieving file: {e}", err=True) raise click.Abort() @click.command() @click.option('--format', 'output_format', type=click.Choice(['table', 'json', 'yaml', 'simple']), default=lambda: get_default_format(['table', 'json', 'yaml', 'simple']), help='Output format') @click.option('--names-only', is_flag=True, help='Show only filenames (no metadata)') @click.pass_context def md_list_command(ctx, output_format, names_only): """ List all stored markdown files and their status. Shows all markdown files that have been processed and stored in the MarkiTect database with their basic metadata. Examples: markitect md-list markitect md-list --format table markitect md-list --format json markitect md-list --names-only """ config = ctx.obj or {} try: if config.get('verbose', False): click.echo("Retrieving all stored files...") db_manager = config.get('db_manager') files = db_manager.list_markdown_files() if not files: click.echo("No files found in database.") click.echo("Use 'markitect md-ingest ' to add files.") return # Handle names-only option if names_only: for file_info in files: click.echo(file_info['filename']) return # Handle different output formats if output_format == 'simple': # Original emoji format click.echo(f"Found {len(files)} file(s):") click.echo() for file_info in files: click.echo(f"📄 {file_info['filename']}") if config.get('verbose', False): click.echo(f" Created: {file_info['created_at']}") if file_info.get('front_matter'): try: front_matter = eval(file_info['front_matter']) if front_matter: click.echo(f" Front matter: {list(front_matter.keys())}") except (ValueError, TypeError, SyntaxError): click.echo(f" Front matter: (parsing error)") click.echo() else: # Use structured format (table, json, yaml) if output_format == 'json': import json click.echo(json.dumps(files, indent=2, default=str)) elif output_format == 'yaml': import yaml click.echo(yaml.dump(files, default_flow_style=False)) else: # table format (default) # Simple table output click.echo(f"Found {len(files)} file(s):") click.echo(f"{'Filename':<30} {'Created':<20}") click.echo("-" * 50) for file_info in files: click.echo(f"{file_info['filename']:<30} {file_info['created_at']:<20}") except Exception as e: click.echo(f"Error listing files: {e}", err=True) raise click.Abort()