|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Convert markdown documentation to PDF using Python libraries""" |
| 3 | + |
| 4 | +import sys |
| 5 | +import os |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +# Force UTF-8 encoding for console output on Windows |
| 9 | +if sys.platform == 'win32': |
| 10 | + sys.stdout.reconfigure(encoding='utf-8') |
| 11 | + sys.stderr.reconfigure(encoding='utf-8') |
| 12 | + |
| 13 | +def check_dependencies(): |
| 14 | + """Check if required packages are installed""" |
| 15 | + try: |
| 16 | + import markdown |
| 17 | + except ImportError: |
| 18 | + print("Installing required package: markdown...") |
| 19 | + import subprocess |
| 20 | + subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'markdown']) |
| 21 | + import markdown |
| 22 | + |
| 23 | + try: |
| 24 | + import xhtml2pdf |
| 25 | + except ImportError: |
| 26 | + print("Installing required package: xhtml2pdf...") |
| 27 | + import subprocess |
| 28 | + subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'xhtml2pdf']) |
| 29 | + import xhtml2pdf |
| 30 | + |
| 31 | + return True |
| 32 | + |
| 33 | +def markdown_to_html(md_content, title="Documentation"): |
| 34 | + """Convert markdown to HTML with styling""" |
| 35 | + import markdown |
| 36 | + |
| 37 | + # Convert markdown to HTML |
| 38 | + md = markdown.Markdown(extensions=[ |
| 39 | + 'extra', |
| 40 | + 'codehilite', |
| 41 | + 'toc', |
| 42 | + 'tables', |
| 43 | + 'fenced_code' |
| 44 | + ]) |
| 45 | + html_content = md.convert(md_content) |
| 46 | + |
| 47 | + # Create full HTML document with CSS |
| 48 | + html = f""" |
| 49 | +<!DOCTYPE html> |
| 50 | +<html> |
| 51 | +<head> |
| 52 | + <meta charset="utf-8"> |
| 53 | + <title>{title}</title> |
| 54 | + <style> |
| 55 | + @page {{ |
| 56 | + size: letter; |
| 57 | + margin: 1in; |
| 58 | + }} |
| 59 | + body {{ |
| 60 | + font-family: Arial, sans-serif; |
| 61 | + line-height: 1.6; |
| 62 | + color: #333; |
| 63 | + max-width: 100%; |
| 64 | + }} |
| 65 | + h1, h2, h3, h4, h5, h6 {{ |
| 66 | + color: #d32f2f; |
| 67 | + margin-top: 1.5em; |
| 68 | + margin-bottom: 0.5em; |
| 69 | + }} |
| 70 | + h1 {{ |
| 71 | + font-size: 2em; |
| 72 | + border-bottom: 2px solid #d32f2f; |
| 73 | + padding-bottom: 0.3em; |
| 74 | + }} |
| 75 | + h2 {{ |
| 76 | + font-size: 1.5em; |
| 77 | + }} |
| 78 | + code {{ |
| 79 | + background-color: #f5f5f5; |
| 80 | + padding: 2px 4px; |
| 81 | + border-radius: 3px; |
| 82 | + font-family: 'Courier New', monospace; |
| 83 | + font-size: 0.9em; |
| 84 | + }} |
| 85 | + pre {{ |
| 86 | + background-color: #f5f5f5; |
| 87 | + padding: 10px; |
| 88 | + border-left: 3px solid #d32f2f; |
| 89 | + overflow-x: auto; |
| 90 | + border-radius: 3px; |
| 91 | + }} |
| 92 | + pre code {{ |
| 93 | + background-color: transparent; |
| 94 | + padding: 0; |
| 95 | + }} |
| 96 | + table {{ |
| 97 | + border-collapse: collapse; |
| 98 | + width: 100%; |
| 99 | + margin: 1em 0; |
| 100 | + }} |
| 101 | + th, td {{ |
| 102 | + border: 1px solid #ddd; |
| 103 | + padding: 8px; |
| 104 | + text-align: left; |
| 105 | + }} |
| 106 | + th {{ |
| 107 | + background-color: #d32f2f; |
| 108 | + color: white; |
| 109 | + }} |
| 110 | + tr:nth-child(even) {{ |
| 111 | + background-color: #f9f9f9; |
| 112 | + }} |
| 113 | + a {{ |
| 114 | + color: #d32f2f; |
| 115 | + text-decoration: none; |
| 116 | + }} |
| 117 | + a:hover {{ |
| 118 | + text-decoration: underline; |
| 119 | + }} |
| 120 | + blockquote {{ |
| 121 | + border-left: 4px solid #d32f2f; |
| 122 | + padding-left: 1em; |
| 123 | + margin-left: 0; |
| 124 | + color: #666; |
| 125 | + }} |
| 126 | + .toc {{ |
| 127 | + background-color: #f9f9f9; |
| 128 | + border: 1px solid #ddd; |
| 129 | + padding: 1em; |
| 130 | + margin-bottom: 2em; |
| 131 | + }} |
| 132 | + </style> |
| 133 | +</head> |
| 134 | +<body> |
| 135 | + {html_content} |
| 136 | +</body> |
| 137 | +</html> |
| 138 | +""" |
| 139 | + return html |
| 140 | + |
| 141 | +def convert_md_to_pdf(md_file, output_file): |
| 142 | + """Convert a single markdown file to PDF""" |
| 143 | + from xhtml2pdf import pisa |
| 144 | + |
| 145 | + # Read markdown file |
| 146 | + with open(md_file, 'r', encoding='utf-8') as f: |
| 147 | + md_content = f.read() |
| 148 | + |
| 149 | + # Get title from filename |
| 150 | + title = md_file.stem.replace('-', ' ').replace('_', ' ').title() |
| 151 | + title = f"Event Video Playback - {title}" |
| 152 | + |
| 153 | + # Convert to HTML |
| 154 | + html = markdown_to_html(md_content, title) |
| 155 | + |
| 156 | + # Convert HTML to PDF |
| 157 | + with open(output_file, 'wb') as pdf_file: |
| 158 | + pisa_status = pisa.CreatePDF(html, dest=pdf_file) |
| 159 | + |
| 160 | + if pisa_status.err: |
| 161 | + raise Exception(f"PDF generation failed with {pisa_status.err} errors") |
| 162 | + |
| 163 | +def main(): |
| 164 | + """Main function""" |
| 165 | + # Check dependencies |
| 166 | + print("Checking dependencies...") |
| 167 | + if not check_dependencies(): |
| 168 | + return 1 |
| 169 | + |
| 170 | + print("✓ All dependencies installed\n") |
| 171 | + |
| 172 | + # Set paths |
| 173 | + script_dir = Path(__file__).parent |
| 174 | + project_root = script_dir.parent |
| 175 | + docs_dir = project_root / 'gh_pages' / '_docs' |
| 176 | + output_dir = script_dir / 'packages' / 'EventvideoPlayback.Documentation' / 'bin' |
| 177 | + |
| 178 | + # Verify input directory exists |
| 179 | + if not docs_dir.exists(): |
| 180 | + print(f"✗ Documentation directory not found: {docs_dir}") |
| 181 | + return 1 |
| 182 | + |
| 183 | + # Create output directory |
| 184 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 185 | + |
| 186 | + # Get all markdown files |
| 187 | + md_files = sorted(docs_dir.glob('*.md')) |
| 188 | + |
| 189 | + if not md_files: |
| 190 | + print(f"✗ No markdown files found in {docs_dir}") |
| 191 | + return 1 |
| 192 | + |
| 193 | + print(f"Found {len(md_files)} markdown files") |
| 194 | + print(f"Output directory: {output_dir}\n") |
| 195 | + |
| 196 | + success = 0 |
| 197 | + failed = 0 |
| 198 | + |
| 199 | + # Convert each file |
| 200 | + for md_file in md_files: |
| 201 | + pdf_name = md_file.stem + '.pdf' |
| 202 | + pdf_path = output_dir / pdf_name |
| 203 | + |
| 204 | + print(f"Converting {md_file.name}...", end=' ') |
| 205 | + |
| 206 | + try: |
| 207 | + convert_md_to_pdf(md_file, pdf_path) |
| 208 | + print("✓") |
| 209 | + success += 1 |
| 210 | + except Exception as e: |
| 211 | + print(f"✗ {e}") |
| 212 | + failed += 1 |
| 213 | + |
| 214 | + # Create combined PDF |
| 215 | + print(f"\nCreating combined PDF...", end=' ') |
| 216 | + try: |
| 217 | + from xhtml2pdf import pisa |
| 218 | + |
| 219 | + # Combine all markdown content |
| 220 | + combined_content = [] |
| 221 | + for md_file in md_files: |
| 222 | + with open(md_file, 'r', encoding='utf-8') as f: |
| 223 | + content = f.read() |
| 224 | + # Add a page break between documents |
| 225 | + combined_content.append(content) |
| 226 | + combined_content.append("\n\n---\n\n") |
| 227 | + |
| 228 | + # Convert to HTML |
| 229 | + html = markdown_to_html(''.join(combined_content), "Event Video Playback - Complete Documentation") |
| 230 | + |
| 231 | + # Convert to PDF |
| 232 | + combined_path = output_dir / 'complete-documentation.pdf' |
| 233 | + with open(combined_path, 'wb') as pdf_file: |
| 234 | + pisa_status = pisa.CreatePDF(html, dest=pdf_file) |
| 235 | + |
| 236 | + if pisa_status.err: |
| 237 | + raise Exception(f"PDF generation failed with {pisa_status.err} errors") |
| 238 | + |
| 239 | + print("✓") |
| 240 | + success += 1 |
| 241 | + except Exception as e: |
| 242 | + print(f"✗ {e}") |
| 243 | + failed += 1 |
| 244 | + |
| 245 | + # Summary |
| 246 | + print(f"\n{'='*40}") |
| 247 | + print(f"Success: {success}, Failed: {failed}") |
| 248 | + print(f"{'='*40}") |
| 249 | + |
| 250 | + if failed == 0: |
| 251 | + print("\n✓ All PDFs generated successfully!") |
| 252 | + print(f"Output: {output_dir}") |
| 253 | + |
| 254 | + return 0 if failed == 0 else 1 |
| 255 | + |
| 256 | +if __name__ == '__main__': |
| 257 | + sys.exit(main()) |
0 commit comments