返回 html-ppt-skill
render.sh
根目录 / scripts / render.sh
1 #!/usr/bin/env bash
2 # html-ppt :: render.sh — headless Chrome screenshot(s)
3 #
4 # Usage:
5 # render.sh <html-file> # one PNG, slide 1
6 # render.sh <html-file> <N> # N PNGs, slides 1..N, via #/k
7 # render.sh <html-file> all # autodetect .slide count
8 # render.sh <html-file> <N> <out-dir> # custom output dir
9 #
10 # Requires: Google Chrome at /Applications/Google Chrome.app (macOS).
11
12 set -euo pipefail
13
14 CHROME="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
15 if [[ ! -x "$CHROME" ]]; then
16 echo "error: Chrome not found at $CHROME" >&2
17 exit 1
18 fi
19
20 FILE="${1:-}"
21 if [[ -z "$FILE" ]]; then
22 echo "usage: render.sh <html> [N|all] [out-dir]" >&2
23 exit 1
24 fi
25 if [[ ! -f "$FILE" ]]; then
26 echo "error: $FILE not found" >&2
27 exit 1
28 fi
29
30 COUNT="${2:-1}"
31 OUT="${3:-}"
32
33 ABS="$(cd "$(dirname "$FILE")" && pwd)/$(basename "$FILE")"
34 STEM="$(basename "${FILE%.*}")"
35
36 if [[ "$COUNT" == "all" ]]; then
37 COUNT="$(grep -c 'class="slide"' "$FILE" || true)"
38 [[ -z "$COUNT" || "$COUNT" -lt 1 ]] && COUNT=1
39 fi
40
41 if [[ -z "$OUT" ]]; then
42 if [[ "$COUNT" -gt 1 ]]; then
43 OUT="$(dirname "$FILE")/${STEM}-png"
44 mkdir -p "$OUT"
45 fi
46 fi
47
48 render_one() {
49 local url="$1" target="$2"
50 "$CHROME" \
51 --headless=new \
52 --disable-gpu \
53 --hide-scrollbars \
54 --no-sandbox \
55 --virtual-time-budget=4000 \
56 --window-size=1920,1080 \
57 --screenshot="$target" \
58 "$url" >/dev/null 2>&1
59 echo " ✔ $target"
60 }
61
62 if [[ "$COUNT" == "1" ]]; then
63 OUT_FILE="${OUT:-$(dirname "$FILE")/${STEM}.png}"
64 render_one "file://$ABS" "$OUT_FILE"
65 else
66 for i in $(seq 1 "$COUNT"); do
67 render_one "file://$ABS#/$i" "$OUT/${STEM}_$(printf '%02d' "$i").png"
68 done
69 fi
70
71 echo "done: rendered $COUNT slide(s) from $FILE"
72
72 lines BASH