davinci-paper/scripts/postprocess-web-output.sh
scripts/postprocess-web-output.shRaw
1#!/usr/bin/env bash
2set -euo pipefail
3
4if [[ $# -lt 2 ]]; then
5 echo "usage: $0 <output-dir> <file> [file...]" >&2
6 exit 1
7fi
8
9OUTPUT_DIR="$1"
10shift
11MEDIA_DIR="$OUTPUT_DIR/media"
12
13escape_regex() {
14 printf '%s' "$1" | sed -e 's/[][(){}.^$*+?|\\/]/\\&/g'
15}
16
17escape_replacement() {
18 printf '%s' "$1" | sed -e 's/[\\/&]/\\&/g'
19}
20
21replace_path() {
22 local target_file="$1"
23 local from_path="$2"
24 local to_path="$3"
25 local from_escaped to_escaped
26 from_escaped="$(escape_regex "$from_path")"
27 to_escaped="$(escape_replacement "$to_path")"
28 perl -0777 -i -pe "s#${from_escaped}(?=[\\\"'\\)\\s>])#${to_escaped}#g" "$target_file"
29}
30
31# Convert PDFs to PNG for browser-friendly rendering.
32if [[ -d "$MEDIA_DIR" ]] && command -v pdftoppm >/dev/null 2>&1; then
33 while IFS= read -r -d '' pdf_file; do
34 png_file="${pdf_file%.pdf}.png"
35 if [[ ! -f "$png_file" ]]; then
36 pdftoppm -singlefile -png "$pdf_file" "${pdf_file%.pdf}" >/dev/null 2>&1 || true
37 fi
38 done < <(find "$MEDIA_DIR" -type f -name '*.pdf' -print0)
39fi
40
41# Ensure extensionless media files also have stable, browser-friendly suffixes.
42if [[ -d "$MEDIA_DIR" ]] && command -v file >/dev/null 2>&1; then
43 while IFS= read -r -d '' raw_file; do
44 mime_type="$(file -b --mime-type "$raw_file" || true)"
45 case "$mime_type" in
46 image/png) normalized_file="${raw_file}.png" ;;
47 image/jpeg) normalized_file="${raw_file}.jpg" ;;
48 image/webp) normalized_file="${raw_file}.webp" ;;
49 image/svg+xml) normalized_file="${raw_file}.svg" ;;
50 application/pdf) normalized_file="${raw_file}.pdf" ;;
51 text/plain) normalized_file="${raw_file}.txt" ;;
52 *) continue ;;
53 esac
54 [[ -f "$normalized_file" ]] || cp "$raw_file" "$normalized_file"
55 done < <(find "$MEDIA_DIR" -type f ! -name '*.*' -print0)
56fi
57
58for target_file in "$@"; do
59 [[ -f "$target_file" ]] || continue
60
61 # Replace embeds with imgs and drop hard-coded sizing attributes.
62 perl -0777 -i -pe '
63 s#<embed\b#<img#g;
64 s#\sstyle="[^"]*(?:width|height):[^"]*"##g;
65 s#\sstyle='\''[^'\'']*(?:width|height):[^'\'']*'\''##g;
66 s#\swidth="[^"]*"##g;
67 s#\sheight="[^"]*"##g;
68 ' "$target_file"
69
70 # Rewrite .pdf links to .png only where PNG counterparts exist.
71 if [[ -d "$MEDIA_DIR" ]]; then
72 while IFS= read -r -d '' png_file; do
73 rel_png="${png_file#"$MEDIA_DIR"/}"
74 rel_base="${rel_png%.png}"
75 replace_path "$target_file" "media/${rel_base}.pdf" "media/${rel_png}"
76 done < <(find "$MEDIA_DIR" -type f -name '*.png' -print0)
77
78 # Rewrite extensionless media paths to explicit extensions.
79 while IFS= read -r -d '' explicit_file; do
80 rel_explicit="${explicit_file#"$MEDIA_DIR"/}"
81 rel_base="${rel_explicit%.*}"
82 replace_path "$target_file" "media/${rel_base}" "media/${rel_explicit}"
83 done < <(find "$MEDIA_DIR" -type f \( -name '*.png' -o -name '*.jpg' -o -name '*.jpeg' -o -name '*.webp' -o -name '*.svg' -o -name '*.pdf' -o -name '*.txt' \) -print0)
84
85 # If pandoc emitted .so payload links, redirect to PNG when possible.
86 while IFS= read -r -d '' so_file; do
87 rel_so="${so_file#"$MEDIA_DIR"/}"
88 base_file="${so_file%.so}"
89 if [[ -f "${base_file}.png" ]]; then
90 replace_path "$target_file" "media/${rel_so}" "media/${rel_so%.so}.png"
91 elif [[ -f "${base_file}.txt" ]]; then
92 replace_path "$target_file" "media/${rel_so}" "media/${rel_so%.so}.txt"
93 fi
94 done < <(find "$MEDIA_DIR" -type f -name '*.so' -print0)
95 fi
96done
97
98# Remove stale .so blobs from published assets. They are not needed for rendering.
99if [[ -d "$MEDIA_DIR" ]]; then
100 find "$MEDIA_DIR" -type f -name '*.so' -delete
101fi