#!/usr/bin/perl

#    xmlmanhtml - simple xml to man converter
#    Copyright (C) 2000-2002  Oliver Kurth <oku@masqmail.cx>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use XML::Parser;

my $section = "";

my %terminators = (
		   manpage => "</td></tr></table></center>\n",
		   synopsis => "</b>\n",
		   description => "\n",
		   options => "",
		   seealso => "\n",
		   section => "\n",
#		   option => "\n",
		   arg => "</em>",
		   file => "</em>",
		   opt => "</b>",
#		   optdesc => "\n",
		   cmd => "<br>\n",
		   p => "</p>"
		  );

sub flush_buf{
  local $_;

  print $buffer;

  $buffer = "";
}

sub handle_start{
  local $_;

  my $expat = shift;
  my $element = shift;

  my %attr = @_;

  flush_buf();

  $_ = $element;

  /^manpage$/ and do{
    print "<center><table width=\"80%\">\n<tr><td>";
    print "<h1>$attr{name}</h1>\n";
    print "<h2>" . $attr{desc} . "</h2>\n";
  };
  /^synopsis$/ and do{
    print "<h2>Synopsis</h2>\n<b>";
    $section = $element;
  };
  /^description$/ and do{
    print "<h2>Description</h2>\n";
    $section = $element;
  };
  /^options$/ and do{
    print "<h2>Options</h2>\n";
    $section = $element;
  };
  /^seealso$/ and do{
    print "<h2>see also</h2>\n";
    $section = $element;
  };
  /^section$/ and do{
    print "<h2>".$attr{name}."</h2>\n";
    $section = $attr{name};
  };
  /^arg$/ and do{
    print "<em>";
  };
  /^file$/ and do{
    print "<em>";
  };
  /^opt$/ and do{
    print "<b>";
    $opt = 1;
  };
  /^manref$/ and do{
    if(!$attr{href}){
      print "<b>" . $attr{name} ." (" . $attr{section} . ")</b>";
    }else{
      print "<a href=\"$attr{href}\">" . $attr{name}. "</a>";
    }
  };
  /^p$/ and do{
    print "<p>";
#    $paragraph = 1;
#    $break = 1;
  };
  /^url$/ and do{
    print "<a href = \"$attr{href}\">".$attr{href}."</a>";
  };
}

sub handle_end{
  local $_;

  my $expat = shift;
  my $element = shift;

  flush_buf();

  my %attr = @_;

  $section = "" if($section eq $element);

  print $terminators{$element} if($terminators{$element});
}

sub handle_char{
  local $_;
  my $expat = shift;
  my $string = shift;

  $string =~ s/&/&amp;/gs;
  $string =~ s/</&lt;/gs;
  $string =~ s/>/&gt;/gs;
  $string =~ s/"/&quot;/gs;

  $buffer .= $string;
}

MAIN:{
  my $file = shift;

  if (!$file) {
    print STDERR "You need to specify a file to parse\n";
    exit(1);
  }

  my $parser = new XML::Parser(Handlers => {Start => \&handle_start,
					    End   => \&handle_end,
					    Char  => \&handle_char});

  print '<body text="#000000" link="#0000ff" bgcolor="#ffffff">';
  $parser->parsefile($file);
  print "</body>\n";
}
