Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

fix bad HTML comments

by chipmunk (Parson)
on Nov 29, 2000 at 09:45 UTC ( #43852=sourcecode: print w/replies, xml ) Need Help??
Category: Web Stuff
Author/Contact Info
Description:

This Perl filter fixes bad HTML comments, such as <!----------- really ---------bad ------ comments ---------->. (Such comments are bad because, according to the spec, each -- -- pair within <! > delimits a comment. This means <!-- -- --> is not a complete comment, for example.)

The code reads in the entire file, then finds each occurrence of <!-- ... --> and uses tr/// to squash each run of hyphens to a single hyphen. The assignment to $x is necessary because $1 is read-only.

#!perl -p0

s/<!-(-.*?-)->/ (my $x = $1) =~ tr,-,,s; $x = '--' if $x eq '-'; "<!-$
+x->" /gse;

UPDATE: Fixed so it no longer turns <!----> into <!--->. Thanks to extremely for pointing that out.

UPDATE: Caveat: don't use this code on files where Perl code may be embedded in an HTML comment, as in this HTML::Mason example: <!-- <% $x-- %> -->. Thanks to extremely for pointing this out too.

Replies are listed 'Best First'.
Re: fix bad HTML comments
by extremely (Priest) on Nov 29, 2000 at 09:56 UTC
    Great so when I put a HTML::Mason tag in a comment for testing porpoises (yeah, fishing for errors) like this: <!-- <% $x-- %> --> you gonna blow it down? Oh yeah, what if they do <!---->?

    I shouldn't pick on you but you should really search about on this site a bit, super search on keywords qw( regex HTML fix ); and see what you get...

    --
    $you = new YOU;
    honk() if $you->love(perl)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://43852]
help
Chatterbox?
[Corion]: Oh yay. The (external, not guided by me) programmers have chosen Moose+DBIx::Class for some implementation, and now seem to do 1+n SELECT statements for each row, as is usual when using ORMs.
[Corion]: So maybe I should investigate how to plug in a cache in front of DBIx::Class so I can do a ->selectall_hashre f and then satisfy the "sub"-selects from that cached single SELECT statement ...
[erix]: then you might as well send that patch to the DBIC guys :)

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2017-09-25 11:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (279 votes). Check out past polls.

    Notices?