Repository navigation
Expand file tree
/
Copy pathrender
More file actions
executable file
·329 lines (279 loc) · 9.34 KB
/
Copy pathrender
File metadata and controls
executable file
·329 lines (279 loc) · 9.34 KB
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
#!/usr/bin/env zsh
#
# Copyright (C) 2015-2019 Dyne.org Foundation
#
# Designed, written and maintained by Denis Roio <jaromil@dyne.org>
#
# This source code is free software; you can redistribute it
# and/or modify it under the terms of the GNU Public License
# as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later
# version.
#
# This source code is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. Please refer to the GNU Public License for more
# details.
#
# You should have received a copy of the GNU Public License
# along with this source code; if not, write to: Free
# Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
# 02139, USA.
# full path to writedown's system
[[ -r /usr/local/writedown ]] && R=/usr/local/writedown
S=`pwd`
[[ -d ${S}/writedown ]] && R=${S}/writedown
# this nails invocation to be from inside the project's directory
writedown_version=0.5
writedown_release_date="Aug/2017"
source $R/zuper/zuper
vars+=(x_pandoc f_bib)
vars+=(input_format input_ext)
vars+=(output_format output_ext)
vars+=(section)
vars+=(book_title book_filename)
# for gettext
TEXTDOMAIN=writedown
# export DOWSE to env with path to the installation
# else start it from inside the dir with source dowse
DEBUG=${DEBUG:-0}
QUIET=${QUIET:-0}
LOG=${LOG:-""}
# conclude initialization
source $R/zuper/zuper.init
func "args: $*"
# load project configuration
[[ "$WRITEDOWN_CONFIG" = "" ]] && {
[[ -r /etc/writedown/config.zsh ]] && \
WRITEDOWN_CONFIG=/etc/writedown/config.zsh
[[ -r config.zsh ]] && WRITEDOWN_CONFIG=config.zsh }
[[ "$WRITEDOWN_CONFIG" = "" ]] || {
act "loading config: $WRITEDOWN_CONFIG"
source $WRITEDOWN_CONFIG
}
book_title=${WRITEDOWN_TITLE:-"Writedown Example Book"}
book_filename=${WRITEDOWN_FILENAME:-${book_title// /_}}
book_author=${WRITEDOWN_AUTHOR:-Anonymous}
book_affiliation=${WRITEDOWN_AFFILIATION:-Made with Writedown by Dyne.org}
book_date=${WRITEDOWN_DATE:-`date +'%A, %d %B, %Y'`}
book_nrsec=${WRITEDOWN_NRSEC:-no}
book_toc=${WRITEDOWN_TOC:-yes}
book_class=${WRITEDOWN_CLASS:-article}
book_citationstyle=${WRITEDOWN_CITSTYLE:-harvard-kings-college-london}
book_fontsize=${WRITEDOWN_FONTSIZE:-14pt}
book_tags=${WRITEDOWN_TAGS:-"[comma, separated, words, between, square, brackets]"}
# experimental, until extras/pandoc-zotxt is fixed
book_zotero=${WRITEDOWN_ZOTERO:-no}
input_format=${WRITEDOWN_INPUT:-markdown}
input_ext=${WRITEDOWN_INPUT_EXT:-md}
f_bib=${WRITEDOWN_BIB:-$S/views/references.bib}
x_pandoc=${WRITEDOWN_PANDOC:="pandoc --listings --standalone -f $input_format "}
[[ $DEBUG != 0 ]] && x_pandoc+=" --verbose "
# add bibliographic references to pandoc rendering if file is present
if [[ "$book_zotero" = "yes" ]]; then
x_pandoc+=" --filter pandoc-zotxt --filter pandoc-citeproc"
elif [[ -r $f_bib ]]; then
x_pandoc+=" --filter pandoc-citeproc --bibliography $f_bib "
fi
[[ "$*" = "source" ]] && return 0
WRITEDOWN_OUTPUT_FORMAT=${1:-$WRITEDOWN_OUTPUT_FORMAT}
[[ "$WRITEDOWN_OUTPUT_FORMAT" = "" ]] && WRITEDOWN_OUTPUT_FORMAT=docx
WRITEDOWN_OUTPUT_EXTENSION=${1:-$WRITEDOWN_OUTPUT_EXTENSION}
[[ "$WRITEDOWN_OUTPUT_EXTENSION" = "" ]] && WRITEDOWN_OUTPUT_EXTENSION=docx
correct_extended_chars() {
fn correct_extended_chars
local f out dest
local extended corrected
local errl corrl
f="$1"
out="$2"
req=(f out)
freq=($f)
ckreq || return 1
ztmp
dest=$ztmpfile
cp $f $dest
extended=`perl -ane '{ if(m/[[:^ascii:]]/) { print } }' $dest`
if [[ ${#extended} -gt 0 ]]; then
errl=`print $extended | wc -l`
act "correcting invalid characters in $f"
if [[ "$(uname) " = "Darwin " ]]; then
sed -i '' -e "s/‘/'/g; s/’/'/g; s/…/.../g" $dest
sed -i '' -e 's/“/"/g; s/”/"/g; s/¨/"/g' $dest
else
sed -i'' \
-e "s/‘/'/g; s/’/'/g; s/…/.../g" \
-e 's/“/"/g; s/”/"/g; s/¨/"/g' $dest
fi
corrected=`perl -ane '{ if(m/[[:^ascii:]]/) { print } }' $dest`
corrl=`print $corrected | wc -l`
print
print - "$corrected"
print
warning "extended characters detected on $errl lines:"
warning "lines listed above may break the pdf rendering"
warning "attempted fix..."
if [[ $corrl != $errl ]]; then
notice "corrected $(( $errl - $corrl )) lines"
else
error "cannot apply autocorrection on any of the lines above"
fi
fi
cp $dest $out
rm -f $ztmpfile
}
# If you use the --template option it ignores the -H
# --include-in-header option so we move the include into the
# template.tex
case $WRITEDOWN_OUTPUT_FORMAT in
pdf|latex)
notice "Configuring pdf/latex output format"
if [[ "$WRITEDOWN_LATEX_TEMPLATE" != "" ]] &&
[[ -r "$WRITEDOWN_LATEX_TEMPLATE" ]]; then
tmpl=$WRITEDOWN_LATEX_TEMPLATE
else
try=/etc/writedown/template.tex; [[ -r $try ]] && tmpl=$try
try=$R/template.tex; [[ -r $try ]] && tmpl=$try
try=$S/template.tex; [[ -r $try ]] && tmpl=$try
try=$S/views/template.tex; [[ -r $try ]] && tmpl=$try
fi
[[ -r $tmpl ]] || {
error "Latex template not found ($tmpl)"
error "set WRITEDOWN_LATEX_TEMPLATE in config.zsh"
error "default: views/template.tex (from $R/template.tex)"
return 1 }
act "latex template: $tmpl"
x_pandoc+=" --template=$tmpl "
if [[ "$WRITEDOWN_OUTPUT_FORMAT" = "latex" ]]; then
output_format=latex
output_ext=tex
act "rendering to latex"
elif [[ "$WRITEDOWN_OUTPUT_FORMAT" = "pdf" ]]; then
output_format=latex
output_ext=pdf
act "rendering to pdf"
fi
;;
docx)
output_format=docx
output_ext=docx
;;
*)
output_format=$WRITEDOWN_OUTPUT_FORMAT
output_ext=$WRITEDOWN_OUTPUT_EXTENSION
;;
esac
# optional second argument, single section (file) to render
section="$2"
[[ $DEBUG != 0 ]] && zdump
[[ "$section" = "" ]] || {
source=""
try="views/${section}.$input_ext"; [[ -r "$try" ]] && source="$try"
try="views/${section}"; [[ -r "$try" ]] && source="$try"
try="${section}.$input_ext"; [[ -r "$try" ]] && source="$try"
try="${section}"; [[ -r "$try" ]] && source="$try"
[[ "$source" = "" ]] && {
error "$section not found"
return 1 }
notice "Rendering: $source"
act "to format $output_format with extension $output_ext"
ztmp
tmpsrc=$ztmpfile
correct_extended_chars "$source" $tmpsrc
if [[ -d writedown ]]; then # we are inside a project
output="$S/pub/${book_filename}-${section/\//-}.$output_ext"
else
# writedown called around from outside a project for the
# conversion of a single file
output="$S/${book_filename}.$output_ext"
fi
# reset output file
[[ -r "$output" ]] && rm -f "$output"
act "rendering to $output"
pushd $S/views
func "${=x_pandoc} -t ${output_format} $tmpsrc -o $output"
${=x_pandoc} -t ${output_format} $tmpsrc -o $S/$output
res=$?
popd
[[ $res != 0 ]] && {
error "Error in pandoc, returned code: $res"
return $res
}
return 0
}
# render all files in views
# create temp dir
ztmp
frontpage=$ztmpfile
cslstyle=$ztmpfile
cat <<EOF > $frontpage
---
title: ${book_title}
author: ${book_author}
affiliation: ${book_affiliation}
date: ${book_date}
pdf_document:
keep_tex: yes
number_sections: ${book_nrsec}
toc: ${book_toc}
documentclass: ${book_class}
fontsize: ${book_fontsize}
bibliography: ["${f_bib}"]
csl: $R/citstyle/${book_citationstyle}.csl
keywords: ${book_tags}
EOF
[[ -r views/abstract.txt ]] && {
print "abstract: |\n" >> $frontpage
for i in ${(f)"$(cat views/abstract.txt)"}; do
## add two spaces before each line, else not valid yml
if [[ "$i[2]" != " " ]]; then
print " $i" >> $frontpage
else
print "$i" >> $frontpage
fi
done
}
# [[ $DEBUG = 0 ]] || {
# notice "Frontpage rendered:"
# cat $frontpage
# }
views=(`grep -v '^#' views/index.txt`)
[[ ${#views} = 0 ]] && {
error "Nothing found in views, please write down something!"
return 1 }
for i in $views; do
f="$i"
[[ -r "$f" ]] || f="views/$i"
[[ -r "$f" ]] || {
error "error in views/index.txt"
error "file not found: $i"
return 1
}
act "rendering file: ::1 path::" $f
ztmp
dest=$ztmpfile
correct_extended_chars $f $dest
cat <<EOF >> $frontpage
`cat $dest`
EOF
done
[[ -r $f_bib ]] && {
cat <<EOF >> $frontpage
# Bibliography
EOF
}
# reset output file
output=${WRITEDOWN_OUTPUT:-$S/pub/$book_filename.$output_ext}
[[ -r "$output" ]] && rm -f "$output"
# render using pandoc
notice "pandoc-cmd: $x_pandoc -t $output_format $frontpage -o $output"
func "$x_pandoc -t $output_format $frontpage -o $output"
pushd $S/views
${=x_pandoc} -t $output_format $frontpage -o "$output"
[[ $? = 0 ]] && {
notice "Succesful rendering to file: pub/$book_filename.$output_ext"
ls -lh $S/pub/$book_filename.$output_ext
file $S/pub/$book_filename.$output_ext
}
popd