forked from opencobra/cobratoolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepareTutorials_backup.sh
executable file
·402 lines (346 loc) · 16.1 KB
/
prepareTutorials_backup.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/bin/bash
usage="$(basename $0) -p=pdfPath -t=COBRATutorialsPath -c=COBRAToolBoxPath [-f=folderNameOfATutorial] [-h] [-l] [-m=mode] -- script to create tutorial documentation for the COBRA Toolbox.
where:
-p path of the PDFs
-c path of the COBRAToolbox local clone
-t path of the COBRA.tutorials local clone
-f name of a folder of a tutorial
-h show this help text
-m mode (all,html,md,pdf,png,rst)
-e matlab executable path"
if [ -d "/usr/lib/x86_64-linux-gnu/" ] ; then
PATH="/usr/lib/x86_64-linux-gnu:$PATH"
fi
#server="https://king.nuigalway.ie"
server="https://gibbs.unal.edu.co/cobradoc"
echo_time() {
echo `date +\%Y-\%m-\%d\ \%H:\%M:\%S` " $*"
}
declare -A subs
subs[analysis]="Analysis"
subs[base]="Base"
subs[dataIntegration]="Data integration"
subs[design]="Design"
subs[reconstruction]="Reconstruction"
subs[visualization]="Visualization"
buildTutorialList(){
nTutorial=0
if [[ -z "$specificTutorial" ]]; then
for d in $(find $COBRATutorials -maxdepth 7 -type d)
do
if [[ "${d}" == *additionalTutorials* ]]; then
continue # if not a directory, skip
fi
if [[ "${d}" == *template* ]]; then
continue # if the directory is the template directory, skip
fi
# check for MLX files.
for tutorial in ${d}/*.mlx
do
if ! [[ -f "$tutorial" ]]; then
break
fi
let "nTutorial+=1"
tutorials[$nTutorial]="$tutorial"
echo_time " - ${tutorials[$nTutorial]}"
done
done
else
for d in $(find $COBRATutorialsPath -maxdepth 7 -type d)
do
if [[ "${d}" == *"$(basename $specificTutorial)"* ]]; then
singleTutorial="$d/tutorial_$(basename $specificTutorial).mlx"
if [[ -f "$singleTutorial" ]]; then
let "nTutorial+=1"
tutorials[$nTutorial]="$singleTutorial"
echo_time " - ${tutorials[$nTutorial]}"
else
echo_time "> the supplied tutorial does not exist: ""$singleTutorial";
fi
fi
done
fi
if [[ nTutorial == 0 ]]; then
echo_time;
echo_time "List of tutorial is empty."
echo_time "$usage"; exit 1;
fi
# sort tutorial by ascending alphabetical order
IFS=$'\n'
tutorials=($(sort <<<"${tutorials[*]}"))
unset IFS
}
createLocalVariables(){
tutorial=$1
tutorialDir=${tutorial%/*}
tutorialName=${tutorial##*/}
tutorialName="${tutorialName%.*}"
tutorialFolder=${tutorialDir#$COBRATutorialsPath/}
if [[ -f "$pdfPath/tutorials/$tutorialFolder/$tutorialName.html" ]]; then
tutorialTitle=`awk '/<title>/ { show=1 } show; /<\/title>/ { show=0 }' $pdfPath/tutorials/$tutorialFolder/$tutorialName.html | sed -e 's#.*<title>\(.*\)</title>.*#\1#'`
else
tutorialTitle="tutorialNoName"
fi
foo="${tutorialName:9}"
tutorialLongTitle="${tutorialName:0:8}${foo^}"
readmePath="$COBRATutorialsPath/$tutorialFolder"
htmlPath="$COBRAToolboxPath/docs/source/_static/tutorials"
rstPath="$COBRAToolboxPath/docs/source/tutorials" # should be changed later to mimic structure of the src folder.
pngPath="$pdfPath/tutorials/$tutorialFolder"
pdfHyperlink="$server/cobratoolbox/tutorials/$tutorialFolder/$tutorialName.pdf"
pngHyperlink="$server/cobratoolbox/tutorials/$tutorialFolder/$tutorialName.png"
htmlHyperlink="$server/cobratoolbox/tutorials/$tutorialFolder/iframe_$tutorialName.html"
mlxHyperlink="https://github.com/opencobra/COBRA.tutorials/raw/master/$tutorialFolder/$tutorialName.mlx"
mHyperlink="https://github.com/opencobra/COBRA.tutorials/raw/master/$tutorialFolder/$tutorialName.m"
dirHyperlink="https://github.com/opencobra/COBRA.tutorials/tree/master/$tutorialFolder"
previousSection=""
if [[ -n $section ]]; then
previousSection=$section
fi
section=${tutorialFolder%%/*}
echo_time " - $tutorialTitle ($tutorialName) $tutorialFolder - $section ($previousSection)"
}
buildHTMLTutorials(){
$matlab -nodesktop -nosplash -r "restoredefaultpath;initCobraToolbox;addpath('.artenolis');generateTutorials('$pdfPath');restoredefaultpath;savepath;exit;"
#Added system libary path to avoid error below
#wkhtmltopdf: /usr/local/bin/MATLAB/R2021a/bin/glnxa64/libQt5WebKit.so.5: no version information available (required by wkhtmltopdf)
echo $LD_LIBRARY_PATH
if [ -d "/usr/lib/x86_64-linux-gnu/" ] ; then
LD_LIBRARY_PATH="/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH"
fi
for tutorial in "${tutorials[@]}" #"${tutorials[@]}"
do
createLocalVariables $tutorial
# create PDF file
wkhtmltopdf --page-size A8 --margin-right 2 --margin-bottom 3 --margin-top 3 --margin-left 2 $pdfPath/tutorials/$tutorialFolder/$tutorialName.html $pdfPath/tutorials/$tutorialFolder/$tutorialName.pdf
sed 's#<html><head>#&<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/opencobra/cobratoolbox@ffa0229fc0c01c9236bb7e961f65712443277719/latest/_static/js/iframeResizer.contentWindow.min.js"></script>#g' "$pdfPath/tutorials/$tutorialFolder/$tutorialName.html" > "$pdfPath/tutorials/$tutorialFolder/iframe_$tutorialName.html"
sed -i.bak 's/white-space:\ pre-wrap/white-space:\ normal/g' "$pdfPath/tutorials/$tutorialFolder/iframe_$tutorialName.html"
sed -i.bak 's/white-space:\ pre/white-space:\ normal/g' "$pdfPath/tutorials/$tutorialFolder/iframe_$tutorialName.html"
rm "$pdfPath/tutorials/$tutorialFolder/iframe_$tutorialName.html.bak"
done
}
buildHTMLSpecificTutorial(){
specificTutorial=$1
$matlab -nodesktop -nosplash -r "restoredefaultpath;initCobraToolbox;addpath('.artenolis');generateTutorials('$pdfPath', '$specificTutorial');restoredefaultpath;savepath;exit;"
createLocalVariables $specificTutorial
# create PDF file
wkhtmltopdf --page-size A8 --margin-right 2 --margin-bottom 3 --margin-top 3 --margin-left 2 $pdfPath/tutorials/$tutorialFolder/$tutorialName.html $pdfPath/tutorials/$tutorialFolder/$tutorialName.pdf
sed 's#<html><head>#&<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/opencobra/cobratoolbox@ffa0229fc0c01c9236bb7e961f65712443277719/latest/_static/js/iframeResizer.contentWindow.min.js"></script>#g' "$pdfPath/tutorials/$tutorialFolder/$tutorialName.html" > "$pdfPath/tutorials/$tutorialFolder/iframe_$tutorialName.html"
sed -i.bak 's/white-space:\ pre-wrap/white-space:\ normal/g' "$pdfPath/tutorials/$tutorialFolder/iframe_$tutorialName.html"
sed -i.bak 's/white-space:\ pre/white-space:\ normal/g' "$pdfPath/tutorials/$tutorialFolder/iframe_$tutorialName.html"
rm "$pdfPath/tutorials/$tutorialFolder/iframe_$tutorialName.html.bak"
}
# default vallues
mode="all"
buildHTML=false
buildPDF=false
buildRST=false
buildMD=false
buildPNG=false
matlab=/Applications/MATLAB_R2016b.app/bin/matlab
for i in "$@"
do
case $i in
-c=*)
COBRAToolboxPath="${i#*=}"
;;
-p=*)
pdfPath="${i#*=}"
;;
-m=*)
mode="${i#*=}"
;;
-f=*)
specificTutorial="${i#*=}"
;;
-t=*)
COBRATutorialsPath="${i#*=}"
;;
-e=*)
matlab="${i#*=}"
;;
*)
echo_time "$usage" # unknown argument
exit
;;
esac
done
mode=${mode,,} # lowercase the mode
# input checking
if [[ -z "$COBRAToolboxPath" ]]; then
echo_time "> COBRAToolboxPath is empty"; echo_time; echo_time "$usage"; exit 1;
fi
if [[ -z "$COBRATutorialsPath" ]]; then
echo_time "> COBRATutorialsPath is empty"; echo_time; echo_time "$usage"; exit 1;
fi
if [[ -z "$pdfPath" ]]; then
echo_time "> pdfPath is empty"; echo_time; echo_time "$usage"; exit 1;
fi
if [[ $mode = *"all"* ]]; then
buildPDF=true
buildHTML=true
buildRST=true
buildMD=true
buildPNG=true
fi
if [[ $mode = *"pdf"* ]] || [[ $mode = *"html"* ]]; then
buildPDF=true
buildHTML=true
fi
if [[ $mode = *"rst"* ]]; then
buildRST=true
fi
if [[ $mode = *"md"* ]]; then
buildMD=true
fi
if [[ $mode = *"png"* ]]; then
buildPNG=true
fi
pdfPath="${pdfPath/#\~/$HOME}"
COBRATutorialsPath="${COBRATutorialsPath/#\~/$HOME}"
COBRAToolboxPath="${COBRAToolboxPath/#\~/$HOME}"
echo_time "Path to the COBRAToolBox: " $COBRAToolboxPath
echo_time "Path to the COBRA.Tutorials: " $COBRATutorialsPath
echo_time "Path of the generated files: " $pdfPath
echo_time
echo_time "Building: PDF:$buildPDF, HTML:$buildHTML, RST:$buildRST, MD:$buildMD, PNG:$buildPNG"
# check if the triggering file is present
if ! [[ -z "$triggeringFile" ]]; then
if ! [[ -f "$triggeringFile" ]]; then
echo_time
echo_time " > triggering file ($triggeringFile) is not present: build aborted";
exit;
else
rm "$triggeringFile"
fi
fi
# build list of tutorial if parameter '-f' is not set.
buildTutorialList
# gather tutorial long names.
nTutorial=0
for tutorial in "${tutorials[@]}" #"${tutorials[@]}"
do
createLocalVariables $tutorial
let "nTutorial+=1"
tutorialLongNames[$nTutorial]="$tutorial $section $tutorialTitle"
done
# printf '%s\n' "${tutorialLongNames[@]}"
# sort tutorial by ascending alphabetical order of section and then name.
IFS=$'\n'
tutorials=($(sort -f -k2 -k3 <<<"${tutorialLongNames[*]}" | awk '{print $1}'))
unset IFS
# printf '%s\n' "${tutorialLongNames[@]}"
# gather section names
IFS=$'\n'
sections=($(sort -f -k2 <<<"${tutorialLongNames[*]}" | awk '{print $2}' | sort -u))
unset IFS
# printf '%s\n' "${sections[@]}"
tutorialPath="../tutorials"
rstPath="$COBRAToolboxPath/docs/source/tutorials"
mkdir -p "$tutorialDestination"
if [[ $buildHTML = true ]]; then
cd $COBRAToolboxPath
if [[ -z "$specificTutorial" ]]; then
buildHTMLTutorials;
else
buildHTMLSpecificTutorial "$specificTutorial";
fi
fi
# now loop through the above array
if [ $buildPNG = true ] || [ $buildMD = true ] || [ $buildRST = true ]; then
# clean destination folder for the RST and HTML tutorial files
if [ $buildRST = true ]; then
echo_time "Creating $rstPath/index.rst"
echo >> $rstPath/index.rst
echo >> $rstPath/index.rst
echo ".. raw:: html" >> $rstPath/index.rst
echo >> $rstPath/index.rst
echo " <style>h2 {font-size:0px;}</style>" >> $rstPath/index.rst
echo >> $rstPath/index.rst
echo_time "Cleaning destination folders for rst files"
find "$rstPath" -name "tutorial*.rst" -exec rm -f {} \;
fi
echo_time "Creating requested files for tutorial(s):"
echo ${tutorials[@]}
firstSection=true
for tutorial in "${tutorials[@]}" #"${tutorials[@]}"
do
createLocalVariables $tutorial
if [ $buildPNG = true ]; then
mkdir -p $pngPath
if [[ -f $pngPath/${tutorialName}.png ]]; then
rm $pngPath/${tutorialName}.png
fi
echo $pdfPath/tutorials/$tutorialFolder/$tutorialName.pdf
export PATH=/usr/local/bin:$PATH;
# change directory for the generation of PNGs
cd $pngPath
/usr/local/bin/convert -density 768 "$pdfPath/tutorials/$tutorialFolder/$tutorialName.pdf" ${tutorialName}_%04d.png
/usr/local/bin/convert -shave 4%x5% -append ${tutorialName}*.png ${tutorialName}.png && rm ${tutorialName}_*.png
#/usr/local/bin/pngquant ${tutorialName}2.png --ext -2.png && mv ${tutorialName}2-2.png $pngPath/${tutorialName}.png && rm ${tutorialName}2.png
echo " >> $tutorialName.png generated in $pngPath \n"
fi
# create markdown README
if [ $buildMD = true ]; then
echo $readmePath
mkdir -p $readmePath
echo "<p align=\"center\">" > $readmePath/README.md
echo " <a href=\"$pdfHyperlink\" title=\"Download PDF file\" target=\"_blank\"><img src=\"$server/cobratoolbox/img/icon_pdf.png\" height=\"90px\" alt=\"pdf\"></a> <a href=\"$mlxHyperlink\" title=\"Download Live Script file\" target=\"_blank\"><img src=\"$server/cobratoolbox/img/icon_mlx.png\" height=\"90px\" alt=\"MLX\"></a> <a href=\"$mHyperlink\" title=\"Download MATLAB file\" target=\"_blank\"><img src=\"$server/cobratoolbox/img/icon_m.png\" height=\"90px\" alt=\"M file\"></a> <a href=\"$dirHyperlink\" title=\"View on Github\" target=\"_blank\"><img src=\"$server/cobratoolbox/img/icon_view.png\" height=\"90px\" alt=\"view\"></a><a href=\"https://opencobra.github.io/cobratoolbox/latest/tutorials/index.html\" title=\"Tutorials\"><img src=\"$server/cobratoolbox/img/icon_tut.png\" height=\"90px\" alt=\"tutorials\"></a>" >> $readmePath/README.md
echo "<br><br>" >> $readmePath/README.md
echo "</p>" >> $readmePath/README.md
echo "<p align=\"center\">" >> $readmePath/README.md
echo " <a href=\"https://github.com/opencobra/COBRA.tutorials/blob/master/$tutorialFolder/README.md\"><img src=\"$pngHyperlink\" width=\"100%\"/></a>" >> $readmePath/README.md
echo "</p>" >> $readmePath/README.md
fi
# create rst file
if [ $buildRST = true ]; then
mkdir -p $rstPath
chrlen=${#tutorialTitle}
underline=`printf '=%.0s' $(seq 1 $chrlen);`
sed "s/#tutorialLongTitle#/$tutorialLongTitle/g" "$rstPath/template.rst" > "$rstPath/$tutorialLongTitle.rst"
sed -i.bak "s/#tutorialTitle#/$tutorialTitle/g" "$rstPath/$tutorialLongTitle.rst"
sed -i.bak "s/#underline#/$underline/g" "$rstPath/$tutorialLongTitle.rst"
sed -i.bak "s~#PDFtutorialPath#~$pdfHyperlink~g" "$rstPath/$tutorialLongTitle.rst"
sed -i.bak "s~#MLXtutorialPath#~$mlxHyperlink~g" "$rstPath/$tutorialLongTitle.rst"
sed -i.bak "s~#MtutorialPath#~$mHyperlink~g" "$rstPath/$tutorialLongTitle.rst"
sed -i.bak "s~#DIRtutorialPath#~$dirHyperlink~g" "$rstPath/$tutorialLongTitle.rst"
sed -i.bak "s~#IFRAMEtutorialPath#~$htmlHyperlink~g" "$rstPath/$tutorialLongTitle.rst"
rm "$rstPath/$tutorialLongTitle.rst.bak"
# Create sections for tutorials
if ! [[ $previousSection = $section ]]; then
if [ $firstSection = false ]; then
echo >> $rstPath/index.rst
echo ".. raw:: html" >> $rstPath/index.rst
echo >> $rstPath/index.rst
echo " <br>" >> $rstPath/index.rst
echo " </div>" >> $rstPath/index.rst
echo " </div>" >> $rstPath/index.rst
echo "" >> $rstPath/index.rst
echo "" >> $rstPath/index.rst
fi
chrlen=${#section}
underline=`printf -- '-%.0s' $(seq 1 $chrlen);`
echo "${subs[$section]}" >> $rstPath/index.rst
echo "$underline" >> $rstPath/index.rst
echo "" >> $rstPath/index.rst
echo "" >> $rstPath/index.rst
echo ".. raw:: html" >> $rstPath/index.rst
echo "" >> $rstPath/index.rst
echo " <div class=\"tutorialSectionBox $section\">" >> $rstPath/index.rst
echo " <div class=\"sectionLogo\"><img class=\"avatar\" src=\"$server/cobratoolbox/img/icon_${section}_wb.png\" alt=\"$section\"></div>" >> $rstPath/index.rst
echo " <div class=\"sectionTitle\"><h3>${subs[$section]}<a class=\"headerlink\" href=\"#$section\" title=\"Permalink to this headline\">¶</a></h3></div>" >> $rstPath/index.rst
echo " <div class=\"sectionContent\">" >> $rstPath/index.rst
echo >> $rstPath/index.rst
firstSection=false
fi
echo "* :doc:\`$tutorialLongTitle\`" >> $rstPath/index.rst
fi
done
echo >> $rstPath/index.rst
echo ".. raw:: html" >> $rstPath/index.rst
echo >> $rstPath/index.rst
echo " </div>" >> $rstPath/index.rst
echo " </div>" >> $rstPath/index.rst
echo " <br>" >> $rstPath/index.rst
echo >> $rstPath/index.rst
fi