[librdf-rdfa-generator-perl] 03/64: Improve and document API.

Jonas Smedegaard dr at jones.dk
Sat Dec 23 12:22:49 UTC 2017


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag debian/0.106-1
in repository librdf-rdfa-generator-perl.

commit bf0982ef4cbeb3c444d30d2d9f3901b1fccee506
Author: Toby Inkster <mail at tobyinkster.co.uk>
Date:   Fri May 7 22:47:55 2010 +0000

    Improve and document API.
    
    --HG--
    branch : RDF-RDFa-Generator
    extra : convert_revision : svn%3A3fe8e991-6959-4966-b76d-b07eca2b6e37/RDF-RDFa-Generator%40334
---
 example1.pl                           |   6 +-
 lib/RDF/RDFa/Generator.pm             | 166 ++++++++++++++++++++++++++++++++++
 lib/RDF/RDFa/Generator/HTML/Head.pm   |  54 ++++++++++-
 lib/RDF/RDFa/Generator/HTML/Hidden.pm |   7 +-
 lib/RDF/RDFa/Generator/HTML/Pretty.pm |  23 ++++-
 5 files changed, 247 insertions(+), 9 deletions(-)

diff --git a/example1.pl b/example1.pl
index 9d6554c..61860eb 100644
--- a/example1.pl
+++ b/example1.pl
@@ -22,9 +22,5 @@ my $graph = rdf_parse(<<TURTLE, type=>'turtle');
 
 TURTLE
 
-my $gen = RDF::RDFa::Generator::HTML::Pretty->new(base=>'http://example.net/');
+print RDF::RDFa::Generator::HTML::Hidden->create_document($graph)->toString;
 
-foreach my $n ($gen->nodes($graph))
-{
-	print $n->toString . "\n";
-}
diff --git a/lib/RDF/RDFa/Generator.pm b/lib/RDF/RDFa/Generator.pm
index e69de29..cda446c 100644
--- a/lib/RDF/RDFa/Generator.pm
+++ b/lib/RDF/RDFa/Generator.pm
@@ -0,0 +1,166 @@
+=head1 NAME
+
+RDF::RDFa::Generator - generate some data in RDFa
+
+=head1 VERSION
+
+0.01
+
+=cut
+
+package RDF::RDFa::Generator;
+
+use 5.008;
+use common::sense;
+
+our $VERSION = '0.01';
+
+use RDF::RDFa::Generator::HTML::Head;
+use RDF::RDFa::Generator::HTML::Hidden;
+use RDF::RDFa::Generator::HTML::Pretty;
+
+=head1 DESCRIPTION
+
+=head2 Constructor
+
+=over 4
+
+=item C<< $gen = RDF::RDFa::Generator->new($type, %options) >>
+
+Creates a new generator object. Type is one of the following case-sensitive strings:
+'HTML::Head' (the default), 'HTML::Hidden' or 'HTML::Pretty'. You can also construct
+an object like this:
+
+  $gen = RDF::RDFa::Generator::HTML::Head->new(%options);
+
+Options include:
+
+=over 4
+
+=item * B<base> - the base URL where the output data will be published. This allows in some cases for the generated RDFa to include relative URIs.
+
+=item * B<data_context> - if non-null, a URI (string) which indicates the context (named graph) containing the data to generate RDFa for.
+
+=item * B<ns> - a {uri=>prefix} hashref of preferred CURIE prefixes. There are already some defaults, and if you clash with them BAD THINGS will happen.
+
+=item * B<prefix_attr> - use the @prefix attribute for CURIE prefixes (RDFa 1.1 only).  Boolean, defaults to false.
+
+=item * B<title> - assign a <title> element for generated XHTML documents.
+
+=item * B<version> - set generated RDFa version. Valid values are '1.0' (the default) or '1.1'.
+
+=back
+
+=back
+
+=cut
+
+sub new
+{
+	my ($class, $implementation, %opts) = @_;
+	$implementation ||= 'HTML::Head';
+	$implementation = sprintf('%s::%s', __PACKAGE__, $implementation);
+	return $implementation->new(%opts);
+}
+
+=head2 Public Methods
+
+=over 4
+
+=item C<< $gen->create_document($model) >>
+
+Creates a new RDFa file containing triples. $model is an RDF::Trine::Model object
+providing the triples. Returns an XML::LibXML::Document object suitable
+for serialising using its C<toString> method.
+
+If you're planning on serving the RDFa with the text/html media type, then
+it is recommended that you use HTML::HTML5::Writer to serialise the
+document rather than C<toString>.
+
+Can also be called as a class method:
+
+ $document = RDF::RDFa::Generator->create_document($model)
+ # Same as:
+ # $document = RDF::RDFa::Generator->new->create_document($model)
+
+=cut
+
+sub create_document
+{
+	my $proto = shift;
+	my $self = (ref $proto) ? $proto : $proto->new;
+	return $self->create_document(@_);
+}
+
+=item C<< $gen->inject_document($document, $model) >>
+
+Injects an existing document with triples. $document is an XML::LibXML::Document
+to inject, or a well-formed XML string. $model is an RDF::Trine::Model object providing
+the triples. Returns an XML::LibXML::Document object suitable
+for serialising using its C<toString> method.
+
+See C<create_document> for information about serving the RDFa with the
+text/html media type.
+
+Can also be called as a class method. See C<create_document> for details.
+
+=cut
+
+sub inject_document
+{
+	my $proto = shift;
+	my $self = (ref $proto) ? $proto : $proto->new;
+	return $self->inject_document(@_);
+}
+
+=item C<< $gen->nodes($model) >>
+
+Provides triple-laden XML::LibXML::Elements to be added to a document.
+$model is an RDF::Trine::Model object providing the triples. If called in
+list context, returns a list of XML::LibXML::Element objects which can be
+added to a document; otherwise returns an XML::LibXML::NodeList containing
+a list of such elements.
+
+Can also be called as a class method. See C<create_document> for details.
+
+=cut
+
+sub nodes
+{
+	my $proto = shift;
+	my $self = (ref $proto) ? $proto : $proto->new;
+	return $self->nodes(@_);
+}
+
+1;
+
+__END__
+
+=back
+
+=head1 BUGS
+
+Please report any bugs to L<http://rt.cpan.org/>.
+
+=head1 SEE ALSO
+
+L<HTML::HTML5::Writer>, L<XML::LibXML>, L<RDF::RDFa::Parser>, L<RDF::Trine>.
+
+L<http://www.perlrdf.org/>.
+
+=head1 AUTHOR
+
+Toby Inkster E<lt>tobyink at cpan.orgE<gt>.
+
+=head1 COPYRIGHT AND LICENCE
+
+Copyright (C) 2010 by Toby Inkster
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself, either Perl version 5.8 or,
+at your option, any later version of Perl 5 you may have available.
+
+TODO: check image licences are proper.
+
+=cut
+
diff --git a/lib/RDF/RDFa/Generator/HTML/Head.pm b/lib/RDF/RDFa/Generator/HTML/Head.pm
index 9694e4d..eabf435 100644
--- a/lib/RDF/RDFa/Generator/HTML/Head.pm
+++ b/lib/RDF/RDFa/Generator/HTML/Head.pm
@@ -1,6 +1,7 @@
 package RDF::RDFa::Generator::HTML::Head;
 
 use 5.008;
+use base qw'RDF::RDFa::Generator';
 use common::sense;
 use XML::LibXML qw':all';
 
@@ -18,6 +19,55 @@ sub new
 	bless \%opts, $class;
 }
 
+sub injection_site
+{
+	return '//xhtml:head';
+}
+
+sub inject_document
+{
+	my ($proto, $html, $model) = @_;
+	my $dom   = $proto->_get_dom($html);
+	my @nodes = $proto->nodes($model);
+	
+	my $xc = XML::LibXML::XPathContext->new($dom);
+	$xc->registerNs('xhtml', 'http://www.w3.org/1999/xhtml');
+	my @sites = $xc->findnodes($proto->injection_site);
+	
+	die "No suitable place to inject this document." unless @sites;
+	
+	$sites[0]->appendChild($_) foreach @nodes;
+	return $dom;
+}
+
+sub create_document
+{
+	my ($proto, $model) = @_;
+	my $self = (ref $proto) ? $proto : $proto->new;
+	
+	my $html = sprintf(<<HTML, ($self->{'version'}||'1.0'), ($self->{'title'} || 'RDFa Document'), ref $self);
+<html xmlns="http://www.w3.org/1999/xhtml" version="XHTML+RDFa %1\$s">
+<head profile="http://www.w3.org/1999/xhtml/vocab">
+<title>%2\$s</title>
+<meta nane="generator" value="%3\$s" />
+</head>
+<body />
+</html>
+HTML
+
+	return $proto->inject_document($html, $model);
+}
+
+sub _get_dom
+{
+	my ($proto, $html) = @_;
+	
+	return $html if UNIVERSAL::isa($html, 'XML::LibXML::Document');
+	
+	my $p = XML::LibXML->new;
+	return $p->parse_string($html);
+}
+
 sub nodes
 {
 	my ($proto, $model) = @_;
@@ -98,7 +148,7 @@ sub _process_subject
 	if ($st->subject->is_resource) 
 		{ $node->setAttribute('about', $st->subject->uri); }
 	else
-		{ $node->setAttribute('about', '_:'.$st->subject->blank_identifier); }
+		{ $node->setAttribute('about', '[_:'.$st->subject->blank_identifier.']'); }
 	
 	return $self;
 }
@@ -163,7 +213,7 @@ sub _process_object
 	}
 	elsif ($st->object->is_blank)
 	{
-		$node->setAttribute('resource', '_:'.$st->object->blank_identifier);
+		$node->setAttribute('resource', '[_:'.$st->object->blank_identifier.']');
 		return $self;
 	}
 	
diff --git a/lib/RDF/RDFa/Generator/HTML/Hidden.pm b/lib/RDF/RDFa/Generator/HTML/Hidden.pm
index c55543e..ced9790 100644
--- a/lib/RDF/RDFa/Generator/HTML/Hidden.pm
+++ b/lib/RDF/RDFa/Generator/HTML/Hidden.pm
@@ -5,6 +5,11 @@ use base qw'RDF::RDFa::Generator::HTML::Head';
 use common::sense;
 use XML::LibXML qw':all';
 
+sub injection_site
+{
+	return '//xhtml:body';
+}
+
 sub nodes
 {
 	my ($proto, $model) = @_;
@@ -82,7 +87,7 @@ sub _process_subject
 	elsif ($st->subject->is_resource) 
 		{ $node->setAttribute('src', $st->subject->uri); }
 	else
-		{ $node->setAttribute('about', '_:'.$st->subject->blank_identifier); }
+		{ $node->setAttribute('about', '[_:'.$st->subject->blank_identifier.']'); }
 	
 	return $self;
 }
diff --git a/lib/RDF/RDFa/Generator/HTML/Pretty.pm b/lib/RDF/RDFa/Generator/HTML/Pretty.pm
index 37b49e9..7eaec85 100644
--- a/lib/RDF/RDFa/Generator/HTML/Pretty.pm
+++ b/lib/RDF/RDFa/Generator/HTML/Pretty.pm
@@ -6,6 +6,27 @@ use common::sense;
 use constant XHTML_NS => 'http://www.w3.org/1999/xhtml';
 use XML::LibXML qw':all';
 
+sub create_document
+{
+	my ($proto, $model) = @_;
+	my $self = (ref $proto) ? $proto : $proto->new;
+	
+	my $html = sprintf(<<HTML, ($self->{'version'}||'1.0'), ($self->{'title'} || 'RDFa Document'), ref $self);
+<html xmlns="http://www.w3.org/1999/xhtml" version="XHTML+RDFa %1\$s">
+<head profile="http://www.w3.org/1999/xhtml/vocab">
+<title>%2\$s</title>
+<meta nane="generator" value="%3\$s" />
+</head>
+<body>
+<h1>%2\$s</h1>
+<p><small>Generated by %3\$s.</small></p>
+</body>
+</html>
+HTML
+
+	return $proto->inject_document($html, $model);
+}
+
 sub nodes
 {
 	my ($proto, $model) = @_;
@@ -160,7 +181,7 @@ sub _resource_statements
 			$DD->setAttribute('class', 'blank');
 			
 			my $A = $DD->addNewChild(XHTML_NS, 'span');
-			$A->setAttribute('about', '_:'.$st->object->blank_identifier);
+			$A->setAttribute('about', '[_:'.$st->object->blank_identifier.']');
 			$A->appendTextNode('_:'.$st->object->blank_identifier);
 		}
 		elsif ($st->object->is_literal

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/librdf-rdfa-generator-perl.git



More information about the Pkg-perl-cvs-commits mailing list