-
Notifications
You must be signed in to change notification settings - Fork 26
/
generate
executable file
·440 lines (405 loc) · 16.3 KB
/
generate
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
#!/usr/bin/perl
use strict;
use warnings;
use File::Compare;
use Template;
use IPC::Run qw(run);
my $version = '2.2.0';
# some useful options (see below for full list)
my $config = {
# INCLUDE_PATH => '/search/path', # or list ref
# INTERPOLATE => 1, # expand "$var" in plain text
POST_CHOMP => 1, # cleanup whitespace
# PRE_PROCESS => 'header', # prefix each template
EVAL_PERL => 1, # evaluate Perl code blocks
};
my ($id, $lang_name);
my $outpath = '../snowballstem.github.io/';
print "Generating output in '$outpath'\n";
$ENV{PYTHONIOENCODING} = 'UTF-8';
my $code_path_prefix = 'code/';
my $code_path_suffix = '.sbl';
my $coderepo_path_prefix = '../snowball/algorithms/';
system("mkdir", "-p", $outpath) == 0 or die "Failed to ensure '$outpath' exists\n";
# Stuff that just gets copied over.
foreach my $f (qw(
CNAME
favicon.ico
robots.txt
snub-dodecahedron.gif
styles.css
js
codesets
algorithms
otherapps
texts
runtime
compiler
)) {
system("rm", "-rf", $outpath.$f) == 0 or die "Failed to delete '$outpath$f'\n";
system("cp", "-a", $f, $outpath.$f) == 0 or die "Failed to copy '$f'\n";
}
# Currently needed for .tt files in algorithms - can be removed once we
# generate everything there.
system('find', $outpath, '-name', '*.tt', '-o', '-name', '*.gv', '-delete') == 0 or die "Failed to remove .tt files from generated website\n";
my $template = Template->new($config);
1;
# define template variables for replacement
my $vars = {};
$$vars{version} = $version;
# For stemming demo: FIXME: Dynamically generate this from UnicodeData
$$vars{word_regexp} = 'A-Za-z\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376-\u037D\u037F-\u0383\u0386\u0388-\u03F5\u03F7-\u0481\u048A-\u0559\u0561-\u0588\u05D0-\u05F2\u0620-\u064A\u066E-\u066F\u0671-\u06D3\u06D5\u06E5-\u06E6\u06EE-\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1-\u07BF\u07CA-\u07EA\u07F4-\u07F5\u07FA-\u0815\u081A\u0824\u0828\u0840-\u0858\u08A0-\u08E2\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0980\u0985-\u09BB\u09BD\u09CE-\u09D6\u09DC-\u09E1\u09F0-\u09F1\u0A05-\u0A3B\u0A59-\u0A65\u0A72-\u0A74\u0A85-\u0ABB\u0ABD\u0AD0-\u0AE1\u0AF9-\u0B00\u0B05-\u0B3B\u0B3D\u0B5C-\u0B61\u0B71\u0B83-\u0BBD\u0BD0-\u0BD6\u0C05-\u0C3D\u0C58-\u0C61\u0C85-\u0CBB\u0CBD\u0CDE-\u0CE1\u0CF1-\u0D00\u0D05-\u0D3D\u0D4E-\u0D56\u0D5F-\u0D61\u0D7A-\u0D81\u0D85-\u0DC9\u0E01-\u0E30\u0E32-\u0E33\u0E40-\u0E46\u0E81-\u0EB0\u0EB2-\u0EB3\u0EBD-\u0EC7\u0EDC-\u0F00\u0F40-\u0F70\u0F88-\u0F8C\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065-\u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10FA\u10FC-\u135C\u1380-\u138F\u13A0-\u13FF\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16F1-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u1771\u1780-\u17B3\u17D7\u17DC\u1820-\u18A8\u18AA-\u191F\u1950-\u19CF\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4F\u1B83-\u1BA0\u1BAE-\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1CE9-\u1CEC\u1CEE-\u1CF1\u1CF5-\u1CF7\u1D00-\u1DBF\u1E00-\u1FBC\u1FBE\u1FC2-\u1FCC\u1FD0-\u1FDC\u1FE0-\u1FEC\u1FF2-\u1FFC\u2071-\u2073\u207F\u2090-\u209F\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2183-\u2184\u2C00-\u2CE4\u2CEB-\u2CEE\u2CF2-\u2CF8\u2D00-\u2D6F\u2D80-\u2DDF\u2E2F\u3005-\u3006\u3031-\u3035\u303B-\u303C\u3041-\u3098\u309D-\u309F\u30A1-\u30FA\u30FC-\u318F\u31A0-\u31BF\u31F0-\u31FF\u3400-\u4DBF\u4E00-\uA48F\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A-\uA66E\uA67F-\uA69D\uA6A0-\uA6E5\uA717-\uA71F\uA722-\uA788\uA78B-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA8FD-\uA8FF\uA90A-\uA925\uA930-\uA946\uA960-\uA97F\uA984-\uA9B2\uA9CF\uA9E0-\uA9E4\uA9E6-\uA9EF\uA9FA-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA7E-\uAAAF\uAAB1\uAAB5-\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB5A\uAB5C-\uABE2\uAC00-\uD7FF\uF900-\uFB1D\uFB1F-\uFB28\uFB2A-\uFBB1\uFBD3-\uFD3D\uFD50-\uFDFB\uFE70-\uFEFE\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFDF\u{10000}-\u{100FF}\u{10280}-\u{102DF}\u{10300}-\u{1031F}\u{10330}-\u{10340}\u{10342}-\u{10349}\u{10350}-\u{10375}\u{10380}-\u{1039E}\u{103A0}-\u{103CF}\u{10400}-\u{1049F}\u{10500}-\u{1056E}\u{10600}-\u{10856}\u{10860}-\u{10876}\u{10880}-\u{108A6}\u{108E0}-\u{108FA}\u{10900}-\u{10915}\u{10920}-\u{1093E}\u{10980}-\u{109BB}\u{109BE}-\u{109BF}\u{10A00}\u{10A10}-\u{10A37}\u{10A60}-\u{10A7C}\u{10A80}-\u{10A9C}\u{10AC0}-\u{10AC7}\u{10AC9}-\u{10AE4}\u{10B00}-\u{10B38}\u{10B40}-\u{10B57}\u{10B60}-\u{10B77}\u{10B80}-\u{10B98}\u{10C00}-\u{10CF9}\u{11003}-\u{11037}\u{11083}-\u{110AF}\u{110D0}-\u{110EF}\u{11103}-\u{11126}\u{11150}-\u{11172}\u{11176}-\u{1117F}\u{11183}-\u{111B2}\u{111C1}-\u{111C4}\u{111DA}\u{111DC}\u{11200}-\u{1122B}\u{11280}-\u{112A8}\u{112B0}-\u{112DE}\u{11305}-\u{1133B}\u{1133D}\u{11350}-\u{11356}\u{1135D}-\u{11361}\u{11480}-\u{114AF}\u{114C4}-\u{114C5}\u{114C7}-\u{114CF}\u{11580}-\u{115AE}\u{115D8}-\u{115DB}\u{11600}-\u{1162F}\u{11644}-\u{1164F}\u{11680}-\u{116AA}\u{11700}-\u{1171C}\u{118A0}-\u{118DF}\u{118FF}-\u{123FF}\u{12480}-\u{16A5F}\u{16AD0}-\u{16AEF}\u{16B00}-\u{16B2F}\u{16B40}-\u{16B43}\u{16B63}-\u{16F50}\u{16F93}-\u{1BC9B}\u{1D400}-\u{1D6C0}\u{1D6C2}-\u{1D6DA}\u{1D6DC}-\u{1D6FA}\u{1D6FC}-\u{1D714}\u{1D716}-\u{1D734}\u{1D736}-\u{1D74E}\u{1D750}-\u{1D76E}\u{1D770}-\u{1D788}\u{1D78A}-\u{1D7A8}\u{1D7AA}-\u{1D7C2}\u{1D7C4}-\u{1D7CD}\u{1E800}-\u{1E8C6}\u{1EE00}-\u{1EEEF}\u{20000}-\u{E0000}';
$$vars{highlight} = sub {
my $in = shift;
my $lang = shift;
# Remove leading and trailing whitespace-only lines.
$in =~ s/\A\s*\n//;
$in =~ s/\n\s+\z/\n/;
$in =~ /\&[#\w]+;/ and warn "HTML entity in Snowball code? [$&]";
my $out;
if (defined $lang) {
run ['pygmentize', "-l$lang", '-fhtml'], \$in, \$out, \*STDERR
or die "pygmentize: $?";
} else {
run ['python3', 'highlight_snowball.py'], \$in, \$out, \*STDERR
or die "python3: $?";
}
return $out;
};
$$vars{highlight_inline} = sub {
my $in = shift;
# Remove leading and trailing whitespace-only lines.
$in =~ s/\A\s*\n//;
$in =~ s/\n\s+\z/\n/;
$in =~ /\&[#\w]+;/ and warn "HTML entity in Snowball code? [$&]";
$in =~ /\n/ and warn "Newline in inline Snowball code? [$in]";
my $out;
run ['python3', 'highlight_snowball.py'], \$in, \$out, \*STDERR
or die "python3: $?";
$out =~ s!<div([^.]*)><pre>!<span$1><code>!;
$out =~ s!</pre></div>!</code></span>!;
return $out;
};
$$vars{highlight_file} = sub {
my $file = shift;
if ($file =~ /\.\w+$/) {
return scalar(`pygmentize -fhtml \Qcode/$file\E`);
}
my $source = "$code_path_prefix$file$code_path_suffix";
my $corerepo_source = "$coderepo_path_prefix$file$code_path_suffix";
if (-e $corerepo_source) {
if (compare($corerepo_source, $source) != 0) {
print STDERR "warning: $source and $corerepo_source differ\n";
}
}
return scalar(`python3 highlight_snowball.py < \Q$source\E`);
};
# <li> tags for a stemming algorithm.
$$vars{algorithm_lis} = sub {
my ($new_id, $new_lang_name) = @_;
$id = $new_id;
$lang_name = $new_lang_name;
my $out = << "__END__";
<li> <a href="/demo.html#\u$id">Javascript demo</a></li>
<li> <a href="https://raw.githubusercontent.com/snowballstem/snowball/master/algorithms/$id.sbl">The stemmer in Snowball</a></li>
<li> <a href="https://raw.githubusercontent.com/snowballstem/snowball-data/master/$id/voc.txt">Sample $lang_name vocabulary</a></li>
<li> <a href="https://raw.githubusercontent.com/snowballstem/snowball-data/master/$id/output.txt">Its stemmed equivalent</a></li>
__END__
if (-f "algorithms/$id/stop.txt") {
$out .= << "__END__";
<li> <a href="stop.txt">$lang_name stop word list (UTF-8 encoding)</a>
__END__
}
return $out;
};
# Output from: snowball --help
$$vars{snowball_help} = sub {
my $o;
run ['../snowball/snowball', '--help'], \undef, \$o, \*STDERR
or die "../snowball/snowball $?";
return $o;
};
$$vars{algorithm_vocab} = sub {
my ($voc1, $voc2) = @_;
die unless defined $id && defined $lang_name;
if (!defined($voc2)) {
# New approach - number of entries and two starting words, from which
# we pull words from the snowball-data voc.txt for this language.
my ($n, $a, $b) = @$voc1;
$voc1 = [];
$voc2 = [];
my $vocfile = "../snowball-data/$id/voc.txt";
open VOC, '<', $vocfile or die "$vocfile: $!";
while (<VOC>) {
chomp;
last if $_ eq $a;
}
die "Word '$a' not found in '$vocfile'\n" unless defined $_;
for my $i (1..$n) {
push @$voc1, $_;
$_ = <VOC>;
chomp;
}
while (<VOC>) {
chomp;
last if $_ eq $b;
}
die "Word '$b' not found in '$vocfile'\n" unless defined $_;
for my $i (1..$n) {
push @$voc2, $_;
$_ = <VOC>;
chomp;
}
}
my (@out1, @out2);
{
my $i = join '', map{"$_\n"} @$voc1;
my $o;
run ['../snowball/stemwords', '-l', $id], \$i, \$o, \*STDERR
or die "../snowball/stemwords $?";
chomp($o);
@out1 = split /\n/, $o;
}
{
my $i = join '', map{"$_\n"} @$voc2;
my $o;
run ['../snowball/stemwords', '-l', $id], \$i, \$o, \*STDERR
or die "../snowball/stemwords $?";
chomp($o);
@out2 = split /\n/, $o;
}
my $out = << "__END__";
<p>
Here is a sample of $lang_name vocabulary, with the stemmed forms that will be generated by this algorithm:
</p>
<div style="background-color:lightpink">
<TABLE CELLPADDING=0>
<TR><TD> <B>word</B> </TD>
<TD></TD><TD> </TD>
<TD></TD><TD> <B>stem</B> </TD>
<TD></TD><TD> </TD>
<TD></TD><TD> <B>word</B> </TD>
<TD></TD><TD> </TD>
<TD></TD><TD> <B>stem</B> </TD>
</TR>
<TR><TD>
__END__
for my $w (@$voc1) {
$out .= $w . "<BR>\n";
}
$out .= << "__END__";
</TD>
<TD></TD><TD> <b>⇒</b> </TD>
<TD></TD><TD>
__END__
for my $w (@out1) {
$out .= $w . "<BR>\n";
}
$out .= << "__END__";
</TD>
<TD></TD><TD> </TD>
<TD></TD><TD>
__END__
for my $w (@$voc2) {
$out .= $w . "<BR>\n";
}
$out .= << "__END__";
</TD>
<TD></TD><TD> <b>⇒</b> </TD>
<TD></TD><TD>
__END__
for my $w (@out2) {
$out .= $w . "<BR>\n";
}
$out .= << "__END__";
</TD>
</TR>
</TABLE>
</div>
__END__
return $out;
};
# The footer of the page.
# There should be no output after this.
$$vars{footer} = <<'__END__';
</div><!-- /.col-md-10 -->
</div><!-- /.row -->
</div><!-- /.container -->
<div class="container">
<footer class="footer">
<p>
<a href="/lists.html">Write to our mailing list</a> if you have comments or questions about the project.
</p>
</footer>
</div> <!-- /container -->
<script src="https://cdn.jsdelivr.net/jquery/1.11.3/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</body>
</html>
__END__
my @menu = (
[ "index" => "Introduction" ],
[ "demo" => "Demo" ],
[ "algorithms" => "Algorithms" ],
[ "download" => "Download" ],
[ "lists" => "Mailing Lists" ],
[ "license" => "License" ],
[ "credits" => "Credits" ],
[ "projects" => "Projects" ]
);
my @stemmer_js_files;
my $stemmer_select = '<select id="lang" onchange="lang_changed(this);location.hash=this.options[this.selectedIndex].value">';
for (sort glob('js/*-stemmer.js')) {
if (m!/(.*)-!) {
push @stemmer_js_files, $_;
if ($1 ne 'base') {
my $default = $1 eq 'english' ? ' selected' : '';
$stemmer_select .= "<option value='\u$1'$default>\u$1</option>";
}
}
}
run ['closure-compiler',
'--accept_const_keyword',
qw(--compilation_level ADVANCED_OPTIMIZATIONS),
qw(--warning_level VERBOSE),
@stemmer_js_files],
\undef, '>', $outpath."/js/stemmers.js", '2>', \*STDERR;
$stemmer_select .= "</select>";
$$vars{stemmer_js_files} = ['js/stemmers.js']; #\@stemmer_js_files;
$$vars{stemmer_select} = $stemmer_select;
# specify input filename, or file handle, text reference, etc.
for my $in (<*.tt */*.tt */*/*.tt>) {
((my $page = $in) =~ s/\.tt$//) or next;
my $out = "$outpath$page.html";
my $pretty_name = "\u$page";
if ($page eq 'index') {
$pretty_name = '';
} elsif ($page eq 'lists') {
$pretty_name = 'Mailing Lists';
}
$id = $lang_name = undef;
my $menu = "<div class='list-group'>";
my $found_entry = 0;
for (@menu) {
my ($link, $desc) = @{$_};
my $class = 'list-group-item';
if ($link eq $page || "$link/index" eq $page) {
$class .= ' active';
++$found_entry;
}
if ($link eq "index") {
$link = '/';
} elsif (-d $link) {
$link = "/$link/";
} else {
$link = "/$link.html";
}
$menu .= '<a class="'.$class.'" href="'.$link.'">'.$desc.'</a>';
}
$menu .= <<'__END__';
<a class="list-group-item" href="https://github.com/snowballstem">Source on github</a><br>
</div>
<!--
<form NAME=P METHOD=GET ACTION="omega.cgi" TARGET="_top">
<center>
<input NAME=P VALUE="" SIZE=12>
<input TYPE=SUBMIT VALUE="Search" BORDER=0>
<input TYPE=hidden NAME=DB VALUE="snowball-website">
<input TYPE=hidden NAME=FMT VALUE="snquery">
</form>
<br>
-->
__END__
# FIXME: Sort out sub-level navigation.
if ($found_entry > 1) {
print "$in: error: $found_entry menu entries highlighted\n";
$menu = '';
} elsif ($found_entry == 0) {
print "$in: warning: No corresponding menu entry\n" unless $in eq '404.tt';
$menu = '';
}
# $default title is the title of the page if one isn't explicitly specified.
my $default_title;
if ($pretty_name eq "") {
$pretty_name = "Snowball";
} else {
$default_title = $pretty_name;
}
# This should be output first.
$$vars{header} = sub {
my $title = shift // $default_title;
my $heading = $title;
if (defined $title) {
$title .= ' - Snowball';
} else {
$heading = $title = 'Snowball';
}
$title =~ s/\&/\&/g;
$title =~ s/</\</g;
$title =~ s/>/\>/g;
return <<__END__;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="/favicon.ico">
<title>$title</title>
<link href="https://cdn.jsdelivr.net/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<link href="/styles.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="https://cdn.jsdelivr.net/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://cdn.jsdelivr.net/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<!-- Static navbar -->
<nav class="navbar navbar-default navbar-static-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="/" class="pull-left"><img src="/snub-dodecahedron.gif" alt="Snowball Logo" style="margin: 4px; height: 42px;"></a>
<a class="navbar-brand" href="/">Snowball</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="/">Home</a></li>
<li><a href="/credits.html">Credits</a></li>
</ul>
<span id="forkongithub"><a href="https://github.com/snowballstem/snowball">Fork me on GitHub</a></span>
</div><!--/.nav-collapse -->
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-2">
$menu
</div>
<div class="col-md-10">
<h1>$heading</h1>
__END__
};
my $out_str;
$template->process($in, $vars, \$out_str) || die $template->error();
# Kill empty <span> tags.
$out_str =~ s!<span></span>!!g;
open my $h_out, '>', $out or die $!;
print $h_out $out_str or die $!;
close $h_out or die $!;
}
for my $in (<*.gv */*.gv */*/*.gv>) {
((my $base = $in) =~ s/\.gv$//) or next;
my $tmp = "$outpath$base.tmp.png";
my $out = "$outpath$base.png";
run ['neato', '-Tpng', $in], \undef, '>', $tmp, '2>', \*STDERR;
run ['pngcrush', '-q', $tmp, $out], \undef, \*STDOUT, \*STDERR;
unlink $tmp;
}