/usr/share/perl5/HTML/Microformats/Format.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 | =head1 NAME
HTML::Microformats::Format - base microformat class
=head1 DESCRIPTION
HTML::Microformats::Format cannot be instantiated directly but many other classes
inherit from it.
=cut
package HTML::Microformats::Format;
use base qw(HTML::Microformats::Mixin::RDF);
use common::sense;
use 5.010;
use Carp;
use HTML::Microformats::Utilities qw(searchClass searchRel searchRev);
use RDF::Trine;
use Scalar::Util qw[];
use Object::AUTHORITY;
BEGIN {
$HTML::Microformats::Format::AUTHORITY = 'cpan:TOBYINK';
$HTML::Microformats::Format::VERSION = '0.104';
}
our $AUTOLOAD;
# Derived classes...
# MUST override: new
# SHOULD override: format_signature, add_to_model, profiles
# MIGHT WANT TO override: id, extract_all, data
=head2 Constructors
The constructors cannot actually be called on this package. Call them on descendent
classes instead.
=over 4
=item C<< $object = HTML::Microformats::Format->new($element, $context, %options) >>
Parse a microformat with root element $element.
=cut
sub new
{
die "Cannot instantiate HTML::Microformats::Format.\n";
}
=item C<< $object = HTML::Microformats::Format->extract_all($element, $context, %options) >>
Find and parse all such microformats within element $element.
=back
=cut
sub extract_all
{
my ($class, $dom, $context, %options) = @_;
my @rv;
my $hclass = $class->format_signature->{'root'};
my $rel = $class->format_signature->{'rel'};
my $rev = $class->format_signature->{'rev'};
unless (defined $rel || defined $rev || defined $hclass)
{
die "extract_all failed.\n";
}
if (defined $hclass)
{
$hclass = [$hclass] unless ref $hclass eq 'ARRAY';
foreach my $hc (@$hclass)
{
my @elements = searchClass($hc, $dom);
foreach my $e (@elements)
{
my $object = $class->new($e, $context, %options);
next unless $object;
next if grep { $_->id eq $object->id } @rv; # avoid duplicates
push @rv, $object if ref $object;
}
}
}
if (defined $rel)
{
$rel = [$rel] unless ref $rel eq 'ARRAY';
foreach my $r (@$rel)
{
my @elements = searchRel($r, $dom);
foreach my $e (@elements)
{
my $object = $class->new($e, $context, %options);
next unless $object;
next if grep { $_->id eq $object->id } @rv; # avoid duplicates
push @rv, $object if ref $object;
}
}
}
if (defined $rev)
{
$rev = [$rev] unless ref $rev eq 'ARRAY';
foreach my $r (@$rev)
{
my @elements = searchRev($r, $dom);
foreach my $e (@elements)
{
my $object = $class->new($e, $context, %options);
next unless $object;
next if grep { $_->id eq $object->id } @rv; # avoid duplicates
push @rv, $object if ref $object;
}
}
}
return @rv;
}
=head2 Public Methods - Accessors
There are a number of property accessor methods defined via Perl's AUTOLOAD mechanism.
For any microformat property (e.g. 'fn' in hCard) there are get_X, set_X, add_X and
clear_X methods defined.
C<get_X>: for singular properties, returns the value of property X. For plural properties, returns a
list of values if called in list context, or the first value otherwise.
C<set_X>: for singular properties, sets the value of property X to the first given parameter.
For plural properties, sets the values of property X to the list of parameters.
B<This feature is deprecated and will be removed in a future release.>
C<add_X>: for singular properties, sets the value of property X to the first given parameter,
but croaks if X is already set. For concatenated singular properties, concatenates to the
end of any existing value of X. For plural properties, adds any given parameters to the
list of values of property X.
B<This feature is deprecated and will be removed in a future release.>
C<clear_X>: removes any values of property X, but croaks if the property is a required
property.
B<This feature is deprecated and will be removed in a future release.>
For example, an HTML::Microformats::hCard object will have a method called get_fn which
gets the value of the hCard's "fn" property, a method called set_fn which sets it, a
method called add_fn which also sets it (but croaks if it's already set), and a method
called clear_fn which croaks if called (because "fn" is a required property).
B<Deprecated features:> the C<set_X>, C<add_X> and C<clear_X> methods are
deprecated and will be removed soon. In general you should treat objects which are
instances of HTML::Microformats::Format as read-only.
=cut
sub AUTOLOAD
{
my $self = shift;
my $func = $AUTOLOAD;
if ($func =~ /^.*::(get|set|add|clear)_([^:]+)$/)
{
my $method = $1;
my $datum = $2;
my $opts = undef;
my $classes = $self->format_signature->{'classes'};
$datum =~ s/_/\-/g;
foreach my $c (@$classes)
{
if ($c->[0] eq $datum)
{
$opts = $c->[1];
last;
}
elsif ($c->[2]->{'use-key'} eq $datum)
{
$datum = $c->[2]->{'use-key'};
$opts = $c->[1];
last;
}
}
croak "Function $func unknown.\n" unless defined $opts;
if ($method eq 'get')
{
return $self->{'DATA'}->{$datum};
}
elsif ($method eq 'clear')
{
croak "Attempt to clear required property $datum.\n"
if $opts =~ /[1\+]/;
delete $self->{'DATA'}->{$datum};
}
elsif ($method eq 'add')
{
croak "Attempt to add more than one value to singular property $datum.\n"
if $opts =~ /[1\?]/ && defined $self->{'DATA'}->{$datum};
if ($opts =~ /[1\?]/)
{
$self->{'DATA'}->{$datum} = shift;
}
elsif ($opts =~ /[\&]/)
{
$self->{'DATA'}->{$datum} .= shift;
}
else
{
push @{ $self->{'DATA'}->{$datum} }, @_;
}
}
elsif ($method eq 'set')
{
if ($opts =~ /[1\?\&]/)
{
$self->{'DATA'}->{$datum} = shift;
}
else
{
$self->{'DATA'}->{$datum} = \@_;
}
}
}
else
{
croak "No function '$func' defined.\n"
unless $func =~ /::(DESTROY|no|import)$/;
}
}
=head2 Public Methods - Other
=over 4
=item C<< $object->format_signature >> or C<< $class->format_signature >>
This method may be called as a class or object method. It returns various information
about the definition of this microformat (e.g. what is the root class, which properties
exist, etc). You may need to do some digging to figure out what everything means.
=cut
sub format_signature
{
return {
'root' => undef ,
'rel' => undef ,
'classes' => [] ,
'options' => {} ,
'rdf:type' => 'http://www.w3.org/2002/07/owl#Thing' ,
'rdf:property' => {} ,
};
}
=item C<< $object->profiles >> or C<< $class->profiles >>
This method may be called as a class or object method. It returns HTML profile
URIs which indicate the presence of this microformat.
=cut
sub profiles
{
return qw();
}
=item C<< $object->context >>
Returns the parsing context (as supplied to C<new>).
=cut
sub context
{
return $_[0]->{'context'};
}
=item C<< $object->data >>
Returns a hashref of object data. This is a reference to the live data inside the
object. Any changes to the returned hashref will change the values inside the object.
=cut
sub data
{
return {} unless defined $_[0]->{'DATA'};
return $_[0]->{'DATA'};
}
sub TO_JSON
{
return data( $_[0] );
}
=item C<< $object->element >>
Returns the root element.
=cut
sub element
{
return $_[0]->{'element'};
}
=item C<< $object->cache >>
Shortcut for C<< $object->context->cache >>.
=cut
sub cache
{
return $_[0]->{'cache'};
}
=item C<< $object->id([$trine_obj], [$role]) >>
Returns a blank node identifier or identifying URI for the object.
If $trine_obj is true, the return value is an RDF::Trine::Node object. Otherwise,
it's a string (using the '_:' convention to identify blank nodes).
If $role is undefined, then returns the identifier for the object itself.
If it's defined then it returns an identifier for a resource with a fixed
relationship to the object.
$identifier_for_business_card = $hcard->id;
$identifier_for_person = $hcard->id(undef, 'holder');
=cut
sub id
{
my ($self, $as_trine, $role) = @_;
my $id = defined $role ? $self->{"id.${role}"} : $self->{'id'};
unless (defined $id)
{
$self->{ defined $role ? "id.${role}" : 'id' } = $self->context->make_bnode;
$id = defined $role ? $self->{"id.${role}"} : $self->{'id'};
}
return $id unless $as_trine;
return ($id =~ /^_:(.*)$/) ?
RDF::Trine::Node::Blank->new($1) :
RDF::Trine::Node::Resource->new($id);
}
=item C<< $object->add_to_model($model) >>
Given an RDF::Trine::Model object, adds relevant data to the model.
=cut
sub add_to_model
{
my $self = shift;
my $model = shift;
$self->_simple_rdf($model);
return $self;
}
=item C<< $object->model >>
Creates a fresh, new RDF::Trine::Model object, containing relevant data.
=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.
=back
=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);
}
sub _isa # utility function for subclasses to use
{
return Scalar::Util::blessed($_[1]) && $_[1]->isa($_[2]);
}
1;
=head1 BUGS
Please report any bugs to L<http://rt.cpan.org/>.
=head1 SEE ALSO
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
|