/usr/share/perl5/HTML/Microformats/Format/XOXO.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 580 581 582 583 584 585 | =head1 NAME
HTML::Microformats::Format::XOXO - the XOXO microformat
=head1 SYNOPSIS
use HTML::Microformats::DocumentContext;
use HTML::Microformats::Format::XOXO;
my $context = HTML::Microformats::DocumentContext->new($dom, $uri);
my @objects = HTML::Microformats::Format::XOXO->extract_all(
$dom->documentElement, $context);
my $list = $objects[0];
# Let's assume this structure:
#
# <ol class="xoxo people">
# <li>
# <a href="http://tobyinkster.co.uk/">Toby Inkster</a>
# <dl>
# <dt>Eye colour</dt>
# <dd>Blue</dt>
# <dt>Hair colour</dt>
# <dd>Blonde</dt>
# <dd>Brown</dt>
# </dl>
# </li>
# </ol>
print $list->data->as_array->[0]->get_link_title;
# Toby Inkster
print $list->data->as_array->[0]->get_properties
->get_value('Eye colour')->[0];
# Blue
print join '-', $list->data->as_array->[0]
->get_value('Hair colour');
# Blonde-Brown
=head1 DESCRIPTION
HTML::Microformats::Format::XOXO inherits from HTML::Microformats::Format. See the
base class definition for a description of property getter/setter methods,
constructors, etc.
Unlike most of the modules in the HTML::Microformats suite,
the C<data> method returns an HTML::Microformats::Format::XOXO::UL,
HTML::Microformats::Format::XOXO::OL or HTML::Microformats::Format::XOXO::DL
object, rather than a plain hashref.
=cut
package HTML::Microformats::Format::XOXO;
use base qw(HTML::Microformats::Format);
use common::sense;
use 5.010;
use HTML::Microformats::Utilities qw(stringify xml_stringify);
use JSON qw/to_json/;
use Object::AUTHORITY;
BEGIN {
$HTML::Microformats::Format::XOXO::AUTHORITY = 'cpan:TOBYINK';
$HTML::Microformats::Format::XOXO::VERSION = '0.104';
}
sub new
{
my ($class, $element, $context) = @_;
my $cache = $context->cache;
return $cache->get($context, $element, $class)
if defined $cache && $cache->get($context, $element, $class);
my $self = {
'element' => $element ,
'context' => $context ,
'cache' => $cache ,
};
bless $self, $class;
if ($element->hasAttribute('id') && length $element->getAttribute('id'))
{
$self->{'id'} = $context->uri('#' . $element->getAttribute('id'));
}
else
{
$self->{'id'} = $context->make_bnode($element);
}
return undef unless $element->localname =~ /^[DOU]L$/i;
$self->{'DATA'} = $self->_parse_list($element->cloneNode(1));
$cache->set($context, $element, $class, $self)
if defined $cache;
return $self;
}
sub _parse_list
{
my ($self, $e) = @_;
if (lc $e->localname eq 'ul')
{ return HTML::Microformats::Format::XOXO::UL->parse($e, $self); }
elsif (lc $e->localname eq 'ol')
{ return HTML::Microformats::Format::XOXO::OL->parse($e, $self); }
elsif (lc $e->localname eq 'dl')
{ return HTML::Microformats::Format::XOXO::DL->parse($e, $self); }
return undef;
}
sub format_signature
{
return {
'root' => 'xoxo',
'classes' => [],
'options' => {},
'rdf:type' => [] ,
'rdf:property' => {},
};
}
sub add_to_model
{
my $self = shift;
my $model = shift;
$model->add_statement(RDF::Trine::Statement->new(
$self->id(1),
RDF::Trine::Node::Resource->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
RDF::Trine::Node::Resource->new('http://purl.org/dc/dcmitype/Dataset'),
));
$model->add_statement(RDF::Trine::Statement->new(
$self->id(1),
RDF::Trine::Node::Resource->new('http://open.vocab.org/terms/json'),
$self->_make_literal( to_json($self, {canonical=>1,convert_blessed=>1}) ),
));
return $self;
}
sub profiles
{
return qw(http://microformats.org/profile/xoxo
http://ufs.org/x/xoxo
http://microformats.org/profile/specs
http://ufs.org/x/specs
http://purl.org/uF/2008/03/);
}
1;
package HTML::Microformats::Format::XOXO::AbstractList;
use common::sense;
use 5.010;
sub parse
{
my ($class, $e, $xoxo) = @_;
my @items;
foreach my $li ($e->getChildrenByTagName('li'))
{ push @items, HTML::Microformats::Format::XOXO::LI->parse($li, $xoxo); }
bless \@items, $class;
}
sub TO_JSON
{
return [ @{$_[0]} ];
}
sub as_array
{
my ($self) = @_;
return wantarray ? @$self : $self;
}
1;
=head2 HTML::Microformats::Format::XOXO::DL
Represents an HTML DL element.
=over 4
=item C<< $dl->get_values($key) >>
Treating a DL as a key-value structure, returns a list of values for a given key.
Each value is an HTML::Microformats::Format::XOXO::DD object.
=item C<< $dl->as_hash >>
Returns a hash of keys pointing to arrayrefs of values, where each value is an
HTML::Microformats::Format::XOXO::DD object.
=item C<< $dl->as_array >>
Logically what you think get_values("*") might do.
=back
=cut
package HTML::Microformats::Format::XOXO::DL;
use base qw[HTML::Microformats::Format::XOXO::AbstractList];
use common::sense;
use 5.010;
use HTML::Microformats::Utilities qw(stringify xml_stringify);
sub parse
{
my ($class, $e, $xoxo) = @_;
my $dict = {};
my $term;
foreach my $kid ($e->childNodes)
{
next unless $kid->isa('XML::LibXML::Element');
if ($kid->localname =~ /^DT$/i)
{
$term = stringify($kid);
if ($kid->hasAttribute('id'))
{
$dict->{$term}->{'id'} = $kid->getAttribute('id');
}
}
elsif (defined $term)
{
push @{ $dict->{$term}->{'items'} }, HTML::Microformats::Format::XOXO::DD->parse($kid, $xoxo);
}
}
bless $dict, $class;
}
sub TO_JSON
{
my $self = shift;
my $rv = {};
while (my ($k, $v) = each %$self)
{
$rv->{$k} = $v->{'items'};
}
return $rv;
}
sub get_values
{
my ($self, $key) = @_;
return wantarray ? @{ $self->{$key}->{'items'} } : $self->{$key}->{'items'}
if defined $self->{$key}->{'items'};
}
sub as_hash
{
my ($self) = @_;
return $self->TO_JSON;
}
sub as_array
{
my ($self, $key) = @_;
my @rv;
foreach my $key (sort keys %$self)
{
push @rv, @{ $self->{$key}->{'items'} };
}
return wantarray ? @rv : \@rv;
}
1;
=head2 HTML::Microformats::Format::XOXO::UL
Represents an HTML UL element.
=over 4
=item C<< $ul->as_array >>
Returns an array of values, where each is a HTML::Microformats::Format::XOXO::LI object.
=back
=cut
package HTML::Microformats::Format::XOXO::UL;
use base qw(HTML::Microformats::Format::XOXO::AbstractList);
use common::sense;
use 5.010;
1;
=head2 HTML::Microformats::Format::XOXO::OL
Represents an HTML OL element.
=over 4
=item C<< $ol->as_array >>
Returns an array of values, where each is a HTML::Microformats::Format::XOXO::LI object.
=back
=cut
package HTML::Microformats::Format::XOXO::OL;
use base qw(HTML::Microformats::Format::XOXO::AbstractList);
use common::sense;
use 5.010;
1;
package HTML::Microformats::Format::XOXO::AbstractListItem;
use common::sense;
use 5.010;
use HTML::Microformats::Utilities qw(stringify xml_stringify);
our $for_get_them_not = 'a|dl|li|ol|ul';
sub parse
{
my ($class, $e, $xoxo) = @_;
my $self = bless {}, $class;
my $a = $self->_get_them($e, 'a');
my $dl = $self->_get_them($e, 'dl');
my $l = $self->_get_them($e, 'ol|ul');
if ($a)
{
$self->{'url'} = $xoxo->context->uri($a->getAttribute('href'))
if $a->hasAttribute('href');
$self->{'type'} = $a->getAttribute('type')
if $a->hasAttribute('type');
$self->{'rel'} = $a->getAttribute('rel')
if $a->hasAttribute('rel');
$self->{'title'} = $a->getAttribute('title') || stringify($a);
}
if ($dl)
{
$self->{'properties'} = HTML::Microformats::Format::XOXO::DL->parse($dl, $xoxo);
$dl->parentNode->removeChild($dl);
}
if (defined $l && lc $l->localname eq 'ul')
{
$self->{'children'} = HTML::Microformats::Format::XOXO::UL->parse($l, $xoxo);
$l->parentNode->removeChild($l);
}
elsif (defined $l && lc $l->localname eq 'ol')
{
$self->{'children'} = HTML::Microformats::Format::XOXO::OL->parse($l, $xoxo);
$l->parentNode->removeChild($l);
}
$self->{'text'} = stringify($e);
$self->{'html'} = xml_stringify($e);
return $self;
}
sub _get_them
{
my ($self, $e, $pattern) = @_;
my @rv;
my @check = $e->childNodes;
while (@check)
{
my $elem = shift @check;
next unless $elem->isa('XML::LibXML::Element');
if ($elem->localname =~ /^($pattern)$/i)
{
if (wantarray)
{ push @rv, $elem; }
else
{ return $elem; }
}
if ($elem->localname !~ /^($for_get_them_not)$/i)
{
unshift @check, $elem->childNodes;
}
}
if (wantarray)
{ return @rv; }
else
{ return undef; }
}
sub TO_JSON
{
my %rv = %{$_[0]};
delete $rv{'html'};
return \%rv;
}
sub get_link_href
{
my ($self) = @_;
return $self->{'url'};
}
sub get_link_rel
{
my ($self) = @_;
return $self->{'rel'};
}
sub get_link_type
{
my ($self) = @_;
return $self->{'type'};
}
sub get_link_title
{
my ($self) = @_;
return $self->{'title'};
}
sub get_text
{
my ($self) = @_;
return $self->{'text'};
}
sub get_html
{
my ($self) = @_;
return $self->{'html'};
}
sub get_properties
{
my ($self) = @_;
return $self->{'properties'};
}
sub get_children
{
my ($self) = @_;
return $self->{'children'};
}
sub get_value
{
my ($self, $key) = @_;
return $self->get_properties->get_values($key)
if $self->get_properties;
}
1;
=head2 HTML::Microformats::Format::XOXO::LI
Represents an HTML LI element.
=over 4
=item C<< $li->get_link_href >>
Returns the URL linked to by the B<first> link found within the item.
=item C<< $li->get_link_rel >>
Returns the value of the rel attribute of the first link found within the item.
This is an unparsed string.
=item C<< $li->get_link_type >>
Returns the value of the type attribute of the first link found within the item.
This is an unparsed string.
=item C<< $li->get_link_title >>
Returns the value of the rel attribute of the first link found within the item
if present; the link text otherwise.
=item C<< $li->get_text >>
Returns the value of the text in the LI element B<except> for the first DL
element within the LI, and the first UL or OL element.
=item C<< $li->get_html >>
Returns the HTML code in the LI element B<except> for the first DL
element within the LI, and the first UL or OL element.
=item C<< $li->get_properties >>
Returns an HTML::Microformats::Format::XOXO::DL object representing the first
DL element within the LI.
=item C<< $li->get_children >>
Returns an HTML::Microformats::Format::XOXO::OL or HTML::Microformats::Format::XOXO::UL
object representing the first OL or UL element within the LI.
=item C<< $li->get_value($key) >>
A shortcut for C<< $li->get_properties->get_values($key) >>.
=back
=cut
package HTML::Microformats::Format::XOXO::LI;
use base qw(HTML::Microformats::Format::XOXO::AbstractListItem);
use common::sense;
use 5.010;
1;
=head2 HTML::Microformats::Format::XOXO::DD
This has an identical interface to HTML::Microformats::Format::XOXO::LI.
=cut
package HTML::Microformats::Format::XOXO::DD;
use base qw(HTML::Microformats::Format::XOXO::AbstractListItem);
use common::sense;
use 5.010;
1;
=head1 MICROFORMAT
HTML::Microformats::Format::XOXO supports XOXO as described at
L<http://microformats.org/wiki/xoxo>.
=head1 RDF OUTPUT
XOXO does not map especially naturally to RDF, so this module returns
the data as a JSON literal using the property L<http://open.vocab.org/terms/json>.
=head1 BUGS
Please report any bugs to L<http://rt.cpan.org/>.
=head1 SEE ALSO
L<HTML::Microformats::Format>,
L<HTML::Microformats>.
=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
|