File Coverage

blib/lib/RDF/RDFa/Generator.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             RDF::RDFa::Generator - generate some data in RDFa
4              
5             =cut
6              
7             package RDF::RDFa::Generator;
8              
9 1     1   23545 use 5.008;
  1         3  
  1         35  
10 1     1   5 use strict;
  1         2  
  1         48  
11              
12             our $VERSION = '0.103';
13              
14 1     1   631 use RDF::RDFa::Generator::HTML::Head;
  0            
  0            
15             use RDF::RDFa::Generator::HTML::Hidden;
16             use RDF::RDFa::Generator::HTML::Pretty;
17             use RDF::Trine;
18              
19             BEGIN
20             {
21             $RDF::Trine::Serializer::serializer_names{ 'rdfa' } = __PACKAGE__;
22             foreach my $type (qw(application/xhtml+xml text/html))
23             {
24             $RDF::Trine::Serializer::media_types{ $type } = __PACKAGE__;
25             }
26             }
27              
28             =head1 DESCRIPTION
29              
30             =head2 Constructor
31              
32             =over 4
33              
34             =item C<< $gen = RDF::RDFa::Generator->new(style => $style, %options) >>
35              
36             Creates a new generator object. $style is one of the following case-sensitive strings:
37             'HTML::Head' (the default), 'HTML::Hidden' or 'HTML::Pretty'. You can also construct
38             an object like this:
39              
40             $gen = RDF::RDFa::Generator::HTML::Head->new(%options);
41              
42             Options include:
43              
44             =over 4
45              
46             =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.
47              
48             =item * B<data_context> - if non-null, a URI (string) which indicates the context (named graph) containing the data to generate RDFa for.
49              
50             =item * B<namespaces> - a {prefix=>uri} hashref of preferred CURIE prefixes.
51              
52             =item * B<ns> - a {uri=>prefix} hashref of preferred CURIE prefixes. DEPRECATED - use B<namespaces> instead.
53              
54             =item * B<prefix_attr> - use the @prefix attribute for CURIE prefixes (RDFa 1.1 only). Boolean, defaults to false.
55              
56             =item * B<safe_xml_literals> - prevents XML literals from injecting arbitrary XHTML into the output. Boolean, B<defaults to FALSE>.
57              
58             =item * B<title> - assign a <title> element for generated XHTML documents.
59              
60             =item * B<version> - set generated RDFa version. Valid values are '1.0' (the default) or '1.1'.
61              
62             =back
63              
64             =back
65              
66             =cut
67              
68             sub new
69             {
70             my ($class, %opts) = @_;
71             my $implementation = sprintf('%s::%s', __PACKAGE__, $opts{'style'} || 'HTML::Head');
72             return $implementation->new(%opts);
73             }
74              
75             =head2 Public Methods
76              
77             =over 4
78              
79             =item C<< $gen->create_document($model) >>
80              
81             Creates a new RDFa file containing triples. $model is an RDF::Trine::Model object
82             providing the triples. Returns an XML::LibXML::Document object suitable
83             for serialising using its C<toString> method.
84              
85             If you're planning on serving the RDFa with the text/html media type, then
86             it is recommended that you use HTML::HTML5::Writer to serialise the
87             document rather than C<toString>.
88              
89             Can also be called as a class method:
90              
91             $document = RDF::RDFa::Generator->create_document($model)
92             # Same as:
93             # $document = RDF::RDFa::Generator->new->create_document($model)
94              
95             =cut
96              
97             sub create_document
98             {
99             my $proto = shift;
100             my $self = (ref $proto) ? $proto : $proto->new;
101             return $self->create_document(@_);
102             }
103              
104             =item C<< $gen->inject_document($document, $model) >>
105              
106             Injects an existing document with triples. $document is an XML::LibXML::Document
107             to inject, or a well-formed XML string. $model is an RDF::Trine::Model object providing
108             the triples. Returns an XML::LibXML::Document object suitable
109             for serialising using its C<toString> method.
110              
111             See C<create_document> for information about serving the RDFa with the
112             text/html media type.
113              
114             Can also be called as a class method. See C<create_document> for details.
115              
116             =cut
117              
118             sub inject_document
119             {
120             my $proto = shift;
121             my $self = (ref $proto) ? $proto : $proto->new;
122             return $self->inject_document(@_);
123             }
124              
125             =item C<< $gen->nodes($model) >>
126              
127             Provides triple-laden XML::LibXML::Elements to be added to a document.
128             $model is an RDF::Trine::Model object providing the triples. If called in
129             list context, returns a list of XML::LibXML::Element objects which can be
130             added to a document; otherwise returns an XML::LibXML::NodeList containing
131             a list of such elements.
132              
133             Can also be called as a class method. See C<create_document> for details.
134              
135             The HTML::Pretty generator can be passed a couple of additional options:
136              
137             $gen->nodes($model, notes_heading=>'Additional Info', notes=>\@notes);
138              
139             The notes are a list of RDF::RDFa::Generator::HTML::Pretty::Note objects
140             which are added as notes to the end of each subject's data.
141              
142             =cut
143              
144             sub nodes
145             {
146             my $proto = shift;
147             my $self = (ref $proto) ? $proto : $proto->new;
148             return $self->nodes(@_);
149             }
150              
151             =back
152              
153             Additionally the methods C<serialize_model_to_file>, C<serialize_model_to_string>,
154             C<serialize_iterator_to_file> and C<serialize_iterator_to_string> are provided for
155             compatibility with the L<RDF::Trine::Serializer> interface.
156              
157             =cut
158              
159             sub serialize_model_to_string
160             {
161             my ($proto, $model) = @_;
162             return $proto->create_document($model)->toString;
163             }
164              
165             sub serialize_model_to_file
166             {
167             my ($proto, $fh, $model) = @_;
168             print {$fh} $proto->create_document($model)->toString;
169             }
170              
171             sub serialize_iterator_to_string
172             {
173             my ($proto, $iter) = @_;
174             my $model = RDF::Trine::Model->temporary_model;
175             while (my $st = $iter->next)
176             {
177             $model->add_statement($st);
178             }
179             return $proto->serialize_model_to_string($model);
180             }
181              
182             sub serialize_iterator_to_file
183             {
184             my ($proto, $fh, $iter) = @_;
185             my $model = RDF::Trine::Model->temporary_model;
186             while (my $st = $iter->next)
187             {
188             $model->add_statement($st);
189             }
190             return $proto->serialize_model_to_file($fh, $model);
191             }
192              
193             1;
194              
195             __END__
196              
197             =head1 BUGS
198              
199             Please report any bugs to L<http://rt.cpan.org/>.
200              
201             =head1 SEE ALSO
202              
203             L<HTML::HTML5::Writer>, L<XML::LibXML>, L<RDF::RDFa::Parser>, L<RDF::Trine>,
204             L<RDF::Prefixes>.
205              
206             L<http://www.perlrdf.org/>.
207              
208             =head1 AUTHOR
209              
210             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
211              
212             =head1 COPYRIGHT AND LICENCE
213              
214             Copyright (C) 2010 by Toby Inkster
215              
216             This library is free software; you can redistribute it and/or modify
217             it under the same terms as Perl itself, either Perl version 5.8 or,
218             at your option, any later version of Perl 5 you may have available.
219              
220             =head2 Icons
221              
222             RDF::RDFa::Generator::HTML::Pretty uses the FamFamFam Silk icons;
223             see L<http://famfamfam.com/lab/icons/silk/>.
224              
225             =cut
226