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
|
#! /bin/sh
### listings-ext.sh ---
## Author: Dr. Jobst Hoffmann <j.hoffmann@fh-aachen.de>
## Version: $Id: listings-ext.dtx 58 2010-05-18 14:23:40Z ax006ho $
## Keywords: LaTeX, listings
## Copyright: (C) 2008-2010 Jobst Hoffmann, <j.hoffmann@fh-aachen.de>
##-------------------------------------------------------------------
##
## This file may be distributed and/or modified under the
## conditions of the LaTeX Project Public License, either version 1.2
## of this license or (at your option) any later version.
## The latest version of this license is in:
##
## http://www.latex-project.org/lppl.txt
##
## and version 1.2 or later is part of all distributions of LaTeX
## version 1999/12/01 or later.
##
## This file belongs to the listings-ext package.
##
## listings-ext.sh creates code to be inserted into a
## LaTeX file from a source file.
##
## The commented code can be seen in listings-ext.pdf, which is provided
## with the distribution package.
ERR_HELP=1
ME=$(basename $0)
USAGE="usage:\n\t${ME} [-c|--command] [-e|--ext] [-g|--debug] \\\\\n\
\t\t[-h|--help] [-n|--no-header] \\\\\n\
\t\t[{-o|--output-file} [<output filename>]] <filename>, ...\n\
\t-c:\tgenerate commands, which can be input and later used by\n\
\t\t\\\\lstuse, optional\n\
\t-e:\tinclude file name extension into the identifier\n\
\t-g:\tdebug mode, create some additional output, optional\n\
\t-h:\tshow this help, optional\n\
\t-n:\twrite no header into the LaTeX code, optional; valid only,\n\
\t\tif -c isn't used\n\
\t-o [<output filename>]: if this argument is present, the output will\n\
\t\tbe written into a file <output filename>; if the\n\
\t\t<output filename> is omitted, the output is redirected\n\
\t\tinto a file with a basename corresponding to the name\n\
\t\tof the current directory, it has an extension of \".lst\".\n\
"
DEBUG_MODE=0
EXTENSION=0
HEADER=1
show_usage() # display help massage
{
printf "${USAGE}"
exit ${ERR_HELP}
}
print_header()
{
FILE=$1
echo "%% -- file $1 generated on $(date) by ${ME}"
FILE=$(echo $(basename $1 .lst) | sed -e s/[_\-]//g)
echo "\\csname ${FILE}LstLoaded\\endcsname"
echo "\\expandafter\\let\\csname ${FILE}LstLoaded\\endcsname\\endinput"
}
do_the_job()
{
PATHCOMPONENT=$(pwd)
SOURCEFILE=$1
if [ ! -f ${SOURCEFILE} ]
then
echo ${SOURCEFILE} is no valid file
return $ERR_NOFILE
fi
SOURCEFILE=${SOURCEFILE##${PATHCOMPONENT}/}
grep -n "^ *[/%;#!][/\* ;]\ *[cbe]e:" $1 | \
awk -v pathcomponent="${PATHCOMPONENT}" -v file="${SOURCEFILE}" \
-v header="${HEADER}" -v command="${COMMAND}" -v application="${ME}" \
-v debugmode="${DEBUG_MODE}" -v respect_extension="${EXTENSION}"\
-F : \
'
BEGIN {
is_opened = 0;
initialised = 0;
configured = 0;
join = 0;
combine = 0;
idChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
split(idChar, idArray, "")
idPt = 0
linerange = "";
linelen = 75; # length of the output line
addchars = 8; # number of additional characters like %, > or so
if ( debugmode ) printf("path: %s\n", pathcomponent) > "/dev/stderr"
if ( debugmode ) printf("file: %s\n", file) > "/dev/stderr"
n = split(pathcomponent, parts, "/");
curr_dir = parts[n]
n++;
parts[n] = file;
inputfile = "/" parts[2];
for ( i = 3; i <= n; i++ ) {
if ( length(inputfile) + length(parts[i]) < linelen - 1) {
inputfile = inputfile "/" parts[i];
} else {
inputfile = inputfile "%\n /" parts[i];
}
}
n = split(file, parts, "/")
n = split(parts[n], parts, ".")
identifier = parts[1]
for ( i = 2; i < n; i++ )
{
identifier = identifier parts[i]
}
if ( respect_extension ) {
identifier = identifier parts[n]
}
identifier = replace_characters(identifier)
if ( debugmode ) printf("identifier: %s\n", identifier) > "/dev/stderr"
}
{
if ( debugmode ) print $0 > "/dev/stderr"
if ( !initialised ) {
if ( match($2, /ce/) > 0 ) {
n = split($(NF), text, ",");
if ( match(text[1], "join") ) {
join = 1;
} else if ( match(text[1], "combine") ) {
combine = 1;
}
for ( i = 2; i <= n; i++ ) {
}
} else if ( match($2, /be/) > 0 ) {
opening_tag = $(NF)
is_opened = 1
start = $1 + 1; # entry $1 is the line number
} else if ( match($2, /ee/) > 0 ) {
print_error($1, "missing \"be\" declaration")
}
initialised = 1;
} else {
tag = $(NF)
if ( match($2, /be/) > 0 ) {
if ( is_opened ) {
print_error($1, "incorrect grouping, previous group"\
" not closed")
} else {
opening_tag = $(NF)
is_opened = 1
start = $1 + 1; # entry $1 is the line number
}
} else {
if ( match($2, /ee/) > 0 ) {
closing_tag = $(NF)
if ( !is_opened ) {
print_error($1, "missing \"be\" declaration")
} else if ( opening_tag == closing_tag ) {
is_opened = 0
split($(NF), text, "*"); # omit a trailing comment
sub(/ /, "", text[1]); # delete leading spaces
gsub(/"/, "", text[1]);
if ( index(text[1], "_") > 0 ) gsub(/_/, "\\_", text[1]);
caption = "\\lstinline|" file "|: " text[1]
# setup the prefixes
len = linelen - addchars - length(caption);
begin_prefix = "%";
cnt = 0;
while ( cnt < len) {
begin_prefix = begin_prefix "=";
cnt++;
};
begin_prefix = begin_prefix ">";
end_prefix = begin_prefix;
sub(/%/, "%==", end_prefix);
if ( join ) {
linerange = linerange ", " start "-" $1-1;
} else if ( combine ) {
sub(/ /, "", closing_tag)
if ( combine_array[closing_tag] == "" ) {
combine_array[closing_tag] = start "-" $1-1
} else {
combine_array[closing_tag] = \
combine_array[closing_tag] ", " start "-" $1-1
}
if ( debugmode ) printf("combine_array: >%s<\n",\
combine_array[closing_tag]) > "/dev/stderr"
} else {
linerange = start "-" $1-1;
if ( command ) {
print_command(\
(identifier toB26(idPt++)), \
linerange);
} else {
print_linerange(\
(identifier toB26(idPt++)), \
caption, linerange);
}
}
} else if ( opening_tag != closing_tag ) {
print_error($1, "opening and closing tags differ")
} else {
print_error($1, "unknown error")
}
}
}
}
}
END {
if ( is_opened ) {
print_error($1, "missing \"ee\" declaration")
}
caption = "\\lstinline|" file "|";
if ( join ) {
sub(/, /, "", linerange);
if ( command ) {
print_command(identifier, linerange);
} else {
caption = "\\lstinline|" file "|";
print_linerange(identifier, caption, linerange);
}
} else if ( combine ) {
for ( range in combine_array ) {
if ( debugmode ) printf("range: %s, combine_array[range]: >%s<\n", \
range, combine_array[range]) > "/dev/stderr"
printf "%%-->> %s <<--\n", range
if ( command ) {
print_command((identifier toB26(idPt++)), \
combine_array[range]);
} else {
print_linerange((identifier toB26(idPt++)), caption, \
combine_array[range]);
}
}
}
}
function replace_characters(identifier)
{
tmp = ""
toUppercase = 0
n = split(identifier, sequence, "") # split the string into an array
# of single characters
for ( i = 1; i <= n; i++ )
{
if ( (sequence[i] == "_") || (sequence[i] == "-") ) {
toUppercase = 1
} else {
if ( toUppercase ) {
cTmp = toupper(sequence[i])
tmp = (tmp cTmp)
toUppercase = 0
} else {
tmp = (tmp sequence[i])
}
}
}
return tmp
}
function print_command(identifier, linerange)
{
if ( debugmode ) printf("print_command.linerange: >%s<\n", linerange) > "/dev/stderr"
print break_line("\\lstdef{" identifier "}{" inputfile "}{" \
linerange "}", linelen)
}
function print_linerange(identifier, caption, linerange)
{
print begin_prefix "begin{" \
identifier"}\n{%\n \\def\\inputfile{%\n " inputfile "\n }";
local_style = "";
if ( header )
{
print " \\ifLecture{%\n \\lstdefinestyle{localStyle}{%\n " \
"caption={" caption"}\n }%\n }\n {%\n" \
" \\lstdefinestyle{localStyle}{%\n" \
" title={" caption "}\n }%\n }";
local_style="style=localStyle, "
}
print " \\lstinputlisting[" local_style "linerange={" linerange "}]" \
"%\n {%\n \\inputfile\n }\n" \
"}%\n" end_prefix "end{"identifier"}";
}
function print_error(linenumber, error_message)
{
printf "%--> error (line number %d): %s\n", \
linenumber, error_message > "/dev/stderr"
}
function break_line(input_line, line_len)
{
n = split(input_line, parts, "/");
output_line = parts[1];
len_curr = length(output_line)
for ( i = 2; i <= n; i++ ) {
len_next = length(parts[i])
if ( len_curr + len_next + 1 < linelen ) {
output_line = output_line "/" parts[i];
len_curr += len_next + 1
} else {
output_line = output_line "%\n /" parts[i];
len_curr = len_next + 7
}
}
return output_line
}
function toB26(n10)
{
v26 = ""
do {
remainder = n10%26
v26 = idArray[remainder+1] v26
n10 = int(n10/26)
} while ( n10 != 0 )
return v26
}
'
return $?
}
if [ $# -eq 0 ]
then
show_usage
fi
GETOPT=$(getopt -o ceghno:: \
--longoptions command,debug-mode,ext,help,no-header,output-file:: \
-n ${ME} -- "$@")
if [ $? -ne 0 ] # no success
then
show_usage
fi
eval set -- "$GETOPT"
while true
do
case "$1" in
-c|--command) COMMAND=1; HEADER=0; shift;;
-e|--ext) EXTENSION=1; shift;;
-g|--debug-mode) DEBUG_MODE=1; shift;;
-h|--help) show_usage ;;
-n|--no-header) HEADER=0; shift;;
-o|--output-file)
case "$2" in
"") OUTFILE=$(basename $(pwd)).lst; shift 2 ;;
*) OUTFILE=$2; shift 2 ;;
esac ;;
--) shift ; break ;;
*) show_usage ;;
esac
done
if [ -n "${OUTFILE}" ]
then
if [ -f "${OUTFILE}" ]
then
echo "%--> file \"${OUTFILE}\" already exists, creating backup"
mv ${OUTFILE} ${OUTFILE}~
fi
exec > ${OUTFILE} # redirect stdout to ${OUTFILE}
CURR_DIR=$(basename $(pwd))
print_header ${OUTFILE} ${CURR_DIR}
fi
# now take all remaining arguments (should be all filenames) and do the job
for arg do
echo "%-->" processing file \"$arg\" 1>&2 # echo the current
# filename to stderr
do_the_job $arg
done
### listings-ext.sh ends here
|