refactor: restructure mytoolkit with new subcommands and md-to-pdf improvements

- Add new subcommands: convert, preflight, server, templates, webpage
- Migrate config from bin/config.json to ~/.config/mytoolkit
- Fix expand_bookmarks to modify writer objects instead of reader
- Improve md_to_pdf with CJK bookmark support
- Update README and project metadata
This commit is contained in:
Zhengshou Lai
2026-05-04 17:19:32 +08:00
parent 38328c28d0
commit 3e4cf618a8
20 changed files with 1846 additions and 214 deletions
+44 -2
View File
@@ -1,11 +1,13 @@
"""PDF utilities."""
import shutil
from pathlib import Path
import click
from pypdf import PdfReader, PdfWriter
from PIL import Image
from bin.md_to_pdf import expand_bookmarks
from bin.utils import handle_errors, run_command
@@ -24,9 +26,15 @@ def pdf():
default="screen",
help="Compression quality level",
)
@click.option(
"--bookmark-depth",
type=int,
default=1,
help="Expand PDF bookmarks to this depth after compression (1=no expansion).",
)
@click.option("--dry-run", "-n", is_flag=True, help="Show what would be done")
@handle_errors
def compress(files, quality, dry_run):
def compress(files, quality, bookmark_depth, dry_run):
"""Compress PDF files using ghostscript."""
for pattern in files:
for pdf_file in Path(".").glob(pattern):
@@ -53,6 +61,8 @@ def compress(files, quality, dry_run):
click.echo(f"Compressing: {pdf_file}")
run_command(cmd, check=True)
output.replace(pdf_file)
if bookmark_depth > 1:
expand_bookmarks(pdf_file, bookmark_depth)
click.echo(f" Done: {pdf_file}")
@@ -102,9 +112,15 @@ def to_tiff(files, density, dry_run):
@pdf.command("merge")
@click.argument("files", nargs=-1, required=True)
@click.option("--output", "-o", required=True, help="Output PDF file")
@click.option(
"--bookmark-depth",
type=int,
default=1,
help="Expand PDF bookmarks to this depth after merge (1=no expansion).",
)
@click.option("--dry-run", "-n", is_flag=True, help="Show what would be done")
@handle_errors
def merge_pdfs(files, output, dry_run):
def merge_pdfs(files, output, bookmark_depth, dry_run):
"""Merge PDFs and/or images into a single PDF."""
writer = PdfWriter()
processed = []
@@ -144,4 +160,30 @@ def merge_pdfs(files, output, dry_run):
with open(output, "wb") as f:
writer.write(f)
if bookmark_depth > 1:
expand_bookmarks(Path(output), bookmark_depth)
click.echo(f"Merged {len(processed)} files into {output}")
@pdf.command("bookmark")
@click.argument("input_file", type=click.Path(exists=True, dir_okay=False, path_type=Path))
@click.option(
"--depth",
type=int,
required=True,
help="Bookmark expansion depth. 1=top-level only, 2=expand to second level, etc.",
)
@click.option(
"-o",
"--output",
type=click.Path(dir_okay=False, path_type=Path),
help="Output PDF path. Defaults to overwriting the input file.",
)
@handle_errors
def bookmark(input_file, depth, output):
"""Set PDF bookmark outline expansion depth."""
target = output if output else input_file
if output and output.resolve() != input_file.resolve():
shutil.copy2(str(input_file), str(target))
expand_bookmarks(target, depth)
click.echo(f"Bookmarks expanded to depth {depth}: {target}")