Sunday, June 22, 2014

use vim to properly formal HTML source code

I want to be able to use Vim to properly format HTML source code. Often when working with WYSIWYG HTML editors, automatically formatted HTML code will have unnecessary space, empty HTML tags, and way too many newlines. I want to use Vim to be able to quickly format this sloppy HTML code into something that's much easier to see. The rules for this code are as follows:

For the purposes of this discussion, let "normal text" mean text that is not enclosed in HTML tags and is not a whitespace character, an dlet "indentation mark" mean two spaces.

* Any whitespace immediately following any HTML tag should be removed.
* Any whitespace characters or newlines between an HTML tag and normal text should be removed.

Wrong:
<td class="ralign">
Monday, May 30</td>

Correct:
<td class="ralign">Monday, May 30</td>

* All opening <table>, <th>, <tbody>, and <thead> tags should be followed by a newline and one more indentation_mark than what the previous line was.

Wrong:
<tbody><tr><td style

Correct:
<tbody>
<tr>
<td style

* All closing <table>, <th>, <tbody>, and <thead> tags should be immediately followed by a newline and one less number of indentations than the number of indentations on the previous line.

Wrong:
</td></tr> </tbody> </table>

Correct:
</td>
</tr>
</tbody>
</table>

* Remove all newlines between opening and closing <a> and <span> tags.

Wrong:
<a href="http://www.google.com">
Do you Google?</a>

Correct:
<a href="http://www.google.com">Do you Google?</a>


Is any of this possible in Vim? If not, can someone recommend another utility to accomplish this?

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: