aboutsummaryrefslogtreecommitdiffstats
path: root/bin/tex2pdf
blob: d3b6270108483725f63be97fea210fa6101b8e5c (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/bash

set -eu

initial_pwd=$(pwd)
filename=""
dirname=""
input_suffix=".tex"
probe_suffix="tex"
output_suffix=".pdf"

get_filename() {
  local base
  base="$(basename "${1}")"
  filename="${base%.*}"
}

get_dirname() {
  dirname="$(dirname "${1}")"
}

probe_extension() {
  local extension="${1##*.}"
  if [ "${extension}" != "${probe_suffix}" ]; then
    echo "File extension (${extension}) is not ${probe_suffix}. Exiting..."
    exit 1
  fi
  return 0
}

probe_tools() {
  local tools=(
    pdflatex
    bibtex
    mupdf
  )
  local tool

  for tool in "${tools[@]}"; do
    if ! command -v "$tool" > /dev/null; then
      printf "%s not available. Exiting...\n" "$tool"
      exit 1
    fi
  done

  return 0
}

create_pdf() {
  cd "${dirname}"
  set +e
  xelatex -interaction=nonstopmode "${filename}${input_suffix}"
  xelatex -interaction=nonstopmode "${filename}${input_suffix}"
  set -e
  cd "${initial_pwd}"
  return 0
}

show_pdf() {
  echo "mupdf: ${dirname}/${filename}${output_suffix}."
  if ! pkill -u "$(whoami)" -f "mupdf ${dirname}/${filename}${output_suffix}" --signal SIGHUP; then
   DISPLAY=':0' mupdf "${dirname}/${filename}${output_suffix}" &
  fi
}

cleanup() {
  rm -f "${dirname}/"*.{acn,acr,alg,aux,bbl,bcf,blg,dvi,glg,glo,gls,glsdefs,idx,ist,lof,lol,lot,nav,out,run.xml,snm,toc,upa,upb,vrb,xdy} \
    "${dirname}/"*/*.{acn,acr,alg,aux,bbl,bcf,blg,dvi,glg,glo,gls,glsdefs,idx,ist,lof,lol,lot,nav,out,run.xml,snm,toc,upa,upb,vrb,xdy} \
    ./*.{acn,acr,alg,aux,bbl,bcf,blg,dvi,glg,glo,gls,glsdefs,idx,ist,lof,lol,lot,nav,out,run.xml,snm,toc,upa,upb,vrb,xdy} \
    ./*/*.{acn,acr,alg,aux,bbl,bcf,blg,dvi,glg,glo,gls,glsdefs,idx,ist,lof,lot,nav,out,run.xml,snm,toc,upa,upb,vrb,xdy}
}

render_diags() {
  echo "Rendering diagrams to pdfs."
  if [ -d "${dirname}/diag/" ] && [ -x /usr/bin/seqdiag ] ; then
    for _diag in "${dirname}/diag/"*.diag; do
      seqdiag -T 'pdf' "${_diag}"
    done
  fi
}

render_svgs() {
  echo "Rendering SVGs to pdfs."
  for dir in "${dirname}/images" "${dirname}/../images"; do
    if [ -d "${dir}" ]; then
      for svg in "${dir}/"*.svg; do
        if [ -f "${svg}" ]; then
          inkscape -D -o "${svg%\.svg}".pdf "${svg}"
        fi
      done
    fi
  done
}

probe_extension "$1"
get_filename "$1"
get_dirname "$1"
render_diags
render_svgs
echo "tex2pdf: Converting $filename$input_suffix to $filename$output_suffix."
probe_tools
create_pdf
cleanup
show_pdf

exit 0