Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6198375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:59:10+00:00 2026-05-24T03:59:10+00:00

I’d written a Perl script to generate a table of contents from HTML pages

  • 0

I’d written a Perl script to generate a table of contents from HTML pages which is working fine (and generating valid HTML) except for that the Perl output is removing closing tags for some elements like p. This is not validating against DocType of strict.

Please scroll down the post to see the Perl code.

What should I do to correct it?

#!/usr/bin/perl -w
#Copyright anurag gupta ; free to use under GNU GPL License

use strict;
use feature "switch";

use Common;

use HTML::Element;

use HTML::TreeBuilder;
#"F:/anurag/work/indiacustomercare/airtel/recharge.html";
my $filename="F:/tmp/t9.html";

my $index=0;
my $labelprefix="anu555ltg-";

my $tocIndex=100001;

my $toc;

my @stack;

my $prevHtag="h2";

sub hTagEncountered($)
{
    my $hTag=shift;

    my $currLevel=(split //, $hTag)[1];

    given($hTag)
    {
        when(/h1/)
        {
           break; 
        }
        default{
            my $countCurr= (split /h/,$hTag)[1];
            my $countPrev= (split /h/,$prevHtag)[1];



            if($countCurr>$countPrev)
            {
                push @stack,($currLevel);
                $toc.="<ul>";
            }
            elsif($countCurr<$countPrev)
            {
                # Now check in the stack

                while ( @stack and $currLevel < $stack[$#stack])
                {
                    pop @stack;
                    $toc.="</ul>";
                }
            }
        }

    }

    $prevHtag=$hTag;
}

sub getLabel
{
my $name=$labelprefix.++$tocIndex;
}

sub traversehtml
{
    my $node=$_[0];
   # $node->dump();
   # print "-----------------\n";
   # print $node->tag()."\n";

  #  print ref($node),"->\n";

    if((ref(\$node) ne "SCALAR" )and ($node->tag() =~m/^h[2-7]$/i))  #it's an H Element!
    {

        my @h = $node->content_list();

        if(@h==1 and ref(\$h[0]) eq "SCALAR")  #H1 contains simple string and nothing else
        {
                    hTagEncountered($node->tag());

                    my $label=getLabel();

                    my $a = HTML::Element->new('a', name => $label);

                    my $text=$node->as_trimmed_text();

                    $a->push_content($text);

                    $node->delete_content();

                    $text=HTML::Entities::encode_entities($text);

                    $node->push_content($a);
                    $toc.=<<EOF;
                    <li><a href="#$label">$text</a>
EOF
        }
        elsif (  @h==1 and ($h[0]->tag() eq "a"))   # <h1><a href="abc.com">ttt</a></h1> case
            {
                #See if any previous label already exists

                my $prevlabel = $h[0]->attr("name");


                $h[0]->attr("name",undef) if(defined($prevlabel) and $prevlabel=~m/$labelprefix/); #delete previous name tag if any

                #set the new label
                my $label=getLabel();

                $h[0]->attr("name",$label);

                hTagEncountered($node->tag());
                my $text=HTML::Entities::encode_entities($node->as_trimmed_text());
                $toc.=<<EOF;
                <li><a href="#$label">$text</a>
EOF

            }
        elsif (@h>1)  #<h1>some text here<a href="abc.com">ttt</a></h1> case
        {
           die "h1 must not contain any html elements";

        }

    }

    my @h = $node->content_list();

    foreach my $item (@h)
    {

       if(ref(\$item) ne "SCALAR")  {traversehtml($item); } #skip scalar items
    }


}

   die "File $filename not found" if !-r $filename;

    my $tree = HTML::TreeBuilder->new();

    $tree->parse_file($filename);


    my @h = $tree->content_list();

    traversehtml($h[1]);

    while(pop @stack)
    {
        $toc.="</ul>";
    }

    $toc="<ul>$toc</ul>";

    print qq{<div id="icctoc"><h2>TOC</h2>$toc</div>};

    my @list1=$tree->content_list();

    my @list2=$list1[1]->content_list();

for(my $i=0;$i<@list2;++$i){
    if(ref(\$list2[$i]) eq "SCALAR")
       {
        print $list2[$i]
       }
    else{
    print $list2[$i]->as_HTML();
    }


    }
        # Finally:
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:59:11+00:00Added an answer on May 24, 2026 at 3:59 am

    Try passing {} for the \%optional_end_tags argument to as_HTML. See the documentation for details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.