rwburden has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a program to apply a Dreamweaver template to a directory tree of HTML files, among other transformations. In order to make it work, I must find the HTML comments in the template that mark the beginning and end of editable sections. I've been using the HTML package to parse the template file and the files to which the template must be applied, then to generate a new, templatized HTML file. I've had no difficulty finding specific tags with specific attributes, and removing or replacing them, except where I must find a comment tag. HTML::TreeBuilder has given me a tree of the template, but HTML::Element->look_down("_tag", "~comment") returns an empty list (or undef. in scalar context), as does HTML::Element->look_down("_tag", qr/comment/) whereas HTML::Element->look_down("_tag", "body"), HTML::Element->look_down("_tag", "title"), etc. works as expected:
open (TEMPLATE, $templatePath) || die "can't open $templatePath: $!"; my $template_tree = HTML::TreeBuilder->new(); $template_tree->parse_file (\*TEMPLATE); my $first_comment = $template_tree->look_down("_tag", "~comment"); if (not defined $first_comment) { warn "No comment found in template\n"; } prompt>perl -w test.pl No comment found in template prompt>

Replies are listed 'Best First'.
Re: finding comments with HTML::Element
by GrandFather (Saint) on Nov 14, 2006 at 07:26 UTC

    Try setting store_comments before you parse the file:

    my $template_tree = HTML::TreeBuilder->new(); $template_tree->->store_comments(1); $template_tree->parse_file (\*TEMPLATE);

    Note: the HTML::TreeBuilder documentation says:

    $root->store_comments(value)
    This determines whether TreeBuilder will normally store comments found while parsing content into $root. Currently, this is off by default.

    DWIM is Perl's answer to Gödel