オープンソース・ソフトウェアの開発とダウンロード

CVS リポジトリの参照

Contents of /perldocjp/docs/modules/HTML-TokeParser-Simple-2.1/Simple.pod

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download)
Sat Jun 21 06:52:53 2003 UTC (20 years, 10 months ago) by ktats
Branch: MAIN
initial version

1 =head1 NAME
2
3 HTML::TokeParser::Simple - HTML::TokeParser のインターフェースを簡単に使う
4
5 =head1 SYNOPSIS
6
7 use HTML::TokeParser::Simple;
8 my $p = HTML::TokeParser::Simple->new( $somefile );
9
10 while ( my $token = $p->get_token ) {
11 # This prints all text in an HTML doc (i.e., it strips the HTML)
12 next unless $token->is_text;
13 print $token->as_is;
14 }
15
16
17 =head1 DESCRIPTION
18
19 C<HTML::TokeParser> is a fairly common method of parsing HTML. However, the
20 tokens returned are not exactly intuitive to parse:
21
22 C<HTML::TokeParser>は、HTMLを解析するのに、適切な、一般的なメソッドです。
23 ですが、返って来るトークンは、あまり直観的には解析できません:
24
25 ["S", $tag, $attr, $attrseq, $text]
26 ["E", $tag, $text]
27 ["T", $text, $is_data]
28 ["C", $text]
29 ["D", $text]
30 ["PI", $token0, $text]
31
32 To simplify this, C<HTML::TokeParser::Simple> allows the user ask more
33 intuitive (read: more self-documenting) questions about the tokens returned.
34 Specifically, there are 7 C<is_foo> type methods and 5 C<return_bar> type
35 methods. The C<is_> methods allow you to determine the token type and the
36 C<return_> methods get the data that you need.
37
38 これを簡単にするために、C<HTML::TokeParser::Simple>では、
39 返って来るトークンについて、ユーザがより直観的な(より自己ドキュメントな)質問を尋ねることができるようにします。
40 具体的には、7つのC<is_foo>タイプのメソッドと 5つのC<return_bar>タイプのメソッドがあります。
41 C<is_>メソッドはトークンのタイプを判断できるようにして、C<return_>メソッドは、必要とするデータを得られるようにします。
42
43 You can also rebuild some tags on the fly. Frequently, the attributes
44 associated with start tags need to be altered, added to, or deleted. This
45 functionality is built in.
46
47 オンザフライにいくつかのタグを再構築することもできます。
48 よくあるのは、開始タグに関連した属性が変えられるか、加えられるか、
49 削除されるといったことをしなければなりません。この機能性は組み込まれています。
50
51 Since this is a subclass of C<HTML::TokeParser>, all C<HTML::TokeParser>
52 methods are available. To truly appreciate the power of this module, please
53 read the documentation for C<HTML::TokeParser> and C<HTML::Parser>.
54
55 HTML::TokeParser::Simpleは、C<HTML::TokeParser>のサブクラスであり、
56 すべてのC<HTML::TokeParser>メソッドが利用できます。
57 このモジュールの真価をわかるには、C<HTML::TokeParser>と、C<HTML::Parser>の
58 ドキュメントを読んでください。
59
60 The following will be brief descriptions of the available methods followed by
61 examples.
62
63 以下では、利用できるメソッドを、例とともに簡潔に説明します。
64
65 =head1 C<is_> メソッド
66
67 =over 4
68
69 =item * C<is_start_tag([$tag])>
70
71 Use this to determine if you have a start tag. An optional "tag type" may be
72 passed. This will allow you to match if it's a I<particular> start tag. The
73 supplied tag is case-insensitive.
74
75 開始タグがあるかどうかを判断するのに使ってください。
76 オプションとして、"タグタイプ"を渡すこともできます。
77 I<特定の>開始タグかどうかにマッチさせることができます。
78 渡されるタグは大文字と小文字を区別しません。
79
80 if ( $token->is_start_tag( 'font' ) ) { ... }
81
82 Optionally, you may pass a regular expression as an argument. To match all
83 header (h1, h2, ... h6) tags:
84
85 オプションとして、正規表現を引数に渡せます。
86 全てのヘッダタグ(h1, h2 ... h6)にマッチさせるためには、以下のようにします:
87
88 if ( $token->is_start_tag( qr/^h[123456]$/ ) ) { ... }
89
90 =item * C<is_end_tag([$tag])>
91
92 Use this to determine if you have an end tag. An optional "tag type" may be
93 passed. This will allow you to match if it's a I<particular> end tag. The
94 supplied tag is case-insensitive.
95
96 終了タグがあるかどうかを判断するのに使います。
97 オプションとして、"タグタイプ"を渡すことが出来ます。
98 I<特定の>終了タグかどうかにマッチさせることができます。
99 渡されるタグは大文字と小文字を区別しません。
100
101 When testing for an end tag, the forward slash on the tag is optional.
102
103 終了タグのテストをするときには、タグの前のスラッシュはオプションです。
104
105 while ( $token = $p->get_token ) {
106 if ( $token->is_end_tag( 'form' ) ) { ... }
107 }
108
109 または:
110
111 while ( $token = $p->get_token ) {
112 if ( $token->is_end_tag( '/form' ) ) { ... }
113 }
114
115 Optionally, you may pass a regular expression as an argument.
116
117 オプションとして、正規表現を引数に渡せます。
118
119 =item * C<is_tag([$tag])>
120
121 Use this to determine if you have any tag. An optional "tag type" may be
122 passed. This will allow you to match if it's a I<particular> tag. The
123 supplied tag is case-insensitive.
124
125 どんなタグを持っているか判断するのに使います。
126 オプションとして、"タグタイプ"を渡せます。
127 I<特定の>タグかどうかにマッチさせることができます。
128 渡されるタグは大文字と小文字を区別しません。
129
130 if ( $token->is_tag ) { ... }
131
132 Optionally, you may pass a regular expression as an argument.
133
134 オプションとして、正規表現を引数に渡せます。
135
136 =item * C<is_text()>
137
138 Use this to determine if you have text. Note that this is I<not> to be
139 confused with the C<return_text> (I<deprecated>) method described below!
140 C<is_text> will identify text that the user typically sees display in the Web
141 browser.
142
143 テキストを持っているかどうかを判断するのに使います。
144 下で説明するC<return_text>(I<軽視されている>)メソッドと混乱させるためにあるのではI<ありません>!
145 C<is_text>は、ユーザが典型的に見ているWebブラウザで表示されるテキストと一致するでしょう。
146
147 =item * C<is_comment()>
148
149 Are you still reading this? Nobody reads POD. Don't you know you're supposed
150 to go to CLPM, ask a question that's answered in the POD and get flamed? It's
151 a rite of passage.
152
153 まだこれを読んでいます? 誰もPODを読みません。
154 CLPMにに行くとすると、PODの中で答えられる質問を尋ねて、
155 フレイムになるのを知らない? それは通過儀礼です。
156
157 Really.
158
159 本当です。
160
161 C<is_comment> is used to identify comments. See the HTML::Parser documentation
162 for more information about comments. There's more than you might think.
163
164 C<is_comment>は、コメントに一致させるのに使います。
165 コメントについて、より詳しい情報は、HTML::Parser のドキュメントを見てください。
166 あなたが思っている以上のことがあります。
167
168 =item * C<is_declaration()>
169
170 This will match the DTD at the top of your HTML. (You I<do> use DTD's, don't
171 you?)
172
173 HTMLのトップにあるDTDにマッチします。(DTDをI<使ってますか>、使ってないでしょ?)
174
175 =item * C<is_process_instruction()>
176
177 Process Instructions are from XML. This is very handy if you need to parse out
178 PHP and similar things with a parser.
179
180 プロセスインストラクションはXML出身です。
181 PHPと類似のものをパーサで解析しなければならないなら、とても手軽です。
182
183 =back
184
185 =head1 The C<return_> メソッド
186
187 =head2 注意:
188
189 In case it's not blindingly obvious (I've been bitten by this myself when
190 writing the tests), you should generally test what type of token you have
191 B<before> you call some C<return_> methods. For example, if you have an end
192 tag, there is no point in calling the C<return_attrseq> method. Calling an
193 innapropriate method will return an empty string.
194
195 万一に備えて、めがくらむようにはっきりとしていません(自分自身、テストを書くときに、これを理解しています)。
196 C<return_>メソッドを呼ぶB<前に>、何のタイプのトークンを持っているかをテストすべきです。
197 たとえば、終了タグがあれば、C<retrun_attrseq>メソッドの呼び出しに何の意義もありません。
198 不適当なメソッドの呼び出しのために、空の文字列が返されるでしょう。
199
200 As noted for the C<is_> methods, these methods are case-insensitive after the
201 C<return_> part.
202
203 C<is_>メソッドのために注意としては、これらのメソッドはC<return_>部分の後は、大文字小文字を区別しません。
204
205 =over 4
206
207 =item * C<return_tag()>
208
209 Do you have a start tag or end tag? This will return the type (lower case).
210
211 開始タグか終了タグがありますか? そのタイプを返します(小文字)。
212
213 =item * C<return_attr()>
214
215 If you have a start tag, this will return a hash ref with the attribute names
216 as keys and the values as the values.
217
218 開始タグがあれば、キーに属性の名前、値に属性の値の、ハッシュリファレンスを返します。
219
220 =item * C<return_attrseq()>
221
222 For a start tag, this is an array reference with the sequence of the
223 attributes, if any.
224
225 開始タグに、もしあれば、連続した属性の配列リファレンスを返します。
226
227 =item * C<return_text()>
228
229 This method has been deprecated in favor of C<as_is>. Programmers were getting
230 confused over the difference between C<is_text>, C<return_text>, and some
231 parser methods such as C<HTML::TokeParser::get_text> and friends. This
232 confusion stems from the fact that your author is a blithering idiot when it
233 comes to choosing methods names :)
234
235 このメソッドは、C<as_is>のために、非難されています。プログラマは、
236 C<is_text>と、C<return_text>と、C<HTML::TokeParser::get_text>とその仲間を混乱しています。
237 この混乱は、メソッドの名前を選ぶことに関して、著者がひどくまぬけであるという事実に起因します :)
238
239 Using this method still succeeds, but will now carp.
240
241 このメソッドの使用はまだ成功します。ですが、現在は警告がでます。
242
243 =item * C<as_is()>
244
245 This is the exact text of whatever the token is representing.
246
247 トークンが表されているものはなんでも、正確なテキストです。
248
249 =item * C<return_token0()>
250
251 For processing instructions, this will return the token found immediately after
252 the opening tag. Example: For <?php, "php" will be the start of the returned
253 string.
254
255 プロセッシングインストラクションで、開きタグのすぐ後に見つけられるトークンを返します。
256 例えば: <?php なら、 "php" が返される文字列の始まりになります。
257
258 =back
259
260 =head1 タグマンジングメソッド
261
262 The C<delete_attr()> and C<set_attr()> methods allow the programmer to rewrite
263 tag attributes on the fly. It should be noted that bad HTML will be
264 "corrected" by this. Specifically, the new tag will have all attributes
265 lower-cased with the values properly quoted.
266
267 C<delete_attr()>とC<set_attr()>メソッドで、プログラマはタグ属性をオンザフライに
268 書き直すことができます。悪いHTMLがこれによって"訂正される"ことに気が付くべきです。
269 特に、新しいタグは全ての属性が小文字になり、その値は適切にクォートされます。
270
271 Self-closing tags (e.g. E<lt>hr /E<gt>) are also handled correctly. Some older
272 browsers require a space prior to the final slash in a self-closed tag. If
273 such a space is detected in the original HTML, it will be preserved.
274
275 自動で閉じるタグ(例えば、E<lt>hr /E<gt>)も、正確にあつかわれます。
276 より古いブラウザには、自動で閉じられるタグには、最後のスラッシュより前に空白を必要とするものがあります。
277 そのようなスペースが、元のHTMLで見付かると、それはそのままになります。
278
279 =over 4
280
281 =item * C<delete_attr($name)>
282
283 This method attempts to delete the attribute specified. It will C<croak> if
284 called on anything other than a start tag. The argument is case-insensitive,
285 but must otherwise be an exact match of the attribute you are attempting to
286 delete. If the attribute is not found, the method will return without changing
287 the tag.
288
289 このメソッドは特定の属性を消そうとします。このメソッドが開始タグ以外の何かで呼ばれると、
290 C<croak>します。引数は大文字と小文字を区別しません。
291 ですが、別な方法で、消そうとしている属性に正確にマッチさせる必要があります。
292 もし、属性が見付からなければ、メソッドはタグを変更せずに返します。
293
294 # <body bgcolor="#FFFFFF">
295 $token->delete_attr('bgcolor');
296 print $token->as_is;
297 # <body>
298
299 After this method is called, if successful, the C<as_is()>, C<return_attr()>
300 and C<return_attrseq()> methods will all return updated results.
301
302 このメソッドが呼ばれた後は、成功すれば、C<as_id()>と、C<return_attr()>と
303 C<return_attrseq()>メソッドは更新された結果を返します。
304
305 =item * C<set_attr($name,$value)>
306
307 This method will set the value of an attribute. If the attribute is not found,
308 then C<return_attrseq()> will have the new attribute listed at the end. Two
309 arguments
310
311 このメソッドは、属性の値をセットします。属性が見付からなければ、
312 C<return_attrseq()>に、最後にリストされる新しい属性があります。
313 2つの引数です。
314
315 # <p>
316 $token->set_attr('class','some_class');
317 print $token->as_is;
318 # <p class="some_class">
319
320 # <body bgcolor="#FFFFFF">
321 $token->set_attr('bgcolor','red');
322 print $token->as_is;
323 # <body bgcolor="red">
324
325 After this method is called, if successful, the C<as_is()>, C<return_attr()>
326 and C<return_attrseq()> methods will all return updated results.
327
328 このメソッドが呼ばれた後は、C<as_is()>、C<return_attr()>、C<return_attrseq>メソッドは、
329 全て、更新された結果を返します。
330
331 =item * C<rewrite_tag()>
332
333 This method rewrites the tag. The tag name and the name of all attributes will
334 be lower-cased. Values that are not quoted with double quotes will be. This
335 may be called on both start or end tags. Note that both C<set_attr()> and
336 C<delete_attr()> call this method prior to returning.
337
338 このメソッドは、タグを書き直します。タグの名前と全ての属性の名前は、小文字になります。
339 ダブルクォートでクォートされていない値は、クォートされます。開始タグと終了タグの両方で呼ばれます。
340 C<set_attr()>とC<delete_attr()>の両方がこのメソッドを値を返す前に呼ぶことに気を付けてください。
341
342 If called on a token that is not a tag, it simply returns. Regardless of how
343 it is called, it returns the token.
344
345 タグでないトークンで呼ばれると、単純に返ります。どのようにメソッドが呼ばれたかを無視し、
346 トークンを返します。
347
348 # <body alink=#0000ff BGCOLOR=#ffffff class='none'>
349 $token->rewrite_tag;
350 print $token->as_is;
351 # <body alink="#0000ff" bgcolor="#ffffff" class="none">
352
353 A quick cleanup of sloppy HTML is now the following:
354
355 いい加減なHTMLを素早く綺麗にするには、次のようにします:
356
357 my $parser = HTML::TokeParser::Simple->new( $ugly_html );
358 while (my $token = $parser->get_token) {
359 $token->rewrite_tag;
360 print $token->as_is;
361 }
362
363 =head1 重要事項:
364
365 Some people get confused and try to call parser methods on tokens and token
366 methods (those described above) on methods. To prevent this,
367 C<HTML::TokeParser::Simple> versions 1.4 and above now bless all tokens into a
368 new class which inherits nothing. Please keep this in mind while using this
369 module (and many thanks to PodMaster
370 L<http://www.perlmonks.org/index.pl?node_id=107642> for pointing out this issue
371 to me.
372
373 人々には混乱する人がいて、パーサーメソッドをトークンで呼ぼうとし、トークンメソッド(上で説明しました)を、
374 メソッドで呼ぼうとします。
375 これを防ぐために、C<HTML::TokeParser::Simple>のバージョン 1.4以上では、現在、
376 何も継承しない新しいクラスに全てのトークンを bless しています。
377 このモジュールを使う間、心にとめておいてください(そして、この問題を私に指摘した PodMaster
378 L<http://www.perlmonks.org/index.pl?node_id=107642>にとても感謝します。
379
380 =head1 例
381
382 =head2 コメントを見つける
383
384 For some strange reason, your Pointy-Haired Boss (PHB) is convinced that the
385 graphics department is making fun of him by embedding rude things about him in
386 HTML comments. You need to get all HTML comments from the HTML.
387
388 ある変わった理由のために、とんがった髪型のボス(PHB)は、
389 グラフィック部が、HTMLのコメントに彼に対して失礼なことを埋め込んで、
390 彼をからかっていると確信しています。HTMLから全てのコメントを得る必要があります。
391
392 use strict;
393 use HTML::TokeParser::Simple;
394
395 my @html_docs = glob( "*.html" );
396
397 open PHB, "> phbreport.txt" or die "Cannot open phbreport for writing: $!";
398
399 foreach my $doc ( @html_docs ) {
400 print "Processing $doc\n";
401 my $p = HTML::TokeParser::Simple->new( $doc );
402 while ( my $token = $p->get_token ) {
403 next unless $token->is_comment;
404 print PHB $token->as_is, "\n";
405 }
406 }
407
408 close PHB;
409
410 =head2 コメントをはぎ取る
411
412 Uh oh. Turns out that your PHB was right for a change. Many of the comments
413 in the HTML weren't very polite. Since your entire graphics department was
414 just fired, it falls on you need to strip those comments from the HTML.
415
416 うーあー。PHBには変更する権利があります。HTMLのコメントのほとんどが、
417 あまり行儀良くありませんでした。グラフィック部の全てが、すぐにクビになりました。
418 おかげで、HTMLからそれらのコメントをはぎ取らなければならなくなりました。
419
420 use strict;
421 use HTML::TokeParser::Simple;
422
423 my $new_folder = 'no_comment/';
424 my @html_docs = glob( "*.html" );
425
426 foreach my $doc ( @html_docs ) {
427 print "Processing $doc\n";
428 my $new_file = "$new_folder$doc";
429
430 open PHB, "> $new_file" or die "Cannot open $new_file for writing: $!";
431
432 my $p = HTML::TokeParser::Simple->new( $doc );
433 while ( my $token = $p->get_token ) {
434 next if $token->is_comment;
435 print PHB $token->as_is;
436 }
437 close PHB;
438 }
439
440 =head2 フォームタグを変更する
441
442 Your company was foo.com and now is bar.com. Unfortunately, whoever wrote your
443 HTML decided to hardcode "http://www.foo.com/" into the C<action> attribute of
444 the form tags. You need to change it to "http://www.bar.com/".
445
446 会社は foo.com でしたが、たった今 bar.com になります。不幸なことに、
447 誰もが、フォームタグのC<action>属性に"http://www.foo.com/"をハードコードすると決めてHTMLを書いていました。
448 "http://www.bar.com" にそれを変えなければなりません。
449
450
451 use strict;
452 use HTML::TokeParser::Simple;
453
454 my $new_folder = 'new_html/';
455 my @html_docs = glob( "*.html" );
456
457 foreach my $doc ( @html_docs ) {
458 print "Processing $doc\n";
459 my $new_file = "$new_folder$doc";
460
461 open FILE, "> $new_file" or die "Cannot open $new_file for writing: $!";
462
463 my $p = HTML::TokeParser::Simple->new( $doc );
464 while ( my $token = $p->get_token ) {
465 if ( $token->is_start_tag('form') ) {
466 my $action = $token->return_attr->{action};
467 $action =~ s/www\.foo\.com/www.bar.com/;
468 $token->set_attr('action', $action);
469 }
470 print FILE $token->as_is;
471 }
472 close FILE;
473 }
474
475 =head1 著作権
476
477 (原文まま)
478
479 Copyright (c) 2001 Curtis "Ovid" Poe. All rights reserved. This program is
480 free software; you may redistribute it and/or modify it under the same terms as
481 Perl itself
482
483 =head1 著者
484
485 (原文まま)
486
487 Curtis "Ovid" Poe L<poec@yahoo.com>
488
489 =head1 バグ
490
491 Use of C<$HTML::Parser::VERSION> which is less than 3.25 may result in
492 incorrect behavior as older versions do not always handle XHTML correctly. It
493 is the programmer's responsibility to verify that the behavior of this code
494 matches the programmer's needs.
495
496 3.25より古いC<$HTML::Parser::VERSION>を使うと、結果として、不正確な動きをします。
497 古いバージョンでは、XHTMLをいつも正しく取り扱わないからです。
498 このコードの動きががプログラマの必要に合っていることを確かめるのは、プログラマの責任です。
499
500 Note that C<HTML::Parser> processes text in 512 byte chunks. This sometimes
501 will cause strange behavior and cause text to be broken into more than one
502 token. You can suppress this behavior with the following command:
503
504 C<HTML::Parser>は、512バイトの固まりでテキストを処理することに、気を付けてください。
505 このことが原因で、おかしな動きを引き起こしたり、テキストが壊れて、2つ以上のトークンになったりします。
506 この動きを下のコマンドで、抑えることができます:
507
508 $p->unbroken_text( [$bool] );
509
510 See the C<HTML::Parser> documentation and
511 http://www.perlmonks.org/index.pl?node_id=230667 for more information.
512
513 C<HTM::Parser>ドキュメントとhttp://www.perlmonks.org/index.pl?node_id=230667
514 より情報があるので、見てください。
515
516 Address bug reports and comments to: L<poec@yahoo.com>. When sending bug
517 reports, please provide the version of C<HTML::Parser>, C<HTML::TokeParser>,
518 C<HTML::TokeParser::Simple>, the version of Perl, and the version of the
519 operating system you are using.
520
521 バグレポートとコメントは次のアドレスに: L<poec@yahoo.com>。
522 バグレポートを送るときには、C<HTML::Parser>と、C<HTML::TokeParser>と、C<HTML::TokeParser::Simple>と、
523 Perlのバージョンと、使っているOSのバージョンを提供してください。

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26