feat: implement comprehensive asset shipping for md-render command
Some checks failed
Test Suite / unit-tests (3.11) (push) Has been cancelled
Test Suite / unit-tests (3.12) (push) Has been cancelled
Test Suite / code-quality (push) Has been cancelled
Test Suite / security-scan (push) Has been cancelled
Test Suite / integration-tests (push) Has been cancelled
Test Suite / e2e-tests (push) Has been cancelled
Test Suite / performance-tests (push) Has been cancelled
Test Suite / test-summary (push) Has been cancelled

Add automatic asset copying when rendering markdown to different output
directories with intelligent defaults and full user control.

Key Features:
- Environment variable support: MARKITECT_OUTPUT_DIR sets default output directory
- Smart defaults: auto-ship assets for directory output, disabled for file output
- CLI control flags: --ship-assets and --no-ship-assets for explicit control
- Timestamp-based copying: only copies when source newer than destination
- Path preservation: maintains relative directory structure in output
- Graceful error handling: missing assets logged as warnings, not failures

Technical Implementation:
- Enhanced asset discovery in markitect/assets/discovery.py with discover_assets_from_markdown()
- Added environment variable priority: CLI --output > MARKITECT_OUTPUT_DIR > input directory
- Comprehensive asset shipping logic with _ship_assets() function
- Directory vs file output detection for intelligent default behavior

Examples and Testing:
- Added image-assets example directory with 6 sample images and comprehensive README
- Created comprehensive TDD test suite with 10 tests covering all functionality
- Tests validate environment variables, CLI flags, asset discovery, shipping logic,
  timestamp handling, missing assets, path preservation, and default behaviors

Usage:
  markitect md-render file.md -o /output/dir/     # Auto-ships assets
  markitect md-render file.md --no-ship-assets   # Suppresses shipping
  MARKITECT_OUTPUT_DIR=/docs markitect md-render file.md  # Uses env var

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-29 23:12:44 +01:00
parent ed33766c91
commit 3a353b4d4f
12 changed files with 510 additions and 3 deletions

View File

@@ -1974,9 +1974,14 @@ def md_list_command(ctx, output_format, names_only):
help='Don\'t use publication directory for output')
@click.option('--nodogtag', is_flag=True,
help='Don\'t add HTML generation dogtag at end of document')
@click.option('--ship-assets', is_flag=True, default=None,
help='Copy referenced assets to output directory')
@click.option('--no-ship-assets', is_flag=True,
help='Don\'t copy referenced assets to output directory')
@click.pass_context
def md_render_command(ctx, input_file, output, theme, css, edit, insert, editor_theme,
keyboard_shortcuts, use_publication_dir, dont_use_publication_dir, nodogtag):
keyboard_shortcuts, use_publication_dir, dont_use_publication_dir, nodogtag,
ship_assets, no_ship_assets):
"""
Render a markdown file to HTML with basic templates and live preview capabilities.
@@ -2008,17 +2013,61 @@ def md_render_command(ctx, input_file, output, theme, css, edit, insert, editor_
if edit and insert:
raise click.BadParameter("Cannot use both --edit and --insert flags simultaneously. Choose one mode.")
# Determine output path
# Validate asset shipping flags
if ship_assets and no_ship_assets:
raise click.BadParameter("Cannot use both --ship-assets and --no-ship-assets flags simultaneously.")
# Determine output path with environment variable support
if output:
output_path = Path(output)
# If output is a directory, use canonical filename within that directory
if output_path.is_dir() or (not output_path.suffix and not output_path.exists()):
# Ensure the directory exists
output_path.mkdir(parents=True, exist_ok=True)
# Use canonical filename (input name + .html) in the specified directory
canonical_filename = input_path.with_suffix('.html').name
output_path = output_path / canonical_filename
output_is_directory = True
else:
output_is_directory = False
else:
output_path = input_path.with_suffix('.html')
# Check for environment variable
import os
env_output_dir = os.environ.get('MARKITECT_OUTPUT_DIR')
if env_output_dir:
output_path = Path(env_output_dir)
output_path.mkdir(parents=True, exist_ok=True)
canonical_filename = input_path.with_suffix('.html').name
output_path = output_path / canonical_filename
output_is_directory = True
else:
output_path = input_path.with_suffix('.html')
output_is_directory = False
# Use publication directory if specified
if use_publication_dir and not dont_use_publication_dir:
pub_dir = get_publication_directory()
ensure_publication_directory(pub_dir)
output_path = pub_dir / get_output_filename(input_path)
output_is_directory = True # Publication dir is always a directory output
# Determine if we should ship assets
should_ship_assets = False
if no_ship_assets:
should_ship_assets = False
elif ship_assets:
should_ship_assets = True
elif output_is_directory:
# Default: ship assets when output is a directory
should_ship_assets = True
# Discover and ship assets if needed
if should_ship_assets:
if output_is_directory:
# For directory output, ship to the same directory as the HTML file
_ship_assets(input_path, output_path.parent, config.get('verbose', False))
# For file output, we don't ship assets (shouldn't reach here anyway)
# Initialize clean document manager
from markitect.clean_document_manager import CleanDocumentManager
@@ -3433,3 +3482,76 @@ class FilenameDecoder:
return [self.decode(filename) for filename in filenames]
def _ship_assets(input_path: Path, output_dir: Path, verbose: bool = False):
"""
Ship (copy) assets referenced in markdown file to output directory.
Args:
input_path: Path to the markdown file
output_dir: Directory where assets should be copied
verbose: Whether to print verbose output
"""
import shutil
from markitect.assets.discovery import discover_assets_from_markdown
try:
# Read the markdown content
markdown_content = input_path.read_text(encoding='utf-8')
# Discover assets
base_path = input_path.parent
assets = discover_assets_from_markdown(markdown_content, base_path)
shipped_count = 0
skipped_count = 0
missing_count = 0
for asset_ref in assets:
# Skip URLs and broken assets
if asset_ref.asset_path.startswith(('http:', 'https:', 'mailto:', 'data:')):
continue
if asset_ref.is_broken or not asset_ref.resolved_path:
missing_count += 1
if verbose:
click.echo(f" ⚠ Missing asset: {asset_ref.asset_path}", err=True)
continue
# Determine output path (preserve relative directory structure)
clean_path = asset_ref.asset_path.lstrip('./')
dest_path = output_dir / clean_path
# Create destination directory
dest_path.parent.mkdir(parents=True, exist_ok=True)
# Check if we need to copy (timestamp-based)
should_copy = True
if dest_path.exists():
source_mtime = asset_ref.resolved_path.stat().st_mtime
dest_mtime = dest_path.stat().st_mtime
if source_mtime <= dest_mtime:
should_copy = False
skipped_count += 1
if should_copy:
shutil.copy2(asset_ref.resolved_path, dest_path)
shipped_count += 1
if verbose:
click.echo(f" ✓ Copied: {asset_ref.asset_path}")
elif verbose:
click.echo(f" → Skipped (up-to-date): {asset_ref.asset_path}")
# Summary
if verbose or shipped_count > 0:
if shipped_count > 0:
click.echo(f"✓ Shipped {shipped_count} assets")
if skipped_count > 0:
click.echo(f" → Skipped {skipped_count} up-to-date assets")
if missing_count > 0:
click.echo(f"{missing_count} assets not found", err=True)
except Exception as e:
if verbose:
click.echo(f"Error shipping assets: {e}", err=True)