The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

XML::Filter::XML_Directory_2XHTML - SAX2 filter for munging XML::Directory::SAX output into XHTML

SYNOPSIS

 use strict;

 package MySAX;
 use base qw (XML::SAX::Base);

 use Image::Magick;

 sub parse_uri {
     my $self = shift;
     my $uri  = shift;

     my $magick = Image::Magick->new();

     if (my $err = $magick->Read($uri)) {
        carp $err;
        return 0;
     }

     if (my $comment = $magick->Get("comment")) {
       $self->SUPER::start_element({Name=>"p"});
       $self->SUPER::characters({Data=>$comment});
       $self->SUPER::end_element({Name=>"p"});
     }

     return 1;
 }

 package main;

 use IO::File;
 use XML::SAX::Writer;

 use XML::Directory::SAX;
 use XML::Filter::XML_Directory_2XHTML;

 my $file   = IO::File->new(">/htdocs/myimages/index.html");
 my $writer = XML::SAX::Writer->new(Output=>$file);
 my $filter = XML::Filter::XML_Directory_2XHTML->new(Handler=>$writer);

 # As Canadian as possible, under the circumstances
 $filter->set_lang("en-ca");

 # Define some images to associate with directory listing.

 $filter->set_images({
                      # Some defaults
                      directory => {src=>"/icons/dir.gif",height=>20,width=>20},
                      file      => {src=>"/icons/unknown.gif",height=>20,width=>20},

                      # An image for a file whose media type
                      # as defined by MIME::Types is 'image'.
                      # This is the case for .pl and .pm files
                      image => {src=>"/icons/image3.gif",height=>20,width=>20},
                    });

 # This package inherits from XML::Filter::XML_Directory_2::Base
 # which defines a framework for defining event based callbacks
 # and handlers.

 $filter->set_callbacks({
                         link     => sub { return "file://".$_[0];  },

                         # This is not the greatest example because
                         # this is actually what the linktext is set
                         # to if no 'linktext' callback or handler is
                         # defined but you get the idea.
                         linktext => sub { return &basename($_[0]); },

                         title    => sub { return "woot woot woot"; },
                        });

 $filter->set_handlers({
                        file => MySAX::File->new(Handler=>$writer),
                       });

 # In turn, XML::Filter::XML_Directory_2::Base inherits from 
 # XML::Filter::XML_Directory_Pruner which provides hooks for 
 # restricting the output of XML::Directory::SAX

 $filter->exclude(ending=>[".html"]);

 my $directory = XML::Directory::SAX->new(depth=>0,detail=>2,Handler=>$filter);

 $directory->order_by("a");
 $directory->parse_dir("/htdocs/myimages");

DESCRIPTION

SAX2 filter for munging XML::Directory::SAX output into XHTML.

EVENTS

This package allows for the following event callbacks and/or handlers to be registered:

Since this functionaliy is inherited from XML::Filter::XML_Directory_2::Base, please consult the documentation for that package for details.

Handler events

  • linktext

    Modify the linktext for the current document. The default value is the filename itself.

  • file

    Define additional output to follow the name of the current file.

  • directory

    Define additional output to follow the name of the current directory.

Callback events

  • link

    Modify the value of the HTML a@href attribute for the current document. The default value is the absolute path of the document itself.

  • linktext

    Modify the linktext for the current document. The default value is the filename itself.

  • title

    Set the value of the HTML <title> element for your document. The default is the absolute path of the directory you are parsing.

  • file

    Define additional output to follow the name of the current file.

  • directory

    Define additional output to follow the name of the current directory.

CSS AND HTML

Each directory and file in the XML::Directory output is wrapped in HTML <div> elements. Each element is assigned a class attributes whose name matches the type of file, either a file or directory.

The default CSS styles for those classes are :

 .file {
         border:1px dotted #ccc;
         margin-left:10px;
         margin-bottom:5px;
         margin-top:5px;
         padding-right:50px;
       }

 .directory {
        border:1px dotted #666;
        margin-left:10px;
        margin-bottom:10px;
        }

They can be altered by passing a user-defined CSS stylesheet via the filter's set_styles object method.

OBJECT METHODS

$pkg = XML::Filter::XML_Directory_2XHTML->new()

Object constructor. Returns an object. Woot!

$pkg->set_lang($lang)

Set the language code to be assigned to the <html@xml:lang> and <html@lang> attributes.

$pkg->set_images(\%args)

Define image files to be included with a file or a directory.

Valid arguments are a hash ref whose key may be :

  • directory

  • file

  • string - the value returned by the MIME::Types::mediaType function for a document.

Each key defines it own hash reference whose keys are :

  • src

    String. Required.

  • height

    Int. Required.

  • width

    Int. Required.

  • alt

    String.

$pkg->set_styles(\@styles)

Define additional stylesheets for your document.

Valid arguments are an array reference of hash reference. Each hash ref may contain the following keys:

  • href

    String. Required.

  • rel

    String. Default is "stylesheet"

  • media

    String. Default is "all"

  • title

$pkg->set_scripts(\@scripts)

Define scripts for your document.

Valid arguments are an array reference of hash reference. Each hash ref may contain the following keys:

  • src

    String. Required.

$pkg->set_handlers(\%args)

Please consults the docs for XML::Filter::XML_Directory_2::Base for details

$pkg->set_callbacks(\%args)

Please consults the docs for XML::Filter::XML_Directory_2::Base for details

VERSION

1.1

DATE

July 03, 2002

AUTHOR

Aaron Straup Cope

TO DO

  • Add hooks to set <meta> tags

  • Add hooks to set <link> tags

SEE ALSO

XML::Filter::XML_Directory_2::Base

XML::Directory::SAX

LICENSE

Copyright (c) 2002, Aaron Straup Cope. All Rights Reserved.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 191:

You forgot a '=back' before '=head1'