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; }