From acde07ef09f81a7e6070b20c65262191b95ad829 Mon Sep 17 00:00:00 2001 From: daymade Date: Tue, 3 Mar 2026 00:26:32 +0800 Subject: [PATCH] =?UTF-8?q?pdf-creator:=20=E8=87=AA=E5=8A=A8=20DYLD=5FLIBR?= =?UTF-8?q?ARY=5FPATH=20+=20=E5=88=97=E8=A1=A8=E6=B8=B2=E6=9F=93=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=20+=20CSS=20=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - macOS ARM Homebrew 库路径自动检测(不再需要手动 export) - 添加 markdown 预处理器:确保列表前有空行,防止解析为段落文本 - CSS word-break: break-all → overflow-wrap: break-word(中英混排友好) - batch_convert.py: 修复跨目录运行时的 import 路径 - SKILL.md: 移除手动环境变量步骤 Co-Authored-By: Claude Opus 4.6 --- pdf-creator/SKILL.md | 10 ++------ pdf-creator/scripts/batch_convert.py | 1 + pdf-creator/scripts/md_to_pdf.py | 37 +++++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/pdf-creator/SKILL.md b/pdf-creator/SKILL.md index c369752..ff94255 100644 --- a/pdf-creator/SKILL.md +++ b/pdf-creator/SKILL.md @@ -12,6 +12,7 @@ Create professional PDF documents from markdown with proper Chinese font support Convert a single markdown file: ```bash +cd ~/workspace/claude-code-skills/pdf-creator uv run --with weasyprint --with markdown scripts/md_to_pdf.py input.md output.pdf ``` @@ -21,14 +22,7 @@ Batch convert multiple files: uv run --with weasyprint --with markdown scripts/batch_convert.py *.md --output-dir ./pdfs ``` -## macOS Environment Setup - -If encountering library errors, set these environment variables first: - -```bash -export DYLD_LIBRARY_PATH="/opt/homebrew/lib:$DYLD_LIBRARY_PATH" -export PKG_CONFIG_PATH="/opt/homebrew/lib/pkgconfig:$PKG_CONFIG_PATH" -``` +macOS ARM (Homebrew) 的 `DYLD_LIBRARY_PATH` 会自动检测配置,无需手动设置。 ## Font Configuration diff --git a/pdf-creator/scripts/batch_convert.py b/pdf-creator/scripts/batch_convert.py index 9eaa37f..25765d5 100644 --- a/pdf-creator/scripts/batch_convert.py +++ b/pdf-creator/scripts/batch_convert.py @@ -15,6 +15,7 @@ import argparse import sys from pathlib import Path +sys.path.insert(0, str(Path(__file__).parent)) from md_to_pdf import markdown_to_pdf diff --git a/pdf-creator/scripts/md_to_pdf.py b/pdf-creator/scripts/md_to_pdf.py index 796bb75..cf79ea4 100644 --- a/pdf-creator/scripts/md_to_pdf.py +++ b/pdf-creator/scripts/md_to_pdf.py @@ -16,9 +16,20 @@ Requirements: export DYLD_LIBRARY_PATH="/opt/homebrew/lib:$DYLD_LIBRARY_PATH" """ +import os +import platform +import re import sys from pathlib import Path +# Auto-configure library path on macOS ARM (Homebrew) — must be before weasyprint import +if platform.system() == 'Darwin': + _homebrew_lib = '/opt/homebrew/lib' + if Path(_homebrew_lib).is_dir(): + _cur = os.environ.get('DYLD_LIBRARY_PATH', '') + if _homebrew_lib not in _cur: + os.environ['DYLD_LIBRARY_PATH'] = f"{_homebrew_lib}:{_cur}" if _cur else _homebrew_lib + import markdown from weasyprint import CSS, HTML @@ -89,8 +100,8 @@ th, td { border: 1px solid #666; padding: 8px 6px; text-align: left; - word-wrap: break-word; - word-break: break-all; + overflow-wrap: break-word; + word-break: normal; } th { @@ -134,6 +145,24 @@ blockquote { """ +def _ensure_list_spacing(text: str) -> str: + """Ensure blank lines before list items for proper markdown parsing. + + The Python markdown library requires a blank line before a list when it + follows a paragraph. Without it, list items render as plain text. + """ + lines = text.split('\n') + result = [] + list_re = re.compile(r'^(\s*)([-*+]|\d+\.)\s') + for i, line in enumerate(lines): + if i > 0 and list_re.match(line): + prev = lines[i - 1] + if prev.strip() and not list_re.match(prev): + result.append('') + result.append(line) + return '\n'.join(result) + + def markdown_to_pdf(md_file: str, pdf_file: str | None = None) -> str: """ Convert markdown file to PDF with Chinese font support. @@ -150,8 +179,8 @@ def markdown_to_pdf(md_file: str, pdf_file: str | None = None) -> str: if pdf_file is None: pdf_file = str(md_path.with_suffix('.pdf')) - # Read markdown content - md_content = md_path.read_text(encoding='utf-8') + # Read and preprocess markdown content + md_content = _ensure_list_spacing(md_path.read_text(encoding='utf-8')) # Convert to HTML html_content = markdown.markdown(