forked from transpect/docx2tex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd2t
executable file
·220 lines (197 loc) · 4.73 KB
/
d2t
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
#!/bin/bash
usage() {
echo ""
echo "docx2tex"
echo ""
echo "Usage: d2t [options ...] <docx file>"
echo ""
echo "Options:"
echo " -o path to custom output directory"
echo " -c path to custom word2tex configuration file"
echo " -p generate PDF with pdflatex"
echo " -r omit page and cross-references and labels"
echo " -t use tabular instead of tabularx for tables"
echo " -l draw table grid lines"
echo " -x custom XSLT stylesheet for Hub processing"
echo " -d debug mode"
1>&2; exit 1;
}
# print out error step and exit code
function exitonerror {
echo "Errors encountered while running $2. Please see $LOG for details."
exit 1
}
# readlink -f is unavailable on Mac OS X
function real_dir() {
SOURCE="$1"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
echo "$( cd -P "$( dirname "$SOURCE" )" && pwd )"
}
# cygwin check
cygwin=false;
case "`uname`" in
CYGWIN*) cygwin=true;
esac
# script directory
PWD="$( pwd)"
DIR="$( real_dir "${BASH_SOURCE[0]}" )"
CALABASH=$DIR/calabash/calabash.sh
# invoke pdflatex
PDFLATEX=pdflatex
# specify options
while getopts ":o:c:x:ptlrd" opt; do
case "${opt}" in
o)
OUT_DIR=${OPTARG}
;;
c)
CONF=${OPTARG}
;;
x)
CUSTOMXSL=${OPTARG}
;;
p)
PDF=yes
;;
t)
TABLEMODEL=tabular
;;
l)
TABLEGRID=yes
;;
r)
REFS=no
;;
d)
DEBUG=yes
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage
;;
esac
done
shift $((OPTIND-1))
# check if argument for file is set
if [ -z "$1" ]; then
usage
fi
# file as 1st argument
FILE="$1"
if ! [[ $FILE =~ \.docx$ ]]; then
echo "Only docx files are supported."
exit 1
fi
# set basename
BASENAME="$(basename "$(basename "$FILE" .docx)" .docm)"
BASENAME="${BASENAME// /_}"
# output directory path
if [[ -z "$OUT_DIR" ]]; then
OUT_DIR="$(real_dir "$FILE")"
else
if [[ "$OUT_DIR" != /* ]]; then
OUT_DIR="$PWD/$OUT_DIR"
fi
fi
mkdir -p "$OUT_DIR"
# table-model
if [[ -z "$TABLEMODEL" ]]; then
TABLEMODEL="tabularx"
fi
# table-grid
if [[ -z "$TABLEGRID" ]]; then
TABLEGRID="no"
fi
# references
if [[ -z "$REFS" ]]; then
REFS="yes"
fi
# path to configuration file
if [[ -z "$CONF" ]]; then
CONF="$DIR/conf/conf.csv"
else
if [[ "$CONF" != /* ]]; then
CONF="$PWD/$CONF"
fi
fi
# logging
LOG="$OUT_DIR/$BASENAME.d2t.log"
# remove log from previous runs
if [ -e "$LOG" ]; then
rm "$LOG"
fi
# debugging
DEBUG_DIR="$OUT_DIR/$BASENAME.debug"
# pdflatex outdir path
PDFLATEX_OUT="$OUT_DIR"
# make absolute paths
if $cygwin; then
FILE="$(cygpath -ma "$FILE")"
OUT_DIR="$(cygpath -ma "$OUT_DIR")"
DIR="$(cygpath -ma "$DIR")"
CONF="$(cygpath -ma "$CONF")"
DEBUG_DIR_URI=file:/$(cygpath -ma "$DEBUG_DIR" )
else
DEBUG_DIR_URI="file:$DEBUG_DIR"
fi
DEBUG_DIR_URI="${DEBUG_DIR_URI// /%20}"
OUT_DIR_PSEUDOURI="${OUT_DIR// /%20}"
FILE_CP="$OUT_DIR/$BASENAME.docx"
cp "$FILE" "$FILE_CP" 2>&1 2>>"$LOG"
# check if file exists
if [ ! -f "$FILE" ]; then
echo "Error: input file not found: $FILE"
usage
fi
echo "starting docx2tex"
if [ "$DEBUG" = "yes" ]; then
echo "debug mode: $DEBUG"
echo "storing debug files to $DEBUG_DIR"
echo ""
echo "Parameters"
echo " workdir: $DIR"
echo " outdir: $OUT_DIR"
echo " file: $FILE"
echo " config: $CONF"
echo " refs and labels: $REFS"
echo " table model: $TABLEMODEL"
echo " draw table grid lines: $TABLEGRID"
echo " custom xslt: $CUSTOMXSL"
echo ""
fi
# docx2tex xproc pipeline
$CALABASH \
-o result="$OUT_DIR_PSEUDOURI/$BASENAME.tex" \
-o hub="$OUT_DIR_PSEUDOURI/$BASENAME.xml" \
"$DIR/xpl/docx2tex.xpl" \
docx="$FILE_CP" \
conf="$CONF" \
custom-xsl="$CUSTOMXSL" \
conf-template="$OUT_DIR/$BASENAME.csv" \
table-model=$TABLEMODEL \
table-grid=$TABLEGRID \
refs=$REFS \
debug=$DEBUG \
debug-dir-uri=$DEBUG_DIR_URI \
status-dir-uri=$DEBUG_DIR_URI/status \
2>&1 2>>"$LOG" || exitonerror $? docx2tex
echo "writing texfile => $OUT_DIR/$BASENAME.tex"
# run pdflatex with "-p"
if [ "$PDF" = "yes" ]; then
cd "$OUT_DIR" && "$PDFLATEX" \
-interaction=nonstopmode \
-output-directory="$OUT_DIR" \
"$PDFLATEX_OUT/$BASENAME.tex" \
1>>"$LOG" || exitonerror $? pdflatex
echo "writing pdf => $OUT_DIR/$BASENAME.pdf"
fi
echo ""
echo "docx2tex finished."