forked from TexNAK/Science-Paper-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·123 lines (104 loc) · 3.04 KB
/
build.sh
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env bash
OUTPUT_FOLDER="out"
OUTPUT_FILE="${OUTPUT_FOLDER}/main"
# Flags for pandoc
PANDOC_INPUT=$(find ./src/ -type f -name '*.md' -not -path '*/.*' | sort)
FLAGS_PANDOC=$(cat .build/FlagsGeneral.txt)
FLAGS_PDF="${FLAGS_PANDOC} $(cat .build/FlagsPDF.txt)"
FLAGS_EPUB="${FLAGS_PANDOC} $(cat .build/FlagsEPUB.txt)"
FLAGS_HTML="${FLAGS_PANDOC} $(cat .build/FlagsHTML.txt)"
FLAGS_PLAINTEXT="${FLAGS_PANDOC} $(cat .build/FlagsPlaintext.txt)"
build() {
PREFIX=""
if [ "$2" = true ]; then
PREFIX="$(date "+[%H:%M:%S]") "
fi
MESSAGE="${PREFIX}Building for target '${TARGET}' ..."
echo ${MESSAGE}
mkdir -p ${OUTPUT_FOLDER}
case $1 in
pdf)
pandoc ${PANDOC_INPUT} ${FLAGS_PDF} -o "${OUTPUT_FILE}.pdf"
;;
plaintext)
pandoc ${PANDOC_INPUT} ${FLAGS_PLAINTEXT} -o "${OUTPUT_FILE}.txt"
;;
html)
pandoc ${PANDOC_INPUT} ${FLAGS_HTML} -o "${OUTPUT_FILE}.html"
;;
epub)
pandoc ${PANDOC_INPUT} ${FLAGS_EPUB} -t epub -o "${OUTPUT_FILE}.epub"
;;
*)
echo -e "\r\033[1A\033[0KInvalid target: '${TARGET}'"
return -1
;;
esac
local status=$?
if [ $status -ne 0 ]; then
echo -e "\n${MESSAGE} \033[31merror\033[0m" >&2
else
echo -e "\r\033[1A\033[0K${MESSAGE} \033[32mdone\033[0m"
fi
return $status
}
help() {
cat << EOF
Markdown typesetting tool.
usage:
build.sh [--target type][--docker][--help]
parameters:
--target Determines the output file type. One of: pdf, html, plaintext, epub
--docker Run inside docker container. Ignored if already inside a container.
--remote-image Use the image from GitHub Packages. Requires login ( docker login -u USERNAME -p PERSONAL_ACCESS_TOKEN docker.pkg.github.com ).
--help Print this help.
examples:
build.sh watch
build.sh --docker build
Report bugs to: https://github.com/TexNAK/TFL-Template/issues
EOF
}
PARAMETERS="$@"
DOCKER=false
COMPOSE_SERVICE="pandoc-local"
TARGET=pdf
WATCH=false
while [[ $# -gt 0 ]]; do
case $1 in
-t|--target)
TARGET="$2"
shift
shift
;;
-d|--docker)
DOCKER=true
shift
;;
-r|--remote-image)
COMPOSE_SERVICE=pandoc
shift
;;
-w|--watch)
WATCH=true
shift
;;
-h|--help)
help
exit
;;
*)
# unknown option
shift
;;
esac
done
# When asked to run in docker and we are not already then move into the matrix.
if [ ! -f /.dockerenv ] && [ "$DOCKER" = true ]; then
BUILD_PARAMETERS="${PARAMETERS}" docker-compose -p latex_build -f .docker/docker-compose.yml up $COMPOSE_SERVICE
exit
fi
if [ "$WATCH" = true ]; then
fswatch src | while read -r changed_path; do build $TARGET true; done
else
build $TARGET
fi