dotfiles

My personal shell configs and stuff
git clone git://git.alex.balgavy.eu/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

markdown2epub (2479B)


      1 #!/bin/sh
      2 # Convert a markdown file to a formatted EPUB, via pandoc.
      3 command -v pandoc 1>/dev/null 2>&1 || { printf "error: pandoc not installed.\n"; exit 1; }
      4 [ $# -eq 2 ] || { printf "Usage: markdown2epub input.md output.epub\n"; exit 1; }
      5 [ -f "$1" ] || { printf "File %s not found.\n" "$1"; exit 1; }
      6 
      7 write_css() {
      8   # CSS intentionally formatted like crap, it doesn't matter
      9   cat <<- 'EOF' > "$1"
     10 h4{text-align:left}h5{text-align:left}h6{text-align:left}nav#landmarks
     11 ol,nav#toc ol{padding:0;margin-left:1em}nav#landmarks ol li,nav#toc ol
     12 li{list-style-type:none;margin:0;padding:0}a.footnote-ref{vertical-align:super}em,em
     13 em em,em em em em em{font-style:italic}em em,em em em
     14 em{font-style:normal}span.smallcaps{font-variant:small-caps}span.underline{text-decoration:underline}q{quotes:"“"
     15 "”" "‘"
     16 "’"}div.column{display:inline-block;vertical-align:top;width:50%}div.hanging-indent{margin-left:1.5em;text-indent:-1.5em}
     17 body{font-family:"DejaVu
     18 Serif",serif;margin:5% 5% 20%
     19 5%;text-align:justify;font-size:.75em;line-height:1.5}h1{text-align:left;font-size:2em}
     20 h2{text-align:left}h3{text-align:left}h1.title{text-align:center}p.author{text-align:center}code{font-family:"DejaVu Sans
     21 Mono",monospace;background-color:#ebebeb;font-size:100%;line-height:inherit;white-space:pre-wrap}
     22 pre{font-family:"DejaVu Sans Mono",monospace;padding:1em;overflow:auto;font-size:100%;line-height:inherit;border-radius:.25em;
     23 background-color:#ebebeb;white-space:pre-wrap}table{border-collapse:collapse;font-size:100%}td,th{padding:.8em}
     24 table,td,th{border:.06em solid #000}a{text-decoration:none}blockquote{border-style:solid solid solid
     25 solid;border-color:#a41434;border-width:medium;border-radius:.5em;padding-left:1em;padding-right:1em;background-color:#fcecf4}@media
     26 screen{.sourceCode{overflow:visible!important;white-space:pre-wrap!important}}
     27 EOF
     28 }
     29 
     30 outfile="${2%%.epub}.epub"
     31 
     32 cssfile="$(mktemp)"
     33 trap 'rm $cssfile' INT TERM EXIT
     34 write_css "$cssfile"
     35 
     36 printf "Title: "; read -r title
     37 while [ -z "$title" ]; do printf "Title required: "; read -r title; done
     38 printf "Author: "; read -r author
     39 while [ -z "$author" ]; do printf "Author required: "; read -r author; done
     40 printf "Language (default 'en-US'): "; read -r lang
     41 pandoc "$1" -f gfm --toc --standalone --top-level-division=chapter \
     42   --css="$cssfile" \
     43   -M title="$title" -M author="$author" -M lang="${lang:-en-US}" \
     44   -o "$outfile"
     45 trap - INT TERM EXIT
     46 rm "$cssfile"
     47 printf "Output saved to %s.\n" "$outfile"