<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env perl

# converts vim documentation to simple html
# Sirtaj Singh Kang (taj@kde.org)

# Sun Feb 24 14:49:17 CET 2002

use strict;
use vars qw/%url $date/;

%url = ();
$date = `date`;
chop $date;

sub maplink
{
	my $tag = shift;
	if( exists $url{ $tag } ){
		return $url{ $tag };
	} else {
		#warn "Unknown hyperlink target: $tag\n";
		$tag =~ s/\.txt//;
		$tag =~ s/&lt;/&amp;lt;/g;
		$tag =~ s/&gt;/&amp;gt;/g;
		return "&lt;code class=\"badlink\"&gt;$tag&lt;/code&gt;";
	}
}

sub readTagFile
{
	my($tagfile) = @_;
	my( $tag, $file, $name );

	open(TAGS,"$tagfile") || die "can't read tags\n";

	while( &lt;TAGS&gt; ) {
		next unless /^(\S+)\s+(\S+)\s+/;

		$tag = $1;
		my $label = $tag;
		($file= $2) =~ s/.txt$/.html/g;
		$label =~ s/\.txt//;

		$url{ $tag } = "&lt;a href=\"$file#".escurl($tag)."\"&gt;".esctext($label)."&lt;/a&gt;";
	}
	close( TAGS );
}

sub esctext
{
	my $text = shift;
	$text =~ s/&amp;/&amp;amp;/g;
	$text =~ s/&lt;/&amp;lt;/g;
	$text =~ s/&gt;/&amp;gt;/g;
	return $text;
}

sub escurl
{
	my $url = shift;
	$url =~ s/"/%22/g;
	$url =~ s/~/%7E/g;
	$url =~ s/&lt;/%3C/g;
	$url =~ s/&gt;/%3E/g;
	$url =~ s/=/%20/g;
	$url =~ s/#/%23/g;
	$url =~ s/\//%2F/g;

	return $url;
}

sub vim2html
{
	my( $infile ) = @_;
	my( $outfile );

	open(IN, "$infile" ) || die "Couldn't read from $infile: $!.\n";

	($outfile = $infile) =~ s:.*/::g;
	$outfile =~ s/\.txt$//g;

	open( OUT, "&gt;$outfile.html" )
			|| die "Couldn't write to $outfile.html: $!.\n";
	my $head = uc( $outfile );

	print OUT&lt;&lt;EOF;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;VIM: $outfile&lt;/title&gt;
&lt;link rel="stylesheet" href="vim-stylesheet.css" type="text/css"&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h2&gt;$head&lt;/h2&gt;
&lt;pre&gt;
EOF

	my $inexample = 0;
	while( &lt;IN&gt; ) {
		chop;
		if ( /^\s*[-=]+\s*$/ ) {
			print OUT "&lt;/pre&gt;&lt;hr&gt;&lt;pre&gt;";
			next;
		}

		# examples
		elsif( /^&gt;$/ || /\s&gt;$/ ) {
			$inexample = 1;
			chop;
		}
		elsif ( $inexample &amp;&amp; /^([&lt;\S])/ ) {
			$inexample = 0;
			$_ = $' if $1 eq "&lt;";
		}

		s/\s+$//g;

		# Various vim highlights. note that &lt; and &gt; have already been escaped
		# so that HTML doesn't get screwed up.

		my @out = ();
		#		print "Text: $_\n";
		LOOP:
		foreach my $token ( split /((?:\|[^\|]+\|)|(?:\*[^\*]+\*))/ ) {
			if ( $token =~ /^\|([^\|]+)\|/ ) {
				# link
				push( @out, "|".maplink( $1 )."|" );
				next LOOP;
			}
			elsif ( $token =~ /^\*([^\*]+)\*/ ) {
				# target
				push( @out,
					"&lt;b class=\"vimtag\"&gt;\*&lt;a name=\"".escurl($1)."\"&gt;".esctext($1)."&lt;\/a&gt;\*&lt;\/b&gt;");
				next LOOP;
			}

			$_ = esctext($token);
			s/CTRL-(\w+)/&lt;code class="keystroke"&gt;CTRL-$1&lt;\/code&gt;/g;
			# parameter &lt;...&gt;
			s/&amp;lt;(.*?)&amp;gt;/&lt;code class="special"&gt;&amp;lt;$1&amp;gt;&lt;\/code&gt;/g;

			# parameter {...}
			s/\{([^}]*)\}/&lt;code class="special"&gt;{$1}&lt;\/code&gt;/g;

			# parameter [...]
			s/\[(range|line|count|offset|cmd|[-+]?num)\]/&lt;code class="special"&gt;\[$1\]&lt;\/code&gt;/g;
			# note
			s/(Note:?)/&lt;code class="note"&gt;$1&lt;\/code&gt;/gi;

			# local heading
			s/^(.*)\~$/&lt;code class="section"&gt;$1&lt;\/code&gt;/g;
			push( @out, $_ );
		}

		$_ = join( "", @out );

		if( $inexample == 2 ) {
			print OUT "&lt;code class=\"example\"&gt;$_&lt;/code&gt;\n";
		} else {
			print OUT $_,"\n";
		}

		$inexample = 2 if $inexample == 1;
	}
	print OUT&lt;&lt;EOF;
&lt;/pre&gt;
&lt;p&gt;&lt;i&gt;Generated by vim2html on $date&lt;/i&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
EOF

}

sub usage
{
die&lt;&lt;EOF;
vim2html.pl: converts vim documentation to HTML.
usage:

	vim2html.pl &lt;tag file&gt; &lt;text files&gt;
EOF
}



sub writeCSS
{
	open( CSS, "&gt;vim-stylesheet.css"  ) || die "Couldn't write stylesheet: $!\n";
	print CSS&lt;&lt;EOF;
body { background-color: white; color: black;}
:link { color: rgb(0,137,139); }
:visited { color: rgb(0,100,100);
           background-color: white; /* should be inherit */ }
:active { color: rgb(0,200,200);
          background-color: white; /* should be inherit */ }

B.vimtag { color : rgb(250,0,250); }

h1, h2 { color: rgb(82,80,82); text-align: center; }
h3, h4, h5, h6 { color: rgb(82,80,82); }
.headline { color: rgb(0,137,139); }
.header { color: rgb(164, 32, 246); }
.section { color: rgb(164, 32, 246); }
.keystroke { color: rgb(106, 89, 205); }
.vim { }
.example { color: rgb(0, 0, 255); }
.option { }
.notvi { }
.special { color: rgb(106, 89, 205); }
.note { color: blue; background-color: yellow; }
.sub {}
.badlink { color: rgb(0,37,39); }
EOF

}

# main
usage() if $#ARGV &lt; 2;

print "Processing tags...\n";
readTagFile( $ARGV[ 0 ] );

foreach my $file ( 1..$#ARGV ) {
	print "Processing ".$ARGV[ $file ]."...\n";
	vim2html( $ARGV[ $file ] );
}
print "Writing stylesheet...\n";
writeCSS();
print "done.\n"
</pre></body></html>