Skip to content

Commit d6af6ec

Browse files
Add more support for HTML. (#225)
* Add more support for HTML. The support is currently marked "experimental" as some features do not work correctly or are untested. * Update README.md Co-authored-by: Chris Fenner <cfenn@google.com> --------- Co-authored-by: Chris Fenner <cfenn@google.com>
1 parent f99a3b9 commit d6af6ec

3 files changed

Lines changed: 77 additions & 15 deletions

File tree

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,29 @@ You can then run the following:
8080

8181
```sh
8282
./docker_run \
83-
--extra_resource_dir path/to/resource_dir \
83+
--extra_resource_dir path/to/my_resource_dir \
8484
--template extra/my_resource_dir/template.tex \
8585
--reference_doc extra/my_resource_dir/reference_doc.docx \
8686
--csl extra/my_resource_dir/reference_style.csl \
8787
--pdf output.pdf \
8888
input.tcg
8989
```
90+
91+
## Rendering HTML (experimental)
92+
93+
HTML support is experimental. Known issues:
94+
95+
- `\listoffigures` and `\listoftables` are not supported, as they render without clickable links.
96+
- Mermaid figures currently render as fixed-width.
97+
98+
To render a document to HTML:
99+
100+
```sh
101+
./docker_run \
102+
--extra_resource_dir path/to/my_resource_dir \
103+
--template_html extra/my_resource_dir/html.template \
104+
--html_stylesheet extra/my_resource_dir/style1.css \
105+
--html_stylesheet extra/my_resource_dir/style2.css \
106+
--html output.html \
107+
input.tcg
108+
```

build.sh

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,9 @@ if [ "${BLOCK_QUOTES_ARE_INFORMATIVE_TEXT}" == "yes" ]; then
532532
fi
533533

534534
# Use sed to perform some basic fixups on certain input files.
535+
#
536+
# If the second argument is provided and is "html", strip out `\listoffigures` and `\listoftables`,
537+
# as these don't render correctly in HTML.
535538
do_md_fixups() {
536539
local input=$1
537540
# \newpage is rendered as the string "\newpage" in GitHub markdown.
@@ -563,6 +566,15 @@ do_md_fixups() {
563566
EOF
564567
fi
565568
fi
569+
570+
# These don't render correctly:
571+
# - The lists are raw text and not clickable entries.
572+
# - The "List of Figures" and "List of Tables" headers show up in the toc
573+
# as raw text and not clickable entries.
574+
if test "$2" == "html"; then
575+
sed -i.bak 's/^\\listoffigures$//g' "${input}"
576+
sed -i.bak 's/^\\listoftables$//g' "${input}"
577+
fi
566578
}
567579

568580
# latexdiff is pretty great, but it has some incompatibilities with our template, so we
@@ -693,6 +705,22 @@ analyze_latex_logs() {
693705
fi
694706
}
695707

708+
# Copy generated files (if any) back to the source directory so they can be cached and speed up future runs.
709+
cache_generated_files() {
710+
find . -type f \( \
711+
-name "*.aux" -o \
712+
-name "*.lof" -o \
713+
-name "*.lot" -o \
714+
-name "*.toc" -o \
715+
-name "*.upa" -o \
716+
-name "*.upb" -o \
717+
-name "*.convert.pdf" -o \
718+
-name "*.mermaid.pdf" -o \
719+
-name "*.mermaid.svg" -o \
720+
-name "*.aasvg.pdf" \
721+
\) -exec cp --parents {} "${SOURCE_DIR}" \; 2>/dev/null
722+
}
723+
696724
# Takes Markdown input and writes LaTeX output using pandoc.
697725
do_latex() {
698726
local input=$1
@@ -777,18 +805,7 @@ do_pdf() {
777805
# Write any LaTeX errors to stderr.
778806
>&2 grep -A 5 "] ! " "${logfile}"
779807

780-
# Copy generated files (if any) back to the source directory so they can be cached and speed up future runs.
781-
find . -type f \( \
782-
-name "*.aux" -o \
783-
-name "*.lof" -o \
784-
-name "*.lot" -o \
785-
-name "*.toc" -o \
786-
-name "*.upa" -o \
787-
-name "*.upb" -o \
788-
-name "*.convert.pdf" -o \
789-
-name "*.mermaid.pdf" -o \
790-
-name "*.aasvg.pdf" \
791-
\) -exec cp --parents {} "${SOURCE_DIR}" \; 2>/dev/null
808+
cache_generated_files
792809
echo "Elapsed time: $(($end-$start)) seconds"
793810
# Write any LaTeX errors to stderr.
794811
>&2 grep -A 5 "! " "${logfile}"
@@ -846,6 +863,8 @@ do_docx() {
846863
else
847864
echo "DOCX output generated to file: ${output}"
848865
fi
866+
867+
cache_generated_files
849868
}
850869

851870
# Takes Markdown input and writes HTML output using pandoc.
@@ -884,6 +903,10 @@ do_html() {
884903
--metadata=logo:/resources/img/tcg.png
885904
--metadata=titlepage-rule-height:0
886905
--metadata=colorlinks:true
906+
--metadata=link-bibliography
907+
--metadata=link-citations
908+
--metadata=link-references
909+
--citeproc
887910
--metadata=contact:admin@trustedcomputinggroup.org
888911
--from=${FROM}
889912
${EXTRA_PANDOC_OPTIONS}
@@ -895,6 +918,7 @@ do_html() {
895918
FAILED=true
896919
echo "HTML output failed"
897920
else
921+
cache_generated_files
898922
echo "HTML output generated to file: ${output}"
899923
fi
900924
}
@@ -924,6 +948,7 @@ fi
924948

925949
# Generate the html output
926950
if [ -n "${HTML_OUTPUT}" ]; then
951+
do_md_fixups "${BUILD_DIR}/${INPUT_FILE}" "html"
927952
do_html "${BUILD_DIR}/${INPUT_FILE}" "${SOURCE_DIR}/${HTML_OUTPUT}" "${CROSSREF_TYPE}"
928953
fi
929954

filter/mermaid-filter.lua

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
-- Turn mermaid-classed code blocks into figures, retaining other classes on the
22
-- code block as classes on the figure.
33

4+
output_classes =
5+
{
6+
["pdf"] = {
7+
["extension"] = ".mermaid.pdf",
8+
["image_attrs"] = {},
9+
},
10+
["html"] = {
11+
["extension"] = ".mermaid.svg",
12+
["image_attrs"] = {
13+
"", -- No identifier
14+
{}, -- No classes
15+
{width = "500px"}
16+
},
17+
}
18+
}
19+
420
function runCommandWithInput(command, input)
521
local pipe = io.popen(command, "w")
622
if not pipe then
@@ -26,7 +42,9 @@ function fileExists(file)
2642
end
2743

2844
function mermaidFigure(code, caption, attrs)
29-
local filename = getContentsHash('code=' .. code .. 'caption=' .. pandoc.utils.stringify(caption) .. 'attrs=' .. pandoc.utils.stringify(attrs)) .. '.mermaid.pdf'
45+
output_class = output_classes[FORMAT] or output_classes["pdf"]
46+
47+
local filename = getContentsHash('code=' .. code .. 'caption=' .. pandoc.utils.stringify(caption) .. 'attrs=' .. pandoc.utils.stringify(attrs)) .. output_class["extension"]
3048
if fileExists(filename) then
3149
print(string.format('%s already exists; not re-rendering it', filename))
3250
else
@@ -38,7 +56,7 @@ function mermaidFigure(code, caption, attrs)
3856
end
3957
end
4058

41-
local img = pandoc.Image(caption, filename)
59+
local img = pandoc.Image(caption, filename, "", output_class["image_attrs"])
4260
return pandoc.Figure(img, caption, attrs)
4361
end
4462

0 commit comments

Comments
 (0)