1
1
#! /usr/bin/env bash
2
2
3
- IS_TMP=" yes" # default to no tmp directory
4
3
RESOURCE_DIR=" /" # default to root of pandoc container buildout
5
4
DO_GITVERSION=" yes"
6
5
DO_GITSTATUS=" yes"
@@ -20,8 +19,8 @@ dbus-daemon --system || echo "Failed to start dbus daemon"
20
19
21
20
# Setup an EXIT handler
22
21
on_exit () {
23
- if [[ " ${IS_TMP} " == " yes " && -e " ${build_dir } " ]]; then
24
- rm -rf " ${build_dir } "
22
+ if [[ -e " ${BUILD_DIR } " ]]; then
23
+ rm -rf " ${BUILD_DIR } "
25
24
fi
26
25
}
27
26
@@ -36,7 +35,7 @@ print_usage() {
36
35
echo
37
36
echo " Options:"
38
37
echo
39
- echo " Output Control"
38
+ echo " Output Control (note that output file names are always relative to the current directory) "
40
39
echo " --docx=output: enable output of docx and specify the output file name."
41
40
echo " --pdf=output: enable output of pdf and specify the output file name."
42
41
echo " --latex=output: enable output of latex and specify the output file name."
@@ -46,7 +45,6 @@ print_usage() {
46
45
echo
47
46
echo " Miscellaneous"
48
47
echo " --resourcedir=dir: Set the resource directory, defaults to root for pandoc containers"
49
- echo " --notmp: Do not use a tempory directory for processing steps, instead create a directory called \" build\" in CWD"
50
48
echo " --gitversion: legacy flag, no effect (default starting with 0.9.0)"
51
49
echo " --gitstatus: legacy flag, no effect (default starting with 0.9.0)"
52
50
echo " --nogitversion: Do not use git to describe the generate document version and revision metadata."
@@ -59,7 +57,7 @@ print_usage() {
59
57
}
60
58
61
59
62
- if ! options=$( getopt --longoptions=help,puppeteer,notmp, gitversion,gitstatus,nogitversion,table_rules,plain_quotes,versioned_filenames,pr_number:,pr_repo:,diff:,pdf:,latex:,pdflog:,pdf_engine:,docx:,html:,resourcedir: --options=" " -- " $@ " ) ; then
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
61
echo " Incorrect options provided"
64
62
print_usage
65
63
exit 1
@@ -97,10 +95,6 @@ while true; do
97
95
# legacy option; just ignore this
98
96
shift
99
97
;;
100
- --notmp)
101
- IS_TMP=" no"
102
- shift
103
- ;;
104
98
--docx)
105
99
DOCX_OUTPUT=" ${2} "
106
100
shift 2
@@ -154,7 +148,6 @@ while true; do
154
148
done
155
149
156
150
# Mark globals set from the command line as readonly when we're done updating them.
157
- readonly IS_TMP
158
151
readonly RESOURCE_DIR
159
152
readonly DO_GITVERSION
160
153
readonly DO_GITSTATUS
@@ -193,13 +186,15 @@ if [ "${PDF_ENGINE}" != "xelatex" -a "${PDF_ENGINE}" != "lualatex" ]; then
193
186
exit 1
194
187
fi
195
188
196
- # Set up the output directory, either tmp or build in pwd.
197
- if [ " ${IS_TMP} " == " yes" ]; then
198
- build_dir=" /tmp/tcg.pandoc"
199
- else
200
- build_dir=" $( pwd) /build"
201
- fi
202
- mkdir -p " ${build_dir} "
189
+ # Set up the build directory.
190
+ readonly BUILD_DIR=" /tmp/tcg.pandoc"
191
+ readonly SOURCE_DIR=$( pwd)
192
+ mkdir -p " ${BUILD_DIR} "
193
+ # Copy everything into the build directory, then cd to that directory.
194
+ # This will allow us to manipulate the Git state without side effects
195
+ # to callers of docker_run.
196
+ cp -r . " ${BUILD_DIR} "
197
+ cd " ${BUILD_DIR} "
203
198
204
199
# Get the default browser
205
200
if ! browser=$( command -v " chromium-browser" ) ; then
@@ -369,9 +364,8 @@ echo "docx: ${DOCX_OUTPUT:-none}"
369
364
echo " pdf: ${PDF_OUTPUT:- none} (engine: ${PDF_ENGINE} )"
370
365
echo " latex: ${latex_ouput:- none} "
371
366
echo " html: ${html_ouput:- none} "
372
- echo " use tmp: ${IS_TMP} "
373
367
echo " resource dir: ${RESOURCE_DIR} "
374
- echo " build dir: ${build_dir } "
368
+ echo " build dir: ${BUILD_DIR } "
375
369
echo " browser: ${browser} "
376
370
echo " use git version: ${DO_GITVERSION} "
377
371
echo " use table rules: ${TABLE_RULES} "
@@ -422,21 +416,18 @@ if [ "${BLOCK_QUOTES_ARE_INFORMATIVE_TEXT}" == "yes" ]; then
422
416
extra_pandoc_options+=" --lua-filter=informative-quote-blocks.lua"
423
417
fi
424
418
425
- mkdir -p " ${build_dir} /$( dirname ${input_file} ) "
426
- cp " ${input_file} " " ${build_dir} /${input_file} "
427
-
428
419
# Hacks
429
420
430
421
# \newpage is rendered as the string "\newpage" in GitHub markdown.
431
422
# Transform horizontal rules into \newpages.
432
423
# Exception: the YAML front matter of the document, so undo the instance on the first line.
433
424
# TODO: Turn this into a Pandoc filter.
434
- sed -i.bak ' s/^---$/\\newpage/g;1s/\\newpage/---/g' " ${build_dir } /${input_file} "
425
+ sed -i.bak ' s/^---$/\\newpage/g;1s/\\newpage/---/g' " ${BUILD_DIR } /${input_file} "
435
426
436
427
# Transform sections before the table of contents into section*, which does not number them.
437
428
# While we're doing this, transform the case to all-caps.
438
429
# TODO: Turn this into a Pandoc filter.
439
- sed -i.bak ' 0,/\\tableofcontents/s/^# \(.*\)/\\section*\{\U\1\}/g' " ${build_dir } /${input_file} "
430
+ sed -i.bak ' 0,/\\tableofcontents/s/^# \(.*\)/\\section*\{\U\1\}/g' " ${BUILD_DIR } /${input_file} "
440
431
441
432
if test " ${DO_GITVERSION} " == " yes" ; then
442
433
# If using the git information for versioning, grab the date from there
@@ -493,10 +484,10 @@ retry () {
493
484
return 1
494
485
}
495
486
496
- readonly TEMP_TEX_FILE=" ${build_dir } /${input_file} .tex"
487
+ readonly TEMP_TEX_FILE=" ${BUILD_DIR } /${input_file} .tex"
497
488
# LaTeX engines choose this filename based on TEMP_TEX_FILE's basename. It also emits a bunch of other files.
498
489
readonly TEMP_PDF_FILE=" $( basename ${input_file} ) .pdf"
499
- readonly LATEX_LOG=" ${build_dir } /latex.log"
490
+ readonly LATEX_LOG=" ${BUILD_DIR } /latex.log"
500
491
501
492
analyze_latex_logs () {
502
493
local logfile=$1
@@ -520,10 +511,10 @@ analyze_latex_logs() {
520
511
# That way, LaTeX errors on PDF output point to lines that match the .tex.
521
512
if [ -n " ${PDF_OUTPUT} " -o -n " ${LATEX_OUTPUT} " ]; then
522
513
if [ -n " ${PDF_OUTPUT} " ]; then
523
- mkdir -p " $( dirname ${PDF_OUTPUT} ) "
514
+ mkdir -p " ${SOURCE_DIR} / $ ( dirname ${PDF_OUTPUT} ) "
524
515
fi
525
516
if [ -n " ${LATEX_OUTPUT} " ]; then
526
- mkdir -p " $( dirname ${LATEX_OUTPUT} ) "
517
+ mkdir -p " ${SOURCE_DIR} / $ ( dirname ${LATEX_OUTPUT} ) "
527
518
fi
528
519
echo " Generating LaTeX Output"
529
520
start=$( date +%s)
@@ -561,7 +552,7 @@ if [ -n "${PDF_OUTPUT}" -o -n "${LATEX_OUTPUT}" ]; then
561
552
${extra_pandoc_options}
562
553
--to=latex
563
554
--output=" '${TEMP_TEX_FILE} '"
564
- " '${build_dir } /${input_file} '" )
555
+ " '${BUILD_DIR } /${input_file} '" )
565
556
retry 5 " ${cmd[@]} "
566
557
if [ $? -ne 0 ]; then
567
558
FAILED=true
@@ -571,14 +562,14 @@ if [ -n "${PDF_OUTPUT}" -o -n "${LATEX_OUTPUT}" ]; then
571
562
echo " Elapsed time: $(( $end - $start )) seconds"
572
563
573
564
if [ -n " ${LATEX_OUTPUT} " ]; then
574
- cp " ${TEMP_TEX_FILE} " " ${LATEX_OUTPUT} "
565
+ cp " ${TEMP_TEX_FILE} " " ${SOURCE_DIR} / ${ LATEX_OUTPUT}"
575
566
fi
576
567
577
568
if [ -n " ${PDF_OUTPUT} " ]; then
578
569
echo " Rendering PDF"
579
570
start=$( date +%s)
580
- # Run twice to populate aux, lof, lot, toc, then update the page numbers due
581
- # to the impact of populating the lof, lot, toc .
571
+ # latexmk takes care of repeatedly calling the PDF engine. A run may take multiple passes due to the need to
572
+ # update .toc and other files .
582
573
latexmk " ${TEMP_TEX_FILE} " -pdflatex=" ${PDF_ENGINE} " -pdf -diagnostics > " ${LATEX_LOG} "
583
574
if [ $? -ne 0 ]; then
584
575
FAILED=true
@@ -588,20 +579,23 @@ if [ -n "${PDF_OUTPUT}" -o -n "${LATEX_OUTPUT}" ]; then
588
579
# Write any LaTeX errors to stderr.
589
580
>&2 grep -A 5 " ] ! " " ${LATEX_LOG} "
590
581
591
- # Clean up after latexmk. Deliberately leave behind aux, lof, lot, and toc to speed up future runs.
592
- rm -f * .fls
593
- rm -f * .log
582
+ # Copy aux, lof, lot, and toc files back to the source directory so they can be cached and speed up future runs.
594
583
if [ -n " ${PDFLOG_OUTPUT} " ]; then
595
584
cp " ${LATEX_LOG} " " ${PDFLOG_OUTPUT} "
596
585
fi
586
+ cp * .aux " ${SOURCE_DIR} "
587
+ cp * .lof " ${SOURCE_DIR} "
588
+ cp * .lot " ${SOURCE_DIR} "
589
+ cp * .toc " ${SOURCE_DIR} "
590
+ # Copy converted images so they can be cached as well.
591
+ cp * .convert.pdf " ${SOURCE_DIR} "
597
592
echo " Elapsed time: $(( $end - $start )) seconds"
598
593
# Write any LaTeX errors to stderr.
599
594
>&2 grep -A 5 " ! " " ${LATEX_LOG} "
600
595
if [[ ! " ${FAILED} " = " true" ]]; then
601
- mv " ${TEMP_PDF_FILE} " " ${PDF_OUTPUT} "
596
+ mv " ${TEMP_PDF_FILE} " " ${SOURCE_DIR} / ${ PDF_OUTPUT}"
602
597
analyze_latex_logs " ${LATEX_LOG} "
603
598
fi
604
- rm -f " ${LATEX_LOG} "
605
599
fi
606
600
fi
607
601
@@ -611,7 +605,7 @@ if [ -n "${DOCX_OUTPUT}" ]; then
611
605
subtitle=" Version ${GIT_VERSION:- ${DATE} } , Revision ${GIT_REVISION:- 0} "
612
606
# Prefix the document with a Word page-break, since Pandoc doesn't do docx
613
607
# title pages.
614
- cat << - 'EOF ' > "${build_dir }/${input_file}.prefixed"
608
+ cat << - 'EOF ' > "${BUILD_DIR }/${input_file}.prefixed"
615
609
```{=openxml}
616
610
<w:p>
617
611
<w:r>
@@ -620,9 +614,9 @@ if [ -n "${DOCX_OUTPUT}" ]; then
620
614
</w:p>
621
615
```
622
616
EOF
623
- cat ${build_dir } /${input_file} >> ${build_dir } /${input_file} .prefixed
617
+ cat ${BUILD_DIR } /${input_file} >> ${BUILD_DIR } /${input_file} .prefixed
624
618
625
- mkdir -p " $( dirname ${DOCX_OUTPUT} ) "
619
+ mkdir -p " ${SOURCE_DIR} / $ ( dirname ${DOCX_OUTPUT} ) "
626
620
echo " Generating DOCX Output"
627
621
cmd=(pandoc
628
622
--embed-resources
@@ -642,8 +636,8 @@ if [ -n "${DOCX_OUTPUT}" ]; then
642
636
--reference-doc=/resources/templates/tcg.docx
643
637
${extra_pandoc_options}
644
638
--to=docx
645
- --output=" '${DOCX_OUTPUT} '"
646
- " '${build_dir } /${input_file} .prefixed'" )
639
+ --output=" '${SOURCE_DIR} / ${ DOCX_OUTPUT} '"
640
+ " '${BUILD_DIR } /${input_file} .prefixed'" )
647
641
retry 5 " ${cmd[@]} "
648
642
if [ $? -ne 0 ]; then
649
643
FAILED=true
656
650
# Generate the html output
657
651
export MERMAID_FILTER_FORMAT=" svg"
658
652
if [ -n " ${HTML_OUTPUT} " ]; then
659
- mkdir -p " $( dirname ${HTML_OUTPUT} ) "
653
+ mkdir -p " ${SOURCE_DIR} / $ ( dirname ${HTML_OUTPUT} ) "
660
654
echo " Generating html Output"
661
655
cmd=(pandoc
662
656
--toc
@@ -689,8 +683,8 @@ if [ -n "${HTML_OUTPUT}" ]; then
689
683
--from=${FROM}
690
684
${extra_pandoc_options}
691
685
--to=html
692
- --output=" '${HTML_OUTPUT} '"
693
- " '${build_dir } /${input_file} '" )
686
+ --output=" '${SOURCE_DIR} / ${ HTML_OUTPUT} '"
687
+ " '${BUILD_DIR } /${input_file} '" )
694
688
retry 5 " ${cmd[@]} "
695
689
if [ $? -ne 0 ]; then
696
690
FAILED=true
709
703
rm -f core
710
704
rm -f mermaid-filter.err .mermaid-config.json
711
705
rm -f .puppeteer.json
712
- rm -f " ${build_dir } /${input_file} .bak"
706
+ rm -f " ${BUILD_DIR } /${input_file} .bak"
713
707
714
708
echo " Overall workflow succeeded"
715
709
exit 0
0 commit comments