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

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.7 - (show annotations) (download)
Sun Feb 6 17:53:00 2011 UTC (13 years, 3 months ago) by ktats
Branch: MAIN
Changes since 1.6: +2 -0 lines
fix pod

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

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