-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
compile_tex.sh
152 lines (139 loc) · 4.34 KB
/
compile_tex.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
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
#!/bin/bash
function usage {
echo -e "\033[0;35mIll-formed arguments. Use the following format.\033[0m"
echo "Usage: $0 [-r <int>] [-p <int>] [-l <int>] [-o <string>]"
echo "-r Create a markdown file with tex source. 1 for yes, 0 for no. Default=1"
echo "-p Method that to convert pdf to png. 0= Imagemagick, 1=pdftopng. Default = 0"
echo "-l Use latexmk to compile documents. 1 for yes, 0 for no. Default=1"
echo "-o Output File for Markdown documentation of tex files. Default=ReadMe.md"
exit 1
}
# Defaults
GEN_README=1
M_PDF2PNG=0
USE_LTMK=1
OUTPUT_FILE="ReadMe.md"
# Get options
while getopts ":r:p:l:o:" o; do
case "${o}" in
r)
GEN_README=${OPTARG}
;;
p)
M_PDF2PNG=${OPTARG}
;;
l)
USE_LTMK=${OPTARG}
;;
o)
OUTPUT_FILE=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
# Validate options
# testing for empty strings, and ensuring numbers are within max ranges
if [[ "$GEN_README" > 1 || "$M_PDF2PNG" > 2 || "$USE_LTMK" > 1 ]]; then
usage
fi
# Not sure how to get find tot ignore .git while maintaining the same functionality, so moving it outside and then back after script is done
# mv .git ../
IGN_FOLDER_LIST='.git .circleci'
dir=$(pwd)
if [ $USE_LTMK -eq 1 ]
then
for d in $(find $dir -maxdepth 4 -type d)
do
curr_dir=$(basename $d)
echo $curr_dir
if [[ " $IGN_FOLDER_LIST " =~ .*\ $d\ .* ]]; then
echo "Not processing this folder, no tex files"
echo $d
else
echo $d
cd $d
latexmk -pdflatex=lualatex -interaction=nonstopmode -pdf
cd $dir
fi
done
fi
if [ $M_PDF2PNG != 2 ]
then
for pdf in $(find $dir -name "*.pdf")
do
# echo $tex
cur_dir=$(dirname $pdf)
# Go to directory with pdf files
cd $cur_dir
new_name=$(basename "$pdf" | cut -d. -f1)
new_image=$(echo "$new_name.png")
if [ $M_PDF2PNG -eq 0 ]
then
if [ -f "./$new_image" ]; then
echo "$new_image exists skipping file."
else
echo "$new_image does not exist, converting pdf to png."
# check if magick exists
if ! [ -x "$(command -v magick)" ]; then
echo 'Error: magick is not installed.' >&2
# find line <policy domain="coder" rights="none" pattern="PDF" /> in /etc/ImageMagick-6/policy.xml and change to <policy domain="coder" rights="read|write" pattern="PDF" />
sed -i 's/rights="none"/rights="read|write"/g' /etc/ImageMagick-6/policy.xml
convert -density 300 -depth 8 -quality 150 $pdf $new_image
else
magick convert -density 300 -depth 8 -quality 150 $pdf $new_image
fi
fi
else
pdftopng -png $pdf $new_name
fi
# Go back to root directory
cd $dir
done
fi
if [ $GEN_README == 1 ]
then
# Make new markdown file every run
rm $OUTPUT_FILE
echo "## Latex Diagrams" >> $OUTPUT_FILE
echo "This repo contains all the diagrams I have generated for my academic career, for fun and referenced from stack overflow." >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
cat "ReadMeToc.md" >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# Generate markdown file containing relative url to image and perhgaps path to tex code.
prev_dir=''
for tex in $(find $dir -name "*.tex")
do
cur_dir=$(dirname $tex)
rel_folder_path=$(realpath --relative-to="$dir" "$cur_dir")
rel_path_file=$(realpath --relative-to="$dir" "$tex")
if [ "$prev_dir" != "$rel_folder_path" ]
then
prev_dir=$rel_folder_path
echo "### $rel_folder_path" >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
else
echo "Parsing $rel_path_file from $rel_folder_path"
fi
### Add diagram to markdown file
# ![Alt text](/relative/path/to/img.jpg?raw=true "Optional Title")
# Replace tex extension with png
rel_image_path=$(basename "$rel_path_file" | cut -d. -f1)
echo "![$rel_folder_path]($rel_folder_path/$rel_image_path.png?raw=true \""$rel_image_path"\")" >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
### Add code to markdown file
echo "\`\`\`tex" >> $OUTPUT_FILE
cat $rel_path_file >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
echo "\`\`\`" >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
# Go to directory with pdf files
# Go back to root directory
# cd $dir
done
fi
# mv ../.git .