OneWheelGeek

OneWheelGeek / pdfify.sh

Last active 5 months ago

Like 0

OneWheelGeek revised this gist 5 months ago · 24f66a0

1 file changed, 28 insertions, 4 deletions

pdfify.sh
@@ -164,7 +164,8 @@ for arg in "$@"; do
164 164 echo ""
165 165 echo -e "${BOLD}Frontmatter:${RESET}"
166 166 echo -e " title, subtitle, author, header, footer, toc-level, date,"
167 - echo -e " numbersections (true/false), numberfrom (1-4), watermark"
167 + echo -e " numbersections (true/false), numberfrom (1-4), watermark,"
168 + echo -e " pagebreak (true/false — page break before each top-level heading)"
168 169 exit 0 ;;
169 170 *) POSITIONAL+=("$arg") ;;
170 171 esac
@@ -226,7 +227,7 @@ RUN npm install -g @mermaid-js/mermaid-cli
226 227 ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
227 228 ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium
228 229 ENV CHROME_PATH=/usr/bin/chromium
229 - RUN echo '{"maxTextSize": 90000, "flowchart": {"useMaxWidth": true}, "theme": "default"}' > /opt/mermaid-config.json
230 + RUN echo '{"maxTextSize": 90000, "flowchart": {"useMaxWidth": true}, "theme": "base", "themeVariables": {"primaryColor": "#3B82F6", "primaryBorderColor": "#1E40AF", "primaryTextColor": "#1E293B", "lineColor": "#475569", "xyChart": {"backgroundColor": "transparent", "plotColorPalette": "#2563EB,#DC2626,#16A34A,#D97706,#9333EA,#0891B2"}}}' > /opt/mermaid-config.json
230 231 RUN echo '{"args": ["--no-sandbox", "--disable-setuid-sandbox", "--disable-dev-shm-usage", "--disable-gpu"]}' > /opt/puppeteer-config.json
231 232 WORKDIR /work
232 233 ENTRYPOINT ["/bin/bash"]
@@ -324,6 +325,7 @@ convert_file() {
324 325 FM_NUMBERSECTIONS=$(extract_fm "numbersections")
325 326 FM_NUMBERFROM=$(extract_fm "numberfrom")
326 327 FM_WATERMARK=$(extract_fm "watermark")
328 + FM_PAGEBREAK=$(extract_fm "pagebreak")
327 329 fi
328 330
329 331 # --- CLI overrides frontmatter ---
@@ -352,6 +354,8 @@ convert_file() {
352 354 local FILE_TOC_LEVEL="${FM_TOC_LEVEL:-3}"
353 355 local FILE_NUMBERS=1
354 356 [[ "$FM_NUMBERSECTIONS" == "false" ]] && FILE_NUMBERS=0
357 + local FILE_PAGEBREAK=1
358 + [[ "$FM_PAGEBREAK" == "false" ]] && FILE_PAGEBREAK=0
355 359
356 360 # Auto-determine numberfrom based on structure (if not explicitly set)
357 361 local FILE_NUMBER_FROM="${FM_NUMBERFROM:-}"
@@ -593,7 +597,7 @@ while IFS= read -r line || [[ -n "$line" ]]; do
593 597 NEXT_CHAR="${line:${#BREAK_HASHES}:1}"
594 598 if [[ "$NEXT_CHAR" != "#" ]]; then
595 599 H1_COUNT=$((H1_COUNT + 1))
596 - if [[ $H1_COUNT -gt 1 ]]; then
600 + if [[ $H1_COUNT -gt 1 && $FILE_PAGEBREAK -eq 1 ]]; then
597 601 echo "" >> "$BREAK_INJECTED"
598 602 echo '```{=latex}' >> "$BREAK_INJECTED"
599 603 echo '\newpage' >> "$BREAK_INJECTED"
@@ -663,6 +667,15 @@ while IFS= read -r line || [[ -n "$line" ]]; do
663 667 fi
664 668 done < "$CALLOUT_MD"
665 669
670 + # --- Strip YAML frontmatter so pandoc doesn't generate its own title ---
671 + # pdfify already parses frontmatter above; letting pandoc see it causes a
672 + # duplicate title (pandoc's \maketitle + pdfify's custom title banner).
673 + if head -1 "$TEMP_MD" | grep -q '^---'; then
674 + STRIPPED_FM=$(mktemp /tmp/pdfify-nofm-XXXXXX.md)
675 + awk 'NR==1 && /^---/{skip=1; next} skip && /^---/{skip=0; next} !skip' "$TEMP_MD" > "$STRIPPED_FM"
676 + mv "$STRIPPED_FM" "$TEMP_MD"
677 + fi
678 +
666 679 # --- Lua filter: protect brackets in headings for titlesec ---
667 680 # Square brackets in headings break titlesec (\SQSPL@scan error) because LaTeX
668 681 # interprets [ as the start of an optional argument.
@@ -754,7 +767,10 @@ cat > "$PREAMBLE" <<'LATEX'
754 767 % Background on code blocks via mdframed
755 768 \usepackage[framemethod=tikz]{mdframed}
756 769
757 - % Override pandoc's Shaded environment
770 + % Override pandoc's Shaded environment (define first if pandoc didn't)
771 + \makeatletter
772 + \@ifundefined{Shaded}{\newenvironment{Shaded}{}{}}{}
773 + \makeatother
758 774 \renewenvironment{Shaded}{%
759 775 \begin{mdframed}[
760 776 backgroundcolor=codebg,
@@ -915,6 +931,12 @@ cat > "$PREAMBLE" <<'LATEX'
915 931 \usepackage{tabularx}
916 932 \arrayrulecolor{codeborder}
917 933
934 + % Alternating row shading
935 + \definecolor{tablerowgray}{HTML}{F3F4F6}
936 + \let\oldlongtable\longtable
937 + \let\endoldlongtable\endlongtable
938 + \renewenvironment{longtable}{\rowcolors{2}{white}{tablerowgray}\oldlongtable}{\endoldlongtable}
939 +
918 940 % Allow line breaks in table cells and shrink monospace to fit
919 941 \usepackage{array}
920 942 \renewcommand{\arraystretch}{1.4}
@@ -966,6 +988,7 @@ FM_DATE_HASH="${FM_DATE_HASH:-}"
966 988 FM_DATE_DIRTY="${FM_DATE_DIRTY:-}"
967 989 FILE_NUMBERS="${FILE_NUMBERS:-1}"
968 990 FILE_NUMBER_FROM="${FILE_NUMBER_FROM:-2}"
991 + FILE_PAGEBREAK="${FILE_PAGEBREAK:-1}"
969 992
970 993 # Escape LaTeX special characters in text fields (uses sed to avoid
971 994 # bash parameter substitution brace-parsing issues with } in replacements)
@@ -1191,6 +1214,7 @@ docker run --rm \
1191 1214 -e "FILE_NUMBER_FROM=$FILE_NUMBER_FROM" \
1192 1215 -e "HIDE_FIRST_H1=$HIDE_FIRST_H1" \
1193 1216 -e "FM_WATERMARK=$FM_WATERMARK" \
1217 + -e "FILE_PAGEBREAK=$FILE_PAGEBREAK" \
1194 1218 --tmpfs /tmp:exec \
1195 1219 "$IMAGE_NAME" "/work/$CONVERT_BASENAME" "$INPUT_FILE" "/output/$OUTPUT_FILE" \
1196 1220 || {

OneWheelGeek revised this gist 5 months ago · fd1d9c1

1 file changed, 1273 insertions

pdfify.sh (file created)

Diff is too large to be shown