#!/usr/bin/perl

use strict;
# Converts vim documentation to simple html for printing
# Uses Vim to do the HTML, so you can use your favourite colourscheme.
# The default colourscheme is printman that comes with this package
# by Dion Nicolaas <dion@nicolaas.net> 

# Set this to your executable of Vim
my $vimcmd = "d:/bin/vim/vim60aw/gvim.exe";
# Set this to your favourite colourscheme
my $colourscheme = "printman";

# ======================================================================
# Nothing below this line should have to be changed.
# ======================================================================

sub vim2html
{
    # viminfile is the original which will be munched by vim.
    while (@_) {
        my $viminfile = shift @_;
        my $infile;
        # execute
        # vim $viminfile -c "so $VIMRUNTIME/syntax/2html.vim" -c "wq" -c "q"
        # to let vim do the HTMLizing
        my (@args) = 
                    ($vimcmd, $viminfile,
                        "-c", "\"colorscheme $colourscheme\"", 
                        "-c", "\"so \$VIMRUNTIME/syntax/2html.vim\"", 
                        "-c", "wq", "-c", "q"
                    );
        system(@args) == 0
            or die "system @args failed: $?";
                                   
        $infile = "$viminfile.html";

        open(IN, "$infile") || die "Couldn't read from $infile.\n";

        # Replace applicable parts
        while(<IN>) {
            my $skip = 0;
            s/<html>//g and $skip = 1;
            s/<\/html>//g and $skip = 1;
            s/<head>//g and $skip = 1;
            s/<\/head>//g and $skip = 1;
            s/<body.*>//g and $skip = 1;
            s/<\/body>//g and $skip = 1;
            s/<title>.*<\/title>//g and $skip = 1;
            s/<meta.*>//g and $skip = 1;
            s/<pre>//g and $skip = 1;
            s/<\/pre>//g and $skip = 1;

            s/^\s*VIM USER MANUAL - by Bram Moolenaar\s*$// and $skip = 1;
            s/^\s*Next chapter: .*$// and $skip = 1;
            s/^\s*Previous chapter: .*$// and $skip = 1;
            s/^\s*Table of contents: .*$// and $skip = 1;
            s/^\s*Copyright: see.*$// and $skip = 1;

            print $_ if not $skip;
        }
        # infile is intermediate, can now go
        unlink $infile;
    }
}

sub usage
{
die<<EOF;
usr2html.pl: converts vim user manual to HTML.
usage:
    usr2html.pl <text files>
    
    <text files> should have the extension .txt
EOF
}

# main

if ( $#ARGV < 1 ) {
    print "ERROR: too few arguments\n";
    usage();
}

print <<EOF;
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>VIM USER MANUAL - by Bram Moolenaar</title>
<meta name="Generator" content="Vim/6.0 and usr2html.pl">
</head>
<body bgcolor="#ffffff" text="#000000">
<pre>
		     VIM USER MANUAL - by Bram Moolenaar

EOF

vim2html(@ARGV);

print <<EOF

Copyright: see |manual-copyright|
</pre>
</body>
</html>
EOF

