package AxKit::XSP::POD2XML;

$VERSION = "0.01";

# taglib stuff
use AxKit 1.4;
use Apache;
use Apache::AxKit::Language::XSP;
use Apache::File;
use XML::LibXML 1.31;
use Pod::XML;
use Time::Piece; # overrides localtime

use vars qw/@ISA $NS $VERSION/;
        

$NS = 'http://axkit.org/NS/xsp/pod2xml/v1';

@ISA = qw(Apache::AxKit::Language::XSP);

use strict;

my $base = "/var/www/pod/";
my $my_name = "";




## Parser subs
        
sub parse_char {
    my ($e, $text) = @_;
    $text =~ s/^\s*//;
    $text =~ s/\s*$//;

    return '' unless $text;

    $text =~ s/\|/\\\|/g;
    if ($e->current_element() =~ /^(name)$/) {
        $my_name = $text;
	warn "NAME VALUE IS: $text \n";
        return "\$_name = q|$text|;";
    }
    warn " NO NAME VALUE ...\n";
    return ''; # nothing else in util: should have text (?)
}

sub parse_start {
    my ($e, $tag, %attribs) = @_; 
#    warn "Checking: $tag\n";


    if ($tag eq 'podfile') {

        $my_name = "";
        my $code = "{# start podfile\nmy (\$_name);\n";
        if ($attribs{name}) {
	    warn "USING attribs for NAME: $attribs{name} \n";
            $code .= '$_name = q|' . $attribs{name} . '|;';
        } else {
	    warn "USING NAME PARAMETER ....\n";
	  $code .= "\$_name = q||\n";
	}
        return $code;
    }
    elsif ($tag eq 'name') {
        $my_name = "";
        return "# Start of name \n";
    }
    else {
        die "Unknown util tag: $tag";
    }
}

sub parse_end {
    my ($e, $tag) = @_;

    if ($tag eq 'podfile') {
      if ( $my_name ){
        return ";\nAxKit::XSP::POD2XML::podfile(\n" .
        "\$document, \$parent, qq|$my_name|" .
        ");}\n";
      } else {
        return ";\nAxKit::XSP::POD2XML::podfile(\n" .
        '$document, $parent, $_name' .
        ");}\n";
      }
    }
    elsif ($tag eq 'name') {
        return "\n# End of Name \n";
    }
    return ";";
}


sub podfile {
  warn "\n\n\n\n\n I am running podfile: @_\n";
  my ($document, $parent, $name) = @_;
  my $content = _get_podfile($name);
  my $doc = XML::LibXML->new()->parse_string($content);
  if ($doc) {
    my $root = $doc->getDocumentElement();
    $root = $document->importNode($root, 1);
    $parent->appendChild($root);
  }
   else {
     die "XML::LibXML returned undef";
  }
}

sub _get_podfile{

  my $name = shift;
  warn "\n Going for podfile: $name \n";
  #my $filename = $base.$name.".pod";
  my $filename = $base.$name;
  return undef unless -f $filename;
  my $parser = Pod::XML->new();
  $parser->{'send_to_string'} = 1;
  $parser->parse_from_file($filename);
  my $content = $parser->{'xml_string'};
  warn "CONTENT OF POD FILE is: $content \n\n\n";
  return $content;

}


sub _get_file_time{
  my $filename = shift;
  my $fh = Apache::File->new($filename) || 
  throw Apache::AxKit::Exception::IO( -text => "error opening $filename");
  my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
                        $atime,$mtime,$ctime,$blksize,$blocks)
			                          = stat($fh);
  $fh->close;
  return $mtime;
}


sub _get_file{
  my $filename = shift;
  my $fh = Apache::File->new($filename) || 
  throw Apache::AxKit::Exception::IO( -text => "error opening $filename");
  flock($fh, 1);
  my $content = "";
  my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
                        $atime,$mtime,$ctime,$blksize,$blocks)
			                          = stat($fh);
  {
    local $/;
    $content = <$fh>;
  }
  $fh->close;
  return ($content, scalar localtime $mtime);
}


1;

__END__

=head1 NAME

AxKit::XSP::POD2XML - Utilities for building XSP pages from POD Documentation


=head1 SYNOPSIS

AxAddXSPTaglib AxKit::XSP::POD2XML

=head1 DESCRIPTION

This Taglib is a basic formula for using XSP pages to include POD Documentation.
Basically, any documentation from .pl, .pm, or any text file will be stripped out, turned into DocBook style markup, and inserted at the point of the XSP tag used.

Lives at: http://www.piersharding.com/download/POD2XML.pm

=head2 How to use

Add this directive to your AxKit Config:

 AxAddXSPTaglib AxKit::XSP::POD2XML

Sample XSP Page:

 <?xml version="1.0"?>
 <?xml-stylesheet type="application/x-xsp" href="."?>
 <?xml-stylesheet type="application/x-xpathscript"
		href="/stylesheets/pod.xps"
		title="default"?>
 <xsp:page 
         xmlns:pod2xml="http://axkit.org/NS/xsp/pod2xml/v1"
         xmlns:util="http://apache.org/xsp/util/v1"
         xmlns:xsp="http://apache.org/xsp/core/v1"
         language="Perl">
 <page>
 <xsp:logic>
   my $query = $r->args();
   warn "\n\nQUERY STRING: $query\n\n";
   if ( $query =~ /^[\w\.]+$/ ){
 </xsp:logic>
     <pod2xml:podfile>
       <pod2xml:name><xsp:expr>$query</xsp:expr></pod2xml:name>
     </pod2xml:podfile>
 <xsp:logic>
   } else {
 </xsp:logic>
     <web:redirect uri='/'/>
 <xsp:logic>
   }
 </xsp:logic>
 </page>
 </xsp:page>

The filename for the POD source can be either determined by a child
element <name/> or attribute name='' on the <podfile/> element.
<pod2xml:podfile name='some.file'/>

Currently the base directory is hard coded as /var/www/pod 
(inline with the default Red Hat installation), and I just choose
to link file into this directory that I want to access.

=head1 AUTHOR

Piers Harding - piers@ompa.net

This relies heavily on Pod::XML - authored by Matt Sergeant

