/usr/share/perl5/HTML/Embedded/Turtle.pm is in libhtml-embedded-turtle-perl 0.333-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 | package HTML::Embedded::Turtle;
use 5.010;
use common::sense; # oh, the irony!
use Data::UUID;
use RDF::RDFa::Parser '1.093';
use RDF::TriN3;
use RDF::Trine qw[iri literal blank statement];
sub biri { $_[0] =~ /^_:(.*)$/ ? blank($1) : iri(@_) }
use namespace::clean;
use Object::AUTHORITY;
BEGIN {
$HTML::Embedded::Turtle::VERSION = '0.333';
$HTML::Embedded::Turtle::AUTHORITY = 'cpan:TOBYINK';
}
my $xhv = RDF::Trine::Namespace->new('http://www.w3.org/1999/xhtml/vocab#');
sub new
{
my ($class, $markup, $base_uri, $options) = @_;
my $self = bless {
markup => $markup ,
options => $options ,
}, $class;
$options->{'rdfa_options'} //= $options->{'markup'} =~ /x(ht)?ml/i
? RDF::RDFa::Parser::Config->new(RDF::RDFa::Parser::Config->HOST_XHTML, RDF::RDFa::Parser::Config->RDFA_10)
: RDF::RDFa::Parser::Config->new(RDF::RDFa::Parser::Config->HOST_HTML5, RDF::RDFa::Parser::Config->RDFA_10);
my $rdfa_parser =
$self->{rdfa_parser} = RDF::RDFa::Parser->new($markup, $base_uri, $options->{'rdfa_options'});
$self->{dom} = $rdfa_parser->dom;
$self->{base_uri} = $rdfa_parser->uri;
$self->_find_endorsed->_extract_graphs;
}
sub _find_endorsed
{
my ($self) = @_;
my $rdfa_parser = $self->{rdfa_parser};
foreach my $o ($rdfa_parser->graph->objects(iri($self->{base_uri}), $xhv->meta))
{
# Endorsements must be URIs.
next unless $o->is_resource;
# Endorsements must be fragments within this document.
my $must_start_with = $self->{base_uri} . '#';
my $must_start_with_re = qr/^\Q$must_start_with\E/;
next unless $o->uri =~ $must_start_with_re;
push @{ $self->{endorsements} }, $o->uri;
}
return $self;
}
sub _extract_graphs
{
my ($self) = @_;
my $uuid = Data::UUID->new;
my @scripts = $self->{'dom'}->getElementsByTagName('script');
foreach my $script (@scripts)
{
my $parser = $self->_choose_parser_by_type($script->getAttribute('type'))
// $self->_choose_parser_by_language($script->getAttribute('language'));
next unless $parser;
my $data = $script->textContent;
my $model = RDF::Trine::Model->temporary_model;
$parser->parse_into_model($self->{base_uri}, $data, $model);
my $graphname = $script->hasAttribute('id')
? join('#', $self->{base_uri}, $script->getAttribute('id'))
: sprintf('_:bn%s', substr $uuid->create_hex, 2);
$self->{'graphs'}->{$graphname} = $model;
}
return $self;
}
sub _choose_parser_by_type
{
my ($self, $type) = @_;
given ($type)
{
when(m'^\s*(application|text)/(x-)?turtle\b'i)
{ return RDF::Trine::Parser::Turtle->new; }
when (m'^\s*text/plain\b'i)
{ return RDF::Trine::Parser::NTriples->new; }
when (m'^\s*(application|text)/(x-)?(rdf\+)?n3\b'i)
{ return RDF::Trine::Parser::Notation3->new; }
when (m'^\s*(application/rdf\+xml)|(text/rdf)\b'i)
{ return RDF::Trine::Parser::RDFXML->new; }
when (m'^\s*application/(x-)?(rdf\+)?json\b'i)
{ return RDF::Trine::Parser::RDFJSON->new; }
}
return undef;
}
sub _choose_parser_by_language
{
my ($self, $language) = @_;
my $parser;
eval { $parser = RDF::Trine::Parser->new($language) };
return $parser;
}
sub graph
{
my ($self, $graph) = @_;
if (!defined $graph)
{
my $model = RDF::Trine::Model->temporary_model;
while (my ($graph, $graph_model) = each %{ $self->{graphs} })
{
$graph_model->as_stream->each(sub {
my ($s, $p, $o) = $_[0]->nodes;
$model->add_statement(statement($s, $p, $o), biri($graph));
});
}
return $model;
}
elsif ($graph eq '::ENDORSED')
{
my $model = RDF::Trine::Model->temporary_model;
while (my ($graph, $graph_model) = each %{ $self->{graphs} })
{
next unless grep { $_ eq $graph } @{$self->{endorsements}};
$graph_model->as_stream->each(sub {
my ($s, $p, $o) = $_[0]->nodes;
$model->add_statement(statement($s, $p, $o), biri($graph));
});
}
return $model;
}
elsif (defined $self->{graphs}->{$graph})
{
return $self->{graphs}->{$graph};
}
}
sub union_graph
{
my ($self) = @_;
return $self->graph;
}
sub endorsed_union_graph
{
my ($self) = @_;
return $self->graph('::ENDORSED');
}
sub graphs
{
my ($self, $graph) = @_;
if (!defined $graph)
{
my $rv = {};
foreach my $graph (keys %{ $self->{graphs} })
{
$rv->{$graph} = $self->{graphs}->{$graph};
}
return $rv;
}
elsif ($graph == '::ENDORSED')
{
my $rv = {};
foreach my $graph (@{ $self->{endorsements} })
{
if (defined $self->{graphs}->{$graph})
{
$rv->{$graph} = $self->{graphs}->{$graph};
}
}
return $rv;
}
elsif (defined $self->{graphs}->{$graph})
{
return { $graph => $self->{graphs}->{$graph} };
}
}
sub all_graphs
{
my ($self) = @_;
return $self->graphs;
}
sub endorsed_graphs
{
my ($self) = @_;
return $self->graphs('::ENDORSED');
}
sub endorsements
{
return @{ $_[0]->{endorsements} };
}
sub dom
{
return $_[0]->{'dom'}
}
sub uri
{
my $self = shift;
return $self->{'rdfa_parser'}->uri(@_);
}
1;
__END__
=head1 NAME
HTML::Embedded::Turtle - embedding RDF in HTML the crazy way
=head1 SYNOPSIS
use HTML::Embedded::Turtle;
my $het = HTML::Embedded::Turtle->new($html, $base_uri);
foreach my $graph ($het->endorsements)
{
my $model = $het->graph($graph);
# $model is an RDF::Trine::Model. Do something with it.
}
=head1 DESCRIPTION
RDF can be embedded in (X)HTML using simple E<lt>scriptE<gt> tags. This is
described at L<http://esw.w3.org/N3inHTML>. This gives you a file format
that can contain multiple (optionally named) graphs. The document as a whole
can "endorse" a graph by including:
<link rel="meta" href="#foo" />
Where "#foo" is a fragment identifier pointing to a graph.
<script type="text/turtle" id="foo"> ... </script>
The rel="meta" stuff is parsed using an RDFa parser, so equivalent RDFa
works too.
This module parses HTML files containing graphs like these, and allows
you to access them each individually; as a union of all graphs on the page;
or as a union of just the endorsed graphs.
Despite the module name, this module supports a variety of
E<lt>script typeE<gt>s: text/turtle, application/turtle, application/x-turtle
text/plain (N-Triples), text/n3 (Notation 3), application/x-rdf+json (RDF/JSON),
application/json (RDF/JSON), and application/rdf+xml (RDF/XML).
The deprecated attribute "language" is also supported:
<script language="Turtle" id="foo"> ... </script>
Languages supported are (case insensitive): "Turtle", "NTriples", "RDFJSON",
"RDFXML" and "Notation3".
=head2 Constructor
=over 4
=item C<< HTML::Embedded::Turtle->new($markup, $base_uri, \%opts) >>
Create a new object. $markup is the HTML or XHTML markup to parse;
$base_uri is the base URI to use for relative references.
Options include:
=over 4
=item * B<markup>
Choose which parser to use: 'html' or 'xml'. The former chooses
HTML::HTML5::Parser, which can handle tag soup; the latter chooses
XML::LibXML, which cannot. Defaults to 'html'.
=item * B<rdfa_options>
A set of options to be parsed to RDF::RDFa::Parser when looking for
endorsements. See L<RDF::RDFa::Parser::Config>. The default is
probably sensible.
=back
=back
=head2 Public Methods
=over 4
=item C<< union_graph >>
A union graph of all graphs found in the document, as an RDF::Trine::Model.
Note that the returned model contains quads.
=item C<< endorsed_union_graph >>
A union graph of only the endorsed graphs, as an RDF::Trine::Model.
Note that the returned model contains quads.
=item C<< graph($name) >>
A single graph from the page.
=item C<< graphs >>
=item C<< all_graphs >>
A hashref where the keys are graph names and the values are
RDF::Trine::Models. Some graph names will be URIs, and others
may be blank nodes (e.g. "_:foobar").
C<graphs> and C<all_graphs> are aliases for each other.
=item C<< endorsed_graphs >>
Like C<all_graphs>, but only returns endorsed graphs. Note that
all endorsed graphs will have graph names that are URIs.
=item C<< endorsements >>
Returns a list of URIs which are the names of endorsed graphs. Note that
the presence of a URI C<$x> in this list does not imply that
C<< $het->graph($x) >> will be defined.
=item C<< dom >>
Returns the page DOM.
=item C<< uri >>
Returns the page URI.
=back
=head1 BUGS
Please report any bugs to L<http://rt.cpan.org/>.
Please forgive me in advance for inflicting this module upon you.
=head1 SEE ALSO
L<RDF::RDFa::Parser>, L<RDF::Trine>, L<RDF::TriN3>.
L<http://www.perlrdf.org/>.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2010-2011 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system 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
|