/usr/share/perl5/HTML/Microformats.pm is in libhtml-microformats-perl 0.104-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | =head1 NAME
HTML::Microformats - parse microformats in HTML
=head1 SYNOPSIS
use HTML::Microformats;
my $doc = HTML::Microformats
->new_document($html, $uri)
->assume_profile(qw(hCard hCalendar));
print $doc->json(pretty => 1);
use RDF::TrineShortcuts qw(rdf_query);
my $results = rdf_query($sparql, $doc->model);
=cut
package HTML::Microformats;
use common::sense;
use 5.010;
use HTML::HTML5::Parser;
use HTML::HTML5::Sanity qw(fix_document);
use HTML::Microformats::DocumentContext;
use HTML::Microformats::Datatype;
use HTML::Microformats::Format;
use JSON;
use RDF::Trine 0.130;
use XML::LibXML;
use Object::AUTHORITY;
BEGIN {
$HTML::Microformats::AUTHORITY = 'cpan:TOBYINK';
$HTML::Microformats::VERSION = '0.104';
}
=head1 DESCRIPTION
The HTML::Microformats module is a wrapper for parser and handler
modules of various individual microformats (each of those modules
has a name like HTML::Microformats::Format::Foo).
The general pattern of usage is to create an HTML::Microformats
object (which corresponds to an HTML document) using the
C<new_document> method; then ask for the data, as a Perl hashref,
a JSON string, or an RDF::Trine model.
=head2 Constructor
=over 4
=item C<< $doc = HTML::Microformats->new_document($html, $uri, %opts) >>
Constructs a document object.
$html is the HTML or XHTML source (string) or an XML::LibXML::Document.
$uri is the document URI, important for resolving relative URL references.
%opts are additional parameters; currently only one option is defined:
$opts{'type'} is set to 'text/html' or 'application/xhtml+xml', to
control how $html is parsed.
=back
=cut
sub new_document
{
my $class = shift;
my $document = shift;
my $uri = shift;
my %opts = @_;
my $self = bless {}, $class;
$self->modules; # force modules to be loaded
if (ref $document && $document->isa('XML::LibXML::Document'))
{
}
elsif ($opts{'type'} =~ /x(ht)?ml/i)
{
my $parser = XML::LibXML->new;
$document = $parser->parse_string($document);
}
else
{
my $parser = HTML::HTML5::Parser->new;
$document = fix_document( $parser->parse_string($document) );
}
$self->{'context'} = HTML::Microformats::DocumentContext->new($document, $uri);
return $self;
}
=head2 Profile Management
HTML::Microformats uses HTML profiles (i.e. the profile attribute on the
HTML <head> element) to detect which Microformats are used on a page. Any
microformats which do not have a profile URI declared will not be parsed.
Because many pages fail to properly declare which profiles they use, there
are various profile management methods to tell HTML::Microformats to
assume the presence of particular profile URIs, even if they're actually
missing.
=over 4
=item C<< $doc->profiles >>
This method returns a list of profile URIs declared by the document.
=cut
sub profiles
{
my $self = shift;
return $self->{'context'}->profiles(@_);
}
=item C<< $doc->has_profile(@profiles) >>
This method returns true if and only if one or more of the profile URIs
in @profiles is declared by the document.
=cut
sub has_profile
{
my $self = shift;
return $self->{'context'}->has_profile(@_);
}
=item C<< $doc->add_profile(@profiles) >>
Using C<add_profile> you can add one or more profile URIs, and they are
treated as if they were found on the document.
For example:
$doc->add_profile('http://microformats.org/profile/rel-tag')
This is useful for adding profile URIs declared outside the document itself
(e.g. in HTTP headers).
Returns a reference to the document.
=cut
sub add_profile
{
my $self = shift;
$self->{'context'}->add_profile(@_);
return $self;
}
=item C<< $doc->assume_profile(@microformats) >>
For example:
$doc->assume_profile(qw(hCard adr geo))
This method acts similarly to C<add_profile> but allows you to use
names of microformats rather than URIs.
Microformat names are case sensitive, and must match
HTML::Microformats::Format::Foo module names.
Returns a reference to the document.
=cut
sub assume_profile
{
my $self = shift;
foreach my $fmt (@_)
{
my $profile = $fmt;
($profile) = "HTML::Microformats::Format::${fmt}"->profiles
if $fmt !~ ':';
$self->add_profile($profile);
}
return $self;
}
=item C<< $doc->assume_all_profiles >>
This method is equivalent to calling C<assume_profile> for
all known microformats.
Returns a reference to the document.
=back
=cut
sub assume_all_profiles
{
my $self = shift;
$self->assume_profile($self->formats);
return $self;
}
=head2 Parsing Microformats
Generally speaking, you can skip this. The C<data>, C<json> and
C<model> methods will automatically do this for you.
=over 4
=item C<< $doc->parse_microformats >>
Scans through the document, finding microformat objects.
On subsequent calls, does nothing (as everything is already parsed).
Returns a reference to the document.
=cut
sub parse_microformats
{
my $self = shift;
return if $self->{'parsed'};
foreach my $fmt ($self->formats)
{
my @profiles = "HTML::Microformats::Format::${fmt}"->profiles;
if ($self->has_profile(@profiles))
{
my @objects = "HTML::Microformats::Format::${fmt}"->extract_all(
$self->{'context'}->document->documentElement,
$self->{'context'});
$self->{'objects'}->{$fmt} = \@objects;
}
}
$self->{'parsed'} = 1;
return $self;
}
=item C<< $doc->clear_microformats >>
Forgets information gleaned by C<parse_microformats> and thus allows
C<parse_microformats> to be run again. This is useful if you've modified
added some profiles between runs of C<parse_microformats>.
Returns a reference to the document.
=back
=cut
sub clear_microformats
{
my $self = shift;
$self->{'objects'} = undef;
$self->{'context'}->cache->clear;
$self->{'parsed'} = 0;
return $self;
}
=head2 Retrieving Data
These methods allow you to retrieve the document's data, and do things
with it.
=over 4
=item C<< $doc->objects($format); >>
$format is, for example, 'hCard', 'adr' or 'RelTag'.
Returns a list of objects of that type. (If called in scalar context,
returns an arrayref.)
Each object is, for example, an HTML::Microformat::hCard object, or an
HTML::Microformat::RelTag object, etc. See the relevent documentation
for details.
=cut
sub objects
{
my $self = shift;
my $fmt = shift;
$self->parse_microformats;
return @{ $self->{'objects'}->{$fmt} }
if wantarray;
return $self->{'objects'}->{$fmt};
}
=item C<< $doc->all_objects >>
Returns a hashref of data. Each hashref key is the name of a microformat
(e.g. 'hCard', 'RelTag', etc), and the values are arrayrefs of objects.
Each object is, for example, an HTML::Microformat::hCard object, or an
HTML::Microformat::RelTag object, etc. See the relevent documentation
for details.
=cut
sub all_objects
{
my $self = shift;
$self->parse_microformats;
return $self->{'objects'};
}
sub TO_JSON
{
return $_[0]->all_objects;
}
=item C<< $doc->json(%opts) >>
Returns data roughly equivalent to the C<all_objects> method, but as a JSON
string.
%opts is a hash of options, suitable for passing to the L<JSON>
module's to_json function. The 'convert_blessed' and 'utf8' options are
enabled by default, but can be disabled by explicitly setting them to 0, e.g.
print $doc->json( pretty=>1, canonical=>1, utf8=>0 );
=cut
sub json
{
my $self = shift;
my %opts = @_;
$opts{'convert_blessed'} = 1
unless defined $opts{'convert_blessed'};
$opts{'utf8'} = 1
unless defined $opts{'utf8'};
return to_json($self->all_objects, \%opts);
}
=item C<< $doc->model >>
Returns data as an RDF::Trine::Model, suitable for serialising as
RDF or running SPARQL queries.
=cut
sub model
{
my $self = shift;
my $model = RDF::Trine::Model->temporary_model;
$self->add_to_model($model);
return $model;
}
=item C<< $object->serialise_model(as => $format) >>
As C<model> but returns a string.
=cut
sub serialise_model
{
my $self = shift;
my %opts = ref $_[0] ? %{ $_[0] } : @_;
$opts{as} ||= 'Turtle';
my $ser = RDF::Trine::Serializer->new(delete $opts{as}, %opts);
return $ser->serialize_model_to_string($self->model);
}
=item C<< $doc->add_to_model($model) >>
Adds data to an existing RDF::Trine::Model.
Returns a reference to the document.
=back
=cut
sub add_to_model
{
my $self = shift;
my $model = shift;
$self->parse_microformats;
foreach my $fmt ($self->formats)
{
foreach my $object (@{ $self->{'objects'}->{$fmt} })
{
$object->add_to_model($model);
}
}
return $self;
}
=head2 Utility Functions
=over 4
=item C<< HTML::Microformats->modules >>
Returns a list of Perl modules, each of which implements a specific
microformat.
=cut
use Module::Pluggable
require => 1,
inner => 0,
search_path => ['HTML::Microformats::Format'],
only => qr/^HTML::Microformats::Format::[^:]+$/,
sub_name => 'modules',
;
=item C<< HTML::Microformats->formats >>
As per C<modules>, but strips 'HTML::Microformats::Format::' off the
module name, and sorts alphabetically.
=back
=cut
sub formats
{
my $class = shift || __PACKAGE__;
return
sort { lc $a cmp lc $b }
map { s/^HTML::Microformats::Format:://; $_ }
$class->modules;
}
1;
__END__
=head1 WHY ANOTHER MICROFORMATS MODULE?
There already exist two microformats packages on CPAN (see L<Text::Microformat>
and L<Data::Microformat>), so why create another?
Firstly, HTML::Microformats isn't being created from scratch. It's actually a
fork/clean-up of a non-CPAN application (Swignition), and in that sense
predates Text::Microformat (though not Data::Microformat).
It has a number of other features that distinguish it from the existing
packages:
=over 4
=item * It supports more formats.
HTML::Microformats supports hCard, hCalendar, rel-tag, geo, adr,
rel-enclosure, rel-license, hReview, hResume, hRecipe, xFolk, XFN,
hAtom, hNews and more.
=item * It supports more patterns.
HTML::Microformats supports the include pattern, abbr pattern, table cell
header pattern, value excerpting and other intricacies of microformat parsing
better than the other modules on CPAN.
=item * It offers RDF support.
One of the key features of HTML::Microformats is that it makes data
available as RDF::Trine models. This allows your application to benefit
from a rich, feature-laden Semantic Web toolkit. Data gleaned from
microformats can be stored in a triple store; output in RDF/XML or
Turtle; queried using the SPARQL or RDQL query languages; and more.
If you're not comfortable using RDF, HTML::Microformats also makes
all its data available as native Perl objects.
=back
=head1 BUGS
Please report any bugs to L<http://rt.cpan.org/>.
=head1 SEE ALSO
L<HTML::Microformats::Documentation::Notes>.
Individual format modules:
=over 4
=item * L<HTML::Microformats::Format::adr>
=item * L<HTML::Microformats::Format::figure>
=item * L<HTML::Microformats::Format::geo>
=item * L<HTML::Microformats::Format::hAtom>
=item * L<HTML::Microformats::Format::hAudio>
=item * L<HTML::Microformats::Format::hCalendar>
=item * L<HTML::Microformats::Format::hCard>
=item * L<HTML::Microformats::Format::hListing>
=item * L<HTML::Microformats::Format::hMeasure>
=item * L<HTML::Microformats::Format::hNews>
=item * L<HTML::Microformats::Format::hProduct>
=item * L<HTML::Microformats::Format::hRecipe>
=item * L<HTML::Microformats::Format::hResume>
=item * L<HTML::Microformats::Format::hReview>
=item * L<HTML::Microformats::Format::hReviewAggregate>
=item * L<HTML::Microformats::Format::OpenURL_COinS>
=item * L<HTML::Microformats::Format::RelEnclosure>
=item * L<HTML::Microformats::Format::RelLicense>
=item * L<HTML::Microformats::Format::RelTag>
=item * L<HTML::Microformats::Format::species>
=item * L<HTML::Microformats::Format::VoteLinks>
=item * L<HTML::Microformats::Format::XFN>
=item * L<HTML::Microformats::Format::XMDP>
=item * L<HTML::Microformats::Format::XOXO>
=back
Similar modules:
L<RDF::RDFa::Parser>,
L<HTML::HTML5::Microdata::Parser>,
L<XML::Atom::Microformats>,
L<Text::Microformat>,
L<Data::Microformats>.
Related web sites:
L<http://microformats.org/>, L<http://www.perlrdf.org/>.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT
Copyright 2008-2011 Toby Inkster
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=head1 DISCLAIMER OF WARRANTIES
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
=cut
|