|
Smart quotes with Perl
|
Social links
Class::Prototype
WWW::Spyder Javascript tricks serial() join function Smart quotes Text to Excel Developing Featherweight Web Services with JavaScript
Miscellaneous
|
|
| Smart quotes with Perl
|
Introduction You want your HTML to be typographically correct (notes here). But typing ‘this’ constantly to get ‘this’ instead of "this" is ludicrous. Perl to the rescue. Sample text file, named “plain-quote.html” Which looks so in your browser… Apply our smart quoter to it G4:jinx[786]/www/perl>quote-fixer plain-quote.html And we get back Which will now render in a browser… Here is the code to do it Discussion This is a great utility but it a simplistic one. Still, an algorithm that works for 99% of given cases is a good one. This one will fail in certain places because it’s not looking for delimited quotes. Therefore it is not smart enough to know what to do with open contractions like ‘cause and ‘Burque and it picks the wrong quote. It also fails when we want the original symbols, as in, “I am 6'2" and change.” You can see in the code that we need to keep track of what tags we’ve descended into. If we fix quotes within an HTML tag, we’ll break it. Fixing them in a style declaration will break the CSS. If we fix quotes in comments, we’ll break SSIs. If we fix quotes in scripts, we’ll break them. If we fix quotes in pre, code, or textarea tags, we’ll change the literal meaning of demonstrated code. So we skip the attributes of tags altogether and avoid messing with the content of pre|code|script|textarea|style tags. A fully robust solution might make use of recursive regexes or Text::Balanced but it would be “breakable” by bad user input (incorrect punctuation) and the one above is much simpler and works quite well as is. |
|
|
Perl Books ·
CPAN ·
mod_perl ·
Perl Monks ·
Perl Mongers ·
Perl Journal ·
Use Perl ·
Perl Jobs ·
ActiveState ·
perldoc.perl.org ·
O’Reilly Perl ·
W3Schools tutorials ·
Ovid's CGI Course ·
Catalyst ·
Perl at Wikipedia
Text, original code, fonts, and graphics ©1990-2009 Ashley Pond V. |
||