summaryrefslogtreecommitdiff
path: root/gup/pdf.gup
blob: 31515af55f1e9b77c4b14cf0f296aec45891fd38 (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
107
108
#!/usr/bin/env zsh

ret=0

logged() {
    printf ">>> ‘%s’ in %s\n" "$*" $(pwd) >&2
    ${@}
    ret=$?
    printf ">>> %d\n" $ret >&2
}

typeset -a cleanupList
cleanupList=()
cleanup() {
    for cmd in ${cleanupList}; do
        eval $cmd
    done
}
trap cleanup EXIT

typeset -a beamerAspects
beamerAspects=(43 169)
beamerAspect=""

if [[ $beamerAspects[(i)${2:r:e}] -le $#beamerAspects ]]; then
    beamerAspect=${2:r:e}
    2=${2:r:r}.${2:e}
fi

base=(${2:r}.(md|lhs|tex)([1]))
metadata=${2:r}.meta.yml

if ! [[ -e ${metadata} ]] && ! gup --buildable ${metadata}; then
    metadata=""
fi

gup -u ${base} ${metadata} preamble.tex

buildDir=$(pwd)

base=${base:A}
metadata=${metadata:A}
cd ${base:h}

input=${base}

typeset -a extraArgs
extraArgs=(-R -V "geometry=margin=2cm" -V "fontfamily=libertine" -H ${buildDir}/preamble.tex --listings --latex-engine=lualatex --biblatex)
#extraArgs+=(--filter=pandoc-citeproc)


compileDir=$(mktemp -d)
cleanupList+=("cd /" "rm -rfv ${compileDir}")

if [[ -n ${metadata} ]]; then
    input=$(mktemp)
    cleanupList+=("rm -v ${input}")

    typeset -a class
    class=()
    [[ ${base:e} == "tex" ]] && class=(-f latex)
    
    logged pandoc -f markdown -t native -s ${metadata} | head -n -1 >>${input}
    logged pandoc -R ${class} -t native ${base} >>${input}

    extraArgs+=(-f native)


    bibFiles=$(pandoc -f markdown -t json -s ${metadata} | jq -r '.meta.bibliography.c[] | (.c[0].c)? // .c')

    gup -u ${(f)bibFiles}

    for bibFile (${(f)bibFiles}); do
        targetPath=${compileDir}/$(realpath --relative-to=${base:h} -- ${base:h}/${bibFile:h})
        
        [[ ! -e ${targetPath} ]] && logged mkdir -p ${targetPath}
        logged cp ${bibFile} ${targetPath}/${bibFile:t}
    done
fi

if [[ ${base:t:r} == "presentation" ]]; then
    gup -u ${buildDir}/beamer-template.tex
    extraArgs+=(--template=${buildDir}/beamer-template.tex)
    [[ -n "${beamerAspect}" ]] && extraArgs+=(-M "aspectratio=${beamerAspect}")
fi

logged pandoc ${extraArgs} -o ${compileDir}/${base:t:r}.tex ${input} >&2

cd ${compileDir}

# cat -n ${base:t:r}.tex >&2

max=10
runs=0
run=true
while ${run} && [[ ${runs} -lt ${max} ]]; do
    output=$(mktemp)
    cleanupList+=("rm -v ${output}")
    logged lualatex ${base:t:r}.tex >${output} </dev/null
    ( return $ret ) || { logged cat ${output}; break }
    grep -C 2 -i "warning" ${output}
    run=false
    grep -Eq $'(\\(re\\)run Biber)|(Please rerun LaTeX)' ${output} && run=true
    [[ -e ${base:t:r}.bcf && runs -eq 0 ]] && logged biber ${base:t:r}
    runs=$((runs + 1))
done

cp -v ${base:t:r}.pdf $1