nysus has asked for the wisdom of the Perl Monks concerning the following question:
Let's say I have the following html that is an outline using header tags:
<h1>blah</h1> <p>blah<p> <h2>blah</h2> <p>blah</p> <h3>blah</h3> <p>blah</p> <h2>blah blah blah</h2> <p>blah</p> <h1>blah</h2> <h2>blah</h2> <h2>blah</h2>
I want to convert that to this, wrapping each h2 with a div:
In other words, the algorithm must search for all h2 tags, then find the next sibling tag that is not an h1 tag or EOF, and wrap that section in a div tag. HTML::Element and HTML::TreeBuilder seem like the right tools for the job but this problem seems so common I'm wondering if there is something that works out of the box for dealing with HTML that is set up like an outline.<h1>blah</h1> <p>blah<p> <div> <h2>blah</h2> </div> <div> <p>blah</p> <h3>blah</h3> <p>blah</p> </div> <div> <h2>blah blah blah</h2> <p>blah</p> </div> <h1>blah</h1> <div> <h2>blah</h2> </div> <div> <h2>blah</h2> </div>
$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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Wrapping HTML "sections" with a div
by haukex (Archbishop) on Oct 25, 2018 at 21:21 UTC | |
|
Re: Wrapping HTML "sections" with a div
by marto (Cardinal) on Oct 25, 2018 at 18:19 UTC | |
|
Re: Wrapping HTML "sections" with a div
by choroba (Cardinal) on Oct 26, 2018 at 11:15 UTC | |
|
Re: Wrapping HTML "sections" with a div
by tangent (Parson) on Oct 26, 2018 at 15:38 UTC | |
|
Re: Wrapping HTML "sections" with a div
by LanX (Saint) on Oct 25, 2018 at 17:56 UTC | |
by nysus (Parson) on Oct 25, 2018 at 18:04 UTC |