Skip to content
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

Lua mermaid filter #178

Merged
merged 2 commits into from
Sep 11, 2024
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
3 changes: 2 additions & 1 deletion .github/workflows/render.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ jobs:
*.toc
*.upa
*.upb
media/*.convert.pdf
*.convert.pdf
*.mermaid.pdf
key: latex-${{ inputs.input }}-${{ inputs.container-version }}-${{ github.run_id }}
restore-keys: latex-${{ inputs.input }}-${{ inputs.container-version }}

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ RUN apt install -y \
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser

RUN npm install --global --unsafe-perm [email protected] [email protected] mermaid-[email protected] [email protected] [email protected]
RUN npm install --global --unsafe-perm [email protected] [email protected] @mermaid-js/[email protected] [email protected] [email protected]

# Important: /usr/local/texlive/bin/ paths come before other paths. We want to use the texlive we
# built above, not any that happen to have come along with our base image.
Expand Down
19 changes: 4 additions & 15 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,6 @@ echo "Date (English): ${DATE_ENGLISH}"
FROM="markdown+gfm_auto_identifiers+fenced_divs+implicit_figures+multiline_tables+grid_tables+table_captions-markdown_in_html_blocks"

cp /resources/filters/mermaid-config.json .mermaid-config.json
export MERMAID_FILTER_FORMAT="pdf"
export MERMAID_FILTER_BACKGROUND="transparent"

# The Mermaid filter loses track of the web browser it uses to render diagrams
# sometimes (maybe 5% of the time or so).
Expand Down Expand Up @@ -582,8 +580,7 @@ do_latex() {
--standalone
--no-highlight
--template=tcg.tex
--lua-filter=mermaid-code-class-pre.lua
--filter=mermaid-filter
--lua-filter=mermaid-filter.lua
--lua-filter=informative-sections.lua
--lua-filter=convert-images.lua
--lua-filter=center-images.lua
Expand Down Expand Up @@ -655,6 +652,7 @@ do_pdf() {
cp *.upb "${SOURCE_DIR}" 2>/dev/null
# Copy converted images so they can be cached as well.
cp *.convert.pdf "${SOURCE_DIR}" 2>/dev/null
cp *.mermaid.pdf "${SOURCE_DIR}" 2>/dev/null
echo "Elapsed time: $(($end-$start)) seconds"
# Write any LaTeX errors to stderr.
>&2 grep -A 5 "! " "${logfile}"
Expand Down Expand Up @@ -688,8 +686,7 @@ do_docx() {
cmd=(pandoc
--embed-resources
--standalone
--lua-filter=mermaid-code-class-pre.lua
--filter=mermaid-filter
--lua-filter=mermaid-filter.lua
--lua-filter=convert-images.lua
--lua-filter=parse-html.lua
--lua-filter=apply-classes-to-tables.lua
Expand Down Expand Up @@ -728,8 +725,7 @@ do_html() {
-V toccolor=blue
--embed-resources
--standalone
--lua-filter=mermaid-code-class-pre.lua
--filter=mermaid-filter
--lua-filter=mermaid-filter.lua
--lua-filter=parse-html.lua
--lua-filter=apply-classes-to-tables.lua
--lua-filter=landscape-pages.lua
Expand Down Expand Up @@ -790,12 +786,6 @@ if [ -n "${DOCX_OUTPUT}" ]; then
do_docx "${BUILD_DIR}/${INPUT_FILE}" "${SOURCE_DIR}/${DOCX_OUTPUT}"
fi

# Generate the html output
export MERMAID_FILTER_FORMAT="svg"
if [ -n "${HTML_OUTPUT}" ]; then
do_html "${BUILD_DIR}/${INPUT_FILE}" "${SOURCE_DIR}/${HTML_OUTPUT}"
fi

# Diffs may fail in some circumstances. Do not fail the entire workflow here.
PRE_DIFFING_FAILED="${FAILED}"

Expand All @@ -804,7 +794,6 @@ PRE_DIFFING_FAILED="${FAILED}"
readonly TEMP_DIFFBASE_TEX_FILE="${BUILD_DIR}/${INPUT_FILE}.diffbase.tex"
readonly TEMP_DIFF_TEX_FILE="${BUILD_DIR}/${INPUT_FILE}.diff.tex"
readonly TEMP_LATEXDIFF_LOG="${BUILD_DIR}/latexdiff.log"
export MERMAID_FILTER_FORMAT="pdf"
if [ -n "${DIFFPDF_OUTPUT}" -o -n "${DIFFTEX_OUTPUT}" ]; then
git fetch --unshallow --quiet 2>/dev/null
git reset --hard ${DIFFBASE}
Expand Down
22 changes: 0 additions & 22 deletions filter/mermaid-code-class-pre.lua

This file was deleted.

63 changes: 63 additions & 0 deletions filter/mermaid-filter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
-- Turn mermaid-classed code blocks into figures, retaining other classes on the
-- code block as classes on the figure.

function runCommandWithInput(command, input)
local pipe = io.popen(command, "w")
if not pipe then
return false
end
pipe:write(input)
pipe:flush()
pipe:close()
return true
end

function getContentsHash(contents)
return pandoc.sha1(contents):sub(1,10)
end

function fileExists(file)
local f = io.open(file)
if f then
f:close()
return true
end
return false
end

function mermaidFigure(code, caption, attrs)
local filename = getContentsHash('code=' .. code .. 'caption=' .. pandoc.utils.stringify(caption) .. 'attrs=' .. pandoc.utils.stringify(attrs)) .. '.mermaid.pdf'
if fileExists(filename) then
print(string.format('%s already exists; not re-rendering it', filename))
else
print(string.format('rendering %s using Mermaid...', filename))
if not runCommandWithInput(string.format(
"mmdc --configFile /resources/filters/mermaid-config.json --puppeteerConfigFile ./.puppeteer.json --width 2000 --height 2000 --backgroundColor transparent --pdfFit --input - --output %s 2>&1", filename), code) then
print(string.format('failed to convert %s to %s using drawio, falling back to letting latex try to pick it up', source, dest))
return false
end
end

local img = pandoc.Image(caption, filename)
return pandoc.Figure(img, caption, attrs)
end

function CodeBlock(el)
local isMermaid = false
local figure_classes = pandoc.List({})
for i, class in ipairs(el.classes) do
if class == 'mermaid' then
isMermaid = true
else
figure_classes:insert(class)
end
end
if isMermaid then
local caption = {long = pandoc.Plain(pandoc.Str(el.attributes.caption))}
local attrs = pandoc.Attr(el.identifier, figure_classes)
el.identifier = nil
el.classes = {'mermaid'}
return mermaidFigure(el.text, caption, attrs)
end
return el
end