Had you provided valid input and output matching the algorithm, I could have given you an xsh answer, something like
open :F html input.html ; for my $h2 in //h2 { my $h1 = $h2/following-sibling::h1[1] ; my $chunk ; if $h1 { $chunk = $h2/following-sibling::text()[following-sibling::h1[1 +]=$h1][preceding-sibling::h2[1]=$h2] | $h2/following-sibling::*[not(self::h2)][following-sib +ling::h1[1]=$h1][preceding-sibling::h2[1]=$h2] ; } else { $chunk = $h2/following-sibling::text()[preceding-sibling::h2[1 +]=$h2] | $h2/following-sibling::*[not(self::h2)][preceding-sib +ling::h2[1]=$h2] ; } if $chunk { my $div := insert element div after $h2 ; xmove $chunk into $div ; } } my $divs := wrap div //h2 ; save :F html :f output.html ;
It gives the following output, which is different to the expected one, but I'm not sure all the differences are wrong:
<h1>blah</h1> <p>blah</p> <p> </p> <div><h2>blah</h2></div> <div> <p>blah</p> <h3>blah</h3> <p>blah</p> </div> <div><h2>blah blah blah</h2></div> <div> <p>blah</p> </div> <h1>blah</h1> <div><h2>blah</h2></div> <div> </div> <div><h2>blah</h2></div> </body></html>
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

In reply to Re: Wrapping HTML "sections" with a div by choroba
in thread Wrapping HTML "sections" with a div by nysus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.