| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# converts vim documentation to simple html |
| 4 |
# Sirtaj Singh Kang ([email protected]) |
| 5 |
|
| 6 |
# Sun Feb 24 14:49:17 CET 2002 |
| 7 |
|
| 8 |
# slightly modified by dez for using with fim, 20070501, 20160825, 20240207 |
| 9 |
|
| 10 |
use strict; |
| 11 |
use vars qw/%url $date/; |
| 12 |
use POSIX qw(strftime); |
| 13 |
|
| 14 |
%url = (); |
| 15 |
$date = strftime("%Y-%m-%d", gmtime($ENV{SOURCE_DATE_EPOCH} || time)); |
| 16 |
|
| 17 |
sub maplink |
| 18 |
{ |
| 19 |
my $tag = shift; |
| 20 |
if( exists $url{ $tag } ){ |
| 21 |
return $url{ $tag }; |
| 22 |
} else { |
| 23 |
#warn "Unknown hyperlink target: $tag\n"; |
| 24 |
$tag =~ s/\.TXT//; |
| 25 |
$tag =~ s/</</g; |
| 26 |
$tag =~ s/>/>/g; |
| 27 |
return "<code class=\"badlink\">$tag</code>"; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
sub readTagFile |
| 32 |
{ |
| 33 |
my($tagfile) = @_; |
| 34 |
my( $tag, $file, $name ); |
| 35 |
|
| 36 |
open(TAGS,"$tagfile") || die "can't read tags\n"; |
| 37 |
|
| 38 |
while( <TAGS> ) { |
| 39 |
next unless /^(\S+)\s+(\S+)\s+/; |
| 40 |
|
| 41 |
$tag = $1; |
| 42 |
my $label = $tag; |
| 43 |
($file= $2) =~ s/.TXT$/.html/g; |
| 44 |
$label =~ s/\.TXT//; |
| 45 |
|
| 46 |
$url{ $tag } = "<a href=\"$file#".escurl($tag)."\">".esctext($label)."</a>"; |
| 47 |
} |
| 48 |
close( TAGS ); |
| 49 |
} |
| 50 |
|
| 51 |
sub esctext |
| 52 |
{ |
| 53 |
my $text = shift; |
| 54 |
$text =~ s/&/&/g; |
| 55 |
$text =~ s/</</g; |
| 56 |
$text =~ s/>/>/g; |
| 57 |
return $text; |
| 58 |
} |
| 59 |
|
| 60 |
sub escurl |
| 61 |
{ |
| 62 |
my $url = shift; |
| 63 |
$url =~ s/"/%22/g; |
| 64 |
$url =~ s/~/%7E/g; |
| 65 |
$url =~ s/</%3C/g; |
| 66 |
$url =~ s/>/%3E/g; |
| 67 |
$url =~ s/=/%20/g; |
| 68 |
$url =~ s/#/%23/g; |
| 69 |
$url =~ s/\//%2F/g; |
| 70 |
|
| 71 |
return $url; |
| 72 |
} |
| 73 |
|
| 74 |
sub vim2html |
| 75 |
{ |
| 76 |
my( $infile ) = @_; |
| 77 |
my( $outfile ); |
| 78 |
|
| 79 |
open(IN, "$infile" ) || die "Couldn't read from $infile: $!.\n"; |
| 80 |
|
| 81 |
($outfile = $infile) =~ s:.*/::g; |
| 82 |
$outfile =~ s/\.TXT$//g; |
| 83 |
|
| 84 |
open( OUT, ">$outfile.html" ) |
| 85 |
|| die "Couldn't write to $outfile.html: $!.\n"; |
| 86 |
my $head = uc( $outfile ); |
| 87 |
|
| 88 |
print OUT<<EOF; |
| 89 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 90 |
<html> |
| 91 |
<head> |
| 92 |
<title>FIM: $outfile</title> |
| 93 |
<link rel="stylesheet" href="fim-stylesheet.css" type="text/css"> |
| 94 |
</head> |
| 95 |
<body> |
| 96 |
<h2>$head</h2> |
| 97 |
<pre> |
| 98 |
EOF |
| 99 |
|
| 100 |
my $inexample = 0; |
| 101 |
while( <IN> ) { |
| 102 |
chop; |
| 103 |
if ( /^\s*[-=]+\s*$/ ) { |
| 104 |
print OUT "</pre><hr><pre>"; |
| 105 |
next; |
| 106 |
} |
| 107 |
|
| 108 |
# examples |
| 109 |
elsif( /^>$/ || /\s>$/ ) { |
| 110 |
$inexample = 1; |
| 111 |
chop; |
| 112 |
} |
| 113 |
elsif ( $inexample && /^([<\S])/ ) { |
| 114 |
$inexample = 0; |
| 115 |
$_ = $' if $1 eq "<"; |
| 116 |
} |
| 117 |
|
| 118 |
s/\s+$//g; |
| 119 |
|
| 120 |
# Various vim highlights. note that < and > have already been escaped |
| 121 |
# so that HTML doesn't get screwed up. |
| 122 |
|
| 123 |
my @out = (); |
| 124 |
# print "Text: $_\n"; |
| 125 |
LOOP: |
| 126 |
foreach my $token ( split /((?:\|[^\|]+\|)|(?:\*[^\*]+\*))/ ) { |
| 127 |
if ( $token =~ /^\|([^\|]+)\|/ ) { |
| 128 |
# link |
| 129 |
push( @out, "|".maplink( $1 )."|" ); |
| 130 |
next LOOP; |
| 131 |
} |
| 132 |
elsif ( $token =~ /^\*([^\*]+)\*/ ) { |
| 133 |
# target |
| 134 |
push( @out, |
| 135 |
"<b class=\"vimtag\">\*<a name=\"".escurl($1)."\">".esctext($1)."<\/a>\*<\/b>"); |
| 136 |
next LOOP; |
| 137 |
} |
| 138 |
|
| 139 |
$_ = esctext($token); |
| 140 |
s/CTRL-(\w+)/<code class="keystroke">CTRL-$1<\/code>/g; |
| 141 |
# parameter <...> |
| 142 |
s/<(.*?)>/<code class="special"><$1><\/code>/g; |
| 143 |
|
| 144 |
# parameter {...} |
| 145 |
s/\{([^}]*)\}/<code class="special">{$1}<\/code>/g; |
| 146 |
|
| 147 |
# parameter [...] |
| 148 |
s/\[(range|line|count|offset|cmd|[-+]?num)\]/<code class="special">\[$1\]<\/code>/g; |
| 149 |
# note |
| 150 |
s/(Note:?)/<code class="note">$1<\/code>/gi; |
| 151 |
|
| 152 |
# local heading |
| 153 |
s/^(.*)\~$/<code class="section">$1<\/code>/g; |
| 154 |
push( @out, $_ ); |
| 155 |
} |
| 156 |
|
| 157 |
$_ = join( "", @out ); |
| 158 |
|
| 159 |
if( $inexample == 2 ) { |
| 160 |
print OUT "<code class=\"example\">$_</code>\n"; |
| 161 |
} else { |
| 162 |
print OUT $_,"\n"; |
| 163 |
} |
| 164 |
|
| 165 |
$inexample = 2 if $inexample == 1; |
| 166 |
} |
| 167 |
print OUT<<EOF; |
| 168 |
</pre> |
| 169 |
<p><i>Generated by vim2html on $date</i></p> |
| 170 |
</body> |
| 171 |
</html> |
| 172 |
EOF |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
sub usage |
| 177 |
{ |
| 178 |
die<<EOF; |
| 179 |
vim2html.pl: converts vim documentation to HTML. |
| 180 |
usage: |
| 181 |
|
| 182 |
vim2html.pl <tag file> <text files> |
| 183 |
EOF |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
sub writeCSS |
| 189 |
{ |
| 190 |
open( CSS, ">fim-stylesheet.css" ) || die "Couldn't write stylesheet: $!\n"; |
| 191 |
print CSS<<EOF; |
| 192 |
body { background-color: white; color: black;} |
| 193 |
:link { color: rgb(0,137,139); } |
| 194 |
:visited { color: rgb(0,100,100); |
| 195 |
background-color: white; /* should be inherit */ } |
| 196 |
:active { color: rgb(0,200,200); |
| 197 |
background-color: white; /* should be inherit */ } |
| 198 |
|
| 199 |
B.vimtag { color : rgb(250,0,250); } |
| 200 |
|
| 201 |
h1, h2 { color: rgb(82,80,82); text-align: center; } |
| 202 |
h3, h4, h5, h6 { color: rgb(82,80,82); } |
| 203 |
.headline { color: rgb(0,137,139); } |
| 204 |
.header { color: rgb(164, 32, 246); } |
| 205 |
.section { color: rgb(164, 32, 246); } |
| 206 |
.keystroke { color: rgb(106, 89, 205); } |
| 207 |
.vim { } |
| 208 |
.example { color: rgb(0, 0, 255); } |
| 209 |
.option { } |
| 210 |
.notvi { } |
| 211 |
.special { color: rgb(106, 89, 205); } |
| 212 |
.note { color: blue; background-color: yellow; } |
| 213 |
.sub {} |
| 214 |
.badlink { color: rgb(0,37,39); } |
| 215 |
EOF |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
# main |
| 220 |
usage() if $#ARGV < 1; |
| 221 |
|
| 222 |
print "Processing tags...\n"; |
| 223 |
readTagFile( $ARGV[ 0 ] ); |
| 224 |
|
| 225 |
foreach my $file ( 1..$#ARGV ) { |
| 226 |
print "Processing ".$ARGV[ $file ]."...\n"; |
| 227 |
vim2html( $ARGV[ $file ] ); |
| 228 |
} |
| 229 |
print "Writing stylesheet...\n"; |
| 230 |
writeCSS(); |
| 231 |
print "done.\n" |