Search code examples
xhtmlautocommenting

Commenting div closing automation


I'm looking for a solution to a rats nest of code I was handed - it's massive in volume, so I'm looking for suggestions to a programmatic approach to commenting what div closes where.

Example:

BEFORE

<div id="wrapper-item">
<div id="outer-item">
    <div class="inner-item">
<h1>Just Some Placekeeper Copy</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing.</p>
    </div>
</div>  
</div>

AFTER

<div id="wrapper-item">
<div id="outer-item">
    <div class="inner-item">
<h1>Just Some Placekeeper Copy</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing.</p>
    </div><!-- .inner-item -->
</div><!-- #outer-item -->      
</div><!-- #wrapper-item -->

I tried a few Regex attempts with no joy, I'd be curious of what the best approach is..


Solution

  • If it is valid xhtml, then you could just put it in an XML Document and then proceed to process the div tags by finding all of them, then adding a sibling that has the comment node that you want.