Skip to content

Commit f325754

Browse files
committed
Apply perltidy to some code in Pod
1 parent 11873b6 commit f325754

2 files changed

Lines changed: 121 additions & 113 deletions

File tree

README.md

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,23 @@ use HTML::Parser ();
1515

1616
# Create parser object
1717
my $p = HTML::Parser->new(
18-
api_version => 3,
19-
start_h => [\&start, "tagname, attr"],
20-
end_h => [\&end, "tagname"],
21-
marked_sections => 1,
18+
api_version => 3,
19+
start_h => [\&start, "tagname, attr"],
20+
end_h => [\&end, "tagname"],
21+
marked_sections => 1,
2222
);
2323

2424
# Parse document text chunk by chunk
2525
$p->parse($chunk1);
2626
$p->parse($chunk2);
27+
2728
# ...
2829
# signal end of document
2930
$p->eof;
3031

3132
# Parse directly from file
3233
$p->parse_file("foo.html");
34+
3335
# or
3436
open(my $fh, "<:utf8", "foo.html") || die;
3537
$p->parse_file($fh);
@@ -144,12 +146,12 @@ to the `HTML::Parser` object:
144146

145147
```perl
146148
while (1) {
147-
my $chunk = &$code_ref();
148-
if (!defined($chunk) || !length($chunk)) {
149-
$p->eof;
150-
return $p;
151-
}
152-
$p->parse($chunk) || return undef;
149+
my $chunk = &$code_ref();
150+
if (!defined($chunk) || !length($chunk)) {
151+
$p->eof;
152+
return $p;
153+
}
154+
$p->parse($chunk) || return undef;
153155
}
154156
```
155157

@@ -869,18 +871,18 @@ $p->handler(end => "end", "self, tagname, text");
869871
$p->handler(text => "text", "self, text, is_cdata");
870872
$p->handler(process => "process", "self, token0, text");
871873
$p->handler(
872-
comment => sub {
873-
my($self, $tokens) = @_;
874-
for (@$tokens) {$self->comment($_);}
875-
},
876-
"self, tokens"
874+
comment => sub {
875+
my ($self, $tokens) = @_;
876+
for (@$tokens) { $self->comment($_); }
877+
},
878+
"self, tokens"
877879
);
878880
$p->handler(
879-
declaration => sub {
880-
my $self = shift;
881-
$self->declaration(substr($_[0], 2, -1));
882-
},
883-
"self, text"
881+
declaration => sub {
882+
my $self = shift;
883+
$self->declaration(substr($_[0], 2, -1));
884+
},
885+
"self, text"
884886
);
885887
```
886888
@@ -901,21 +903,23 @@ an HTML document. We achieve this by setting up a comment handler that
901903
does nothing and a default handler that will print out anything else:
902904
903905
```perl
904-
use HTML::Parser;
906+
use HTML::Parser ();
905907
HTML::Parser->new(
906-
default_h => [sub { print shift }, 'text'],
907-
comment_h => [""],
908-
)->parse_file(shift || die) || die $!;
908+
default_h => [sub { print shift }, 'text'],
909+
comment_h => [""],
910+
)->parse_file(shift || die)
911+
|| die $!;
909912
```
910913
911914
An alternative implementation is:
912915
913916
```perl
914-
use HTML::Parser;
917+
use HTML::Parser ();
915918
HTML::Parser->new(
916-
end_document_h => [sub { print shift }, 'skipped_text'],
917-
comment_h => [""],
918-
)->parse_file(shift || die) || die $!;
919+
end_document_h => [sub { print shift }, 'skipped_text'],
920+
comment_h => [""],
921+
)->parse_file(shift || die)
922+
|| die $!;
919923
```
920924
921925
This will in most cases be much more efficient since only a single
@@ -931,15 +935,15 @@ parsing as soon as the title end tag is seen:
931935
use HTML::Parser ();
932936
933937
sub start_handler {
934-
return if shift ne "title";
935-
my $self = shift;
936-
$self->handler(text => sub { print shift }, "dtext");
937-
$self->handler(
938-
end => sub {
939-
shift->eof if shift eq "title";
940-
},
941-
"tagname,self"
942-
);
938+
return if shift ne "title";
939+
my $self = shift;
940+
$self->handler(text => sub { print shift }, "dtext");
941+
$self->handler(
942+
end => sub {
943+
shift->eof if shift eq "title";
944+
},
945+
"tagname,self"
946+
);
943947
}
944948
945949
my $p = HTML::Parser->new(api_version => 3);

lib/HTML/Parser.pm

Lines changed: 80 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -129,30 +129,32 @@ HTML::Parser - HTML parser class
129129
130130
=head1 SYNOPSIS
131131
132-
use strict;
133-
use warnings;
134-
use HTML::Parser ();
135-
136-
# Create parser object
137-
my $p = HTML::Parser->new(
138-
api_version => 3,
139-
start_h => [\&start, "tagname, attr"],
140-
end_h => [\&end, "tagname"],
141-
marked_sections => 1,
142-
);
143-
144-
# Parse document text chunk by chunk
145-
$p->parse($chunk1);
146-
$p->parse($chunk2);
147-
# ...
148-
# signal end of document
149-
$p->eof;
150-
151-
# Parse directly from file
152-
$p->parse_file("foo.html");
153-
# or
154-
open(my $fh, "<:utf8", "foo.html") || die;
155-
$p->parse_file($fh);
132+
use strict;
133+
use warnings;
134+
use HTML::Parser ();
135+
136+
# Create parser object
137+
my $p = HTML::Parser->new(
138+
api_version => 3,
139+
start_h => [\&start, "tagname, attr"],
140+
end_h => [\&end, "tagname"],
141+
marked_sections => 1,
142+
);
143+
144+
# Parse document text chunk by chunk
145+
$p->parse($chunk1);
146+
$p->parse($chunk2);
147+
148+
# ...
149+
# signal end of document
150+
$p->eof;
151+
152+
# Parse directly from file
153+
$p->parse_file("foo.html");
154+
155+
# or
156+
open(my $fh, "<:utf8", "foo.html") || die;
157+
$p->parse_file($fh);
156158
157159
=head1 DESCRIPTION
158160
@@ -262,14 +264,14 @@ Parsing will also abort if one of the event handlers calls $p->eof.
262264
263265
The effect of this is the same as:
264266
265-
while (1) {
266-
my $chunk = &$code_ref();
267-
if (!defined($chunk) || !length($chunk)) {
268-
$p->eof;
269-
return $p;
267+
while (1) {
268+
my $chunk = &$code_ref();
269+
if (!defined($chunk) || !length($chunk)) {
270+
$p->eof;
271+
return $p;
272+
}
273+
$p->parse($chunk) || return undef;
270274
}
271-
$p->parse($chunk) || return undef;
272-
}
273275
274276
But it is more efficient as this loop runs internally in XS code.
275277
@@ -988,24 +990,24 @@ HTML::Parser version 2 callback methods.
988990
989991
This is equivalent to the following method calls:
990992
991-
$p->handler(start => "start", "self, tagname, attr, attrseq, text");
992-
$p->handler(end => "end", "self, tagname, text");
993-
$p->handler(text => "text", "self, text, is_cdata");
994-
$p->handler(process => "process", "self, token0, text");
995-
$p->handler(
996-
comment => sub {
997-
my($self, $tokens) = @_;
998-
for (@$tokens) {$self->comment($_);}
999-
},
1000-
"self, tokens"
1001-
);
1002-
$p->handler(
1003-
declaration => sub {
1004-
my $self = shift;
1005-
$self->declaration(substr($_[0], 2, -1));
1006-
},
1007-
"self, text"
1008-
);
993+
$p->handler(start => "start", "self, tagname, attr, attrseq, text");
994+
$p->handler(end => "end", "self, tagname, text");
995+
$p->handler(text => "text", "self, text, is_cdata");
996+
$p->handler(process => "process", "self, token0, text");
997+
$p->handler(
998+
comment => sub {
999+
my ($self, $tokens) = @_;
1000+
for (@$tokens) { $self->comment($_); }
1001+
},
1002+
"self, tokens"
1003+
);
1004+
$p->handler(
1005+
declaration => sub {
1006+
my $self = shift;
1007+
$self->declaration(substr($_[0], 2, -1));
1008+
},
1009+
"self, text"
1010+
);
10091011
10101012
Setting up these handlers can also be requested with the "api_version =>
10111013
2" constructor option.
@@ -1023,19 +1025,21 @@ The first simple example shows how you might strip out comments from
10231025
an HTML document. We achieve this by setting up a comment handler that
10241026
does nothing and a default handler that will print out anything else:
10251027
1026-
use HTML::Parser;
1027-
HTML::Parser->new(
1028-
default_h => [sub { print shift }, 'text'],
1029-
comment_h => [""],
1030-
)->parse_file(shift || die) || die $!;
1028+
use HTML::Parser ();
1029+
HTML::Parser->new(
1030+
default_h => [sub { print shift }, 'text'],
1031+
comment_h => [""],
1032+
)->parse_file(shift || die)
1033+
|| die $!;
10311034
10321035
An alternative implementation is:
10331036
1034-
use HTML::Parser;
1035-
HTML::Parser->new(
1036-
end_document_h => [sub { print shift }, 'skipped_text'],
1037-
comment_h => [""],
1038-
)->parse_file(shift || die) || die $!;
1037+
use HTML::Parser ();
1038+
HTML::Parser->new(
1039+
end_document_h => [sub { print shift }, 'skipped_text'],
1040+
comment_h => [""],
1041+
)->parse_file(shift || die)
1042+
|| die $!;
10391043
10401044
This will in most cases be much more efficient since only a single
10411045
callback will be made.
@@ -1046,24 +1050,24 @@ handler. When it sees the title start tag it enables a text handler
10461050
that prints any text found and an end handler that will terminate
10471051
parsing as soon as the title end tag is seen:
10481052
1049-
use HTML::Parser ();
1050-
1051-
sub start_handler {
1052-
return if shift ne "title";
1053-
my $self = shift;
1054-
$self->handler(text => sub { print shift }, "dtext");
1055-
$self->handler(
1056-
end => sub {
1057-
shift->eof if shift eq "title";
1058-
},
1059-
"tagname,self"
1060-
);
1061-
}
1053+
use HTML::Parser ();
1054+
1055+
sub start_handler {
1056+
return if shift ne "title";
1057+
my $self = shift;
1058+
$self->handler(text => sub { print shift }, "dtext");
1059+
$self->handler(
1060+
end => sub {
1061+
shift->eof if shift eq "title";
1062+
},
1063+
"tagname,self"
1064+
);
1065+
}
10621066
1063-
my $p = HTML::Parser->new(api_version => 3);
1064-
$p->handler(start => \&start_handler, "tagname,self");
1065-
$p->parse_file(shift || die) || die $!;
1066-
print "\n";
1067+
my $p = HTML::Parser->new(api_version => 3);
1068+
$p->handler(start => \&start_handler, "tagname,self");
1069+
$p->parse_file(shift || die) || die $!;
1070+
print "\n";
10671071
10681072
More examples are found in the F<eg/> directory of the C<HTML-Parser>
10691073
distribution: the program C<hrefsub> shows how you can edit all links

0 commit comments

Comments
 (0)