package Natura;

require 5.005_62;
use strict;
use warnings;

use XML::DT ;

require Exporter;

our @ISA = qw(Exporter);

# This allows declaration  use Natura ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
   
) ] );

our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

our @EXPORT = qw(
   
);
our $VERSION = '0.01';


sub n {
   my $t = shift;
   for($t) {
      s/\s/+/g;
      tr{áéíóúàèìòùãõâêîôûçÁÉÍÓÚÀÈÌÒÙÃÕÂÊÎÔÛÇ}
        {aeiouaeiouaoaeioucaeiouaeiouaoaeiouc};
   }
   return lc($t);
}

sub mkaxml{ 
  my($f,$name)=@_;
  $f =~ s/(.*)\/.*/$1/;
  "$f/".n($name).".xml"
}


sub template {
  my $file = shift;
  my %value = @_;

  my %handler=(
          '-inputenc' => 'ISO-8859-1',
          '-outputenc' => 'ISO-8859-1',
          '-default'   => sub{ toxml },
          '_field_'    => sub{
             if (defined($value{$v{name}})) {
               $value{$v{name}}
             } else {
               ""
             }},
         );
  return dt($file,%handler);
}


sub small_entry {
  my $file = shift;
  return pathdtstring($file, (
         '-outputenc' => 'ISO-8859-1',
         '-inputenc'  => 'ISO-8859-1',
         '-default'   => sub { "" },
         'entry'      => sub { 
            $c =~ m!^(\s|\n)*(<a.*</a>)!;
            quadro($2,$') },
         'entry/entryref' => sub { 
              "<a href=\"/natura/project/" . n($c) . "\">$c</a><br>" },
         'entry/description' => sub { "<small>$c</small>" },
         'parts' => sub {
                               "<ul>$c</ul>\n" }, 
         'parts/entryref' => sub {
                               "<li>$c</li>\n" },
         ));
}

sub asLaTeX {
  my $file = shift;
  my $bs = '\\verb!\\!';
  return pathdtstring($file, (
           '-outputenc' => 'ISO-8859-1',
           '-pcdata' => sub { $c =~ s/\\/$bs/g; $c },
           '-inputenc'  => 'ISO-8859-1',
           '-default'   => sub { "" },
           'entry'      => sub { $c },
           'entry/entryref' => sub { "\\textbf{$c}" },
           'entry/description' => sub { "\\begin{quote}\n$c\n\\end{quote}" },
           ));
}


sub cat2html {
  my $filename = shift;

  my $path="/";
  my $email;
  my %handler=(
       '-inputenc' => 'ISO-8859-1',
       '-outputenc' => 'ISO-8859-1',
       '-default'   => sub{"<div style='background: #ff0000'>$q:$c</div>"},

       'file/description' => sub{"<small>$c</small>"},
       'demo/description' => sub{"<small>$c</small>"},
       'description' => sub{"<br><b>Description:</b> $c"},
       'catalog/name' => sub{"<h1>$c</h1>"},
       'rating' => sub{""},
       'cvsdir' => sub{""},
       'releases' => sub{"<div><b>Releases:</b><ul>$c</ul></div>"},
       'documentation' => sub{"<div><b>Documentation:</b><ul>$c</ul></div>"},
       'version' => sub{$c},
       'file' => sub{"<li><b>Download:</b>$c"},
       'format' => sub{"<small><tt>[$c]</tt></small>"},

       'person/name' => sub{"<i>$c</i>"},
       'person/email' => sub{$email=$c;""},
       'person' => sub{
          my $x = "<a href='mailto:$email'>$c</a>";
          $email = "";
          return $x;
       },

       'role' => sub{""},
       'author' => sub{"<br><b>Author:</b> $c"},
       'demos' => sub{"$c"},
       'demo' => sub{"<br><b>Demonstração:</b> $c"},
       'url' => sub{"<a href='$c'>$c</a>"},
       'entry/name' => sub{
          if ($c =~ s/^\s*(.+?)\s*$/$1/) {
            $path.="$c/";
          } else {
            $path.="unknown/";
          }
          ""
       },
       'entryref' => sub{
          "<h4><a href='http://natura.di.uminho.pt/cgi-bin/cvsweb.cgi$path'>$c</a></h4>"},
       'catalog' => sub{"<html>$c</html>"},
       'entry' => sub{
         if( $path =~ m{^(.*/).+?$}) { $path = $1;}
         "<div style='border: solid thin #000099'><blockquote>$c</blockquote></div>"},
       'parts' => sub{
             ($c)?"<div style='border: solid thin #0000ff'>$c</div>":""},
       'relations' => sub{"<br>Relations:<ul>$c</ul>"},
       'rel' => sub{"<li><i>$v{type}:</i>$c</li>"},
       'doc' => sub{"<a href='/NATURA$path$c'>$v{title}</a><br>"},
       'tgz' => sub{"<a href='/NATURA$path$c'>$c</a><br>"},
      );
 return pathdtstring($filename,%handler);
}

sub quadro {
   my ($titulo,$texto) = (shift,shift);
   my $width = shift || "80%";
        return <<EOT;
  <b>$titulo</b><br/>$texto<br/>
EOT
}


# Preloaded methods go here.

1;
__END__

=head1 NAME

Natura - Perl extension convertion Nat catalogues to html,tex

=head1 SYNOPSIS

  use Natura;

=head1 DESCRIPTION


=head2 EXPORT

=head1 AUTHOR

=head1 SEE ALSO

=cut
