md-to-pdf/alternatives.sh

88 lines
2.2 KiB
Bash
Raw Permalink Normal View History

2021-09-13 20:52:32 +02:00
#!/usr/bin/env bash
2021-09-15 17:14:44 +02:00
function convert0(){
2021-09-15 17:34:14 +02:00
# Same as the current process (in md-to-pdf), but using WeasyPrint instead of wkhtmltopdf.
# -> https://github.com/Kozea/WeasyPrint/
return 0
}
function convert1(){
2021-09-15 17:14:44 +02:00
file="${1}"
html="${file}".html;
pdf="${file}".pdf;
# depends=(chromium cmark-gfm) # 'chromium' is e.g. provided by the package 'ungoogled-chromium'
# Convert
cmark-gfm "${file}" --to html --unsafe > "${html}"
chromium --headless --disable-gpu --print-to-pdf="${pdf}" file://"${html}"
# Cleanup
rm "${html}"
}
2021-09-15 17:34:14 +02:00
function convert2(){
2021-09-13 20:52:32 +02:00
md="${1}"
out="${md}".pdf
sudo pacman -S --needed pandoc
# kpathsea requires mktexfmt which is provided by texlive-core
sudo pacman -S --needed texlive-core
# Does not support images with relative paths
pandoc "${md}" -o "${out}" \
--from=gfm \
--table-of-contents \
--pdf-engine=xelatex \
--highlight-style=monochrome \
--number-sections \
-V 'fontsize: 12pt' \
-V 'papersize: A4' \
-V 'urlcolor: blue' \
-V 'date: \today{}' \
-V 'documentclass:article' \
-V 'geometry:margin=1.5cm'
}
2021-09-15 17:34:14 +02:00
function convert3(){
2021-09-13 20:52:32 +02:00
md="${1}"
# * https://www.npmjs.com/package/markdown-pdf
# * https://www.npmjs.com/package/remarkable
# * https://www.npmjs.com/package/markdown-toc
#
# > By default, remarkable is configured to be similar to GFM,
# > but with HTML disabled. This is easy to change if you prefer
# > different settings.
sudo pacman -S --needed nodejs-markdown-pdf
sudo pacman -S --needed nodejs-markdown-toc # optional for TOC
# Optional: Insert TOC
add-toc "${md}"
md="${md}".TOC
# Supports relative image paths and HTML images
markdown-pdf \
--paper-format=A4 \
--paper-orientation=portrait \
--paper-border=15mm \
--remarkable-options='{ "html": "false" }' \
"${md}"
}
function add-toc(){
md="${1}"
out="${1}".TOC
# If there is a TOC anchor, the TOC can be inserted in place.
# This is a workaround if no such anchor is present.
head --lines 1 "${md}" > "${out}"
printf '\n\n**Table of Contents**\n\n' >> "${out}"
markdown-toc "${md}" >> "${out}"
printf '\n\n---\n\n' >> "${out}"
tail --lines +2 "${md}" >> "${out}"
}