wml-extended-apis / xhtml / 1.x / std / toc / toc.wml.tt2

## 
##  wml::xhtml1::std::toc - Table of Conents Generator
##  Copyright (c) 2005 Shlomi Fish
##  Distributed under the MIT X11 License
##  http://www.opensource.org/licenses/mit-license.php

<:{
use HTML::TokeParser::Simple;
use vars qw($wml_xhtml_std_toc_section);
$wml_xhtml_std_toc_section = { 'subs' => [] };
}:>
[% SET inner_refs = "" %]
[% FOREACH index IN [2,3,4,5,6] %]
<define-tag h[% index %] whitespace="delete" endtag="required">
<preserve id />
<h[% index %]*<attributes-quote %attributes /> >%body</h[% index %]*>
<set-var %attributes />
<:{

my $id = <<'EOCMMFINVBOWVNIANFINLNGINWNOF';
<get-var id />
EOCMMFINVBOWVNIANFINLNGINWNOF

my $tag_body = <<'EOCMMFINVBOWVNIANFINLNGINWNOF';
%body
EOCMMFINVBOWVNIANFINLNGINWNOF

chomp($id);
chomp($tag_body);
my $parser = HTML::TokeParser::Simple->new(\$tag_body);
my $my_body;
while ($token = $parser->get_token())
{
    if ($token->is_start_tag("a") || $token->is_end_tag("a"))
    {
        # Do nothing - we throw away links.
    }
    else
    {
        $my_body .= $token->as_is();
    }
}

my @prev_sects = ($wml_xhtml_std_toc_section);
foreach my $idx (3 .. [% index %])
{
    if (! exists($prev_sects[-1]->{'subs'}))
    {
        use Data::Dumper;
        print {*STDERR} Dumper(\@prev_sects);
        die "Cannot find a subs in the last prev_sects!";
    }
    push @prev_sects, $prev_sects[-1]->{'subs'}->[-1];
}
push @{$prev_sects[-1]->{subs}}, { 'id' => $id, 'body' => $my_body, 'subs' => [],};
}:>
<restore id />
</define-tag>
[% SET inner_refs = inner_refs _ "->{'subs'}->[-1]" %]
[% END %]

<define-tag toc>
{#WML_XSTD_TOC_ToC#}
</define-tag>

<define-tag toc_finale>
<:{
sub myfunc
{
my $elem = shift;
return ("<li>", "<a href=\"#" . $elem->{id} . "\">" . $elem->{body} . "</a>",
        (@{$_->{subs}} ? 
            ("<br />", "<ul>", 
                (map { myfunc($_)
                    }
                @{$_->{subs}}),
                "</ul>",
            ) :
            ()
        ),
        "</li>");
}
END 
{
my $text;
$text = join ("\n", 
    "<ul>",
    (map { myfunc($_) } @{$wml_xhtml_std_toc_section->{subs}}),
    "</ul>",
    );
print "{#WML_XSTD_TOC_ToC#:$text:##}";
}
}:>
</define-tag>

<toc_finale />
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.