-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_latex_projects.sh
1825 lines (1454 loc) · 50.8 KB
/
build_latex_projects.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# @Author: StarryTony
# @Date: 2018-03-20 23:21:13
# @Last Modified by: StarryTony
# @Last Modified time: 2019-01-24 15:47:53
# @Description: This lightweight script helps to build complicated latex projects.
# -------------------------------
# App functions
# -------------------------------
default(){
echoFuncSeperator "default"
count=0
for index in "${defaultConfigList[@]}"; do
KEY="${index%%::*}"
FUN="${index#*::}"
FUN="${FUN%::*}"
FUN="${FUN//[[:space:]]/}"
DES="${index##*::}"
if [[ "${FUN}" != "" ]]; then
item="${KEY}=${FUN}"
eval ${item}
fi
count=$((${count}+1))
done
show_debug_msg "${ICON_ACCEPT} : Default Configuration Loaded." ${ACCEPT_LEVEL}
}
read_profile(){
echoFuncSeperator "read_profile"
# read from profile
while read line; do
eval "$line"
done < "${profile_path}"
show_debug_msg "${ICON_ACCEPT} : System Configuration Loaded." ${ACCEPT_LEVEL}
}
read_project_specified_config(){
echoFuncSeperator "read_project_specified_config"
# read from profile
while read line; do
eval "$line"
done < "${project_specified_config}"
show_debug_msg "${ICON_ACCEPT} : Project specified configuration Loaded." ${ACCEPT_LEVEL}
}
update_project_specified_config(){
echoFuncSeperator "update_project_specified_config"
if [[ ! -d "${project_path}" ]]; then
show_debug_msg "${ICON_ERR} : the project ${err_msg_color}${project_path}${NC} is not found." ${ERR_LEVEL}
return ${ERR_CODE}
fi
# if profile folder not exist, create it
if [[ "${project_path}" != "" ]] && [[ ! -d "${project_specified_conf_folder}" ]]; then
mkdir "${project_specified_conf_folder}"
fi
count=0
for index in "${ProjectSpecifiedConfigList[@]}"; do
KEY="${index%%::*}"
if [[ ${count} == 0 ]]; then
item="echo "${KEY}=\\\"\${${KEY}}\\\"" > "\"${project_specified_config}\"""
else
item="echo "${KEY}=\\\"\${${KEY}}\\\"" >> "\"${project_specified_config}\"""
fi
eval ${item}
count=$((${count}+1))
done
show_debug_msg "${ICON_ACCEPT} : Project specified configuration updated." ${ACCEPT_LEVEL}
}
load_project(){
echoFuncSeperator "load_project"
# clean before re-loading
# target_output_name=""
# target_output_name="${project_name}"
if [ -f "${profile_path}" ]
then
read_profile
else
default
fi
# if profile folder not exist, create it
if [ ! -d "${profile_folder}" ]
then
mkdir "${profile_folder}"
fi
if [[ ! -d "${project_path}" ]]; then
show_debug_msg "${ICON_ERR} : the project ${err_msg_color}${project_path}${NC} is not found." ${ERR_LEVEL}
return ${ERR_CODE}
else
cd "${project_path}"
# set folders (const)
if [[ "${project_path}" != "" ]]; then
project_specified_conf_folder=${project_path}"/project_specified_config"
project_specified_config=${project_specified_conf_folder}"/project_specified.config"
backup_folder=${project_specified_conf_folder}"/backup"
fi
# override with project specified configuration
if [ -f "${project_specified_config}" ]
then
read_project_specified_config
else
echo -e "${ICON_WARN} No project specified configuration found."
fi
# auto find the most recent version number
autoFindVersionNum tmp_tex_version_count
if [[ "${tex_version_count}" == "" ]] || [[ $((${tex_version_count})) -lt $((${tmp_tex_version_count})) ]]; then
tex_version_count=${tmp_tex_version_count}
fi
updateConfig
show_debug_msg "${ICON_ACCEPT} : Project loaded." ${ACCEPT_LEVEL}
fi
}
autoFindVersionNum(){
# list all backups
lb_except_list=(
"${project_specified_conf_folder##*/}"
)
lb_str="find \"${backup_folder}\" -type d -depth 1"
count=0
for index in "${lb_except_list[@]}"; do
KEY="${index%%::*}"
lb_str=${lb_str}" -not -iname \"${KEY}\""
count=$((${count}+1))
done
lb_str=${lb_str}" -print0 | xargs -0 stat -f '%Sm %N,' | sort"
local bak_list=`eval "${lb_str}"`
# sort list
IFS=$','
bak_list=($(sort <<<"${bak_list[*]}"))
unset IFS
# find mostly recent diff version (the last one)
candidate_diff=""
for index in "${!bak_list[@]}"; do
KEY="${bak_list[index]//*[[:space:]]\///}"
KEY_time=`date -r "${KEY}" '+%Y-%m-%d %H:%M:%S'`
KEY_timestamp=`date -r "${KEY}" '+%s'`
KEY=${KEY##*\/}
item_str=""
if [[ ${index} == 0 ]]; then
item_str="Available history versions: "
else
item_str=" "
fi
item_str=${item_str}"[${KEY##*_}] ${profile_color}${KEY}${NC} "
item_str=${item_str}"${highlight_time}${KEY_time}${NC}"
candidate_diff=${KEY##*_}
show_debug_msg "${item_str}" ${NORMAL_LEVEL}
done
echo -e "Recent changed version: ${profile_color}${candidate_diff}${NC}"
eval "$1=\"${candidate_diff}\""
}
tryAutoFindMainTex(){
echoFuncSeperator "tryAutoFindMainTex"
if [[ "${project_path}" != "" ]]; then
cd "${project_path}"
# try auto-find project name (name of .tex))
temp_name=$(find . -type f -maxdepth 1 -iname "*.tex")
OLD_IFS="$IFS" # OLD_IFS用于备份默认的分隔符,使用完后将之恢复默认
IFS=$'\n' # A list of characters that separate fields; used when the shell splits words as part of expansion.
arr=($temp_name)
temp_project_name=""
# loop in case of not only one tex file
count=0
arrayTex=()
for item in ${arr[@]}
do
item=${item#./}
item=${item%.tex}
arrayTex+=("${count}::${item}")
echo -e "[${count}] ${profile_color}${item}${NC}"
if [[ item != "" ]] && [[ item != "Chapter" ]] ; then
temp_project_name="$item"
fi
count=$((${count}+1))
done
IFS="$OLD_IFS"
# unset IFS # 或者使用unset恢复默认值
# re-choose the main .tex file manually if there are more than 1 .tex files
if [[ ${count} > 1 ]]; then
echo "There are ${count} .tex files, please specify the main .tex file (you can use index number or the .tex file name, the first .tex file will be chosen if nothing input):"
read temp_project_name
if [[ "${temp_project_name}" == "" ]]; then
temp_project_name=0
fi
echo "-------------------"
for index in "${arrayTex[@]}"; do
KEY="${index%%::*}"
VALUE="${index##*::}"
# echo "[${KEY}] ${VALUE}"
if [[ "${temp_project_name}" =~ ^[0-9]+$ ]] && [[ "${temp_project_name}" == "${KEY}" ]]; then
temp_project_name="${VALUE}"
fi
done
fi
isTexExist=false
if [[ temp_project_name != "" ]] ; then
# check if the specified .tex file exists
for index in "${arrayTex[@]}"; do
KEY="${index%%::*}"
VALUE="${index##*::}"
if [[ "${temp_project_name}" == "${VALUE}" ]]; then
project_name="${temp_project_name}"
target_output_name="${project_name}"
isTexExist=true
show_debug_msg "${ICON_ACCEPT} : New main tex ${profile_color}${project_name}.tex${NC} has been chosen." ${ACCEPT_LEVEL}
fi
done
if [[ ${isTexExist} == false ]]; then
show_debug_msg "${ICON_ERR} : the ${err_msg_color}${temp_project_name}.tex${NC} file does not exist." ${ERR_LEVEL}
# tryAutoFindMainTex
return ${ERR_CODE}
fi
else
tryAutoFindMainTex
fi
# write new configuration to profile file (system config only)
updateConfig "false"
show_debug_msg "${ICON_ACCEPT} : New main tex ${profile_color}${project_name}.tex${NC} has been loaded." ${ACCEPT_LEVEL}
else
show_debug_msg "${ICON_ERR} : the project_path \"${profile_color}${profile_path}${NC}\" is invalid." ${ERR_LEVEL}
fi
}
# $1 -> update project specified config?
updateConfig(){
echoFuncSeperator "updateConfig"
count=0
for index in "${defaultConfigList[@]}"; do
KEY="${index%%::*}"
if [[ ${count} == 0 ]]; then
item="echo "${KEY}=\\\"\${${KEY}}\\\"" > "\"${profile_path}\"""
else
item="echo "${KEY}=\\\"\${${KEY}}\\\"" >> "\"${profile_path}\"""
fi
eval ${item}
count=$((${count}+1))
done
show_debug_msg "${ICON_ACCEPT} : System config updated." ${ACCEPT_LEVEL}
if [[ "$1" != "false" ]]; then
update_project_specified_config
fi
}
# $1 -> msg; $2 -> msg_debug_level
show_debug_msg(){
if [[ "$2" =~ ^[0-9]+$ ]];then
msg_debug_level=$(($2))
else
msg_debug_level=${DEBUG_LEVEL_MIN}
fi
if [[ ${DEBUG_LEVEL} -ge ${msg_debug_level} ]]; then
now_time=`date '+%Y-%m-%d %H:%M:%S'`
echo -e "${grey_time}${now_time}${NC} $1"
fi
# hasError? hasWarn?
local msg_status=${1:0:4}
msg_status=${msg_status//[[:space:]]/}
case ${msg_status} in
${ICON_ERR//[[:space:]]/})
hasError=$((${hasError}+1))
;;
${ICON_WARN//[[:space:]]/})
hasWarn=$((${hasWarn}+1))
;;
*)
;;
esac
}
updateRunHistory(){
run_history=$1
updateConfig
}
choose_project(){
echoFuncSeperator "choose_project"
prev_project_path=${project_path}
if [[ "$1" == "" ]]; then
echo "Please input the project path to compile:"
read project_path
else
project_path="$1"
fi
if [[ "${project_path}" != "" ]] && [[ -d "${project_path}" ]]; then
tryAutoFindMainTex
show_debug_msg "${ICON_ACCEPT} : New project \"${profile_color}${project_path}${NC}\" has been choosen." ${ACCEPT_LEVEL}
# update recent projects list
update_recent_projects_list "${project_path}"
# load new project
load_project
else
project_path=${prev_project_path}
show_debug_msg "${ICON_WARN} : Nothing changed." ${WARN_LEVEL}
return ${WARN_CODE}
fi
}
update_recent_projects_list(){
echoFuncSeperator "update_recent_projects_list"
local add_item="$1"
# if [[ ! -f "${recent_open_projects_list}" ]]; then
# return ${ERR_CODE}
# fi
cp "${recent_open_projects_list}" "${recent_open_projects_list}_copy"
IFS=$'\n' read -d '' -r -a recent_projects_list < "${recent_open_projects_list}_copy"
unset IFS
if ! containsElement "${add_item}" "${recent_projects_list[@]}"; then
# add
local recentCount=0
local maxCount=$((${max_recent_projects_count}))
echo -e "Max recent projects count: ${maxCount}"
for item in "${recent_projects_list[@]}"; do
if [[ "${recentCount}" == "1" ]]; then
echo -e "[$(($recentCount-1))] [${grey_time}${item}${NC}]"
echo "${item}" > "${recent_open_projects_list}"
elif [[ "${recentCount}" -ne "0" ]]; then
echo -e "[$(($recentCount-1))] [${grey_time}${item}${NC}]"
echo "${item}" >> "${recent_open_projects_list}"
fi
if [[ ${recentCount} -gt ${maxCount} ]]; then
break
else
recentCount=$(($recentCount+1))
fi
done
echo -e "Add: [$(($recentCount-1))] [${status_seperator_color}${add_item}${NC}]"
echo "${add_item}" >> "${recent_open_projects_list}"
fi
rm -f "${recent_open_projects_list}_copy"
}
choose_recent_projects(){
echoFuncSeperator "choose_recent_projects"
if [[ ! -f "${recent_open_projects_list}" ]]; then
show_debug_msg "${ICON_ERR} : No recent opened projects list found." ${ERR_LEVEL}
return ${ERR_CODE}
fi
# read from profile
IFS=$'\n' read -d '' -r -a recent_projects_list < "${recent_open_projects_list}"
unset IFS
local itemCount=0
for index in "${recent_projects_list[@]}"; do
#statements
echo -e "[${itemCount}] [${menu_color}${index}${NC}]"
itemCount=$((${itemCount}+1))
done
echo -e "Please choose a recent opened project (no changes if nothing chosen): "
read -n1 recent_project_index
echo ;
if [[ "${recent_project_index}" =~ ^[0-9]+$ ]] && [[ $((${recent_project_index})) -ge 0 ]] && [[ $((${recent_project_index})) -le $((${itemCount}-1)) ]]; then
count=0
for index in "${recent_projects_list[@]}"; do
KEY="${index}"
if [[ "${recent_project_index}" == "${count}" ]] || [[ "${recent_project_index}" == "${KEY}" ]]; then
echo -e "[${count}] ${menu_color}${KEY}${NC} has been chosen."
choose_project "${KEY}"
return ${ACCEPT_CODE}
fi
count=$((${count}+1))
done
else
# do nothing
echo -e "${ICON_WARN} Nothing changed."
return ${WARN_CODE}
fi
}
clean_cache(){
echoFuncSeperator "clean_cache"
echo "Clean cache..."
# clean dump files for recompile (bbl,aux,bib)
cache_list=(
'*.bbl'
'*.aux'
'*.log'
'*.blg'
'*.toc'
'*.bcf'
'*.ilg'
'*.lof'
'*.lot'
'*.nlo'
'*.nls'
'*.out'
'*.run.xml'
'*.idx'
'*.ind'
'*.acn'
'*.acr'
'*.alg'
'*.glg'
'*.glo'
'*.gls'
'*.ist'
'*.fdb_latexmk'
'*.fls'
)
count=0
for index in "${cache_list[@]}"; do
KEY="${index%%::*}"
DES="${index##*::}"
find "${project_path}" -type f -iname "${KEY}" -print0 | xargs -0 rm
count=$((${count}+1))
done
# only clean project.pdf
# find "${project_path}" -type f -maxdepth 1 -iname "*.pdf" -print0 | xargs -0 rm
# find "${project_path}" -type f -maxdepth 1 -iname "${project_name}.pdf" -print0 | xargs -0 rm
show_debug_msg "${ICON_ACCEPT} : Clean cache... done." ${ACCEPT_LEVEL}
}
deep_clean_cache(){
echoFuncSeperator "deep_clean_cache"
echo "Deep clean cache..."
# try to clean cache of biber for clean compilation
rm -rf `biber --cache`
# also remove output
if [ -d "output" ]
then
rm -r "output"
fi
clean_cache
show_debug_msg "${ICON_ACCEPT} : Deep clean cache... done" ${ACCEPT_LEVEL}
}
delete_all_backup(){
if [ -d "${backup_folder}" ]
then
echo -e "Delete all backups, ${menu_color}yes${NC} or ${menu_color}no${NC} (${menu_color}y${NC}/${menu_color}n${NC})?"
read option
echo ;
if [[ "${option}" == "y" ]] || [[ "${option}" == "yes" ]]; then
rm -r "$backup_folder"
else
show_debug_msg "${ICON_WARN} : the operation has been cancelled." ${WARN_LEVEL}
return ${WARN_CODE}
fi
fi
}
choose_pdf_compiler(){
echoFuncSeperator "choose_pdf_compiler"
echo -e "Current pdf Compiler: ${profile_color}${default_pdf_compiler}${NC}"
pdf_compiler_list=(
'pdflatex ::A modification of TeX which allows it to output to PDF directly'
'xelatex ::Another modification of the underlying TeX engine, this time to support a wider range of characters beyond just plain English numbers and letters, and to include support for modern font formats'
'lualatex ::An attempt to extend the original TeX program with a more sensible programming language'
'context ::Provide an easy interface to advanced typography features'
)
count=0
for index in "${pdf_compiler_list[@]}"; do
KEY="${index%%::*}"
DES="${index##*::}"
echo -e "[${count}] ${menu_color}${KEY}${NC} -> ${DES}"
count=$((${count}+1))
done
echo -e "Please choose pdf compiler (you can use the index number or compiler name, default is \"pdflatex\" if nothing chosen): "
read -n1 pdf_config
echo ;
if [[ "${pdf_config}" =~ ^[0-9]+$ ]] && [[ $((${pdf_config})) -ge 0 ]] && [[ $((${pdf_config})) -le $((${count}-1)) ]]; then
count=0
for index in "${pdf_compiler_list[@]}"; do
KEY="${index%%::*}"
KEY=${KEY//[[:space:]]/}
DES="${index##*::}"
if [[ "${pdf_config}" == "${count}" ]] || [[ "${pdf_config}" == "${KEY}" ]]; then
default_pdf_compiler="${KEY}"
echo -e "[${count}] ${menu_color}${KEY}${NC} has been chosen."
return
fi
count=$((${count}+1))
done
elif [[ "${default_pdf_compiler}" != "" ]]; then
default_pdf_compiler="pdflatex"
echo -e "[0] ${menu_color}${default_pdf_compiler}${NC} has been chosen."
else
# do nothing
echo -e "${ICON_WARN} Nothing changed."
return ${WARN_CODE}
fi
}
choose_ref_compiler(){
echoFuncSeperator "choose_ref_compiler"
echo -e "Current Reference Compiler: ${profile_color}${default_ref_compiler}${NC}"
ref_compiler_list=(
'Biber ::Write bibliography for entries in AUXFILE to AUXFILE.bbl, along with a log file AUXFILE.blg'
'BibTex ::A bibtex replacement for users of biblatex'
)
count=0
for index in "${ref_compiler_list[@]}"; do
KEY="${index%%::*}"
DES="${index##*::}"
echo -e "[${count}] ${menu_color}${KEY}${NC} -> ${DES}"
count=$((${count}+1))
done
echo -e "Please choose references compiler (you can use the index number or compiler name, default is \"Biber\" if nothing chosen): "
read -n1 ref_config
echo ;
if [[ "${ref_config}" =~ ^[0-9]+$ ]] && [[ $((${ref_config})) -ge 0 ]] && [[ $((${ref_config})) -le $((${count}-1)) ]]; then
count=0
for index in "${ref_compiler_list[@]}"; do
KEY="${index%%::*}"
KEY=${KEY//[[:space:]]/}
DES="${index##*::}"
if [[ "${ref_config}" == "${count}" ]] || [[ "${ref_config}" == "${KEY}" ]]; then
default_ref_compiler="${KEY}"
echo -e "[${count}] ${menu_color}${KEY}${NC} has been chosen."
return
fi
count=$((${count}+1))
done
elif [[ "${default_ref_compiler}" != "" ]]; then
default_ref_compiler="Biber"
echo -e "[0] ${menu_color}${default_ref_compiler}${NC} has been chosen."
else
# do nothing
echo -e "${ICON_WARN} Nothing changed."
return ${WARN_CODE}
fi
}
# choose proper encoding to parse your tex while compile/diff
choose_tex_encoding(){
echoFuncSeperator "choose_tex_encoding"
echo -e "Current .tex encoding: ${profile_color}${default_tex_encoding}${NC}"
tex_encoding_list=(
'ascii ::A 7-bit character set containing 128 characters.'
'utf8 ::A variable width character encoding capable of encoding all 1,112,064 valid code points in Unicode using one to four 8-bit bytes.'
'unicode ::A computing industry standard for the consistent encoding.'
)
count=0
for index in "${tex_encoding_list[@]}"; do
KEY="${index%%::*}"
DES="${index##*::}"
echo -e "[${count}] ${menu_color}${KEY}${NC} -> ${DES}"
count=$((${count}+1))
done
echo -e "Please choose encoding used by *.tex: "
read -n1 encoding_config
echo ;
if [[ "${encoding_config}" =~ ^[0-9]+$ ]] && [[ $((${encoding_config})) -ge 0 ]] && [[ $((${encoding_config})) -le $((${count}-1)) ]]; then
count=0
for index in "${tex_encoding_list[@]}"; do
KEY="${index%%::*}"
KEY=${KEY//[[:space:]]/}
DES="${index##*::}"
if [[ "${encoding_config}" == "${count}" ]] || [[ "${encoding_config}" == "${KEY}" ]]; then
default_tex_encoding="${KEY}"
echo -e "[${count}] ${menu_color}${KEY}${NC} has been chosen."
return
fi
count=$((${count}+1))
done
elif [[ "${default_tex_encoding}" != "" ]]; then
default_tex_encoding="utf8"
echo -e "[0] ${menu_color}${default_tex_encoding}${NC} has been chosen."
else
# do nothing
echo -e "${ICON_WARN} Nothing changed."
return ${WARN_CODE}
fi
}
compile(){
echoFuncSeperator "Compiling with ${default_ref_compiler}"
if [[ "$1" == "" ]]; then
target_tex="${project_name}"
else
target_tex="$1"
target_tex="${target_tex%%.tex}"
fi
# try to clean cache at the first version, or clean_cache after compilation for other version number to save time for each iteration
if [[ "${tex_version_count}" == "" ]]; then
clean_cache
fi
# output should be removed before each compilation
# if [ -d "output" ]
# then
# rm -r "output"
# fi
compiler_str="${default_pdf_compiler} \"${target_tex}\""
eval ${compiler_str}
eval ${compiler_str}
# generate nomenclature list
makeindex "${target_tex}.nlo" -s nomencl.ist -o "${target_tex}.nls"
if [[ "${default_ref_compiler}" == "Biber" ]]; then
biber --output_safechars "${target_tex}"
elif [[ "${default_ref_compiler}" == "BibTex" ]]; then
bibtex "${target_tex}"
fi
eval ${compiler_str}
eval ${compiler_str}
killall ${compiler_str}
mkdir -p "output"
# target_tex.pdf is generated in the ./ dir, so remove any path prefix
target_tex="${target_tex##*/}"
if [[ "${target_output_name}" == "" ]]; then
target_output_name="${target_tex}"
fi
mv "${target_tex}.pdf" "output/""${target_output_name}.pdf"
open_file "output/""${target_output_name}.pdf"
# version count: count+1 once completed
if [[ "$1" == "" ]]; then
compile_count
backup_project
fi
# clean cache for the next compilation just for saving time
clean_cache
show_debug_msg "${ICON_ACCEPT} : Compile... done" ${ACCEPT_LEVEL}
}
compile_count(){
echoFuncSeperator "compile_count"
if [[ "${tex_version_count}" == "" ]]; then
tex_version_count=0
else
tex_version_count=$((${tex_version_count}+1))
fi
show_debug_msg "${ICON_ACCEPT} : new version [${profile_color}${tex_version_count}${NC}] added." ${ACCEPT_LEVEL}
}
live_preview(){
echoFuncSeperator "live_preview"
if [[ "$1" == "" ]]; then
target_tex="${project_name}"
else
target_tex="$1"
target_tex="${target_tex%%.tex}"
fi
# try to clean cache at the first version, or clean_cache after compilation for other version number to save time for each iteration
if [[ "${tex_version_count}" == "" ]]; then
clean_cache
fi
lp_str="latexmk -jobname=\"${target_output_name}\" -pvc -pdf -pdflatex=${default_pdf_compiler} \"${target_tex}.tex\""
eval ${lp_str}
# move result to output
mkdir -p "output"
# target_tex.pdf is generated in the ./ dir, so remove any path prefix
target_tex="${target_tex##*/}"
if [[ "${target_output_name}" == "" ]]; then
target_output_name="${target_tex}"
fi
mv "${target_tex}.pdf" "output/""${target_output_name}.pdf"
# version count: count+1 once completed (only count the final version during live_preview)
if [[ "$1" == "" ]]; then
compile_count
backup_project
fi
# clean cache for the next compilation just for saving time
clean_cache
show_debug_msg "${ICON_ACCEPT} : Live preview ended." ${ACCEPT_LEVEL}
}
show_biber_debugInfo(){
biber -D "${project_name}"
}
debug(){
if [[ "${DEBUG_LEVEL}" == "" ]]; then
DEBUG_LEVEL=${DEBUG_LEVEL_MAX}
else
DEBUG_LEVEL=""
fi
}
backup_project(){
# $1 -> src; $2 -> des;
echoFuncSeperator "backup_project"
if [ ! -d "${backup_folder}" ]
then
mkdir "${backup_folder}"
fi
rsync_except_list=(
"${project_specified_conf_folder##*/}"
"output"
".DS_Store"
)
rsync_str="rsync -a"
count=0
for index in "${rsync_except_list[@]}"; do
KEY="${index%%::*}"
DES="${index##*::}"
rsync_str=${rsync_str}" --exclude=\"${KEY}\""
count=$((${count}+1))
done
rsync_str=${rsync_str}" . \"${backup_folder}/bak_${tex_version_count}\""
eval ${rsync_str}
show_debug_msg "${ICON_ACCEPT} : new backup added." ${ACCEPT_LEVEL}
}
tar_project_for_commit(){
echoFuncSeperator "tar_project_for_commit"
tar_except_list=(
"${project_specified_conf_folder##*/}"
"output"
".DS_Store"
)
tar_str="tar"
count=0
for index in "${tar_except_list[@]}"; do
KEY="${index%%::*}"
DES="${index##*::}"
tar_str=${tar_str}" --exclude=\"${KEY}\""
count=$((${count}+1))
done
if [ ! -d "${backup_folder}" ]
then
mkdir "${backup_folder}"
fi
tar_str=${tar_str}" -zcvf \"output/${target_output_name}.tgz\" ."
eval ${tar_str}
show_debug_msg "${ICON_ACCEPT} : the tar file has been created for commit." ${ACCEPT_LEVEL}
}
list_backup(){
# find all current .tex and the recent modification time
current_except_list=(
"${project_specified_conf_folder##*/}"
"output"
)
current_list_str="find \"${project_path}\" -type d"
count=0
for index in "${current_except_list[@]}"; do
KEY="${index%%::*}"
DES="${index##*::}"
current_list_str=${current_list_str}" -name \"${KEY}\" -prune"
count=$((${count}+1))
done
current_list_str=${current_list_str}" -o -type f -iname '*.tex' -print0 | xargs -0 stat -f '%Sm %N,'"
local current_list=`eval "${current_list_str}"`
# sort list
IFS=$','
current_list=($(sort <<<"${current_list[*]}"))
unset IFS
# as sorted, the last one has the mostly recent modification time
last_item=${current_list[$((${#current_list[@]}-1))]//*[[:space:]]\///}
current_ver_time=`date -r "${last_item}" '+%Y-%m-%d %H:%M:%S'`
current_ver_timestamp=`date -r "${last_item}" '+%s'`
echo -e "${grey_time}Files in current version: ${NC}"
# list all
for index in "${!current_list[@]}"; do
KEY=${current_list[index]//*[[:space:]]\///}
KEY_time=`date -r "${KEY}" '+%Y-%m-%d %H:%M:%S'`
KEY=${KEY##*\/}
if [[ ${index} == $((${#current_list[@]}-1)) ]]; then
echo -e "[${index}] ${notify_color}${KEY_time}${NC} ${KEY}"
else
echo -e "${grey_time}[${index}] ${KEY_time} ${KEY}${NC}"
fi
done
# list all backups
lb_except_list=(
"${project_specified_conf_folder##*/}"
)
lb_str="find \"${backup_folder}\" -type d -depth 1"
count=0
for index in "${lb_except_list[@]}"; do
KEY="${index%%::*}"
lb_str=${lb_str}" -not -iname \"${KEY}\""
count=$((${count}+1))
done
lb_str=${lb_str}" -print0 | xargs -0 stat -f '%Sm %N,' | sort"
local bak_list=`eval "${lb_str}"`
# sort list
IFS=$','
bak_list=($(sort <<<"${bak_list[*]}"))
unset IFS
# find mostly recent diff version
candidate_diff=""
for index in "${!bak_list[@]}"; do
KEY="${bak_list[index]//*[[:space:]]\///}"
KEY_time=`date -r "${KEY}" '+%Y-%m-%d %H:%M:%S'`
KEY_timestamp=`date -r "${KEY}" '+%s'`
KEY=${KEY##*\/}
item_str=""
if [[ ${index} == 0 ]]; then
item_str="Available history versions: "
else
item_str=" "
fi
item_str=${item_str}"[${KEY##*_}] ${profile_color}${KEY}${NC} "
if [[ ${KEY_timestamp} -ge ${current_ver_timestamp} ]]; then
# bak has no changes vs. the current version
item_str=${item_str}"${grey_time}${KEY_time} <nothing changed>${NC}"
else
item_str=${item_str}"${highlight_time}${KEY_time}${NC}"
candidate_diff=${KEY##*_}
fi
echo -e "${item_str}"
done
echo -e "Current version modified time: ${notify_color}${current_ver_time}${NC}"
echo -e "Recent changed version: ${profile_color}${candidate_diff}${NC}"
eval "$1=\"${candidate_diff}\""
}
# $1 -> live_OnOff
diff_file(){
echoFuncSeperator "diff_file"
if [[ "${tex_version_count}" == "" ]]; then
show_debug_msg "${ICON_ERR} : No historical version." ${ERR_LEVEL}
return ${ERR_CODE}
else
candidate_diff=''
list_backup candidate_diff
echo -e "Please choose a version to compare (by default use the most recent version ${highlight_time}${candidate_diff}${NC}):"
# read -n1 diff_version
read diff_version
echo ;
if [[ "${diff_version}" =~ ^[0-9]+$ ]] && [[ $((${diff_version})) -ge 0 ]] && [[ $((${diff_version})) -le $((${tex_version_count})) ]]; then
old_tex="${backup_folder}/bak_${diff_version}/${project_name}.tex"
elif [[ "${diff_version}" == "" ]] && [[ ! "${candidate_diff}" == "" ]]; then