|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Build EPUB from docs/ markdown files using Pandoc. |
| 4 | + * Outputs: epub/git-going-with-github.epub |
| 5 | + */ |
| 6 | + |
| 7 | +const { execSync } = require('child_process'); |
| 8 | +const fs = require('fs'); |
| 9 | +const path = require('path'); |
| 10 | + |
| 11 | +const ROOT = path.resolve(__dirname, '..'); |
| 12 | +const DOCS = path.join(ROOT, 'docs'); |
| 13 | +const OUT = path.join(ROOT, 'epub', 'git-going-with-github.epub'); |
| 14 | +const METADATA = path.join(ROOT, 'epub', 'metadata.yaml'); |
| 15 | +const CSS = path.join(ROOT, 'epub', 'epub.css'); |
| 16 | + |
| 17 | +// Ordered file list: course-guide first, then 00-16, then appendices a-z |
| 18 | +function getDocFiles() { |
| 19 | + const all = fs.readdirSync(DOCS) |
| 20 | + .filter(f => f.endsWith('.md')) |
| 21 | + .sort(); |
| 22 | + |
| 23 | + const courseGuide = all.filter(f => f === 'course-guide.md'); |
| 24 | + const chapters = all.filter(f => /^\d{2}-/.test(f)); |
| 25 | + const appendices = all.filter(f => f.startsWith('appendix-')); |
| 26 | + const rest = all.filter(f => |
| 27 | + !courseGuide.includes(f) && |
| 28 | + !chapters.includes(f) && |
| 29 | + !appendices.includes(f) |
| 30 | + ); |
| 31 | + |
| 32 | + return [...courseGuide, ...chapters, ...appendices, ...rest] |
| 33 | + .map(f => path.join(DOCS, f)); |
| 34 | +} |
| 35 | + |
| 36 | +const files = getDocFiles(); |
| 37 | + |
| 38 | +console.log(`Building EPUB from ${files.length} files...\n`); |
| 39 | +files.forEach(f => console.log(' ', path.relative(ROOT, f))); |
| 40 | + |
| 41 | +const fileArgs = files.map(f => `"${f}"`).join(' '); |
| 42 | + |
| 43 | +const cmd = [ |
| 44 | + 'pandoc', |
| 45 | + '--from markdown+smart', |
| 46 | + '--to epub3', |
| 47 | + `--output "${OUT}"`, |
| 48 | + `--metadata-file "${METADATA}"`, |
| 49 | + `--css "${CSS}"`, |
| 50 | + '--toc', |
| 51 | + '--toc-depth=2', |
| 52 | + '--split-level=1', |
| 53 | + '--syntax-highlighting=tango', |
| 54 | + '--wrap=none', |
| 55 | + fileArgs |
| 56 | +].join(' \\\n '); |
| 57 | + |
| 58 | +console.log('\nRunning pandoc...\n'); |
| 59 | + |
| 60 | +try { |
| 61 | + execSync(cmd, { stdio: 'inherit', cwd: ROOT }); |
| 62 | + const size = (fs.statSync(OUT).size / 1024).toFixed(1); |
| 63 | + console.log(`\nDone. EPUB written to: epub/git-going-with-github.epub (${size} KB)`); |
| 64 | +} catch (err) { |
| 65 | + console.error('\nPandoc failed. Is pandoc installed? Run: brew install pandoc'); |
| 66 | + process.exit(1); |
| 67 | +} |
0 commit comments