summaryrefslogtreecommitdiff
path: root/mdtopdf
blob: cbfcf08cbdf7f7b5209b5142023e584c903aa2d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env bash

set -euo pipefail

# @describe Convert markdown file to PDF
# @meta require-tools pandoc
# @arg inputfile+ markdown file
# @option --font[?`_choice_font_fn`] font (default Arial)
# @option --from[?`_choice_from_fn`] from format
# @option --pdf-engine[typst|xelatex] PDF engine

# Specification: https://github.com/sigoden/argc/blob/main/docs/specification.md

_choice_font_fn() {
  fc-list : family
}

_choice_from_fn() {
  pandoc --list-input-formats
}

find_pdf_engine() {
  for engine in typst xelatex; do
    if command -v "$engine" &>/dev/null; then
      echo "$engine"
      exit
    fi
  done
  exit 2
}

main() {
  local engine
  local from
  engine=${argc_pdf_engine:-$(find_pdf_engine)}
  from=${argc_from:-markdown}
  for infile in "${argc_inputfile[@]?}"; do
    outfile="${infile%.*}.pdf"
    pandoc --from="$from" --pdf-engine="$engine" -V mainfont="${argc_font:-Arial}" "$infile" -o "$outfile"
  done
}

eval "$(argc --argc-eval "$0" "$@")"