#!/usr/bin/perl
#
# $Id: fix-pod-xhtml,v 1.13 2003/04/15 22:43:18 cmdjb Exp $
#
# Format XHTML generated by pod2html (via tidy) for websites
#
# Usage: pod2html ... MODULE.pm | tidy ... | fix-pod-xhtml OUTPUT-FILE
#
# (C) Copyright 2001-2002 Dave Beckett <Dave.Beckett@bristol.ac.uk>
# University of Bristol
#

use strict;
use File::Basename;

my $progname=basename $0;

my $main_title="Redland RDF Application Framework";

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

my $doc_title;

my($file)=@ARGV;

my($pod_rdf_rel)=($file =~ m%^pod/RDF/%) ? '../' : '';
my($pod_rdf_redland_rel)=($file =~ m%^pod/RDF/Redland/%) ? '' : 'Redland/';

open(OUT, ">$file") or die "$progname: Cannot create $file - $!\n";
print OUT qq{<?xml version="1.0" encoding="iso-8859-1"?>\n};
open(IN, "-");
while(<IN>) {
  # Sorry, I'm transitional
  s%<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"%<?xml version="1.0" encoding="iso-8859-1"?>%;
  s%^.*"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">%<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">%;


  if(m%<title>(.*?)</title>%) {
    $doc_title=$1;
    $doc_title =~ s/^(.*) - Redland (.*) Class$/Perl $1 Class/;
    s%<title>(.*?)</title>%<title>$main_title - $doc_title</title>%;
  }

  next if /^\s*<link|meta/i;

  s'(id|name)="([^"]+)"'my($a,$v)=($1,$2); $v=~ s/%/_/g; qq{$a="$v"}'eg;

  s%<h1>%<h2>%; s%</h1>%</h2>%;
  s%<hr />%%;

  s%href="/docs/pod/RDF/Redland/([^"]+)"%href="$pod_rdf_redland_rel$1"%g;
  s%href="/docs/pod/RDF/([^"]+)"%href="$pod_rdf_rel$1"%g;

  s%^<body[^>]*>%<body bgcolor="#ffffff" text="#000085">\n\n<h1 align="center">$main_title - $doc_title</h1>\n\n%;

  s%^<p><a id="__index__".*</p>%%;

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

<p>Copyright 2000-$year <a href="http://purl.org/net/dajobe/">Dave Beckett</a>, <a href="http://www.ilrt.bristol.ac.uk/">Institute for Learning and Research Technology</a>, <a href="http://www.bristol.ac.uk/">University of Bristol</a></p>

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