1 | package HTML::TokeParser::Simple;
|
---|
2 |
|
---|
3 | use strict;
|
---|
4 | use HTML::TokeParser;
|
---|
5 | use HTML::TokeParser::Simple::Token;
|
---|
6 | use HTML::TokeParser::Simple::Token::Tag;
|
---|
7 | use HTML::TokeParser::Simple::Token::Tag::Start;
|
---|
8 | use HTML::TokeParser::Simple::Token::Tag::End;
|
---|
9 | use HTML::TokeParser::Simple::Token::Text;
|
---|
10 | use HTML::TokeParser::Simple::Token::Comment;
|
---|
11 | use HTML::TokeParser::Simple::Token::Declaration;
|
---|
12 | use HTML::TokeParser::Simple::Token::ProcessInstruction;
|
---|
13 |
|
---|
14 | use vars qw/ @ISA $VERSION $REVISION /;
|
---|
15 |
|
---|
16 | $REVISION = '$Id: Simple.pm 13983 2007-03-15 01:32:44Z lh92 $';
|
---|
17 | $VERSION = '3.15';
|
---|
18 | @ISA = qw/ HTML::TokeParser /;
|
---|
19 |
|
---|
20 | # constructors
|
---|
21 |
|
---|
22 | my %FACTORY_CLASSES = (
|
---|
23 | S => 'HTML::TokeParser::Simple::Token::Tag::Start',
|
---|
24 | E => 'HTML::TokeParser::Simple::Token::Tag::End',
|
---|
25 | T => 'HTML::TokeParser::Simple::Token::Text',
|
---|
26 | C => 'HTML::TokeParser::Simple::Token::Comment',
|
---|
27 | D => 'HTML::TokeParser::Simple::Token::Declaration',
|
---|
28 | PI => 'HTML::TokeParser::Simple::Token::ProcessInstruction',
|
---|
29 | );
|
---|
30 |
|
---|
31 | sub _croak {
|
---|
32 | my ($proto, $message) = @_;
|
---|
33 | require Carp;
|
---|
34 | Carp::croak($message);
|
---|
35 | }
|
---|
36 |
|
---|
37 | sub new {
|
---|
38 | my ($class, @args) = @_;
|
---|
39 | return 1 == @args
|
---|
40 | ? $class->SUPER::new(@args)
|
---|
41 | : $class->_init(@args);
|
---|
42 | }
|
---|
43 |
|
---|
44 | sub _init {
|
---|
45 | my ($class, $source_type, $source) = @_;
|
---|
46 | my %sources = (
|
---|
47 | file => sub { $source },
|
---|
48 | handle => sub { $source },
|
---|
49 | string => sub { \$source },
|
---|
50 | url => sub {
|
---|
51 | eval "require LWP::Simple";
|
---|
52 | $class->_croak("Cannot load LWP::Simple: $@") if $@;
|
---|
53 | my $content = LWP::Simple::get($source);
|
---|
54 | $class->_croak("Could not fetch content from ($source)")
|
---|
55 | unless defined $content;
|
---|
56 | return \$content;
|
---|
57 | },
|
---|
58 | );
|
---|
59 | unless (exists $sources{$source_type}) {
|
---|
60 | $class->_croak("Unknown source type ($source_type)");
|
---|
61 | }
|
---|
62 | return $class->new($sources{$source_type}->());
|
---|
63 | }
|
---|
64 |
|
---|
65 | sub get_token {
|
---|
66 | my $self = shift;
|
---|
67 | my @args = @_;
|
---|
68 | my $token = $self->SUPER::get_token( @args );
|
---|
69 | return unless defined $token;
|
---|
70 | if (my $factory_class = $FACTORY_CLASSES{$token->[0]}) {
|
---|
71 | return $factory_class->new($token);
|
---|
72 | }
|
---|
73 | else {
|
---|
74 | # this should never happen
|
---|
75 | $self->_croak("Cannot determine token class for token (@$token)");
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | sub get_tag {
|
---|
80 | my $self = shift;
|
---|
81 | my @args = @_;
|
---|
82 | my $token = $self->SUPER::get_tag( @args );
|
---|
83 | return unless defined $token;
|
---|
84 | return $token->[0] =~ /^\//
|
---|
85 | ? HTML::TokeParser::Simple::Token::Tag::End->new($token)
|
---|
86 | : HTML::TokeParser::Simple::Token::Tag::Start->new($token);
|
---|
87 | }
|
---|
88 |
|
---|
89 | sub peek {
|
---|
90 | my ($self, $count) = @_;
|
---|
91 | $count ||= 1;
|
---|
92 |
|
---|
93 | unless ($count =~ /^\d+$/) {
|
---|
94 | $self->_croak("Argument to peek() must be a positive integer, not ($count)");
|
---|
95 | }
|
---|
96 |
|
---|
97 | my $items = 0;
|
---|
98 | my $html = '';
|
---|
99 | my @tokens;
|
---|
100 | while ( $items++ < $count && defined ( my $token = $self->get_token ) ) {
|
---|
101 | $html .= $token->as_is;
|
---|
102 | push @tokens, $token;
|
---|
103 | }
|
---|
104 | $self->unget_token(@tokens);
|
---|
105 | return $html;
|
---|
106 | }
|
---|
107 |
|
---|
108 | 1;
|
---|
109 |
|
---|
110 | __END__
|
---|
111 |
|
---|
112 | =head1 NAME
|
---|
113 |
|
---|
114 | HTML::TokeParser::Simple - Easy to use C<HTML::TokeParser> interface
|
---|
115 |
|
---|
116 | =head1 SYNOPSIS
|
---|
117 |
|
---|
118 | use HTML::TokeParser::Simple;
|
---|
119 | my $p = HTML::TokeParser::Simple->new( $somefile );
|
---|
120 |
|
---|
121 | while ( my $token = $p->get_token ) {
|
---|
122 | # This prints all text in an HTML doc (i.e., it strips the HTML)
|
---|
123 | next unless $token->is_text;
|
---|
124 | print $token->as_is;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | =head1 DESCRIPTION
|
---|
129 |
|
---|
130 | C<HTML::TokeParser> is an excellent module that's often used for parsing HTML.
|
---|
131 | However, the tokens returned are not exactly intuitive to parse:
|
---|
132 |
|
---|
133 | ["S", $tag, $attr, $attrseq, $text]
|
---|
134 | ["E", $tag, $text]
|
---|
135 | ["T", $text, $is_data]
|
---|
136 | ["C", $text]
|
---|
137 | ["D", $text]
|
---|
138 | ["PI", $token0, $text]
|
---|
139 |
|
---|
140 | To simplify this, C<HTML::TokeParser::Simple> allows the user ask more
|
---|
141 | intuitive (read: more self-documenting) questions about the tokens returned.
|
---|
142 |
|
---|
143 | You can also rebuild some tags on the fly. Frequently, the attributes
|
---|
144 | associated with start tags need to be altered, added to, or deleted. This
|
---|
145 | functionality is built in.
|
---|
146 |
|
---|
147 | Since this is a subclass of C<HTML::TokeParser>, all C<HTML::TokeParser>
|
---|
148 | methods are available. To truly appreciate the power of this module, please
|
---|
149 | read the documentation for C<HTML::TokeParser> and C<HTML::Parser>.
|
---|
150 |
|
---|
151 | =head1 CONTRUCTORS
|
---|
152 |
|
---|
153 | =head2 C<new($source)>
|
---|
154 |
|
---|
155 | The constructor for C<HTML::TokeParser::Simple> can be used just like
|
---|
156 | C<HTML::TokeParser>'s constructor:
|
---|
157 |
|
---|
158 | my $parser = HTML::TokeParser::Simple->new($filename);
|
---|
159 | # or
|
---|
160 | my $parser = HTML::TokeParser::Simple->new($filehandle);
|
---|
161 | # or
|
---|
162 | my $parser = HTML::TokeParser::Simple->new(\$html_string);
|
---|
163 |
|
---|
164 | =head2 C<new($source_type, $source)>
|
---|
165 |
|
---|
166 | If you wish to be more explicit, there is a new style of
|
---|
167 | constructor avaiable.
|
---|
168 |
|
---|
169 | my $parser = HTML::TokeParser::Simple->new(file => $filename);
|
---|
170 | # or
|
---|
171 | my $parser = HTML::TokeParser::Simple->new(handle => $filehandle);
|
---|
172 | # or
|
---|
173 | my $parser = HTML::TokeParser::Simple->new(string => $html_string);
|
---|
174 |
|
---|
175 | Note that you do not have to provide a reference for the string if using the
|
---|
176 | string constructor.
|
---|
177 |
|
---|
178 | As a convenience, you can also attempt to fetch the HTML directly from a URL.
|
---|
179 |
|
---|
180 | my $parser = HTML::TokeParser::Simple->new(url => 'http://some.url');
|
---|
181 |
|
---|
182 | This method relies on C<LWP::Simple>. If this module is not found or the page
|
---|
183 | cannot be fetched, the constructor will C<croak()>.
|
---|
184 |
|
---|
185 | =head1 PARSER METHODS
|
---|
186 |
|
---|
187 | =head2 get_token
|
---|
188 |
|
---|
189 | This method will return the next token that C<HTML::TokeParser::get_token()>
|
---|
190 | method would return. However, it will be blessed into a class appropriate
|
---|
191 | which represents the token type.
|
---|
192 |
|
---|
193 | =head2 get_tag
|
---|
194 |
|
---|
195 | This method will return the next token that C<HTML::TokeParser::get_tag()>
|
---|
196 | method would return. However, it will be blessed into either the
|
---|
197 | L<HTML::TokeParser::Simple::Token::Tag::Start> or
|
---|
198 | L<HTML::TokeParser::Simple::Token::Tag::End> class.
|
---|
199 |
|
---|
200 | =head2 peek
|
---|
201 |
|
---|
202 | As of version C<3.14>, you can now C<peek()> at the upcomings tokens without
|
---|
203 | affecting the state of the parser. By default, C<peek()> will return the text
|
---|
204 | of the next token, but specifying an integer C<$count> will return the text of
|
---|
205 | the next C<$count> tokens.
|
---|
206 |
|
---|
207 | This is useful when you're trying to debug where you are in a document.
|
---|
208 |
|
---|
209 | warn $parser->peek(3); # show the next 3 tokens
|
---|
210 |
|
---|
211 | =head1 ACCESSORS
|
---|
212 |
|
---|
213 | The following methods may be called on the token object which is returned,
|
---|
214 | not on the parser object.
|
---|
215 |
|
---|
216 | =head2 Boolean Accessors
|
---|
217 |
|
---|
218 | These accessors return true or false.
|
---|
219 |
|
---|
220 | =over 4
|
---|
221 |
|
---|
222 | =item * C<is_tag([$tag])>
|
---|
223 |
|
---|
224 | Use this to determine if you have any tag. An optional "tag type" may be
|
---|
225 | passed. This will allow you to match if it's a I<particular> tag. The
|
---|
226 | supplied tag is case-insensitive.
|
---|
227 |
|
---|
228 | if ( $token->is_tag ) { ... }
|
---|
229 |
|
---|
230 | Optionally, you may pass a regular expression as an argument.
|
---|
231 |
|
---|
232 | =item * C<is_start_tag([$tag])>
|
---|
233 |
|
---|
234 | Use this to determine if you have a start tag. An optional "tag type" may be
|
---|
235 | passed. This will allow you to match if it's a I<particular> start tag. The
|
---|
236 | supplied tag is case-insensitive.
|
---|
237 |
|
---|
238 | if ( $token->is_start_tag ) { ... }
|
---|
239 | if ( $token->is_start_tag( 'font' ) ) { ... }
|
---|
240 |
|
---|
241 | Optionally, you may pass a regular expression as an argument. To match all
|
---|
242 | header (h1, h2, ... h6) tags:
|
---|
243 |
|
---|
244 | if ( $token->is_start_tag( qr/^h[123456]$/ ) ) { ... }
|
---|
245 |
|
---|
246 | =item * C<is_end_tag([$tag])>
|
---|
247 |
|
---|
248 | Use this to determine if you have an end tag. An optional "tag type" may be
|
---|
249 | passed. This will allow you to match if it's a I<particular> end tag. The
|
---|
250 | supplied tag is case-insensitive.
|
---|
251 |
|
---|
252 | When testing for an end tag, the forward slash on the tag is optional.
|
---|
253 |
|
---|
254 | while ( $token = $p->get_token ) {
|
---|
255 | if ( $token->is_end_tag( 'form' ) ) { ... }
|
---|
256 | }
|
---|
257 |
|
---|
258 | Or:
|
---|
259 |
|
---|
260 | while ( $token = $p->get_token ) {
|
---|
261 | if ( $token->is_end_tag( '/form' ) ) { ... }
|
---|
262 | }
|
---|
263 |
|
---|
264 | Optionally, you may pass a regular expression as an argument.
|
---|
265 |
|
---|
266 | =item * C<is_text()>
|
---|
267 |
|
---|
268 | Use this to determine if you have text. Note that this is I<not> to be
|
---|
269 | confused with the C<return_text> (I<deprecated>) method described below!
|
---|
270 | C<is_text> will identify text that the user typically sees display in the Web
|
---|
271 | browser.
|
---|
272 |
|
---|
273 | =item * C<is_comment()>
|
---|
274 |
|
---|
275 | Are you still reading this? Nobody reads POD. Don't you know you're supposed
|
---|
276 | to go to CLPM, ask a question that's answered in the POD and get flamed? It's
|
---|
277 | a rite of passage.
|
---|
278 |
|
---|
279 | Really.
|
---|
280 |
|
---|
281 | C<is_comment> is used to identify comments. See the HTML::Parser documentation
|
---|
282 | for more information about comments. There's more than you might think.
|
---|
283 |
|
---|
284 | =item * C<is_declaration()>
|
---|
285 |
|
---|
286 | This will match the DTD at the top of your HTML. (You I<do> use DTD's, don't
|
---|
287 | you?)
|
---|
288 |
|
---|
289 | =item * C<is_process_instruction()>
|
---|
290 |
|
---|
291 | Process Instructions are from XML. This is very handy if you need to parse out
|
---|
292 | PHP and similar things with a parser.
|
---|
293 |
|
---|
294 | Currently, there appear to be some problems with process instructions. You can
|
---|
295 | override C<HTML::TokeParser::Simple::Token::ProcessInstruction> if you need to.
|
---|
296 |
|
---|
297 | =item * C<is_pi()>
|
---|
298 |
|
---|
299 | This is a shorthand for C<is_process_instruction()>.
|
---|
300 |
|
---|
301 | =back
|
---|
302 |
|
---|
303 | =head2 Data Accessors
|
---|
304 |
|
---|
305 | Some of these were originally C<return_> methods, but that name was not only
|
---|
306 | unwieldy, but also went against reasonable conventions. The C<get_> methods
|
---|
307 | listed below still have C<return_> methods available for backwards
|
---|
308 | compatibility reasons, but they merely call their C<get_> counterpart. For
|
---|
309 | example, calling C<return_tag()> actually calls C<get_tag()> internally.
|
---|
310 |
|
---|
311 | =over 4
|
---|
312 |
|
---|
313 | =item * C<get_tag()>
|
---|
314 |
|
---|
315 | Do you have a start tag or end tag? This will return the type (lower case).
|
---|
316 | Note that this is I<not> the same as the C<get_tag()> method on the actual
|
---|
317 | parser object.
|
---|
318 |
|
---|
319 | =item * C<get_attr([$attribute])>
|
---|
320 |
|
---|
321 | If you have a start tag, this will return a hash ref with the attribute names
|
---|
322 | as keys and the values as the values.
|
---|
323 |
|
---|
324 | If you pass in an attribute name, it will return the value for just that
|
---|
325 | attribute.
|
---|
326 |
|
---|
327 | Returns false if the token is not a start tag.
|
---|
328 |
|
---|
329 | =item * C<get_attrseq()>
|
---|
330 |
|
---|
331 | For a start tag, this is an array reference with the sequence of the
|
---|
332 | attributes, if any.
|
---|
333 |
|
---|
334 | Returns false if the token is not a start tag.
|
---|
335 |
|
---|
336 | =item * C<return_text()>
|
---|
337 |
|
---|
338 | This method has been heavily deprecated (for a couple of years) in favor of
|
---|
339 | C<as_is>. Programmers were getting confused over the difference between
|
---|
340 | C<is_text>, C<return_text>, and some parser methods such as
|
---|
341 | C<HTML::TokeParser::get_text> and friends.
|
---|
342 |
|
---|
343 | Using this method still succeeds, but will now carp and B<will be removed>
|
---|
344 | in the next major release of this module.
|
---|
345 |
|
---|
346 | =item * C<as_is()>
|
---|
347 |
|
---|
348 | This is the exact text of whatever the token is representing.
|
---|
349 |
|
---|
350 | =item * C<get_token0()>
|
---|
351 |
|
---|
352 | For processing instructions, this will return the token found immediately after
|
---|
353 | the opening tag. Example: For <?php, "php" will be the start of the returned
|
---|
354 | string.
|
---|
355 |
|
---|
356 | Note that process instruction handling appears to be incomplete in
|
---|
357 | C<HTML::TokeParser>.
|
---|
358 |
|
---|
359 | Returns false if the token is not a process instruction.
|
---|
360 |
|
---|
361 | =back
|
---|
362 |
|
---|
363 | =head1 MUTATORS
|
---|
364 |
|
---|
365 | The C<delete_attr()> and C<set_attr()> methods allow the programmer to rewrite
|
---|
366 | start tag attributes on the fly. It should be noted that bad HTML will be
|
---|
367 | "corrected" by this. Specifically, the new tag will have all attributes
|
---|
368 | lower-cased with the values properly quoted.
|
---|
369 |
|
---|
370 | Self-closing tags (e.g. E<lt>hr /E<gt>) are also handled correctly. Some older
|
---|
371 | browsers require a space prior to the final slash in a self-closed tag. If
|
---|
372 | such a space is detected in the original HTML, it will be preserved.
|
---|
373 |
|
---|
374 | Calling a mutator on an token type that does not support that property is a
|
---|
375 | no-op. For example:
|
---|
376 |
|
---|
377 | if ($token->is_comment) {
|
---|
378 | $token->set_attr(foo => 'bar'); # does nothing
|
---|
379 | }
|
---|
380 |
|
---|
381 | =over 4
|
---|
382 |
|
---|
383 | =item * C<delete_attr($name)>
|
---|
384 |
|
---|
385 | This method attempts to delete the attribute specified. It will silently fail
|
---|
386 | if called on anything other than a start tag. The argument is
|
---|
387 | case-insensitive, but must otherwise be an exact match of the attribute you are
|
---|
388 | attempting to delete. If the attribute is not found, the method will return
|
---|
389 | without changing the tag.
|
---|
390 |
|
---|
391 | # <body bgcolor="#FFFFFF">
|
---|
392 | $token->delete_attr('bgcolor');
|
---|
393 | print $token->as_is;
|
---|
394 | # <body>
|
---|
395 |
|
---|
396 | After this method is called, if successful, the C<as_is()>, C<get_attr()>
|
---|
397 | and C<get_attrseq()> methods will all return updated results.
|
---|
398 |
|
---|
399 | =item * C<set_attr($name,$value)>
|
---|
400 |
|
---|
401 | This method will set the value of an attribute. If the attribute is not found,
|
---|
402 | then C<get_attrseq()> will have the new attribute listed at the end.
|
---|
403 |
|
---|
404 | # <p>
|
---|
405 | $token->set_attr(class => 'some_class');
|
---|
406 | print $token->as_is;
|
---|
407 | # <p class="some_class">
|
---|
408 |
|
---|
409 | # <body bgcolor="#FFFFFF">
|
---|
410 | $token->set_attr('bgcolor','red');
|
---|
411 | print $token->as_is;
|
---|
412 | # <body bgcolor="red">
|
---|
413 |
|
---|
414 | After this method is called, if successful, the C<as_is()>, C<get_attr()>
|
---|
415 | and C<get_attrseq()> methods will all return updated results.
|
---|
416 |
|
---|
417 | =item * C<set_attr($hashref)>
|
---|
418 |
|
---|
419 | Under the premise that C<set_> methods should accept what their corresponding
|
---|
420 | C<get_> methods emit, the following works:
|
---|
421 |
|
---|
422 | $tag->set_attr($tag->get_attr);
|
---|
423 |
|
---|
424 | Theoretically that's a no-op and for purposes of rendering HTML, it should be.
|
---|
425 | However, internally this calls C<$tag-E<gt>rewrite_tag>, so see that method to
|
---|
426 | understand how this may affect you.
|
---|
427 |
|
---|
428 | Of course, this is useless if you want to actually change the attributes, so you
|
---|
429 | can do this:
|
---|
430 |
|
---|
431 | my $attrs = {
|
---|
432 | class => 'headline',
|
---|
433 | valign => 'top'
|
---|
434 | };
|
---|
435 | $token->set_attr($attrs)
|
---|
436 | if $token->is_start_tag('td') && $token->get_attr('class') eq 'stories';
|
---|
437 |
|
---|
438 | =item * C<rewrite_tag()>
|
---|
439 |
|
---|
440 | This method rewrites the tag. The tag name and the name of all attributes will
|
---|
441 | be lower-cased. Values that are not quoted with double quotes will be. This
|
---|
442 | may be called on both start or end tags. Note that both C<set_attr()> and
|
---|
443 | C<delete_attr()> call this method prior to returning.
|
---|
444 |
|
---|
445 | If called on a token that is not a tag, it simply returns. Regardless of how
|
---|
446 | it is called, it returns the token.
|
---|
447 |
|
---|
448 | # <body alink=#0000ff BGCOLOR=#ffffff class='none'>
|
---|
449 | $token->rewrite_tag;
|
---|
450 | print $token->as_is;
|
---|
451 | # <body alink="#0000ff" bgcolor="#ffffff" class="none">
|
---|
452 |
|
---|
453 | A quick cleanup of sloppy HTML is now the following:
|
---|
454 |
|
---|
455 | my $parser = HTML::TokeParser::Simple->new( string => $ugly_html );
|
---|
456 | while (my $token = $parser->get_token) {
|
---|
457 | $token->rewrite_tag;
|
---|
458 | print $token->as_is;
|
---|
459 | }
|
---|
460 |
|
---|
461 | =back
|
---|
462 |
|
---|
463 | =head1 PARSER VERSUS TOKENS
|
---|
464 |
|
---|
465 | The parser returns tokens that are blessed into appropriate classes. Some
|
---|
466 | people get confused and try to call parser methods on tokens and token methods
|
---|
467 | on the parser. To prevent this, C<HTML::TokeParser::Simple> versions 1.4 and
|
---|
468 | above now bless all tokens into appropriate token classes. Please keep this in
|
---|
469 | mind while using this module (and many thanks to PodMaster
|
---|
470 | L<http://www.perlmonks.org/index.pl?node_id=107642> for pointing out this issue
|
---|
471 | to me.)
|
---|
472 |
|
---|
473 | =head1 EXAMPLES
|
---|
474 |
|
---|
475 | =head2 Finding comments
|
---|
476 |
|
---|
477 | For some strange reason, your Pointy-Haired Boss (PHB) is convinced that the
|
---|
478 | graphics department is making fun of him by embedding rude things about him in
|
---|
479 | HTML comments. You need to get all HTML comments from the HTML.
|
---|
480 |
|
---|
481 | use strict;
|
---|
482 | use HTML::TokeParser::Simple;
|
---|
483 |
|
---|
484 | my @html_docs = glob( "*.html" );
|
---|
485 |
|
---|
486 | open PHB, "> phbreport.txt" or die "Cannot open phbreport for writing: $!";
|
---|
487 |
|
---|
488 | foreach my $doc ( @html_docs ) {
|
---|
489 | print "Processing $doc\n";
|
---|
490 | my $p = HTML::TokeParser::Simple->new( file => $doc );
|
---|
491 | while ( my $token = $p->get_token ) {
|
---|
492 | next unless $token->is_comment;
|
---|
493 | print PHB $token->as_is, "\n";
|
---|
494 | }
|
---|
495 | }
|
---|
496 |
|
---|
497 | close PHB;
|
---|
498 |
|
---|
499 | =head2 Stripping Comments
|
---|
500 |
|
---|
501 | Uh oh. Turns out that your PHB was right for a change. Many of the comments
|
---|
502 | in the HTML weren't very polite. Since your entire graphics department was
|
---|
503 | just fired, it falls on you need to strip those comments from the HTML.
|
---|
504 |
|
---|
505 | use strict;
|
---|
506 | use HTML::TokeParser::Simple;
|
---|
507 |
|
---|
508 | my $new_folder = 'no_comment/';
|
---|
509 | my @html_docs = glob( "*.html" );
|
---|
510 |
|
---|
511 | foreach my $doc ( @html_docs ) {
|
---|
512 | print "Processing $doc\n";
|
---|
513 | my $new_file = "$new_folder$doc";
|
---|
514 |
|
---|
515 | open PHB, "> $new_file" or die "Cannot open $new_file for writing: $!";
|
---|
516 |
|
---|
517 | my $p = HTML::TokeParser::Simple->new( $file => doc );
|
---|
518 | while ( my $token = $p->get_token ) {
|
---|
519 | next if $token->is_comment;
|
---|
520 | print PHB $token->as_is;
|
---|
521 | }
|
---|
522 | close PHB;
|
---|
523 | }
|
---|
524 |
|
---|
525 | =head2 Changing form tags
|
---|
526 |
|
---|
527 | Your company was foo.com and now is bar.com. Unfortunately, whoever wrote your
|
---|
528 | HTML decided to hardcode "http://www.foo.com/" into the C<action> attribute of
|
---|
529 | the form tags. You need to change it to "http://www.bar.com/".
|
---|
530 |
|
---|
531 | use strict;
|
---|
532 | use HTML::TokeParser::Simple;
|
---|
533 |
|
---|
534 | my $new_folder = 'new_html/';
|
---|
535 | my @html_docs = glob( "*.html" );
|
---|
536 |
|
---|
537 | foreach my $doc ( @html_docs ) {
|
---|
538 | print "Processing $doc\n";
|
---|
539 | my $new_file = "$new_folder$doc";
|
---|
540 |
|
---|
541 | open FILE, "> $new_file" or die "Cannot open $new_file for writing: $!";
|
---|
542 |
|
---|
543 | my $p = HTML::TokeParser::Simple->new( file => $doc );
|
---|
544 | while ( my $token = $p->get_token ) {
|
---|
545 | if ( $token->is_start_tag('form') ) {
|
---|
546 | my $action = $token->get_attr(action);
|
---|
547 | $action =~ s/www\.foo\.com/www.bar.com/;
|
---|
548 | $token->set_attr('action', $action);
|
---|
549 | }
|
---|
550 | print FILE $token->as_is;
|
---|
551 | }
|
---|
552 | close FILE;
|
---|
553 | }
|
---|
554 |
|
---|
555 | =head1 CAVEATS
|
---|
556 |
|
---|
557 | For compatability reasons with C<HTML::TokeParser>, methods that return
|
---|
558 | references are violating encapsulation and altering the references directly
|
---|
559 | B<will> alter the state of the object. Subsequent calls to C<rewrite_tag()>
|
---|
560 | can thus have unexpected results. Do not alter these references directly
|
---|
561 | unless you are following behavior described in these docs. In the future,
|
---|
562 | certain methods such as C<get_attr>, C<get_attrseq> and others may return a
|
---|
563 | copy of the reference rather than the original reference. This behavior has
|
---|
564 | not yet been changed in order to maintain compatability with previous versions
|
---|
565 | of this module. At the present time, your author is not aware of anyone taking
|
---|
566 | advantage of this "feature," but it's better to be safe than sorry.
|
---|
567 |
|
---|
568 | Use of C<$HTML::Parser::VERSION> which is less than 3.25 may result in
|
---|
569 | incorrect behavior as older versions do not always handle XHTML correctly. It
|
---|
570 | is the programmer's responsibility to verify that the behavior of this code
|
---|
571 | matches the programmer's needs.
|
---|
572 |
|
---|
573 | Note that C<HTML::Parser> processes text in 512 byte chunks. This sometimes
|
---|
574 | will cause strange behavior and cause text to be broken into more than one
|
---|
575 | token. You can suppress this behavior with the following command:
|
---|
576 |
|
---|
577 | $p->unbroken_text( [$bool] );
|
---|
578 |
|
---|
579 | See the C<HTML::Parser> documentation and
|
---|
580 | http://www.perlmonks.org/index.pl?node_id=230667 for more information.
|
---|
581 |
|
---|
582 | =head1 BUGS
|
---|
583 |
|
---|
584 | There are no known bugs, but that's no guarantee.
|
---|
585 |
|
---|
586 | Address bug reports and comments to: E<lt>[email protected]<gt>. When
|
---|
587 | sending bug reports, please provide the version of C<HTML::Parser>,
|
---|
588 | C<HTML::TokeParser>, C<HTML::TokeParser::Simple>, the version of Perl, and the
|
---|
589 | version of the operating system you are using.
|
---|
590 |
|
---|
591 | Reverse the name to email the author.
|
---|
592 |
|
---|
593 | =head1 SUBCLASSING
|
---|
594 |
|
---|
595 | You may wish to change the behavior of this module. You probably do not want
|
---|
596 | to subclass C<HTML::TokeParser::Simple>. Instead, you'll want to subclass one
|
---|
597 | of the token classes. C<HTML::TokeParser::Simple::Token> is the base class for
|
---|
598 | all tokens. Global behavioral changes should go there. Otherwise, see the
|
---|
599 | appropriate token class for the behavior you wish to alter.
|
---|
600 |
|
---|
601 | =head1 SEE ALSO
|
---|
602 |
|
---|
603 | L<HTML::TokeParser::Simple::Token>
|
---|
604 |
|
---|
605 | L<HTML::TokeParser::Simple::Token::Tag>
|
---|
606 |
|
---|
607 | L<HTML::TokeParser::Simple::Token::Text>
|
---|
608 |
|
---|
609 | L<HTML::TokeParser::Simple::Token::Comment>
|
---|
610 |
|
---|
611 | L<HTML::TokeParser::Simple::Token::Declaration>
|
---|
612 |
|
---|
613 | L<HTML::TokeParser::Simple::Token::ProcessInstruction>
|
---|
614 |
|
---|
615 | =head1 COPYRIGHT
|
---|
616 |
|
---|
617 | Copyright (c) 2004 by Curtis "Ovid" Poe. All rights reserved. This program is
|
---|
618 | free software; you may redistribute it and/or modify it under the same terms as
|
---|
619 | Perl itself
|
---|
620 |
|
---|
621 | =head1 AUTHOR
|
---|
622 |
|
---|
623 | Curtis "Ovid" Poe E<lt>[email protected]<gt>
|
---|
624 |
|
---|
625 | Reverse the name to email the author.
|
---|
626 |
|
---|
627 | =cut
|
---|