Sunday, June 2, 2013

HTML blank insertion program

Hi all,

I was concerned about Vim's weird indentation of HTML, and Tim Chase
clued me in about Tidy, which does most of what I want but leaves no
blank lines. I want 3 blank lines above each <h1>, and one blank line
above each of <h[[:digit:]]>, <ol>, <ul>, <table>, <p>, <body>, and
<div>.

However, to make <h1> stand out even more, I don't want any blank lines
immediately below an <h1>, even if the line below it is one that would
normally have a blank line above it. Also, I want to be able to run
this filter over and over again on generations of the file without
building up huge numbers of blank lines. The following Awk program does
all this:

#!/usr/bin/gawk -We
BEGIN {suppressnextblank=0}

{printblank = 0}
tolower($0) ~ /^<h1/ {printblank=1
suppressnextblank=3
print "\n"
}
tolower($0) ~ /^<h[[:digit:]]/ {printblank=1}
tolower($0) ~ /^<ul/ {printblank=1}
tolower($0) ~ /^<ol/ {printblank=1}
tolower($0) ~ /^<table/ {printblank=1}
tolower($0) ~ /^<p/ {printblank=1}
tolower($0) ~ /^<body/ {printblank=1}
tolower($0) ~ /^<div/ {printblank=1}
printblank == 1 {suppressnextblank--}
printblank == 1 && suppressnextblank != 1 {print ""}
$0 !~ /^\s*$/ {print $0}

Thanks for everyone's help,

SteveT

Steve Litt * http://www.troubleshooters.com/
Troubleshooting Training * Human Performance

--
--
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/groups/opt_out.

No comments: