-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki2vim.px
executable file
·66 lines (60 loc) · 1.52 KB
/
wiki2vim.px
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
#!/usr/bin/perl
#
# Generate a Vim script that, when sourced, performs :%s///gc to use English
# contractions throughout the current file.
use strict;
use HTML::TableExtract;
sub quoteVim ($) {
my ($string) = @_;
$string =~ s/'/''/g;
return "'$string'";
}
my $html;
{
local $/;
$html = <>;
}
my $te = HTML::TableExtract->new(headers => [qw(Contraction Meaning)]);
$te->parse($html);
my %contraction;
print "let s:contractions = {";
my $first = 1;
foreach my $row ($te->rows)
{
my ($contraction, $meanings) = @$row;
$meanings =~ s/(\s|\[\d+\])+/ /g;
foreach my $meaning ($meanings =~ /[^\s\/][^\/]*[^\s\/]/g) {
warn "Duplicate meaning $meaning\n" if exists $contraction{$meaning};
$contraction{$meaning} = $contraction;
if ($first) {
undef $first;
} else {
print ", ";
}
print quoteVim(lc $meaning), ":", quoteVim($contraction);
}
}
print "}\n";
print <<'END';
function! s:Contract(meaning)
let l:meaning = substitute(a:meaning, '\_s\+', ' ', 'g')
let l:contraction = s:contractions[tolower(l:meaning)]
if l:meaning[0] ==# toupper(l:meaning[0])
return toupper(l:contraction[0]) . l:contraction[1:]
else
return l:contraction
endif
endfunction
END
print "%s/\\<\\(";
$first = 1;
foreach my $meaning (keys %contraction) {
if ($first) {
undef $first;
} else {
print "\\|";
}
$meaning =~ s/\s+/\\_s\\+/g;
print $meaning;
}
print "\\)\\>/\\=s:Contract(submatch(0))/igc\n";