#!/usr/bin/perl

#vim:et:ts=4:

# converts vim documentation to simple html

# Sirtaj Singh Kang (taj@kde.org)

# Wed Oct 8 01:15:48 EST 1997

# Edited and extended by Dion Nicolaas

# (version 2.0, 3.0, 3.1, 3.3, 3.3.1)

# Made into a CGI by Thorsten Maerz

# (version 3.2)

# New in version 2.0:

# - Added Windows NT date command

# - fixed ARGV bug

# - Let Vim do the HTMLizing, just add links afterwards (this adds syntax

# highlighting)

# Wed 27-09-2000

# New in version 3.0:

# - Use Perl date

# - Use lowercase html tags to work with vim 6.0

# - Match every keyword that appears in the text

# Tue 25-09-01

#

# New in version 3.1:

# - Option for HTML level

# - Do replace < and > when no HTML

# Thu 27-09-01

# New in version 3.2:

# - works as cgi script

# Thu 27-09-01

# New in version 3.3:

# - More nevermatch words

# - Remove modelines from files

# - improved Perl HTMLising

# Fri 28-09-01

# New in version 3.3.1:

# - use vims colorschemes (quickndirty)

use strict;

# set $isCGI to 1 to run as a cgi-script

my $isCGI = 1;

# Set this to your executable of Vim

#my $vimcmd = "d:/bin/vim/vim60/gvim.exe";

my $vimcmd = "vim";

# Set this to all words you don't want to match, in between **

my $nevermatch = "*is* *as* *end* *section* *various* *case* *put* *help* *starting* *go*";

# ======================================================================

# Nothing below this line should have to be changed.

# ======================================================================

my %syn;

my $CGIfilename;

my $CGIsearch;

my $CGIbasename;

if ($isCGI) {

use Fcntl;

use POSIX qw(tmpnam);

use CGI qw/:standard/;

($CGIbasename = $0) =~ s#.*/([^/]+)#$1#;

}

my $date = gmtime;

my %url = ();

my %file = ();

my $opt_htmlising = 2;

my $progID = "vim2html.pl 3.2";

sub readTagFile

{

my($tagfile) = @_;

my($tag, $file);

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

while () {

s/

s/>/>/g;

/^(.*)\t(.*)\t/;

$tag = $1;

if ($isCGI) {

$file{$tag}= $2;

# $url{$tag} = "$tag";

$url{$tag} = "

if (param('cs')) {

$url{$tag} .= "?cs=".param('cs').'&';

}

else {

$url{$tag} .= '?';

}

$url{$tag} .= "page=$file{$tag}#$tag\">$tag";

}

else {

($file{$tag}= $2) =~ s/.txt$/.html/g;

$url{$tag} = "$tag";

}

}

close(TAGS);

}

sub vim2html

{

# viminfile is the original which will be munched by vim.

my ($viminfile) = @_;

my $infile;

$infile = "$viminfile.html";

if ($opt_htmlising < 2) {

open (IN, "$viminfile") or die "Can't read input file $viminfile: $?";

if ($isCGI) {

# try new temporary filenames until we get one that didn't already

# exist; the check should be unnecessary, but you can't be too careful

do { $infile = tmpnam() }

until sysopen(INTER, $infile, O_RDWR|O_CREAT|O_EXCL);

}

else {

open(INTER, ">$infile")

or die "Can't write intermediate file $infile: $?";

}

if ($opt_htmlising == 1) {

# write header

print INTER "

";

}

my ($example, $examplecount) = 0;

my $curly = 0;

# Create a "safe" file

while () {

# Replace tabs with spaces (ts=8)

my $pos;

while (($pos = index($_, "\t")) != -1) {

$pos %= 8;

if ($pos == 8) { $pos = 0; }

my $padding = substr(" ", 0, 8 - $pos);

s/\t/$padding/;

}

s/

s/>/>/g;

if ($opt_htmlising == 1 || $isCGI) {

# bugs

# intro.txt:*CTRL-{char}*

# do syntax highlighting in Perl

s/^\<$//; # only <

s/^\<(\s)/ $1/; # leading <

s/\>$// unless /\</; # trailing >

s/(.*)~$/$syn{'Header'}$1$syn{'end'}/; # trailing ~

s/^[-=]+$/$syn{'SectionDelim'}$&$syn{'end'}/g; # ===== or ----

# dont highlight ^*word* : wouldnt be recognized as jump target later

s/ \*[^ *]*\*/$syn{'HyperTextEntry'}$&$syn{'end'}/g; # *Entry*

s/(?

s/(?

s/(?

s/(?

# multiline fails on usr_40.txt

#s/(?

#if (s/(?

#if ($curly) { s/[^}]*}/$&$syn{'end'}/g and $curly = 0; }

s/(?

s/(?

s/(?

s/(?

s/(?

s/(?

s/(?

s/^CTRL-./$syn{'Special'}$&$syn{'end'}/g;

s/\bN\b/$syn{'Special'}$&$syn{'end'}/g; # Not quite right

#s/"(.*)"/$&<\/b>/g; # bold "word"

}

print INTER $_;

}

if ($opt_htmlising == 1) {

# write footer

print INTER "

";

}

close(INTER);

}

else {

# execute

# gvim $viminfile -c "so $VIMRUNTIME/syntax/2html.vim" -c "wq" -c "q"

# to let vim do the HTMLizing

my $socmd;

if ($ENV{'OSTYPE'} =~ /linux/i) {

$socmd = "so \$VIMRUNTIME/syntax/2html.vim"

}

else {

$socmd = "\"so \$VIMRUNTIME/syntax/2html.vim\""

}

my (@args) = ($vimcmd, $viminfile, "-c", $socmd, "-c", "wq", "-c", "q");

print @args;

system(@args) == 0

or die "system @args failed: $?";

}

my $outfile;

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

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

# infile is called .txt.html

$outfile =~ s/\.txt\.html$//g;

if ($isCGI) {

open(OUT, ">-");

}

else {

open(OUT, ">$outfile.html")

|| die "Couldn't write to $outfile.html.\n";

}

my $dontmatch = ""; # tags not to match in this paragraph

my $currentlabel = ""; # used to build $dontmatch

# Replace applicable parts

while () {

# Change the title and add an H1

s/.*<\/title>/<title>Vim documentation: $outfile<\/title>/g;</p><p>s/<pre>/<h1>Vim documentation: $outfile<\/h1><hr><pre>/g;</p><p># Add bottom line</p><p>s/<\/pre>/<\/pre><hr><p><i>Generated by <tt>$progID<\/tt> on $date<\/i><\/p>/g;</p><p># Remove modeline</p><p>s/ vim:.*:\s*$//;</p><p># Links</p><p># We don't want to link to the section we're reading, so we remember</p><p># where we are.</p><p>if (m/\*[^ *]+\*/) { # label in this line?</p><p># remember it</p><p>$currentlabel .= $_;</p><p>$dontmatch = $nevermatch . $currentlabel;</p><p>} else {</p><p># first line of this section</p><p>$currentlabel = "";</p><p>}</p><p># replace all applicable words with a link</p><p>chomp;</p><p>my $out = ""; # we'll build the output in here.</p><p>REPLACE:</p><p>while (length $_ ) {</p><p># copy various pieces of line to $out, changing them into</p><p># links where appropriate. The order here is significant, as we</p><p># don't want to touch e.g. HTML tags.</p><p># copy leading spaces</p><p>if (s/^(\s+)//) {</p><p>$out .= $1;</p><p>next REPLACE;</p><p>}</p><p># copy html tags</p><p>if (s/^(<[^>]+>)//)</p><p>{</p><p>$out .= $1;</p><p>next REPLACE;</p><p>}</p><p># copy keywords in **</p><p>if (s/^(\*[^| ]+\*)//)</p><p>{</p><p>$out .= $1;</p><p>next REPLACE;</p><p>}</p><p># keywords in ""</p><p># Mostly appear in sentences like: 'the "/" command ...'</p><p># So we replace them almost always with a link.</p><p>if (s/^\"([^| ]+)\"//) {</p><p>my $tag = $1;</p><p># don't link when it appears in $dontmatch</p><p>my $skip = ($dontmatch =~ m/\*\Q$tag\E\*/);</p><p>if ($url{$tag} and not $skip) {</p><p>$out .= "\"$url{$tag}\"";</p><p>}</p><p>else {</p><p>$out .= "\"$tag\"";</p><p>}</p><p>next REPLACE;</p><p>}</p><p># keywords in ||</p><p># We always replace them with a link, if the link exists.</p><p>if (s/^\|([^| ]+)\|//) {</p><p>if ($url{$1}) {</p><p>$out .= "\|$url{$1}\|";</p><p>}</p><p>else {</p><p>$out .= "\|$1\|";</p><p>}</p><p>next REPLACE;</p><p>}</p><p># plain word</p><p># We replace them if not in $dontmatch and longer than 1 char, to</p><p># prevent changing a and I and many others</p><p>if (s/^([^ |<"]+)//) {</p><p>my $tag = $1;</p><p>my $skip = ($dontmatch =~ m/\*\Q$tag\E\*/);</p><p># no one char hits (includes > and <)</p><p>if (length($tag) > 1 and</p><p>$tag ne "<" and</p><p>$tag ne ">"</p><p>and</p><p>$url{$tag}</p><p>and not $skip) {</p><p>$out .= "$url{$tag}";</p><p>}</p><p>else {</p><p>$out .= $tag;</p><p>}</p><p>next REPLACE;</p><p>}</p><p># unmatched <"|, copy.</p><p>if (s/^([|<"])//) {</p><p>$out .= $1;</p><p>next REPLACE;</p><p>}</p><p># NOTREACHED</p><p>die "Nothing matched? line = \"$_\"\n";</p><p>}</p><p># *keyword* is only replaced now, to make skipping them earlier easier</p><p>$out =~ s/\*([^ *]+)\*/\*<a name="$1">$1<\/a>\*/g;</p><p>print OUT "$out\n";</p><p>}</p><p># infile is intermediate, can now go</p><p>unlink $infile;</p><p>}</p><p>sub usage</p><p>{</p><p>die<<EOF;</p><p>$progID (Thu 27-09-2001)</p><p>Converts vim documentation to HTML.</p><p>usage:</p><p>vim2html.pl [-v{0|1|2}] <tag file> <text files></p><p>-v0 means no HTMLising (apart from links)</p><p>-v1 means basic HTML</p><p>-v2 means let Vim do the HTMLising.</p><p>Default is -v2.</p><p><text files> should have the extension .txt</p><p>The output files will have the extension .html</p><p>EOF</p><p>}</p><p># CGI / HTML header and footer</p><p>sub CGIStartHTML</p><p>{</p><p>my $color=param('cs');</p><p>print <<EOF;</p><p>Content-type: text/html</p><p><HTML></p><p><HEAD><TITLE>Vim online doc: $CGIfilename

VimDoc: $CGIfilename

select color

help

manual

PS / PDF

single file

VimOnline

Vim.org

search

VimDoc


EOF

}

sub CGIEndHTML

{

$CGIsearch=param('search') || '';

my $color=param('cs');

print <


Main help   

Table Of Contents

Automatically generated by $progID on $date

EOF

}

sub searchTag

{

my( $file, $name );

my $CGIsearch = shift;

my $count;

foreach (keys(%url)) {

if (/$CGIsearch/i) {

$count++;

print "$file{$_} : $url{$_}\n";

}

}

$count = $count || 'no';

print "\n
$count hits";

}

# main

if ($isCGI) {

my $pipecmd='cvs -z9 -d:pserver:anonymous@cvs1:/cvsroot/vim co -p vim/runtime/doc/';

$opt_htmlising=0;

$CGIfilename=param('page') || 'help.txt';

$CGIfilename = 'search "'.param('search').'"' if param('search');

if (param('cs') eq 'blue') {

$syn{'Header'} = '';

$syn{'SectionDelim'} = '';

$syn{'Example'} = '';

$syn{'HyperTextJump'} = '';

$syn{'HyperTextEntry'} = '';

$syn{'Option'} = '';

$syn{'Special'} = '';

$syn{'color'} = 'bgcolor="#000088" text="#f8fcf8" '

.'LINK="#b8bcb8" '

.'VLINK="#888c88" '

;

$syn{'end'} = '';

}

elsif (param('cs') eq 'elflord') {

$syn{'Header'} = '';

$syn{'SectionDelim'} = '';

$syn{'Example'} = '';

$syn{'HyperTextJump'} = '';

$syn{'HyperTextEntry'} = '';

$syn{'Option'} = '';

$syn{'Special'} = '';

$syn{'color'} = 'bgcolor="#000000" text="#00fcf8" '

.'LINK="#40fcf8" '

.'VLINK="#30aca8" '

;

$syn{'end'} = '';

}

elsif (param('cs') eq 'evening') {

$syn{'Header'} = '';

$syn{'SectionDelim'} = '';

$syn{'Example'} = '';

$syn{'HyperTextJump'} = '';

$syn{'HyperTextEntry'} = '';

$syn{'Option'} = '';

$syn{'Special'} = '';

$syn{'color'} = 'bgcolor="#303030" text="#f8fcf8" '

.'LINK="#40fcf8" '

.'VLINK="#30aca8" '

;

$syn{'end'} = '';

}

elsif (param('cs') eq 'koehler') {

$syn{'Header'} = '';

$syn{'SectionDelim'} = '';

$syn{'Example'} = '';

$syn{'HyperTextJump'} = '';

$syn{'HyperTextEntry'} = '';

$syn{'Option'} = '';

$syn{'Special'} = '';

$syn{'color'} = 'bgcolor="#000000" text="#f8fcf8" '

.'LINK="#40fcf8" '

.'VLINK="#30aca8" '

;

$syn{'end'} = '';

}

elsif (param('cs') eq 'morning') {

$syn{'Header'} = '';

$syn{'SectionDelim'} = '';

$syn{'Example'} = '';

$syn{'HyperTextJump'} = '';

$syn{'HyperTextEntry'} = '';

$syn{'Option'} = '';

$syn{'Special'} = '';

$syn{'color'} = 'bgcolor="#e0e4e0" text="#000000" '

.'LINK="#008888" '

.'VLINK="#006868" '

;

$syn{'end'} = '';

}

elsif (param('cs') eq 'murphy') {

$syn{'Header'} = '';

$syn{'SectionDelim'} = '';

$syn{'Example'} = '';

$syn{'HyperTextJump'} = '';

$syn{'HyperTextEntry'} = '';

$syn{'Option'} = '';

$syn{'Special'} = '';

$syn{'color'} = 'bgcolor="#000000" text="#90ec90" '

.'LINK="#00fcf8" '

.'VLINK="#00aca8" '

;

$syn{'end'} = '';

}

elsif (param('cs') eq 'pablo') {

$syn{'Header'} = '';

$syn{'SectionDelim'} = '';

$syn{'Example'} = '';

$syn{'HyperTextJump'} = '';

$syn{'HyperTextEntry'} = '';

$syn{'Option'} = '';

$syn{'Special'} = '';

$syn{'color'} = 'bgcolor="#000000" text="#f8fcf8" '

.'LINK="#00c0c0" '

.'VLINK="#008080" '

;

$syn{'end'} = '';

}

elsif (param('cs') eq 'peachpuff') {

$syn{'Header'} = '';

$syn{'SectionDelim'} = '';

$syn{'Example'} = '';

$syn{'HyperTextJump'} = '';

$syn{'HyperTextEntry'} = '';

$syn{'Option'} = '';

$syn{'Special'} = '';

$syn{'color'} = 'bgcolor="#f8d8b8" text="#000000" '

.'LINK="#008888" '

.'VLINK="#006868" '

;

$syn{'end'} = '';

}

elsif (param('cs') eq 'printman') {

$syn{'Header'} = '';

$syn{'SectionDelim'} = '';

$syn{'Example'} = '';

$syn{'HyperTextJump'} = '';

$syn{'HyperTextEntry'} = '';

$syn{'Option'} = '';

$syn{'Special'} = '';

$syn{'color'} = 'bgcolor="#f8fcf8" text="#000000" '

.'LINK="#000000" '

.'VLINK="#a8aca8" '

;

$syn{'end'} = '';

}

elsif (param('cs') eq 'ron') {

$syn{'Header'} = '';

$syn{'SectionDelim'} = '';

$syn{'Example'} = '';

$syn{'HyperTextJump'} = '';

$syn{'HyperTextEntry'} = '';

$syn{'Option'} = '';

$syn{'Special'} = '';

$syn{'color'} = 'bgcolor="#000000" text="#00fcf8" '

.'LINK="#00fcf8" '

.'VLINK="#00aca8" '

;

$syn{'end'} = '';

}

elsif (param('cs') eq 'shine') {

$syn{'Header'} = '';

$syn{'SectionDelim'} = '';

$syn{'Example'} = '';

$syn{'HyperTextJump'} = '';

$syn{'HyperTextEntry'} = '';

$syn{'Option'} = '';

$syn{'Special'} = '';

$syn{'color'} = 'bgcolor="#f8fcf8" text="#000000" '

.'LINK="#008888" '

.'VLINK="#006868" '

;

$syn{'end'} = '';

}

elsif (param('cs') eq 'torte') {

$syn{'Header'} = '';

$syn{'SectionDelim'} = '';

$syn{'Example'} = '';

$syn{'HyperTextJump'} = '';

$syn{'HyperTextEntry'} = '';

$syn{'Option'} = '';

$syn{'Special'} = '';

$syn{'color'} = 'bgcolor="#000000" text="#c8ccc8" '

.'LINK="#00fc00" '

.'VLINK="#00ac00" '

;

$syn{'end'} = '';

}

elsif (param('cs') eq 'zellner') {

$syn{'Header'} = '';

$syn{'SectionDelim'} = '';

$syn{'Example'} = '';

$syn{'HyperTextJump'} = '';

$syn{'HyperTextEntry'} = '';

$syn{'Option'} = '';

$syn{'Special'} = '';

$syn{'color'} = 'bgcolor="#f8fcf8" text="#000000" '

.'LINK="#0000f8" '

.'VLINK="#0000a8" '

;

$syn{'end'} = '';

}

else { # (param('cs') eq 'default') {

$syn{'Header'} = '';

$syn{'SectionDelim'} = '';

$syn{'Example'} = '';

$syn{'HyperTextJump'} = '';

$syn{'HyperTextEntry'} = '';

$syn{'Option'} = '';

$syn{'Special'} = '';

$syn{'color'} = 'bgcolor="#ffffff" text="#000000" '

.'LINK="#008888" '

.'VLINK="#006868" '

;

$syn{'end'} = '';

}

CGIStartHTML();

readTagFile($pipecmd.'tags|');

if (param('search')) {

searchTag(param('search'));

}

else {

vim2html($pipecmd.$CGIfilename.'|');

print "

";

}

CGIEndHTML();

}

else {

if ($#ARGV < 1) {

print "ERROR: too few arguments\n";

usage();

}

my $nextarg = 0;

my $more = 0;

do {

print "$ARGV[$nextarg]\n";

$more = 0;

if ($ARGV[$nextarg] eq "-h0") {

$opt_htmlising = 0; # no html

$nextarg++;

$more = 1;

}

elsif ($ARGV[$nextarg] eq "-h1") {

$opt_htmlising = 1; # basic html

$nextarg++;

$more = 1;

}

elsif ($ARGV[$nextarg] eq "-h2") {

$opt_htmlising = 2; # Vim html

$nextarg++;

$more = 1;

}

} while ($more);

print "Processing tags...\n";

readTagFile($ARGV[$nextarg]);

$nextarg++;

foreach my $file ($nextarg..$#ARGV) {

print "Processing ".$ARGV[ $file ]."...\n";

vim2html($ARGV[$file]);

}

}

#introductions

Reply to this note

Please Login to reply.

Discussion

No replies yet.