#!/usr/bin/perl
#
# Format XHTML generated by rdoc (via tidy) for websites
#
# Usage: rdoc ....  
#        for f in `find outdir -name \*.html -print`; 
#           tidy ... $f | fix-rdoc-xhtml $f.new && mv $f.new $f;
#        donek OUTPUT-FILE
#
# Copyright (C) 2001-2005 David Beckett - http://www.dajobe.org/
# Copyright (C) 2001-2005 University of Bristol - http://www.bristol.ac.uk/
#

use strict;
use File::Basename;

my $progname=basename $0;

my $main_title="Redland RDF Application Framework - Ruby RDoc";

die "USAGE: $progname OUTPUT-FILE\n" if @ARGV < 1;

my($file)=@ARGV;

my $is_index = ($file =~ /(fr_file_index|fr_class_index|fr_method_index)/);

open(OUT, ">$file") or die "$progname: Cannot create $file - $!\n";
open(IN, "-");
while(<IN>) {

  if(m%<title>(.*?)</title>%) {
    s%<title>(.*?)</title>%<title>$main_title - $1</title>%;
  }

  if(m%<body>%) {
    print OUT;
    print OUT <<"EOT" if !$is_index;
<div class="outerBlock">

<div class="outerHeader">
Go to <a href="/" target="_parent">Redland Home</a> - 
<a href="/bindings/" target="_parent">Language Bindings Home</a> - 
<a href="/docs/ruby.html" target="_parent">Ruby API Home</a>
</div>

EOT
   next;
  }
 
  next if /^<meta/i;

  if(m%<link%) {
#    $_=qq{<link rel="stylesheet" href="/docs/rdoc/rdoc-style.css" type="text/css" media="screen" />\n};
  }

  if(m%"validator-badges"%) {
    while(<IN>) {
      last if m%</div>%;
    }
    next;
  }

  my $year=1900+(localtime)[5];
  if(m%^</body>%) {
    print OUT <<"EOT";
<hr />

EOT

    print OUT <<"EOT" if !$is_index;
<div class="outerHeader">
Go to <a href="/" target="_parent">Redland Home</a> - 
<a href="/bindings/" target="_parent">Language Bindings Home</a> - 
<a href="/docs/ruby.html" target="_parent">Ruby API Home</a>
</div>

EOT

    print OUT <<"EOT";
<p>(C) Copyright 2004-$year <a href="http://www.dajobe.org/" target="_parent">Dave Beckett</a>, (C) Copyright 2004-2005 <a href="http://www.bristol.ac.uk/" target="_parent">University of Bristol</a></p>

</div> <!-- end outerBlock -->

EOT
  }

  print OUT;
}
close(IN);
close(OUT);
