-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathxslscript.pl
executable file
·444 lines (360 loc) · 9.53 KB
/
xslscript.pl
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/perl
# (C) Maxim Dounin
# (C) Nginx, Inc.
# Convert from XSLScript to XSLT.
#
# Originally XSLScript was written by Paul Tchistopolskii. It is believed
# to be mostly identical to XSLT, but uses shorter syntax. Original
# implementation has major Java dependency, no longer supported and hard
# to find.
#
# This code doesn't pretend to be a full replacement, but rather an attempt
# to provide functionality needed for nginx documentation.
###############################################################################
use warnings;
use strict;
use Parse::RecDescent;
use Getopt::Long;
use Data::Dumper qw/Dumper/;
###############################################################################
my $dump = 0;
my $output;
GetOptions(
"output|o=s" => \$output,
"trace!" => \$::RD_TRACE,
"hint!" => \$::RD_HINT,
"dump!" => \$dump,
)
or die "oops\n";
###############################################################################
my $grammar = <<'EOF';
# XSLScript grammar, reconstructed
startrule : <skip:""> item(s) eofile
{ $return = $item{'item(s)'}; 1 }
item : "<!--" <commit> comment
| "!!" <commit> exclam_double
| "!{" <commit> exclam_xpath
| "!" name <commit> params
{ $return = [
"X:call-template", "name", $item{name}, [],
$item{params}
]; 1 }
| "<%" <commit> space instruction space "%>"
{ $return = $item{instruction}; 1 }
| "<" name attrs space ">" <commit> item(s?) "</" name ">"
{ $return = [ "tag", $item{name}, $item{attrs}, $item{'item(s?)'} ]; 1 }
| "<" <commit> name attrs space "/>"
{ $return = [ "tag", $item{name}, $item{attrs} ]; 1 }
| "X:variable" space <commit> xvariable
| "X:var" space <commit> xvariable
| "X:template" space <commit> xtemplate
| "X:if" space <commit> xif
| "X:param" space <commit> xparam
| "X:for-each" space <commit> xforeach
| "X:sort" space <commit> xsort
| "X:when" space <commit> xwhen
| "X:attribute" space <commit> xattribute
| "X:output" space <commit> xoutput
| "X:copy-of" space <commit> xcopyof
| instruction space <commit> attrs body
{ $return = [ $item{instruction}, $item{attrs}, $item{body} ]; 1 }
| space_notempty
| text
| <error>
# list of simple instructions
instruction : "X:stylesheet"
| "X:transform"
| "X:attribute-set"
| "X:element"
| "X:apply-templates"
| "X:choose"
| "X:otherwise"
| "X:value-of"
| "X:apply-imports"
| "X:number"
| "X:include"
| "X:import"
| "X:strip-space"
| "X:preserve-space"
| "X:copy"
| "X:text"
| "X:comment"
| "X:processing-instruction"
| "X:decimal-format"
| "X:namespace-alias"
| "X:key"
| "X:fallback"
| "X:message"
# comments, <!-- ... -->
# not sure if it's something to be interpreted specially
# likely an artifact of our dump process
comment : /((?!-->).)*/ms "-->"
{ $return = "<!--" . $item[1] . "-->"; 1 }
# special chars: ', ", {, }, \
# if used in text, they needs to be escaped with backslash
text : quoted | unreserved | "'" | "\"" | "{"
quoted : "\\" special
{ $return = $item{special}; 1; }
special : "'" | "\"" | "\\" | "{" | "}"
unreserved : /[^'"\\{}<\s]+\s*/ms
# whitespace
space : /\s*/ms
space_notempty : /\s+/ms
# shortcuts:
#
# !! for X:apply-templates
# !{xpath-expression} for X:value-of select="xpath-expression";
# !foo() for X:call-template name="foo"
# !root (path = { !{ substring($DIRNAME, 2) } })
# !root (path = "substring-after($path, '/')")
exclam_double : space value(?) params(?) attrs space ";"
{ $return = [
"X:apply-templates", "select", $item{'value(?)'}[0],
$item{attrs}, $item{'params(?)'}[0]
]; 1 }
exclam_xpath : xpath "}"
{ $return = [
"X:value-of", "select", $item{xpath}, []
]; 1 }
xpath : /("[^"]*"|'[^']*'|[^}'"])*/ms
# instruction attributes
# name="value"
attrs : attr(s?)
attr : space name space "=" space value
{ $return = $item{name} . "=" . $item{value}; }
name : /[a-z0-9_:-]+/i
value : /"[^"]*"/
# template parameters
# ( bar="init", baz={markup} )
params : space "(" param(s? /\s*,\s*/) ")" space
{ $return = $item[3]; 1 }
param : space name space "=" space value space
{ $return = [
"X:with-param",
"select", $item{value},
"name", $item{name},
[]
]; 1 }
| space name space "=" <commit> space "{" item(s) "}"
{ $return = [
"X:with-param", "name", $item{name}, [],
$item{'item(s)'}
]; 1 }
| space name
{ $return = [
"X:param", "name", $item{name}, []
]; 1 }
# instruction body
# ";" for empty body, "{ ... }" otherwise
body : space ";"
{ $return = ""; }
| space "{" <commit> item(s?) "}" (space ";")(?)
{ $return = $item{'item(s?)'}; 1 }
# special handling of some instructions
# X:if attribute is test=
xif : value body space "else" <commit> body
{ $return = [
"X:choose", [], [
[ "X:when", "test", $item[1], [], $item[2] ],
[ "X:otherwise", [], $item[6] ]
]
]; 1 }
| value attrs body
{ $return = [
"X:if", "test", $item{value}, $item{attrs}, $item{body},
]; 1 }
| attrs body
{ $return = [
"X:if", $item{attrs}, $item{body},
]; 1 }
| <error>
# X:template name(params) = "match" {
# X:template name( bar="init", baz={markup} ) = "match" mode="some" {
xtemplate : name(?) params(?) space
(space "=" space value)(?) attrs body
{ $return = [
"X:template",
"name", $item{'name(?)'}[0],
"match", $item[4][0],
$item{attrs},
[ ($item[2][0] ? @{$item[2][0]} : ()), @{$item{body}} ]
]; 1 }
# X:var LINK = "/article/@link";
# X:var year = { ... }
# semicolon is optional
xvariable : name space "=" space value attrs body
{ $return = [
"X:variable",
"select", $item{value},
"name", $item{name},
$item{attrs}, $item{body}
]; 1 }
| name space "=" space attrs body
{ $return = [
"X:variable",
"name", $item{name},
$item{attrs}, $item{body}
]; 1 }
| name space "=" space value
{ $return = [
"X:variable",
"select", $item{value},
"name", $item{name},
[]
]; 1 }
| name space "="
{ $return = [
"X:variable",
"name", $item{name},
[]
]; 1 }
| <error>
# X:param XML = "'../xml'";
# X:param YEAR;
xparam : name space "=" space value attrs body
{ $return = [
"X:param",
"select", $item{value},
"name", $item{name},
$item{attrs}, $item{body}
]; 1 }
| name attrs body
{ $return = [
"X:param", "name", $item{name},
$item{attrs}, $item{body}
]; 1 }
# X:for-each "section[@id and @name]" { ... }
# X:for-each "link", X:sort "@id" {
xforeach : value attrs body
{ $return = [
"X:for-each", "select", $item{value}, $item{attrs}, $item{body}
]; 1 }
| value attrs space
"," space "X:sort" <commit> space value attrs body
{ $return = [
"X:for-each", "select", $item[1], $item[2], [
[ "X:sort", "select", $item[9], $item[10] ],
@{$item{body}}
]
]; 1 }
# X:sort select
# X:sort "@id"
xsort : value attrs body
{ $return = [
"X:sort", "select", $item{value}, $item{attrs}, $item{body}
]; 1 }
# X:when "position() = 1" { ... }
xwhen : value attrs body
{ $return = [
"X:when", "test", $item{value}, $item{attrs}, $item{body}
]; 1 }
# X:attribute "href" { ... }
xattribute : value attrs body
{ $return = [
"X:attribute", "name", $item{value}, $item{attrs}, $item{body}
]; 1 }
# X:output
# semicolon is optional
xoutput : attrs body(?)
{ $return = [
"X:output", undef, undef, $item{attrs}, $item{body}
]; 1 }
# "X:copy-of"
# semicolon is optional
xcopyof : value attrs body(?)
{ $return = [
"X:copy-of", "select", $item{value}, $item{attrs}, $item{body}
]; 1 }
# eof
eofile : /^\Z/
EOF
###############################################################################
sub format_tree {
my ($tree, $indent) = @_;
my $s = '';
if (!defined $indent) {
$indent = 0;
$s .= '<?xml version="1.0" encoding="utf-8"?>' . "\n";
}
my $space = " " x $indent;
foreach my $el (@{$tree}) {
if (!defined $el) {
warn "Undefined element in output.\n";
$s .= $space . "(undef)" . "\n";
next;
}
if (not ref($el) && defined $el) {
if ($el =~ /^<!--(.*)-->$/s) {
my $comment = $1;
$comment =~ s/--/../sg;
$el = "<!--" . $comment . "-->";
}
$s .= $el;
next;
}
die if ref($el) ne 'ARRAY';
my $tag = $el->[0];
if ($tag eq 'tag') {
my (undef, $name, $attrs, $body) = @{$el};
$s .= "<" . join(" ", $name, @{$attrs});
if ($body) {
$s .= ">" . format_tree($body, $indent + 1)
. "</$name>";
} else {
$s .= "/>";
}
next;
}
if ($tag =~ m/^X:(.*)/) {
my $name = "xsl:" . $1;
my (undef, @a) = @{$el};
my @attrs;
while (@a) {
last if ref($a[0]) eq 'ARRAY';
my $name = shift @a;
my $value = shift @a;
next unless defined $value;
$value = '"' . $value . '"'
unless $value =~ /^"/;
push @attrs, "$name=$value";
}
if ($name eq "xsl:stylesheet") {
push @attrs, 'xmlns:xsl="http://www.w3.org/1999/XSL/Transform"';
push @attrs, 'version="1.0"';
}
my ($attrs, $body) = @a;
$attrs = [ @{$attrs}, @attrs ];
$s .= "<" . join(" ", $name, @{$attrs});
if ($body && scalar @{$body} > 0) {
$s .= ">" . format_tree($body, $indent + 1)
. "</$name>";
} else {
$s .= "/>";
}
next;
}
$s .= format_tree($el, $indent + 1);
}
return $s;
}
###############################################################################
my $parser = Parse::RecDescent->new($grammar)
or die "Failed to create parser.\n";
my $lines;
{
local $/;
$lines = <>;
}
my $tree = $parser->startrule($lines)
or die "Failed to parse $ARGV.\n";
my $formatted = format_tree($tree);
if (defined $output) {
open STDOUT, ">", $output
or die "Can't open $output: $!\n";
}
if ($dump) {
print Dumper($tree);
exit(0);
}
print $formatted;
###############################################################################