Diff of /tags/VIMCDOC_1_6_0/doc/vim2html.pl [000000] .. [r157]  Maximize  Restore

Switch to unified view

a b/tags/VIMCDOC_1_6_0/doc/vim2html.pl
1
#!/usr/bin/env perl
2
3
# converts vim documentation to simple html
4
# Sirtaj Singh Kang (taj@kde.org)
5
6
# Sun Feb 24 14:49:17 CET 2002
7
8
# Updated by Willis (yianwillis@users.sourceforge.net)
9
# Sat Mar 24 12:11:02 EST 2006
10
11
use strict;
12
use utf8;
13
use Text::Tabs; # Willis: expand replaced by myexpand. So no longer necessary.
14
use Encode;
15
use vars qw/%url $date/;
16
17
%url = ();
18
$date = Encode::decode_utf8(`date --universal`);
19
chop $date;
20
21
sub maplink
22
{
23
  my $tag = shift;
24
  if( exists $url{ $tag } ){
25
      return $url{ $tag };
26
  } else {
27
      #warn "Unknown hyperlink target: $tag\n";
28
      $tag =~ s/</&lt;/g;
29
      $tag =~ s/>/&gt;/g;
30
      return "<code class=\"badlink\">$tag</code>";
31
  }
32
}
33
34
sub readTagFile
35
{
36
  my($tagfile) = @_;
37
  my( $tag, $file, $name );
38
39
  open(TAGS,"$tagfile") || die "can't read tags\n";
40
41
  while( <TAGS> ) {
42
      next unless /^(\S+)\s+(\S+)\s+/;
43
44
      $tag = $1;
45
      my $label = $tag;
46
      ($file= $2) =~ s/.\w+$/.html/g; 
47
48
      $url{ $tag } = "<a href=\"$file#".escurl($tag)."\">".esctext($label)."</a>";
49
  }
50
  close( TAGS );
51
}
52
53
sub esctext
54
{
55
  my $text = shift;
56
  $text =~ s/&/&amp;/g;
57
  $text =~ s/</&lt;/g;
58
  $text =~ s/>/&gt;/g;
59
  return $text;
60
}
61
62
sub escurl
63
{
64
  my $url = shift;
65
  $url =~ s/"/%22/g;
66
  $url =~ s/~/%7E/g;
67
  $url =~ s/</%3C/g;
68
  $url =~ s/>/%3E/g;
69
  $url =~ s/=/%20/g;
70
  $url =~ s/#/%23/g;
71
  $url =~ s/\//%2F/g;
72
73
  return $url;
74
}
75
76
# expand() count bytes when computing tabstops. But each utf-8 character may
77
# take up to 3 bytes, we all know CJK characters use double width only.
78
$tabstop = 8;
79
80
sub mylength
81
{
82
    my ($str) = @_;
83
  my $length = length($str);
84
  my $i = 0;
85
  my @chars = unpack("U*", $str);
86
  foreach $i (@chars) {
87
      $length++ if $i > 255;
88
  }
89
  return $length;
90
}
91
92
sub myexpand
93
{   
94
    my (@l) = @_;
95
    for $_ (@l) {
96
        1 while s/(^|\n)([^\t\n]*)(\t+)/
97
            $1. $2 . (" " x 
98
                ($tabstop * length($3)
99
                - (mylength($2) % $tabstop)))
100
            /sex;
101
    }
102
    return @l if wantarray;
103
    return $l[0];
104
}
105
106
sub vim2html
107
{
108
  my( $infile ) = @_;
109
  my( $outfile );
110
111
  open(IN, "<:utf8", "$infile" ) || die "Couldn't read from $infile: $!.\n";
112
113
  ($outfile = $infile) =~ s:.*/::g;
114
115
  my $tagfile =  ($outfile =~ /^tags(-..)?$/);
116
  if ($tagfile) {
117
      $outfile = "tags";
118
  } else {
119
      $outfile =~ s/\.\w+$//g;
120
  }
121
122
  open( OUT, ">:utf8", "$outfile.html" )
123
          || die "Couldn't write to $outfile.html: $!.\n";
124
  binmode STDOUT, ":utf8";
125
  my $head = uc( $outfile );
126
127
  print OUT<<EOF;
128
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
129
<html>
130
<head>
131
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
132
<title>VIM: $outfile</title>
133
<link rel="stylesheet" href="vim-stylesheet.css" type="text/css">
134
</head>
135
<body>
136
<h2>$head</h2>
137
<pre>
138
EOF
139
140
  my $inexample = 0;
141
  my $inheader = 0;
142
  if ($tagfile) {
143
      print OUT "<table>\n";
144
  }
145
  while( <IN> ) {
146
      chop;
147
      if ($tagfile) {
148
          print OUT "<tr>";
149
          my @fields = split(/\t/);
150
          if ($fields[0] eq "!_TAG_FILE_ENCODING") {
151
              print OUT "<td>" .  $fields[0] .  "</td><td>" .  $fields[1];
152
              print OUT "</td><td>" .  $fields[2] .  "</td></tr>\n";
153
          }
154
          else {
155
              $fields[1] =~ s/\.\w+/\.txt/;
156
              print OUT "<td>" .  maplink($fields[0]) .  "</td><td>";
157
              print OUT maplink($fields[1]) . "</td><td>". esctext($fields[2]);
158
              print OUT "</td></tr>\n";
159
          }
160
          next;
161
      }
162
163
      $_ = myexpand($_);
164
      if ( /^=+\s*$/ ) {
165
          print OUT "</pre><hr class=\"doubleline\" /><pre>\n";
166
          $inheader = 1;
167
          next;
168
      }
169
      elsif ( /^\s*-+\s*$/ ) {
170
          print OUT "</pre><hr class=\"singleline\" /><pre>\n";
171
          next;
172
      }
173
174
      # examples
175
      elsif( /^>$/ || /\s>$/ ) {
176
          if ( $inexample && /^(<)/ ) {
177
              $_ = $';
178
              $_ = " " . $_ if /^\s/;
179
          }   
180
          $inexample = 1;
181
          chop;
182
      }
183
      elsif ( $inexample && /^([<\S])/ ) {
184
          $inexample = 0;
185
          $_ = $' if $1 eq "<";
186
          $_ = " " . $_ if /^\s/;
187
      }
188
189
      s/\s+$//g;
190
191
      # Various vim highlights. note that < and > have already been escaped
192
      # so that HTML doesn't get screwed up.
193
194
      my @out = ();
195
      #       print "Text: $_\n";
196
      LOOP:
197
      foreach my $token ( split /((?:\|[^*"|[:space:]]+\|)|(?:\*[^*"|[:space:]]+\*))/ ) {
198
          if ( $token =~ /^\|([^*"|[:space:]]+)\|/ ) {
199
              # link
200
              push( @out, "|".maplink( $1 )."|" );
201
              next LOOP;
202
          }
203
          elsif ( $token =~ /^\*([^*"|[:space:]]+)\*/ ) {
204
              # target
205
              push( @out,
206
                  "<b class=\"vimtag\">\*<a name=\"".escurl($1)."\">".esctext($1)."<\/a>\*<\/b>");
207
              next LOOP;
208
          }
209
210
          $_ = esctext($token);
211
          s/CTRL-(\w+|.)/<code class="keystroke">CTRL-$1<\/code>/g;
212
          # parameter <...>
213
          s/&lt;(.*?)&gt;/<code class="special">&lt;$1&gt;<\/code>/g;
214
215
          # parameter [...]
216
          s/\[(range|line|count|offset|cmd|[-+]?num)\]/<code class="special">\[$1\]<\/code>/g;
217
          # note
218
          s/(Note[:\s])/<code class="note">$1<\/code>/gi;
219
220
          s/(注意|备注)( (?=[^[:print:][:space:]]))?/<code class="note">$1<\/code>/g;
221
          s/VIM (?:参考手册|用户手册).*|译者[注]?/<code class="vim">$&<\/code>/g;
222
223
          # local heading
224
          s/^(.*)\~$/<code class="section">$1<\/code>/g;
225
          push( @out, $_ );
226
      }
227
228
      $_ = join( "", @out );
229
230
      # parameter {...}
231
      s/\{([^}]*)\}/<code class="special">{$1}<\/code>/g;
232
233
      if( $inexample == 2 ) {
234
          print OUT "<code class=\"example\">$_</code>\n";
235
      } elsif ($inheader == 1) {
236
          print OUT "<h4>$_</h4>";
237
      } else {
238
          print OUT $_,"\n";
239
      }
240
241
      $inexample = 2 if $inexample == 1;
242
      $inheader = 0;
243
  }
244
  if ($tagfile) {
245
      print OUT "</table>\n";
246
  }
247
  print OUT<<EOF;
248
</pre>
249
<p><i>Generated by vim2html on $date</i></p>
250
</body>
251
</html>
252
EOF
253
  close ( IN );
254
  close ( OUT );
255
}
256
257
sub usage
258
{
259
die<<EOF;
260
vim2html.pl: converts vim documentation to HTML.
261
usage:
262
263
  vim2html.pl <tag file> <text files>
264
EOF
265
}
266
267
268
269
# main
270
#usage() if $#ARGV < 2;
271
usage() if !defined $ARGV[1];
272
273
print "Processing tags...\n";
274
readTagFile( $ARGV[ 0 ] );
275
276
vim2html( $ARGV[ 0 ] );
277
foreach my $file ( 1..$#ARGV ) {
278
  print "Processing ".$ARGV[ $file ]."...\n";
279
  vim2html( $ARGV[ $file ] );
280
}
281
print "done.\n"

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks