[lug] Vim maps / vimrc files for html editing

Zan Lynx zlynx at acm.org
Mon Feb 19 18:15:43 MST 2007


On Mon, 2007-02-19 at 17:08 -0700, David L. Anselmi wrote:
> Zan Lynx wrote:
> [...]
> > I use perl -i for these things.  I have a pretty complicated script that
> > rewrites the HTML and Javascript for Baen e-books I download.
> 
> What does the script do?  I just ran across the Baen library yesterday. 
>   (Synchronicity.)

It sets everything to use Javascript1.2, which changes the way some
reload and location= actions work.

It writes in link relationships for next, prev, etc, and prefetch for
Mozilla/Firefox.  Prefetch is great when you are reading it off your
home webserver.

It removes any nocache pragmas.  I also have my web server set to give
them a 10 year Expires.

It fixes a setfont Javascript to happen at the beginning of the body
tag.  Otherwise the font doesn't set until loading completes, so it
loads in a small font then flashes big.  That annoys me.

For everyone's enjoyment:

#!/usr/bin/perl -w
use strict;

my $setfontjs = qq(
<script language="JavaScript1.2"><!--
setStyle();
//--></script>
);

my ($back, $next, $toc) = ();
my $saved = '';
while( <> ) {
	if( /a href="([^"]*)">Back/o ) { $back = $1; }
	if( /a href="([^"]*)">Next/o ) { $next = $1; }
	if( /a href="([^"]*)">Contents/o ) { $toc = $1; }
	$saved .= $_;
} continue {
	if( eof ) {
		if( $back && $next && $toc ) {
			my $rels = join("\n", 
				qq(<link rel="prev" href="$back">),
				qq(<link rel="previous" href="$back">),
				qq(<link rel="next" href="$next">),
				qq(<link rel="prefetch" href="$next">),
				qq(<link rel="toc" href="$toc">),
				qq(<link rel="index" href="$toc">),
			);
			$saved =~ s#<link rel="[^>]+>\n?##sgo;
			$saved =~ s#</title>#</title>\n$rels#so;
		}
		$saved =~ s#<meta\s+http-equiv="pragma"\s+content="no-cache"\s*/>##sgo;
		$saved =~ s#language="JavaScript"#language="JavaScript1.2"#sgo;

		if( $saved =~ m#setStyle# ) {
			$saved =~ s#^<body[^>]*>(?!\s*<script)#$&$setfontjs#mgo;
		}

		print $saved;
		#print "$ARGV\t$back\t$next\t$toc\n";
		close ARGV;
		($back, $next, $toc) = ();
		$saved = '';
	}
}

-- 
Zan Lynx <zlynx at acm.org>




More information about the LUG mailing list