[r200]: trunk / doc / vim2html.pl Maximize Restore History

Download this file

vim2html.pl    300 lines (250 with data), 6.3 kB

  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
#!/usr/bin/env perl
# converts vim documentation to simple html
# Sirtaj Singh Kang (taj@kde.org)
# Sun Feb 24 14:49:17 CET 2002
# Updated by Willis (yianwillis@users.sourceforge.net)
# Sat Mar 24 12:11:02 EST 2006
use strict;
use utf8;
use Text::Tabs; # Willis: expand replaced by myexpand. So no longer necessary.
use Encode;
use vars qw/%url $date/;
%url = ();
$date = Encode::decode_utf8(`date -u`);
chop $date;
sub maplink
{
my $tag = shift;
if( exists $url{ $tag } ){
return $url{ $tag };
} else {
#warn "Unknown hyperlink target: $tag\n";
$tag =~ s/</&lt;/g;
$tag =~ s/>/&gt;/g;
return "<code class=\"badlink\">$tag</code>";
}
}
sub readTagFile
{
my($tagfile) = @_;
my( $tag, $file, $name );
open(TAGS,"$tagfile") || die "can't read tags\n";
while( <TAGS> ) {
next unless /^(\S+)\s+(\S+)\s+/;
$tag = $1;
my $label = $tag;
($file= $2) =~ s/.\w+$/.html/g;
$url{ $tag } = "<a href=\"$file#".escurl($tag)."\">".esctext($label)."</a>";
}
$url{ "help-tags" } = "<a href=\"tags.html"."\">".esctext("help-tags")."</a>";
close( TAGS );
}
sub esctext
{
my $text = shift;
$text =~ s/&/&amp;/g;
$text =~ s/</&lt;/g;
$text =~ s/>/&gt;/g;
return $text;
}
sub escurl
{
my $url = shift;
$url =~ s/"/%22/g;
$url =~ s/~/%7E/g;
$url =~ s/</%3C/g;
$url =~ s/>/%3E/g;
$url =~ s/=/%20/g;
$url =~ s/#/%23/g;
$url =~ s/\//%2F/g;
return $url;
}
# expand() count bytes when computing tabstops. But each utf-8 character may
# take up to 3 bytes, we all know CJK characters use double width only.
$tabstop = 8;
sub mylength
{
my ($str) = @_;
my $length = length($str);
my $i = 0;
my @chars = unpack("U*", $str);
foreach $i (@chars) {
$length++ if $i > 255;
}
return $length;
}
# expand tabs.
sub myexpand
{
my (@l) = @_;
for $_ (@l) {
1 while s/(^|\n)([^\t\n]*)(\t+)/
$1. $2 . (" " x
($tabstop * length($3)
- (mylength($2) % $tabstop)))
/sex;
}
return @l if wantarray;
return $l[0];
}
sub vim2html
{
my( $infile ) = @_;
my( $outfile );
open(IN, "<:utf8", "$infile" ) || die "Couldn't read from $infile: $!.\n";
($outfile = $infile) =~ s:.*/::g;
my $tagfile = ($outfile =~ /^tags(-..)?$/);
if ($tagfile) {
$outfile = "tags";
} else {
$outfile =~ s/\.\w+$//g;
}
open( OUT, ">:utf8", "$outfile.html" )
|| die "Couldn't write to $outfile.html: $!.\n";
binmode STDOUT, ":utf8";
my $head = uc( $outfile );
print OUT<<EOF;
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>VIM: $outfile</title>
<link rel="stylesheet" href="vim-stylesheet.css" type="text/css">
</head>
<body>
<h2>$head</h2>
<pre>
EOF
my $inexample = 0;
my $inheader = 0;
if ($tagfile) {
print OUT "<table>\n";
}
while( <IN> ) {
chop;
if ($tagfile) {
print OUT "<tr>";
my @fields = split(/\t/);
if ($fields[0] eq "!_TAG_FILE_ENCODING") {
print OUT "<td>" . $fields[0] . "</td><td>" . $fields[1];
print OUT "</td><td>" . $fields[2] . "</td></tr>\n";
}
else {
$fields[1] =~ s/\.\w+/\.txt/;
print OUT "<td>" . maplink($fields[0]) . "</td><td>";
print OUT maplink($fields[1]) . "</td><td>". esctext($fields[2]);
print OUT "</td></tr>\n";
}
next;
}
$_ = myexpand($_);
if ( /^=+\s*$/ ) {
print OUT "</pre><hr class=\"doubleline\" /><pre>\n";
$inheader = 1;
next;
}
elsif ( /^\s*-+\s*$/ ) {
print OUT "</pre><hr class=\"singleline\" /><pre>\n";
next;
}
# examples
elsif( /^>$/ || /\s>$/ ) {
if ( $inexample && /^(<)/ ) {
$_ = $';
$_ = " " . $_ if /^\s/;
}
$inexample = 1;
chop;
}
elsif ( $inexample && /^([<\S])/ ) {
$inexample = 0;
$_ = $' if $1 eq "<";
$_ = " " . $_ if /^\s/;
}
s/\s+$//g;
if( $inexample == 2 ) {
print OUT "<code class=\"example\">" . esctext($_) . "</code>\n";
$inheader = 0;
next;
}
# Various vim highlights. note that < and > have already been escaped
# so that HTML doesn't get screwed up.
my @out = ();
# print "Text: $_\n";
LOOP:
# split on |token| or *token*
foreach my $token ( split /((?:\|[^*"|[:space:]]+\|)|(?:\*[^*"|[:space:]]+\*(?=[[:space:]]|$)))/ ) {
if ( $token =~ /^\|([^*"|[:space:]]+)\|/ ) {
# link
push( @out, "|".maplink( $1 )."|" );
next LOOP;
}
elsif ( $token =~ /^\*([^*"|[:space:]]+)\*/ ) {
# target
push( @out,
"<b class=\"vimtag\">\*<a name=\"".escurl($1)."\">".esctext($1)."<\/a>\*<\/b>");
next LOOP;
}
$_ = esctext($token);
# keystroke
s/CTRL-(\w+|[^{])/<code class="keystroke">CTRL-$1<\/code>/g;
# parameter '...'
s/'(\w{2,}|t_..)'/maplink($&)/ge;
# parameter <...>
s/&lt;([a-zA-Z0-9_-]*)&gt;/<code class="special">&lt;$1&gt;<\/code>/g;
# parameter [...]
s/\[(range|line|count|offset|cmd|[-+]?num|\+cmd|\+\+opt|arg|arguments|ident|addr|group)\]/<code class="special">\[$1\]<\/code>/g;
s/\s\K\[(\w+)\]/<code class="special">\[$1\]<\/code>/g;
# note
s/(Note[:\s])/<code class="note">$1<\/code>/gi;
s/(������|������)( (?=[^[:print:][:space:]]))?/<code class="note">$1<\/code>/g;
# parameter {...}
s/\{([^ {}'"|]*)\}/<code class="special">{$1}<\/code>/g;
# title
s/VIM (?:������������|������������).*|������[���]?/<code class="vim">$&<\/code>/g;
# local heading
s/^(.*)\~$/<code class="section">$1<\/code>/g;
push( @out, $_ );
}
$_ = join( "", @out );
# compatibility notes: {Vi: }.
s/\{((?:Vi[ :]|���)[^}]*)\}/<code class="notvi">{$1}<\/code>/g;
if ($inheader == 1) {
print OUT "<h4>$_</h4>";
} else {
print OUT $_,"\n";
}
$inexample = 2 if $inexample == 1;
$inheader = 0;
}
if ($tagfile) {
print OUT "</table>\n";
}
print OUT<<EOF;
</pre>
<p><i>Generated by vim2html on $date</i></p>
</body>
</html>
EOF
close ( IN );
close ( OUT );
}
sub usage
{
die<<EOF;
vim2html.pl: converts vim documentation to HTML.
usage:
vim2html.pl <tag file> <text files>
EOF
}
# main
#usage() if $#ARGV < 2;
usage() if !defined $ARGV[1];
print "Processing tags...\n";
readTagFile( $ARGV[ 0 ] );
vim2html( $ARGV[ 0 ] );
foreach my $file ( 1..$#ARGV ) {
print "Processing ".$ARGV[ $file ]."...\n";
vim2html( $ARGV[ $file ] );
}
print "done.\n"