Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

tmpldoc.pl - generate documentation for HTML::Template templates

by LTjake (Prior)
on May 18, 2004 at 17:44 UTC ( [id://354369]=sourcecode: print w/replies, xml ) Need Help??
Category: HTML Utility
Author/Contact Info /msg LTjake
Description:

I noitced the post "Can I automatically generate documentation from HTML::Template?" yesterday and thought that it was a neat idea. So, i whipped this script up.

Example Usage: tmpldoc.pl test.tmpl | pod2html > test.html

Update: jeffa said it should use HTML::Template -- DUH! :)

package HTML::Template::Documentation;

use base qw( HTML::Template );

use strict;
#use warnings;
#use diagnostics;

sub documentation {
    my $self     = shift;
    my $template = __PACKAGE__->new( filehandle => *main::DATA );

    $template->param( title => $self->{ options }->{ filepath } || 'HT
+ML::Template' );

    my $content  = { VAR => [], LOOP => [] };

    for my $param ( sort {
        $self->query( name => $b ) cmp $self->query( name => $a )
        || $a cmp $b
    } $self->param ) {
        my $type = $self->query( name => $param );

        push @{ $content->{ $type } }, +{ name => $param } if $type eq
+ 'VAR';
        $self->_query_loops( $content->{ LOOP }, $param, 2 ) if $type 
+eq 'LOOP';
    }

    $template->param( %$content );

    return $template->output;
}

sub _query_loops {
    my( $self, $loops, $name, $level, $chain ) = @_;

    my @path = $name;
    unshift @path, @$chain if $chain;

    push @{ $loops }, +{ name => $name, level => $level, VAR => [] };

    for my $param ( sort {
        $self->query( name => [ @path, $b ] ) cmp
        $self->query( name => [ @path, $a ] ) ||
        $a cmp $b
    } $self->query( loop => \@path ) ) {
        my $type = $self->query( name => [ @path, $param ] );

        push @{ $loops->[ $#{ $loops } ]->{ $type } }, { name => $para
+m } if $type eq 'VAR';
        $self->_query_loops( $loops, $param, $level + 1, \@path ) if $
+type eq 'LOOP';
    }
}

package main;

print HTML::Template::Documentation->new( filename => shift )->documen
+tation;

__DATA__
=head1 NAME

<!-- TMPL_VAR NAME="title" -->  - Template Documentation

=head1 DESCRIPTION

The following is auto-generated documention for the supplied HTML::Tem
+plate
data.

=head1 VARIABLES
<!-- TMPL_IF NAME="var" -->
=over 4
<!-- TMPL_LOOP NAME="var" -->
=item * <!-- TMPL_VAR NAME="name" -->
<!-- /TMPL_LOOP -->
=back
<!-- /TMPL_IF -->
=head1 LOOPS
<!-- TMPL_IF NAME="loop" -->
<!-- TMPL_LOOP NAME="loop" -->
=head<!-- TMPL_VAR NAME="level" --> <!-- TMPL_VAR NAME="name" -->
<!-- TMPL_IF NAME="var" -->
=over 4
<!-- TMPL_LOOP NAME="var" -->
=item * <!-- TMPL_VAR NAME="name" -->
<!-- /TMPL_LOOP -->
=back
<!-- /TMPL_IF -->
<!-- /TMPL_LOOP -->
<!-- /TMPL_IF -->

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://354369]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-18 17:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found