This repository has been archived by the owner on Nov 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maketex.sh
executable file
·162 lines (119 loc) · 3.1 KB
/
maketex.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
#!/usr/bin/env bash
## function declaration begin
die() {
echo "$1" 1>&2
exit 1
}
# ${MAINFILE_LOCATION}/figures
function get_path_to_figure()
{
local location="./figures"
if [[ ! $# -eq 0 ]]; then
if [[ -d $1 ]]; then
location="$1"
fi
fi
if [[ ! -d "${location}" ]]; then
location="."
fi
echo $location
}
function find_png_a_pdf()
{
find $1 \( -name '*.png' -or -name '*.pdf' \) -maxdepth 1
}
function e_x_bound_box()
{
find_png_a_pdf $1 | xargs extractbb
}
function e_bound_box()
{
find_png_a_pdf $1 | xargs ebb
}
function gen_eps()
{
find_png_a_pdf "$1" | xargs geneps
}
function move_file()
{
[[ ! -d $1 ]] && mkdir -p "$1"
mv -f $2 "$1/"
}
function get_tex_opt()
{
local encode_type=$(nkf --guess "$1")
case "${encode_type}" in
/shift.jis*/i) # Shift_JIS, Shift-jis
echo "-kanji=sjis"
;;
/utf-8*/i) # utf-8
echo "-kanji=utf8"
;;
/euc-jp*/i | /japanese-iso-8bit*/i) # euc
echo "-kanji=euc"
;;
/iso-2022-jp*/i) # jis
echo "-kanji=jis"
;;
*) # no opt
echo ""
;;
esac
}
get_bibfile() {
local texfile="$1"
grep -o "bibliography{[^}]*}" "$texfile"|sed -e 's/[^{]*{\([^}]*\)}/\1/' || echo
}
is_main_file() {
if [[ $(head -1 "$1"|tr -d "\t ") = "%maketex:main" ]]; then
echo "$1"
fi
}; export -f is_main_file
expand_path() { # use Ruby
ruby -e 'puts File.expand_path(ARGV[0])' "$1"
}
filename_only() {
ruby -e 'puts File.basename(ARGV[0], ".tex")' "$1"
}
main() {
local -r output_dir="${HOME}/.maketex.d"
local file_location="$(expand_path $1)"
local -r logfile="${output_dir}/logs/$(date +%s).log"
mkdir -p "$output_dir/logs"
{
set -x
# pre-condition : The main tex file is only one in the directory.
if [[ ! -d "$file_location" ]]; then
file_location="${file_location%/*}"
fi
local filepath=$(find "${file_location}" -name "*.tex" -print0|xargs -0 -I{} bash -c "is_main_file {}"|head -1) || die "main file not found."
gen_eps "${file_location}/figures"
local filename_wo_suffix="$(filename_only "$filepath")"
local platex_options=$(get_tex_opt "$filepath")
cd "${file_location}"
# first platex for aux
platex -output-directory "$output_dir" "${filename_wo_suffix}" $platex_options
local bibfile_wo_suffix=$(get_bibfile "$filepath")
cp "${bibfile_wo_suffix}.bib" "${output_dir}/"
cd "$output_dir"
# generate bbl
pbibtex "${filename_wo_suffix}"
# bbl
sed -i -e 's/^\\newblock//g' "${filename_wo_suffix}.bbl"
sed -i -e 's/^: //g' "${filename_wo_suffix}.bbl"
# sed -i -e 's/^\\par//g' $FILE_NAME_BBL
cp -f "${filename_wo_suffix}.bbl" "${file_location}/"
cd "$file_location"
# refrect bbl to dvi
platex -output-directory "$output_dir" "${filename_wo_suffix}" $platex_options
# resolve each-refs
platex -output-directory "$output_dir" "${filename_wo_suffix}" $platex_options
# convert dvi to pdf
dvipdfmx -o "./${filename_wo_suffix}.pdf" "${output_dir}/${filename_wo_suffix}.dvi"
# open pdf with Preview
open -a Preview "./${filename_wo_suffix}.pdf"
set +x
} > "$logfile"
}
## function declaration end
main "$@"