Skip to content

Allow the template file to be overridden. #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ docker pull ghcr.io/trustedcomputinggroup/pandoc:latest
./docker_run --pdf=guide.pdf guide.tcg
```

Note that the `.tcg` extension is a convention and is not required.

## How to Build Locally

You may wish to send a PR to this repository, to add a feature or fix an issue with the tools.
Expand All @@ -60,3 +62,28 @@ docker build --tag working .

DOCKER_IMAGE=working:latest ./docker_run --pdf=guide.pdf guide.tcg
```

## How to customize the template

You may wish to provide your own LaTeX template, as well as other resources like images or style documents.
Suppose you have a directory `my_resource_dir` with the following contents:

- `img/cover_page_background.png`
- `reference_doc.docx`
- `reference_style.csl`
- `template.tex`

`template.tex` and any input files can refer to files in `resource_dir`, for example
by using `\includegraphics{extra/my_resource_dir/img/cover_page_background.png}`.

You can then run the following:

```sh
./docker_run \
--extra_resource_dir path/to/resource_dir \
--template extra/my_resource_dir/template.tex \
--reference_doc extra/my_resource_dir/reference_doc.docx \
--csl extra/my_resource_dir/reference_style.csl \
--pdf output.pdf \
input.tcg
```
41 changes: 35 additions & 6 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ DIFFBASE=""
PDF_ENGINE=xelatex
CROSSREF_TYPE="iso"
DO_AUTO_BACKMATTER="yes"
TEMPLATE="tcg.tex"
CSL=""


# Start up the dbus daemon (drawio will use it later)
Expand All @@ -35,12 +37,15 @@ print_usage() {
echo "Usage:"
echo "$(basename "${0}") [options] [input-file]"
echo
echo "If a volume is mapped to `/extra_resources/{dir}`, input files may reference its contents as `extra/{dir}`."
echo
echo "Arguments:"
echo " This script takes a single markdown file input for rendering to docx/pdf/LaTeX."
echo
echo "Options:"
echo
echo "Output Control (note that output file names are always relative to the current directory)"
echo " --template=template.tex: override the template."
echo " --docx=output: enable output of docx and specify the output file name."
echo " --crossref=(iso|tcg): set cross-reference style."
echo " --pdf=output: enable output of pdf and specify the output file name."
Expand All @@ -53,6 +58,7 @@ print_usage() {
echo " --diffpdflog=output: enable logging of pdf engine during diffing and specify the output file name."
echo
echo "Miscellaneous"
echo " --reference_doc=path: Override the reference doc for building docx"
echo " --resourcedir=dir: Set the resource directory, defaults to root for pandoc containers"
echo " --gitversion: legacy flag, no effect (default starting with 0.9.0)"
echo " --gitstatus: legacy flag, no effect (default starting with 0.9.0)"
Expand All @@ -64,10 +70,11 @@ print_usage() {
echo " --pr_repo=url: provide the URL for the repository for pull-request drafts (has no effect if --PR_NUMBER is not passed)."
echo " --pdf_engine=(xelatex|lualatex): use the given latex engine (default xelatex)"
echo " --noautobackmatter: don't automatically insert back matter at the end of the document."
echo " --csl: provide a csl file via the command line (instead of in YAML metadata)."
}


if ! options=$(getopt --longoptions=help,puppeteer,gitversion,gitstatus,nogitversion,table_rules,plain_quotes,versioned_filenames,pr_number:,pr_repo:,diffbase:,pdf:,diffpdf:,difftex:,diffpdflog:,latex:,pdflog:,pdf_engine:,docx:,crossref:,html:,resourcedir:,noautobackmatter --options="" -- "$@"); then
if ! options=$(getopt --longoptions=help,puppeteer,gitversion,gitstatus,nogitversion,table_rules,plain_quotes,versioned_filenames,pr_number:,pr_repo:,diffbase:,pdf:,diffpdf:,difftex:,diffpdflog:,latex:,pdflog:,pdf_engine:,template:,reference_doc:,docx:,crossref:,html:,resourcedir:,noautobackmatter,csl: --options="" -- "$@"); then
echo "Incorrect options provided"
print_usage
exit 1
Expand Down Expand Up @@ -145,6 +152,14 @@ while true; do
HTML_OUTPUT="${2}"
shift 2
;;
--template)
TEMPLATE="${2}"
shift 2
;;
--reference_doc)
REFERENCE_DOC="${2}"
shift 2
;;
--resourcedir)
RESOURCE_DIR="${2}"
shift 2
Expand All @@ -165,6 +180,10 @@ while true; do
DO_AUTO_BACKMATTER="no"
shift
;;
--csl)
CSL="${2}"
shift 2
;;
--help)
print_usage
shift
Expand All @@ -187,6 +206,7 @@ readonly PR_REPO
readonly DIFFBASE
readonly PDF_ENGINE
readonly CROSSREF_TYPE
readonly CSL

shift "$(( OPTIND - 1 ))"

Expand Down Expand Up @@ -226,6 +246,10 @@ mkdir -p "${BUILD_DIR}"
# This will allow us to manipulate the Git state without side effects
# to callers of docker_run.
cp -r . "${BUILD_DIR}"
if [[ -d /extra_resources ]]; then
mkdir -p "${BUILD_DIR}/extra"
cp -r /extra_resources/* "${BUILD_DIR}/extra"
fi
cd "${BUILD_DIR}"

# Let git work
Expand Down Expand Up @@ -366,6 +390,10 @@ if test "${DO_GITVERSION}" == "yes"; then

fi # Done with git version handling

if [ -n "${CSL}" ]; then
EXTRA_PANDOC_OPTIONS+=" --csl=${CSL}"
fi

prefix_filename() {
local prefix=$1
local full_filename=$2
Expand Down Expand Up @@ -444,6 +472,7 @@ echo "resource dir: ${RESOURCE_DIR}"
echo "build dir: ${BUILD_DIR}"
echo "browser: ${browser}"
echo "use git version: ${DO_GITVERSION}"
echo "default csl: ${CSL}"
if [ ! -z "${DIFFBASE}" ]; then
echo "diff against: ${DIFFBASE} ($(git rev-parse --verify ${DIFFBASE}))"
fi
Expand Down Expand Up @@ -668,7 +697,7 @@ do_latex() {
local cmd=(pandoc
--standalone
--no-highlight
--template=tcg.tex
--template=${TEMPLATE}
--lua-filter=mermaid-filter.lua
--lua-filter=aasvg-filter.lua
--lua-filter=informative-sections.lua
Expand All @@ -682,7 +711,7 @@ do_latex() {
--citeproc
--lua-filter=tabularx.lua
--lua-filter=divide-code-blocks.lua
--resource-path=.:/resources
--resource-path=.:/resources:${RESOURCE_DIR}
--data-dir=/resources
--top-level-division=section
--variable=block-headings
Expand Down Expand Up @@ -788,11 +817,11 @@ do_docx() {
--lua-filter=landscape-pages.lua
--lua-filter=style-fenced-divs.lua
--filter=pandoc-crossref
--resource-path=.:/resources
--resource-path=.:/resources:${RESOURCE_DIR}
--data-dir=/resources
--from='${FROM}+raw_attribute'
--metadata=subtitle:"'${subtitle}'"
--reference-doc=/resources/templates/tcg.docx
--reference-doc=${REFERENCE_DOC}
${EXTRA_PANDOC_OPTIONS}
--to=docx
--output="'${output}'"
Expand Down Expand Up @@ -829,7 +858,7 @@ do_html() {
--filter=pandoc-crossref
--lua-filter=divide-code-blocks.lua
--lua-filter=style-fenced-divs.lua
--resource-path=.:/resources
--resource-path=.:/resources:${RESOURCE_DIR}
--data-dir=/resources
--top-level-division=section
--variable=block-headings
Expand Down
33 changes: 32 additions & 1 deletion docker_run
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,36 @@ if test "${1}" == "--help"; then
exit 0
fi

EXTRA_RESOURCE_DIR=""

declare -a pass_through_args=()

while [[ $# -gt 0 ]]; do
arg="$1"
case "$arg" in
--extra_resource_dir)
if [[ -n "$2" ]]; then
EXTRA_RESOURCE_DIR="$2"
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;

*)
docker_container_args+=("$arg")
shift
;;
esac
done

declare -a docker_run_args=("--workdir=/workspace" "--volume=$(pwd):/workspace")

if [[ -n "${EXTRA_RESOURCE_DIR}" && -d "${EXTRA_RESOURCE_DIR}" ]]; then
dir=$(basename "${EXTRA_RESOURCE_DIR}")
docker_run_args+=("--volume=${EXTRA_RESOURCE_DIR}:/extra_resources/${dir}")
fi

echo "Launching Container: ${docker_image}"
docker run -v "$(pwd):/workspace" -w/workspace "${docker_image}" "$@"
docker run "${docker_run_args[@]}" "${docker_image}" "${docker_container_args[@]}"
Loading