Skip to content

Commit 73d7936

Browse files
authored
Fix small bugs and style issues (#160)
1 parent b6fd8c6 commit 73d7936

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

build.sh

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,20 @@ if [ $# -ne 1 ]; then
168168
fi
169169

170170
# input file check
171-
input_file=$1
172-
if [ ! -e "${input_file}" ]; then
173-
>&2 echo "${input_file} does not exist, exiting..."
171+
INPUT_FILE=$1
172+
if [ ! -e "${INPUT_FILE}" ]; then
173+
>&2 echo "${INPUT_FILE} does not exist, exiting..."
174174
exit 1
175175
fi
176176

177+
# at least one output must be requested
177178
if [ -z "${PDF_OUTPUT}${LATEX_OUTPUT}${DOCX_OUTPUT}${HTML_OUTPUT}" ]; then
178179
>&2 echo "Expected --pdf, --docx, --html, or --latex option"
179180
print_usage
180181
exit 1
181182
fi
182183

184+
# the pdf engine must be supported
183185
if [ "${PDF_ENGINE}" != "xelatex" -a "${PDF_ENGINE}" != "lualatex" ]; then
184186
>&2 echo "Unsupported PDF engine '${PDF_ENGINE}', expected one of: xelatex, lualatex"
185187
print_usage
@@ -206,7 +208,7 @@ if ! browser=$(command -v "chromium-browser"); then
206208
fi
207209

208210
# figure out git version and revision if needed.
209-
extra_pandoc_options=""
211+
EXTRA_PANDOC_OPTIONS=""
210212
if test "${DO_GITVERSION}" == "yes"; then
211213
git config --global --add safe.directory /workspace
212214

@@ -269,25 +271,25 @@ if test "${DO_GITVERSION}" == "yes"; then
269271

270272
if [ ! -z "${PR_NUMBER}" ]; then
271273
# In the case of a PR, always just provide the PR number and commit
272-
extra_pandoc_options+=" --metadata=PR_NUMBER:${PR_NUMBER}"
273-
extra_pandoc_options+=" --metadata=revision:${GIT_COMMIT}"
274+
EXTRA_PANDOC_OPTIONS+=" --metadata=PR_NUMBER:${PR_NUMBER}"
275+
EXTRA_PANDOC_OPTIONS+=" --metadata=revision:${GIT_COMMIT}"
274276
status="Pull Request"
275277
if [ ! -z "${PR_REPO}" ]; then
276-
extra_pandoc_options+=" --metadata=PR_REPO_url:https://github.com/${PR_REPO}"
278+
EXTRA_PANDOC_OPTIONS+=" --metadata=PR_REPO_url:https://github.com/${PR_REPO}"
277279
fi
278280
else
279281
# Otherwise, populate the full context based on what git show said.
280-
extra_pandoc_options+=" --metadata=version:${GIT_VERSION}"
282+
EXTRA_PANDOC_OPTIONS+=" --metadata=version:${GIT_VERSION}"
281283

282284
if [ ! -z "${GIT_PRERELEASE}" ]; then
283-
extra_pandoc_options+=" --metadata=prerelease:${GIT_PRERELEASE}"
285+
EXTRA_PANDOC_OPTIONS+=" --metadata=prerelease:${GIT_PRERELEASE}"
284286
fi
285287

286288
# Omit the revision if there isn't one (i.e., we are at straight-up Version)
287289
if [ ! -z "${GIT_REVISION}" ]; then
288-
extra_pandoc_options+=" --metadata=revision:${GIT_REVISION}"
290+
EXTRA_PANDOC_OPTIONS+=" --metadata=revision:${GIT_REVISION}"
289291
elif [ -z "${GIT_VERSION}" ]; then
290-
extra_pandoc_options+=" --metadata=revision:${GIT_COMMIT}"
292+
EXTRA_PANDOC_OPTIONS+=" --metadata=revision:${GIT_COMMIT}"
291293
fi
292294

293295
# Do we set document status based on git version?
@@ -304,7 +306,7 @@ if test "${DO_GITVERSION}" == "yes"; then
304306
fi
305307
fi
306308
fi
307-
extra_pandoc_options+=" --metadata=status:\"${status}\""
309+
EXTRA_PANDOC_OPTIONS+=" --metadata=status:\"${status}\""
308310

309311
fi # Done with git version handling
310312

@@ -359,7 +361,7 @@ readonly HTML_OUTPUT
359361
readonly LATEX_OUTPUT
360362

361363
echo "Starting Build with"
362-
echo "file: ${input_file}"
364+
echo "file: ${INPUT_FILE}"
363365
echo "docx: ${DOCX_OUTPUT:-none}"
364366
echo "pdf: ${PDF_OUTPUT:-none} (engine: ${PDF_ENGINE})"
365367
echo "latex: ${latex_ouput:-none}"
@@ -413,7 +415,7 @@ cat <<- EOF > ./.puppeteer.json
413415
EOF
414416

415417
if [ "${BLOCK_QUOTES_ARE_INFORMATIVE_TEXT}" == "yes" ]; then
416-
extra_pandoc_options+=" --lua-filter=informative-quote-blocks.lua"
418+
EXTRA_PANDOC_OPTIONS+=" --lua-filter=informative-quote-blocks.lua"
417419
fi
418420

419421
# Hacks
@@ -422,19 +424,19 @@ fi
422424
# Transform horizontal rules into \newpages.
423425
# Exception: the YAML front matter of the document, so undo the instance on the first line.
424426
# TODO: Turn this into a Pandoc filter.
425-
sed -i.bak 's/^---$/\\newpage/g;1s/\\newpage/---/g' "${BUILD_DIR}/${input_file}"
427+
sed -i.bak 's/^---$/\\newpage/g;1s/\\newpage/---/g' "${BUILD_DIR}/${INPUT_FILE}"
426428

427429
# Transform sections before the table of contents into section*, which does not number them.
428430
# While we're doing this, transform the case to all-caps.
429431
# TODO: Turn this into a Pandoc filter.
430-
sed -i.bak '0,/\\tableofcontents/s/^# \(.*\)/\\section*\{\U\1\}/g' "${BUILD_DIR}/${input_file}"
432+
sed -i.bak '0,/\\tableofcontents/s/^# \(.*\)/\\section*\{\U\1\}/g' "${BUILD_DIR}/${INPUT_FILE}"
431433

432434
if test "${DO_GITVERSION}" == "yes"; then
433435
# If using the git information for versioning, grab the date from there
434436
DATE="$(git show -s --date=format:'%Y/%m/%d' --format=%ad)"
435437
else
436438
# Else, grab the date from the front matter and generate the full date and year.
437-
DATE="$(grep date: "${input_file}" | head -n 1 | cut -d ' ' -f 2)"
439+
DATE="$(grep date: "${INPUT_FILE}" | head -n 1 | cut -d ' ' -f 2)"
438440
fi
439441

440442
YEAR="$(date --date="${DATE}" +%Y)"
@@ -542,7 +544,7 @@ do_latex() {
542544
--metadata=colorlinks:true
543545
--metadata=contact:[email protected]
544546
--from=${FROM}
545-
${extra_pandoc_options}
547+
${EXTRA_PANDOC_OPTIONS}
546548
--to=latex
547549
--output="'${output}'"
548550
"'${input}'")
@@ -563,24 +565,24 @@ do_pdf() {
563565

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

568570
echo "Rendering PDF"
569-
start=$(date +%s)
571+
local start=$(date +%s)
570572
# latexmk takes care of repeatedly calling the PDF engine. A run may take multiple passes due to the need to
571573
# update .toc and other files.
572574
latexmk "${input}" -pdflatex="${PDF_ENGINE}" -pdf -diagnostics > "${logfile}"
573575
if [ $? -ne 0 ]; then
574576
FAILED=true
575577
echo "PDF output failed"
576578
fi
577-
end=$(date +%s)
579+
local end=$(date +%s)
578580
# Write any LaTeX errors to stderr.
579581
>&2 grep -A 5 "] ! " "${logfile}"
580582

581583
# Copy aux, lof, lot, and toc files back to the source directory so they can be cached and speed up future runs.
582584
if [ -n "${PDFLOG_OUTPUT}" ]; then
583-
cp "${logfile}" "${PDFLOG_OUTPUT}"
585+
cp "${logfile}" "${SOURCE_DIR}/${PDFLOG_OUTPUT}"
584586
fi
585587
cp *.aux "${SOURCE_DIR}"
586588
cp *.lof "${SOURCE_DIR}"
@@ -603,7 +605,7 @@ do_docx() {
603605
local output=$2
604606
mkdir -p "$(dirname ${output})"
605607
# Prepare the title-page for the docx version.
606-
subtitle="Version ${GIT_VERSION:-${DATE}}, Revision ${GIT_REVISION:-0}"
608+
local subtitle="Version ${GIT_VERSION:-${DATE}}, Revision ${GIT_REVISION:-0}"
607609
# Prefix the document with a Word page-break, since Pandoc doesn't do docx
608610
# title pages.
609611
cat <<- 'EOF' > "${input}.prefixed"
@@ -615,7 +617,7 @@ do_docx() {
615617
</w:p>
616618
```
617619
EOF
618-
cat "${BUILD_DIR}/${input_file}" >> "${input}.prefixed"
620+
cat "${BUILD_DIR}/${INPUT_FILE}" >> "${input}.prefixed"
619621

620622
echo "Generating DOCX Output"
621623
cmd=(pandoc
@@ -634,7 +636,7 @@ do_docx() {
634636
--from='${FROM}+raw_attribute'
635637
--metadata=subtitle:"'${subtitle}'"
636638
--reference-doc=/resources/templates/tcg.docx
637-
${extra_pandoc_options}
639+
${EXTRA_PANDOC_OPTIONS}
638640
--to=docx
639641
--output="'${output}'"
640642
"'${input}.prefixed'")
@@ -653,7 +655,7 @@ do_html() {
653655
local output=$2
654656
mkdir -p "$(dirname ${output})"
655657
echo "Generating HTML Output"
656-
cmd=(pandoc
658+
local cmd=(pandoc
657659
--toc
658660
-V colorlinks=true
659661
-V linkcolor=blue
@@ -682,7 +684,7 @@ do_html() {
682684
--metadata=colorlinks:true
683685
--metadata=contact:[email protected]
684686
--from=${FROM}
685-
${extra_pandoc_options}
687+
${EXTRA_PANDOC_OPTIONS}
686688
--to=html
687689
--output="'${output}'"
688690
"'${input}'")
@@ -697,9 +699,9 @@ do_html() {
697699

698700
# Generate .tex output if either latex or pdf formats were requested, because
699701
# the .tex is an intermediate requirement to the pdf.
700-
readonly TEMP_TEX_FILE="${BUILD_DIR}/${input_file}.tex"
702+
readonly TEMP_TEX_FILE="${BUILD_DIR}/${INPUT_FILE}.tex"
701703
if [ -n "${PDF_OUTPUT}" -o -n "${LATEX_OUTPUT}" ]; then
702-
do_latex "${BUILD_DIR}/${input_file}" "${TEMP_TEX_FILE}"
704+
do_latex "${BUILD_DIR}/${INPUT_FILE}" "${TEMP_TEX_FILE}"
703705
fi
704706
if [ -n "${LATEX_OUTPUT}" ]; then
705707
cp "${TEMP_TEX_FILE}" "${SOURCE_DIR}/${LATEX_OUTPUT}"
@@ -713,13 +715,13 @@ fi
713715

714716
# Generate the docx output
715717
if [ -n "${DOCX_OUTPUT}" ]; then
716-
do_docx "${BUILD_DIR}/${input_file}" "${SOURCE_DIR}/${DOCX_OUTPUT}"
718+
do_docx "${BUILD_DIR}/${INPUT_FILE}" "${SOURCE_DIR}/${DOCX_OUTPUT}"
717719
fi
718720

719721
# Generate the html output
720722
export MERMAID_FILTER_FORMAT="svg"
721723
if [ -n "${HTML_OUTPUT}" ]; then
722-
do_html "${BUILD_DIR}/${input_file}" "${SOURCE_DIR}/${HTML_OUTPUT}"
724+
do_html "${BUILD_DIR}/${INPUT_FILE}" "${SOURCE_DIR}/${HTML_OUTPUT}"
723725
fi
724726

725727
if [ "${FAILED}" = "true" ]; then

0 commit comments

Comments
 (0)