Skip to content

Commit 9d6f51c

Browse files
authored
Add support for diff pdfs (#161)
* Add support for diff pdfs This change adds new parameters: --diffpdf --diffbase --diffpdflog These parameters control the creation of a pdf diff-document against the provided ref, using latexdiff. * update invocation of --diff in action
1 parent 73d7936 commit 9d6f51c

File tree

4 files changed

+117
-32
lines changed

4 files changed

+117
-32
lines changed

.github/workflows/render-samples.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ jobs:
7676
uses: trustedcomputinggroup/markdown@latest
7777
with:
7878
input-md: guide.tcg
79-
extra-build-options: "--versioned_filenames --pr_number=${{ github.event.number }} --pr_repo=${{ github.repository }} --diff=${{ github.event.pull_request.base.sha }}"
79+
extra-build-options: "--versioned_filenames --pr_number=${{ github.event.number }} --pr_repo=${{ github.repository }} --diffbase=${{ github.event.pull_request.base.sha }}"
8080
output-pdf: guide.pdf
8181
output-tex: guide.tex
8282
output-docx: guide.docx

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@
88
*.lot
99
*.toc
1010
*.fdb_latexmk
11+
*.upa
12+
*.upb

Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ ENV PATH="/usr/local/texlive/bin/aarch64-linux:/usr/local/texlive/bin/x86_64-lin
151151

152152
# Packages that are needed despite not being used explicitly by the template:
153153
# bigfoot, catchfile, fancyvrb, footmisc, hardwrap, lineno, ltablex, latexmk, needspace, pgf, zref
154+
# Package dependencies introduced by latexdiff:
155+
# changebar, datetime2, latexdiff, listings, marginnote, pdfcomment, soulpos, ulem
154156
RUN tlmgr update --self && tlmgr install \
155157
accsupp \
156158
adjustbox \
@@ -165,6 +167,8 @@ RUN tlmgr update --self && tlmgr install \
165167
booktabs \
166168
caption \
167169
catchfile \
170+
changebar \
171+
datetime2 \
168172
draftwatermark \
169173
enumitem \
170174
etoolbox \
@@ -181,29 +185,35 @@ RUN tlmgr update --self && tlmgr install \
181185
hyperref \
182186
hyphenat \
183187
koma-script \
188+
latexdiff \
184189
latexmk \
185190
lineno \
191+
listings \
186192
ltablex \
187193
lualatex-math \
188194
luatex \
189195
luatex85 \
190196
luatexbase \
191197
makecell \
198+
marginnote \
192199
mathtools \
193200
mdframed \
194201
microtype \
195202
multirow \
196203
needspace \
197204
newunicodechar \
198205
pagecolor \
206+
pdfcomment \
199207
pdflscape \
200208
pgf \
201209
polyglossia \
202210
ragged2e \
203211
selnolig \
204212
setspace \
213+
soulpos \
205214
textpos \
206215
titling \
216+
ulem \
207217
unicode-math \
208218
upquote \
209219
varwidth \

build.sh

Lines changed: 104 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ RESOURCE_DIR="/" #default to root of pandoc container buildout
44
DO_GITVERSION="yes"
55
DO_GITSTATUS="yes"
66
PDF_OUTPUT=""
7+
DIFFPDF_OUTPUT=""
78
DOCX_OUTPUT=""
89
HTML_OUTPUT=""
910
LATEX_OUTPUT=""
@@ -41,7 +42,9 @@ print_usage() {
4142
echo " --latex=output: enable output of latex and specify the output file name."
4243
echo " --html=output: enable output of html and specify the output file name."
4344
echo " --pdflog=output: enable logging of pdf engine and specify the output file name."
44-
echo " --diff=commit: create diff documents against the provided commit"
45+
echo " --diffpdf=output: enable output of pdf diff and specify the output file name (requires --diffpdf)"
46+
echo " --diffbase=ref: create diff documents against the provided commit (no effect if --diffpdf is not provided)"
47+
echo " --diffpdflog=output: enable logging of pdf engine during diffing and specify the output file name."
4548
echo
4649
echo "Miscellaneous"
4750
echo " --resourcedir=dir: Set the resource directory, defaults to root for pandoc containers"
@@ -57,7 +60,7 @@ print_usage() {
5760
}
5861

5962

60-
if ! options=$(getopt --longoptions=help,puppeteer,gitversion,gitstatus,nogitversion,table_rules,plain_quotes,versioned_filenames,pr_number:,pr_repo:,diff:,pdf:,latex:,pdflog:,pdf_engine:,docx:,html:,resourcedir: --options="" -- "$@"); then
63+
if ! options=$(getopt --longoptions=help,puppeteer,gitversion,gitstatus,nogitversion,table_rules,plain_quotes,versioned_filenames,pr_number:,pr_repo:,diffbase:,pdf:,diffpdf:,diffpdflog:,latex:,pdflog:,pdf_engine:,docx:,html:,resourcedir: --options="" -- "$@"); then
6164
echo "Incorrect options provided"
6265
print_usage
6366
exit 1
@@ -66,7 +69,7 @@ fi
6669
eval set -- "${options}"
6770
while true; do
6871
case "$1" in
69-
--diff)
72+
--diffbase)
7073
DIFFBASE="${2}"
7174
shift 2
7275
;;
@@ -115,6 +118,14 @@ while true; do
115118
PDF_OUTPUT="${2}"
116119
shift 2
117120
;;
121+
--diffpdf)
122+
DIFFPDF_OUTPUT="${2}"
123+
shift 2
124+
;;
125+
--diffpdflog)
126+
DIFFPDFLOG_OUTPUT="${2}"
127+
shift 2
128+
;;
118129
--html)
119130
HTML_OUTPUT="${2}"
120131
shift 2
@@ -157,6 +168,7 @@ readonly PR_REPO
157168
readonly DIFFBASE
158169
readonly PDF_ENGINE
159170
readonly PDFLOG_OUTPUT
171+
readonly DIFFPDFLOG_OUTPUT
160172

161173
shift "$(( OPTIND - 1 ))"
162174

@@ -198,6 +210,25 @@ mkdir -p "${BUILD_DIR}"
198210
cp -r . "${BUILD_DIR}"
199211
cd "${BUILD_DIR}"
200212

213+
# Let git work
214+
git config --global --add safe.directory "${BUILD_DIR}"
215+
216+
# make sure the diff arguments make sense
217+
if [ -n "${DIFFPDF_OUTPUT}" ]; then
218+
# --diff must be provided, and it must make sense to Git
219+
if [ -z "${DIFFBASE}" ]; then
220+
>&2 echo "--diffpdf was provided, but --diffbase was not."
221+
print_usage
222+
exit 1
223+
fi
224+
git rev-parse --verify "${DIFFBASE}" > /dev/null 2>&1
225+
if [ $? -ne 0 ]; then
226+
>&2 echo "--diffbase was provided, but it was not a valid Git commit, tag, or branch name."
227+
print_usage
228+
exit 1
229+
fi
230+
fi
231+
201232
# Get the default browser
202233
if ! browser=$(command -v "chromium-browser"); then
203234
if ! browser=$(command -v "chromium"); then
@@ -210,8 +241,6 @@ fi
210241
# figure out git version and revision if needed.
211242
EXTRA_PANDOC_OPTIONS=""
212243
if test "${DO_GITVERSION}" == "yes"; then
213-
git config --global --add safe.directory /workspace
214-
215244
# TODO: Should we fail if dirty?
216245
raw_version="$(git describe --always --tags)"
217246
echo "Git version: ${raw_version}"
@@ -348,6 +377,9 @@ if [ "${VERSIONED_FILENAMES}" == "yes" ]; then
348377
if [ ! -z "${PDF_OUTPUT}" ]; then
349378
PDF_OUTPUT=$(prefix_filename "${version_prefix}" "${PDF_OUTPUT}")
350379
fi
380+
if [ ! -z "${DIFFPDF_OUTPUT}" ]; then
381+
DIFFPDF_OUTPUT=$(prefix_filename "${DIFFBASE}_to_${version_prefix}" "${DIFFPDF_OUTPUT}")
382+
fi
351383
if [ ! -z "${LATEX_OUTPUT}" ]; then
352384
LATEX_OUTPUT=$(prefix_filename "${version_prefix}" "${LATEX_OUTPUT}")
353385
fi
@@ -356,6 +388,7 @@ if [ "${VERSIONED_FILENAMES}" == "yes" ]; then
356388
fi
357389
fi
358390
readonly PDF_OUTPUT
391+
readonly DIFFPDF_OUTPUT
359392
readonly DOCX_OUTPUT
360393
readonly HTML_OUTPUT
361394
readonly LATEX_OUTPUT
@@ -364,16 +397,15 @@ echo "Starting Build with"
364397
echo "file: ${INPUT_FILE}"
365398
echo "docx: ${DOCX_OUTPUT:-none}"
366399
echo "pdf: ${PDF_OUTPUT:-none} (engine: ${PDF_ENGINE})"
400+
echo "diff pdf: ${DIFFPDF_OUTPUT:-none} (engine: ${PDF_ENGINE})"
367401
echo "latex: ${latex_ouput:-none}"
368402
echo "html: ${html_ouput:-none}"
369403
echo "resource dir: ${RESOURCE_DIR}"
370404
echo "build dir: ${BUILD_DIR}"
371405
echo "browser: ${browser}"
372406
echo "use git version: ${DO_GITVERSION}"
373-
echo "use table rules: ${TABLE_RULES}"
374-
echo "make block quotes Informative Text: ${BLOCK_QUOTES_ARE_INFORMATIVE_TEXT}"
375407
if [ ! -z "${DIFFBASE}" ]; then
376-
echo "diff against: ${DIFFBASE}"
408+
echo "diff against: ${DIFFBASE} ($(git rev-parse --verify ${DIFFBASE}))"
377409
fi
378410
if test "${DO_GITVERSION}" == "yes"; then
379411
echo "Git Generated Document Version Information"
@@ -418,18 +450,27 @@ if [ "${BLOCK_QUOTES_ARE_INFORMATIVE_TEXT}" == "yes" ]; then
418450
EXTRA_PANDOC_OPTIONS+=" --lua-filter=informative-quote-blocks.lua"
419451
fi
420452

421-
# Hacks
422-
423-
# \newpage is rendered as the string "\newpage" in GitHub markdown.
424-
# Transform horizontal rules into \newpages.
425-
# Exception: the YAML front matter of the document, so undo the instance on the first line.
426-
# TODO: Turn this into a Pandoc filter.
427-
sed -i.bak 's/^---$/\\newpage/g;1s/\\newpage/---/g' "${BUILD_DIR}/${INPUT_FILE}"
428-
429-
# Transform sections before the table of contents into section*, which does not number them.
430-
# While we're doing this, transform the case to all-caps.
431-
# TODO: Turn this into a Pandoc filter.
432-
sed -i.bak '0,/\\tableofcontents/s/^# \(.*\)/\\section*\{\U\1\}/g' "${BUILD_DIR}/${INPUT_FILE}"
453+
# Use sed to perform some basic fixups on certain input files.
454+
do_md_fixups() {
455+
local input=$1
456+
# \newpage is rendered as the string "\newpage" in GitHub markdown.
457+
# Transform horizontal rules into \newpages.
458+
# Exception: the YAML front matter of the document, so undo the instance on the first line.
459+
# TODO: Turn this into a Pandoc filter.
460+
sed -i.bak 's/^---$/\\newpage/g;1s/\\newpage/---/g' "${input}"
461+
462+
# Transform sections before the table of contents into section*, which does not number them.
463+
# While we're doing this, transform the case to all-caps.
464+
# TODO: Turn this into a Pandoc filter.
465+
sed -i.bak '0,/\\tableofcontents/s/^# \(.*\)/\\section*\{\U\1\}/g' "${input}"
466+
}
467+
do_tex_fixups() {
468+
local input=$1
469+
# We have a "code" enviroment that displays everything, including comments.
470+
# Sometimes latexdiff injects comments that it thinks won't be displayed.
471+
# Delete those latexdiff comments.
472+
sed -i.bak 's/%DIFDELCMD.*//g' "${input}"
473+
}
433474

434475
if test "${DO_GITVERSION}" == "yes"; then
435476
# If using the git information for versioning, grab the date from there
@@ -565,7 +606,7 @@ do_pdf() {
565606

566607
local logfile=$3
567608
# LaTeX engines choose this filename based on TEMP_TEX_FILE's basename. It also emits a bunch of other files.
568-
readonly temp_pdf_file="$(basename ${input%.*}).pdf"
609+
local temp_pdf_file="$(basename ${input%.*}).pdf"
569610

570611
echo "Rendering PDF"
571612
local start=$(date +%s)
@@ -580,16 +621,15 @@ do_pdf() {
580621
# Write any LaTeX errors to stderr.
581622
>&2 grep -A 5 "] ! " "${logfile}"
582623

583-
# Copy aux, lof, lot, and toc files back to the source directory so they can be cached and speed up future runs.
584-
if [ -n "${PDFLOG_OUTPUT}" ]; then
585-
cp "${logfile}" "${SOURCE_DIR}/${PDFLOG_OUTPUT}"
586-
fi
587-
cp *.aux "${SOURCE_DIR}"
588-
cp *.lof "${SOURCE_DIR}"
589-
cp *.lot "${SOURCE_DIR}"
590-
cp *.toc "${SOURCE_DIR}"
624+
# Copy aux, lof, lot, toc, upa, and upb files (if any) back to the source directory so they can be cached and speed up future runs.
625+
cp *.aux "${SOURCE_DIR}" 2>/dev/null
626+
cp *.lof "${SOURCE_DIR}" 2>/dev/null
627+
cp *.lot "${SOURCE_DIR}" 2>/dev/null
628+
cp *.toc "${SOURCE_DIR}" 2>/dev/null
629+
cp *.upa "${SOURCE_DIR}" 2>/dev/null
630+
cp *.upb "${SOURCE_DIR}" 2>/dev/null
591631
# Copy converted images so they can be cached as well.
592-
cp *.convert.pdf "${SOURCE_DIR}"
632+
cp *.convert.pdf "${SOURCE_DIR}" 2>/dev/null
593633
echo "Elapsed time: $(($end-$start)) seconds"
594634
# Write any LaTeX errors to stderr.
595635
>&2 grep -A 5 "! " "${logfile}"
@@ -700,7 +740,8 @@ do_html() {
700740
# Generate .tex output if either latex or pdf formats were requested, because
701741
# the .tex is an intermediate requirement to the pdf.
702742
readonly TEMP_TEX_FILE="${BUILD_DIR}/${INPUT_FILE}.tex"
703-
if [ -n "${PDF_OUTPUT}" -o -n "${LATEX_OUTPUT}" ]; then
743+
if [ -n "${PDF_OUTPUT}" -o -n "${LATEX_OUTPUT}" -o -n "${DIFFPDF_OUTPUT}" ]; then
744+
do_md_fixups "${BUILD_DIR}/${INPUT_FILE}"
704745
do_latex "${BUILD_DIR}/${INPUT_FILE}" "${TEMP_TEX_FILE}"
705746
fi
706747
if [ -n "${LATEX_OUTPUT}" ]; then
@@ -711,6 +752,12 @@ fi
711752
readonly LATEX_LOG="${BUILD_DIR}/latex.log"
712753
if [ -n "${PDF_OUTPUT}" ]; then
713754
do_pdf "${TEMP_TEX_FILE}" "${SOURCE_DIR}/${PDF_OUTPUT}" "${LATEX_LOG}"
755+
756+
# Copy the logs, if requested.
757+
if [ -n "${PDFLOG_OUTPUT}" ]; then
758+
mkdir -p "$(dirname ${SOURCE_DIR}/${PDFLOG_OUTPUT})"
759+
cp "${LATEX_LOG}" "${SOURCE_DIR}/${PDFLOG_OUTPUT}"
760+
fi
714761
fi
715762

716763
# Generate the docx output
@@ -724,6 +771,32 @@ if [ -n "${HTML_OUTPUT}" ]; then
724771
do_html "${BUILD_DIR}/${INPUT_FILE}" "${SOURCE_DIR}/${HTML_OUTPUT}"
725772
fi
726773

774+
# Generate the diff output
775+
# Do this last so we can do whatever we want to the build directory
776+
readonly TEMP_DIFFBASE_TEX_FILE="${BUILD_DIR}/${INPUT_FILE}.diffbase.tex"
777+
readonly TEMP_DIFF_TEX_FILE="${BUILD_DIR}/${INPUT_FILE}.diff.tex"
778+
readonly TEMP_LATEXDIFF_LOG="${BUILD_DIR}/latexdiff.log"
779+
export MERMAID_FILTER_FORMAT="pdf"
780+
if [ -n "${DIFFPDF_OUTPUT}" ]; then
781+
git reset --hard ${DIFFBASE}
782+
783+
do_md_fixups "${BUILD_DIR}/${INPUT_FILE}"
784+
do_latex "${BUILD_DIR}/${INPUT_FILE}" "${TEMP_DIFFBASE_TEX_FILE}"
785+
latexdiff --type PDFCOMMENT --driver "${PDF_ENGINE}" "${TEMP_DIFFBASE_TEX_FILE}" "${TEMP_TEX_FILE}" > "${TEMP_DIFF_TEX_FILE}" 2>"${TEMP_LATEXDIFF_LOG}"
786+
do_tex_fixups "${TEMP_DIFF_TEX_FILE}"
787+
do_pdf "${TEMP_DIFF_TEX_FILE}" "${SOURCE_DIR}/${DIFFPDF_OUTPUT}" "${LATEX_LOG}"
788+
789+
# Copy the logs, if requested. Note that this file gets the latexdiff and PDF driver output.
790+
if [ -n "${DIFFPDFLOG_OUTPUT}" ]; then
791+
mkdir -p "$(dirname ${SOURCE_DIR}/${DIFFPDFLOG_OUTPUT})"
792+
echo "latexdiff output:" > "${SOURCE_DIR}/${DIFFPDFLOG_OUTPUT}"
793+
cat "${TEMP_LATEXDIFF_LOG}" >> "${SOURCE_DIR}/${DIFFPDFLOG_OUTPUT}"
794+
echo "" >> "${SOURCE_DIR}/${DIFFPDFLOG_OUTPUT}"
795+
echo "${PDF_ENGINE} output:" >> "${SOURCE_DIR}/${DIFFPDFLOG_OUTPUT}"
796+
cat "${LATEX_LOG}" >> "${SOURCE_DIR}/${DIFFPDFLOG_OUTPUT}"
797+
fi
798+
fi
799+
727800
if [ "${FAILED}" = "true" ]; then
728801
echo "Overall workflow failed"
729802
exit 1

0 commit comments

Comments
 (0)