Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restructuring Existing HTML Files

Tags:

html

regex

vi

There has been times where I need to "convert" an old HTML structure to a new one. To illustrate, converting this:

<!-- Old HTML structure. -->
<div class="class-a">
  <div class="class-b">
    <span>Hello</span>
  </div>
  <div class="class-c">
    <p>How are you doing?</p>
  </div>
</div>
<div class="class-a">
  <div class="class-b">    
  ... (and so on, basically repeats the construct above) ...

To something like:

<!-- New HTML structure. -->
<div class="class-aa">
  <span class="class-ab">Hello</span>
  <p>How are you doing?</p>
</div>
<div class="class-aa">
  <span class="class-ab">Hi!</span>
  ... (and so on, basically repeats the construct above) ...

Note that I still need the text, but the structure needs a complete overhaul.

The worst situation is the HTML file is super long and doing it manually (yep, a lot of typing). What I've been doing so far is either using a simple substitution regex (if not too complex) or resorting to the old school vi macro.

Would somebody please suggest a better approach to accomplish this?

like image 567
moey Avatar asked Nov 29 '25 22:11

moey


1 Answers

You could write a JavaScript/jQuery that does the restructuring and then use the DOM inspector/debugger of the browser get a copy of the modified HTML.

BTW, a while ago I was looking for a editor/tool that does this kind of thing automatically, but there doesn't seem to be such a thing: https://softwareengineering.stackexchange.com/questions/79615/html-text-editor-with-dom-manipulation

like image 123
RoToRa Avatar answered Dec 01 '25 12:12

RoToRa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!