in reply to Wrapping HTML "sections" with a div

What did you try?

If you don't want specialized modules , you can split on empty lines using $/ and regex-match a <h2> at the start.

You should know how to do it.

update

Nope, the example input was misleading.

My suggestion is: use an HTML module.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re^2: Wrapping HTML "sections" with a div
by nysus (Parson) on Oct 25, 2018 at 18:04 UTC

    I got this far before deciding I should see if I'm re-inventing the wheel:

    sub wrap { my $tree = HTML::TreeBuilder->new_from_content(shift); foreach my $h2 ( $tree->find_by_tag_name('h2') ) { my $parent = HTML::Element->new('div', 'data-role' => 'collapsible +'); $h2->replace_with($parent); $parent->push_content($h2); } return $tree->as_HTML; }

    The above only wraps the <h2> tag, not its associated content. The HTML is generated from markdown using Markdent so I think the proper thing to do is figure out how that works. Seems like it can do the job.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks