puntos/.local/bin/exportmd

82 lines
1.8 KiB
Bash
Executable file

#!/bin/sh -e
html() {
echo "<!doctype html>"
echo "<meta charset=utf-8>"
echo "<meta name=viewport content='width=device-width, initial-scale=1.0'>"
if test -n "$style"; then
echo "
<style>
html, body {
margin: 0;
padding: 0;
font-family: 'Liberation Sans', sans-serif;
font-size: 16px;
line-height: 1.3;
}
img {
max-width: 100%;
}
blockquote {
border-left: 3px solid;
margin-left: 1em;
padding-left: 1em;
}
li {
margin-bottom: .5em;
}
</style>
"
fi
cat "$1" | sed 's/ -> / → /g' | cmark --unsafe --smart
}
format="$1"
input="$2"
if test ! -f "$input"; then
echo "no existe el archivo \`$input\`."
exit 1
fi
output="$(dirname "$input")/$(basename "$input" .md).$format"
if grep -E "TODO|XXX" "$input" >/dev/null; then
echo "Ojo, ¡todavía tenés TODOs o XXXs!"
fi
if test "$format" = epub -o "$format" = rtf -o "$format" = docx; then
htmlfile="${input%.md}.html"
style=true html "$input" > "$htmlfile"
#if which pandoc >/dev/null; then
# pandoc --reference-doc=$HOME/template.docx -s -o "$output" "$htmlfile"
if which libreoffice >/dev/null; then
embed_dir="$(mktemp -d)"
libreoffice --headless --writer --convert-to "html:HTML (StarWriter):EmbedImages" --outdir "$embed_dir" "$htmlfile"
libreoffice --headless --writer --convert-to "$format" "$embed_dir"/*
rm -r "$embed_dir"
else
echo "No tengo libreoffice para convertir a $format."
rm "$htmlfile"
exit 1
fi
rm "$htmlfile"
elif test "$format" = html; then
style=true html "$input" > "$output"
elif test "$format" = standalone.html; then
html "$input" > "$output"
echo "<style>" >> "$output"
cat ~/proy/sitio/drip.css >> "$output"
echo "</style>" >> "$output"
elif test "$format" = pdf; then
style=true html "$input" | weasyprint -u "$(dirname "$input")" - "$output"
else
echo "no conozco el formato \`$format\`."
exit 1
fi