📄 ch20_16.htm
字号:
<HTML><HEAD><TITLE>Recipe 20.15. Program: hrefsub (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:46:04Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch20_01.htm"TITLE="20. Web Automation"><LINKREL="prev"HREF="ch20_15.htm"TITLE="20.14. Program: htmlsub"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="ALT="Search this book"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch20_15.htm"TITLE="20.14. Program: htmlsub"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 20.14. Program: htmlsub"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch20_01.htm"TITLE="20. Web Automation"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><a href="index/index.htm"><img border="0" src="../gifs/txtnexta.gif"></a></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch20-chap20_program_1">20.15. Program: hrefsub</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="ch20-idx-1000002703-0"></A><ACLASS="indexterm"NAME="ch20-idx-1000002703-1"></A>hrefsub makes substitutions in HTML files, so that the changes only apply to the text in HREF fields of <<CODECLASS="literal">A</CODE> <CODECLASS="literal">HREF="..."</CODE>> tags. For instance, if you had the scooby.html file from the previous example, and you've moved shergold.html to be cards.html, you need simply say:</P><PRECLASS="programlisting">% hrefsub shergold.html cards.html scooby.html<CODECLASS="userinput"><B><CODECLASS="replaceable"><I><HTML><HEAD><TITLE>Hi!</TITLE></HEAD><BODY></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I><H1>Welcome to Scooby World!</H1></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>I have <A HREF="pictures.html">pictures</A> of the crazy dog</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>himself. Here's one!<P></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I><IMG SRC="scooby.jpg" ALT="Good doggy!"><P></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I><BLINK>He's my hero!</BLINK> I would like to meet him some day,</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>and get my picture taken with him.<P></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>P.S. I am deathly ill. <a href="cards.html">Please send</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>cards</A>.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I></BODY></HTML></I></CODE></B></CODE></PRE><PCLASS="para">The HTML::Filter manual page has a BUGS section that says:</P><BLOCKQUOTECLASS="blockquote"><PCLASS="para">Comments in declarations are removed from the declarations and then inserted as separate comments after the declaration. If you turn on <CODECLASS="literal">strict_comment()</CODE>, then comments with embedded <CODECLASS="literal">"-\|-"</CODE> are split into multiple comments.</P></BLOCKQUOTE><PCLASS="para">This version of hrefsub will always lowercase the <CODECLASS="literal"><a></CODE> and the attribute names within this tag when substitution occurs. If <CODECLASS="literal">$foo</CODE> is a multiword string, then the text given to <CODECLASS="literal">MyFilter->text</CODE> may be broken such that these words do not come together; i.e., the substitution does not work. There should probably be a new option to HTML::Parser to make it not return text until the whole segment has been seen. Also, some people may not be happy with having their 8-bit Latin-1 characters replaced by ugly entities, so htmlsub does that,<CODECLASS="literal"></CODE><ACLASS="indexterm"NAME="ch20-idx-1000004280-0"></A><ACLASS="indexterm"NAME="ch20-idx-1000004280-1"></A><ACLASS="indexterm"NAME="ch20-idx-1000004280-2"></A><ACLASS="indexterm"NAME="ch20-idx-1000004280-3"></A><ACLASS="indexterm"NAME="ch20-idx-1000004280-4"></A> too.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch20-pgfId-1000004374">Example 20.12: hrefsub</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# hrefsub - make substitutions in <A HREF="..."> fields of HTML files# from Gisle Aas <gisle@aas.no>sub usage { die "Usage: $0 <from> <to> <file>...\n" }my $from = shift or usage;my $to = shift or usage;usage unless @ARGV;# The HTML::Filter subclass to do the substitution.package MyFilter;require HTML::Filter;@ISA=qw(HTML::Filter);use HTML::Entities qw(encode_entities);sub start { my($self, $tag, $attr, $attrseq, $orig) = @_; if ($tag eq 'a' && exists $attr->{href}) { if ($attr->{href} =~ s/\Q$from/$to/g) { # must reconstruct the start tag based on $tag and $attr. # wish we instead were told the extent of the 'href' value # in $orig. my $tmp = "<$tag"; for (@$attrseq) { my $encoded = encode_entities($attr->{$_}); $tmp .= qq( $_="$encoded "); } $tmp .= ">"; $self->output($tmp); return; } } $self->output($orig);}# Now use the class.package main;foreach (@ARGV) { MyFilter->new->parse_file($_);}</PRE></DIV><PCLASS="para"></P></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch20_15.htm"TITLE="20.14. Program: htmlsub"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 20.14. Program: htmlsub"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><a href="index/index.htm"><img border="0" src="../gifs/txtnexta.gif"></a></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">20.14. Program: htmlsub</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">Index</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -