-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlatex-templates
executable file
·168 lines (134 loc) · 4.38 KB
/
latex-templates
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env bash
#
# The main script that prompts user selection, processes inputs, and performs
# the file manipulation
#
# Please see https://github.com/qcr/latex-templates for full details on how
# things work, and instructions on how to add your own templates. The more
# templates the merrier!
#################
### VARIABLES ###
#################
# The name of this script, and the directory where it is located
SCRIPT_NAME="$(basename $(readlink -f $0))"
SCRIPT_DIR="$(dirname $(readlink -f $0))"
# Temporary location to install latest templates
TMP_LOCATION="/tmp/qcr_latex_templates"
# Source location for template selection
# Use local location if set, else use tmp location
# Local location is useful for development
SOURCE_LOCATION="$([ -z "${LOCAL_LOCATION:-}" ] && echo "$TMP_LOCATION" || echo "$LOCAL_LOCATION")"
# Screen Reset
RESET='\033[0m'
# Screen Colors
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
WHITE="\033[0;37m"
# Screen helpers
INFO="${GREEN}[INFO]${RESET} "
WARN="${YELLOW}[WARN]${RESET} "
ERROR="${RED}[ERROR]${RESET} "
#################
### FUNCTIONS ###
#################
function print_usage(){
printf "\nCreates a new latex file/folder structure using a code template.
Usage:
latex-templates [-h|--help] [<TEMPLATE>]
Arguments:
<TEMPLATE> the template to use, or leave blank to be prompted with selection.
Templates:
"
TEMPLATES="$(get_templates)"
for i in $TEMPLATES; do
printf " > $i\n"
done
printf "\nExample:
Create new work instruction: mkdir new_wi && cd new_wi && latex-templates work_instruction
"
}
function get_templates() {
TEMPLATES=($(find "${SCRIPT_DIR}" -mindepth 1 -maxdepth 1 -type d -not -name ".*" -exec basename {} \;))
echo "${TEMPLATES[@]}"
}
function display_heading() {
# Prints a heading to the screen
#
# Usage:
# display_heading "<HEADING TEXT>"
# $1 heading text
char="-"
text=" ${1:-} "
(( left_pad=(80 - ${#text})/2 ))
(( right_pad=(81 - ${#text})/2 ))
printf "${GREEN}"
printf "\n"; printf "%.0s$char" {1..80}; printf "\n"
printf "%.0s$char" $(seq 1 $left_pad)
printf "${WHITE}$text${GREEN}"
printf "%.0s$char" $(seq 1 $right_pad)
printf "\n"; printf "%.0s$char" {1..80}; printf "\n"
printf "${RESET}"
}
###################
### MAIN SCRIPT ###
###################
# Display heading
display_heading "QCR LATEX TEMPLATES"
# Error if bash is too old
if [ "$(echo $BASH_VERSION | sed 's/^\([0-9]\)*\..*/\1/')" -lt 4 ]; then
printf "${ERROR}%s\n\t%s\n" "Bash version 4.0+ is required. Detected:" \
"$BASH_VERSION"
exit 1
fi
# Get a local copy of the templates repo
printf "${INFO}Getting latest templates"
if [ -d "$TMP_LOCATION" ]; then rm -rf "$TMP_LOCATION"; fi
mkdir -p "$TMP_LOCATION"
git clone --depth 1 -b main https://github.com/qcr/latex-templates \
"$TMP_LOCATION"
printf "Done\n\n"
# Parse Args
PARSED_ARGS=$(getopt -a -n ${SCRIPT_NAME} -o 'h' --long 'help' -- "$@")
eval set -- "${PARSED_ARGS}"
while true; do
case "$1" in
-h|--help) print_usage; exit 0 ;;
# -- indicates end of arguments, break while loop
--) shift; break ;;
# invalid option was passed, print usage
*)
printf "${ERROR}Unrecognized option '$1'\n"
print_usage
exit 1 ;;
esac
done
# Get list of templates - each directory denotes a valid template
templates=($(find "$SOURCE_LOCATION" -mindepth 1 -maxdepth 1 -type d -not -name '.*' -exec basename {} \;))
# Get template selection - either from command line or user selection
template="${1:-}"
if [ "$template" == '' ]; then
printf "${INFO}Template Selection\n"
fi
if [ -z "$template" ] || ! printf '%s\n' "${templates[@]}" | \
grep -q "^$1$"; then
selection=0
while [ "$selection" -eq 0 ] || [ "$selection" -gt "${#templates[@]}" ]; do
if [ ! "$template" == '' ]; then
printf "${YELLOW}Invalid template provided ('${1:-}')"
fi
printf "${YELLOW}Select from the options:"
for i in "${!templates[@]}"; do
printf "\n\t[$((i+1))] %s" "${templates[i]}"
done
printf "\n\nSelection: ${RESET}"
read -n1 selection
if ! [[ "$selection" =~ ^[0-9]+$ ]]; then selection=0; fi
done
template="${templates[$((selection-1))]}"
printf "\n"
fi
printf "${INFO}Proceeding with template: '$template'\n"
# Create from template in local directory
printf "${INFO}Creating project from template"
rsync -avL "$SOURCE_LOCATION/$template/" .