-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikitex.sh
executable file
·210 lines (176 loc) · 6.21 KB
/
wikitex.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
#!/usr/bin/env bash
# wikitex.sh: shell interface to wikitex.php
# Assumes that $PWD is the base wiki dir.
# Usage: FILE MODULE OUTPATH
#
# WikiTeX: expansible LaTeX module for MediaWiki
# Copyright (C) 2004-6 Peter Danenberg
#
# WikiTeX is licensed under the Artistic License 2.0; to
# view a copy of this license, see COPYING or visit:
#
# http://dev.perl.org/perl6/rfc/346.html
#
declare -ri ARGS=3
declare -ri E_ARGS=2
declare -ri E_FAIL=1
declare -ri E_SUC=0
declare -r HASH="${1}"
declare -r MOD="${2}"
declare -r OUT="${3}"
declare -r EXT='.png'
declare -r CHE='.cache'
declare -r MID='.midi'
declare -r MAP='.map'
declare -r EPS='.eps'
declare -r SUDO='wikitex'
declare -r ERR='<span class="errwikitex">WikiTeX: %s reported a failure, namely:</span><pre>Error code: %d\n%s</pre>\n'
declare -ra LIMIT=(\
0 \ # The maximum size of core files created
unlimited \ # The maximum size of a process's data segment
unlimited \ # The maximum size of files created by the shell
1024 \ # Pending signals
32 \ # The maximum size that may be locked into memory
unlimited \ # The maximum resident set size
1024 \ # The maximum number of open file descriptors (most systems
\ # do not allow this value to be set)
8 \ # The pipe size in 512-byte blocks (this may not be set)
819200 \ # POSIX message queues
10240 \ # The maximum stack size
30 \ # The maximum amount of cpu time in seconds
8185 \ # The maximum number of processes available to a single
\ # user
unlimited \ # The maximum amount of virtual memory available to the
\ # shell
unlimited \ # File locks
)
function wt_error() {
printf "${ERR}" "${1}" ${?} "${2}"
exit ${3}
}
# Preliminaries
(( ${#} >= ${ARGS} )) || wt_error 'wikitex.sh' 'usage: wikitex.sh HASH MODULE OUTPATH' $E_ARGS
# $PWD is initially the wiki-base.
cd "$(dirname "${0}")/tmp" || wt_error 'wikitex.sh' 'unable to change to cache directory' $E_FAIL
# Check cache
[ -r "${HASH}${CHE}" ] && { cat "${HASH}${CHE}"; exit $E_SUC; }
# Requirement: scribe tmp, read hash
[ -w '.' ] || wt_error 'wikitex.sh' "Can't scribe <code>${PWD}</code>; ownership and permissions should allow us to do so, precious." $E_FAIL
[ -r "${HASH}" ] || wt_error 'wikitex.sh' "Can't hash, baby; ownership and permissions should allow us to perceive <code>${PWD}</code>." $E_FAIL
# Perform self-limitation
ulimit -c ${LIMIT[0]} -d ${LIMIT[1]} -f ${LIMIT[2]} -n ${LIMIT[6]} -s ${LIMIT[9]} -t ${LIMIT[10]} -v ${LIMIT[12]} || wt_error 'wikitex.sh' 'ulimit failed.' $E_FAIL
# Move into work directory
cd ${HASH} || wt_error 'wikitex.sh' 'unable to change into work directory.' $E_FAIL
# Extract useful files from work directory
function wt_out() {
wt_exec "mv ${@} ../"
}
# Output image
function wt_img() {
STR="$STR"$(printf "<img src=\"%s\" alt=\"${MOD}\" />" "${1}")
}
# Output link
function wt_anch() {
STR=$(printf '<a href="%s">%s</a>' "${OUT}${HASH}" "${STR}")
}
# Convert to final image
function wt_dvipng() {
wt_sudo "dvipng -gamma 1.5 -T tight ${HASH}"
}
# Trim whitespace
function wt_trim() {
wt_sudo "mogrify -trim ${@}"
}
# Generic execution, which allows for error trapping
function wt_exec() {
PUT=$(${@} 2>&1) || wt_error "${@%%\ *}" "${PUT}" $E_FAIL
}
# Generic sudoing; use exec to protect product from being over-written
# by wt_sudo actions.
function wt_sudo() {
PUT=$(sudo -u ${SUDO} ${@} 2>&1) || wt_error "${@%%\ *}" "${PUT}" $E_FAIL
}
# Catch-all renderer
function wt_generic() {
wt_sudo "latex --interaction=nonstopmode ${HASH}"
wt_dvipng
for i in ${HASH}*${EXT}; do wt_img "${OUT}${i}"; done
wt_anch
}
function wt_graph_map() {
wt_sudo "${1} -Tpng -o ${HASH}${EXT} ${HASH}"
wt_sudo "${1} -Tcmap -o ${HASH}${MAP} ${HASH}"
STR=$(printf '<map name="%s">%s</map>' "${HASH}" "$(<"${HASH}${MAP}")")
STR="${STR}"$(printf '<img src="%s" alt="%s" usemap="#%s"/>' "${OUT}${HASH}${EXT}" "${MOD}" "${HASH}")
STR="${STR}"$(printf '<a href="%s">[source]</a>' "${OUT}${HASH}")
wt_out "${HASH}${MAP}"
}
function circo() {
wt_graph_map "circo"
}
function go() {
wt_sudo "sgf2dg -twoColumn ${HASH}"
wt_sudo "tex --interaction=nonstopmode ${HASH}"
wt_dvipng
for i in ${HASH}*${EXT}; do wt_sudo "mogrify -crop +0-24! ${i}"; wt_trim "${i}"; wt_img "${OUT}${i}"; done
wt_anch
}
function fdp() {
wt_graph_map "fdp"
}
function feyn() {
wt_sudo "latex ${HASH}"
wt_sudo "mpost fmftempl.mp"
wt_sudo "latex ${HASH}"
wt_dvipng
for i in ${HASH}*${EXT}; do wt_img "${OUT}${i}"; done
wt_anch
}
function graph() {
wt_graph_map "dot"
}
function music() {
wt_sudo "lilypond -s -f png -b eps ${HASH}"
for i in ${HASH}*${EXT}; do wt_trim "${i}"; wt_img "${OUT}${i}"; done
wt_anch
# Sourced files may not produce midi
if [ -r "${HASH}${MID}" ]; then
STR="$STR"$(printf '<a href="%s">[listen]</a>' "${OUT}${HASH}${MID}")
wt_out "${HASH}${MID}"
fi
}
function neato() {
wt_graph_map "neato"
}
function plot() {
sed -i -e "s/\%OUTPUT\%/${HASH}${EXT}/" "${HASH}"
wt_sudo "gnuplot ${HASH}"
wt_trim "${HASH}${EXT}"
wt_img "${OUT}${HASH}${EXT}"
wt_anch
}
function svg() {
wt_sudo "convert -size 640x480 ${HASH} ${HASH}${EXT}"
wt_trim "${HASH}${EXT}"
wt_img "${OUT}${HASH}${EXT}"
wt_anch
}
function twopi() {
wt_graph_map "twopi"
}
# Check for module-specific functions; otherwise resort to generic.
if [[ $(type -t "${MOD}") == 'function' ]]; then
"${MOD}" "${HASH}" "${OUT}"
else
wt_generic "${HASH}" "${OUT}"
fi
# Extract images (additional files per class supra)
wt_out ${HASH}*${EXT}
# Move into cache directory; remove work files; extract source
{ cd ..; rm -fr ${HASH}; cat > "${HASH}"; } < "${HASH}" || wt_error 'wikitex.sh' 'unable to remove work directory or add cache source' $E_FAIL
# Cache
echo "${STR}" > "${HASH}${CHE}"
# Produce result
echo "${STR}"
# Adieu
exit ${E_SUC}