vim72-20100325-kaoriya-w64j.zip
[MacVim/KaoriYa.git] / runtime / doc / vim2html.pl
blobd51f607096d04bffc1482367473fc3ee38fbcfdd
1 #!/usr/bin/env perl
3 # converts vim documentation to simple html
4 # Sirtaj Singh Kang (taj@kde.org)
6 # Sun Feb 24 14:49:17 CET 2002
8 use strict;
9 use vars qw/%url $date/;
11 %url = ();
12 $date = `date`;
13 chop $date;
15 sub maplink
17 my $tag = shift;
18 if( exists $url{ $tag } ){
19 return $url{ $tag };
20 } else {
21 #warn "Unknown hyperlink target: $tag\n";
22 $tag =~ s/\.txt//;
23 $tag =~ s/</&lt;/g;
24 $tag =~ s/>/&gt;/g;
25 return "<code class=\"badlink\">$tag</code>";
29 sub readTagFile
31 my($tagfile) = @_;
32 my( $tag, $file, $name );
34 open(TAGS,"$tagfile") || die "can't read tags\n";
36 while( <TAGS> ) {
37 next unless /^(\S+)\s+(\S+)\s+/;
39 $tag = $1;
40 my $label = $tag;
41 ($file= $2) =~ s/.txt$/.html/g;
42 $label =~ s/\.txt//;
44 $url{ $tag } = "<a href=\"$file#".escurl($tag)."\">".esctext($label)."</a>";
46 close( TAGS );
49 sub esctext
51 my $text = shift;
52 $text =~ s/&/&amp;/g;
53 $text =~ s/</&lt;/g;
54 $text =~ s/>/&gt;/g;
55 return $text;
58 sub escurl
60 my $url = shift;
61 $url =~ s/"/%22/g;
62 $url =~ s/~/%7E/g;
63 $url =~ s/</%3C/g;
64 $url =~ s/>/%3E/g;
65 $url =~ s/=/%20/g;
66 $url =~ s/#/%23/g;
67 $url =~ s/\//%2F/g;
69 return $url;
72 sub vim2html
74 my( $infile ) = @_;
75 my( $outfile );
77 open(IN, "$infile" ) || die "Couldn't read from $infile: $!.\n";
79 ($outfile = $infile) =~ s:.*/::g;
80 $outfile =~ s/\.txt$//g;
82 open( OUT, ">$outfile.html" )
83 || die "Couldn't write to $outfile.html: $!.\n";
84 my $head = uc( $outfile );
86 print OUT<<EOF;
87 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
88 <html>
89 <head>
90 <title>VIM: $outfile</title>
91 <link rel="stylesheet" href="vim-stylesheet.css" type="text/css">
92 </head>
93 <body>
94 <h2>$head</h2>
95 <pre>
96 EOF
98 my $inexample = 0;
99 while( <IN> ) {
100 chop;
101 if ( /^\s*[-=]+\s*$/ ) {
102 print OUT "</pre><hr><pre>";
103 next;
106 # examples
107 elsif( /^>$/ || /\s>$/ ) {
108 $inexample = 1;
109 chop;
111 elsif ( $inexample && /^([<\S])/ ) {
112 $inexample = 0;
113 $_ = $' if $1 eq "<";
116 s/\s+$//g;
118 # Various vim highlights. note that < and > have already been escaped
119 # so that HTML doesn't get screwed up.
121 my @out = ();
122 # print "Text: $_\n";
123 LOOP:
124 foreach my $token ( split /((?:\|[^\|]+\|)|(?:\*[^\*]+\*))/ ) {
125 if ( $token =~ /^\|([^\|]+)\|/ ) {
126 # link
127 push( @out, "|".maplink( $1 )."|" );
128 next LOOP;
130 elsif ( $token =~ /^\*([^\*]+)\*/ ) {
131 # target
132 push( @out,
133 "<b class=\"vimtag\">\*<a name=\"".escurl($1)."\">".esctext($1)."<\/a>\*<\/b>");
134 next LOOP;
137 $_ = esctext($token);
138 s/CTRL-(\w+)/<code class="keystroke">CTRL-$1<\/code>/g;
139 # parameter <...>
140 s/&lt;(.*?)&gt;/<code class="special">&lt;$1&gt;<\/code>/g;
142 # parameter {...}
143 s/\{([^}]*)\}/<code class="special">{$1}<\/code>/g;
145 # parameter [...]
146 s/\[(range|line|count|offset|cmd|[-+]?num)\]/<code class="special">\[$1\]<\/code>/g;
147 # note
148 s/(Note:?)/<code class="note">$1<\/code>/gi;
150 # local heading
151 s/^(.*)\~$/<code class="section">$1<\/code>/g;
152 push( @out, $_ );
155 $_ = join( "", @out );
157 if( $inexample == 2 ) {
158 print OUT "<code class=\"example\">$_</code>\n";
159 } else {
160 print OUT $_,"\n";
163 $inexample = 2 if $inexample == 1;
165 print OUT<<EOF;
166 </pre>
167 <p><i>Generated by vim2html on $date</i></p>
168 </body>
169 </html>
174 sub usage
176 die<<EOF;
177 vim2html.pl: converts vim documentation to HTML.
178 usage:
180 vim2html.pl <tag file> <text files>
185 sub writeCSS
187 open( CSS, ">vim-stylesheet.css" ) || die "Couldn't write stylesheet: $!\n";
188 print CSS<<EOF;
189 body { background-color: white; color: black;}
190 :link { color: rgb(0,137,139); }
191 :visited { color: rgb(0,100,100);
192 background-color: white; /* should be inherit */ }
193 :active { color: rgb(0,200,200);
194 background-color: white; /* should be inherit */ }
196 B.vimtag { color : rgb(250,0,250); }
198 h1, h2 { color: rgb(82,80,82); text-align: center; }
199 h3, h4, h5, h6 { color: rgb(82,80,82); }
200 .headline { color: rgb(0,137,139); }
201 .header { color: rgb(164, 32, 246); }
202 .section { color: rgb(164, 32, 246); }
203 .keystroke { color: rgb(106, 89, 205); }
204 .vim { }
205 .example { color: rgb(0, 0, 255); }
206 .option { }
207 .notvi { }
208 .special { color: rgb(106, 89, 205); }
209 .note { color: blue; background-color: yellow; }
210 .sub {}
211 .badlink { color: rgb(0,37,39); }
216 # main
217 usage() if $#ARGV < 2;
219 print "Processing tags...\n";
220 readTagFile( $ARGV[ 0 ] );
222 foreach my $file ( 1..$#ARGV ) {
223 print "Processing ".$ARGV[ $file ]."...\n";
224 vim2html( $ARGV[ $file ] );
226 print "Writing stylesheet...\n";
227 writeCSS();
228 print "done.\n"