File Coverage

blib/lib/RDF/RDFa/Generator.pm
Criterion Covered Total %
statement 23 36 63.8
branch 0 6 0.0
condition 2 2 100.0
subroutine 8 15 53.3
pod 4 8 50.0
total 37 67 55.2


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             RDF::RDFa::Generator - Generate data for RDFa serialization
4              
5             =cut
6              
7             package RDF::RDFa::Generator;
8              
9 4     4   6320271 use 5.008;
  4         41  
10 4     4   23 use strict;
  4         9  
  4         103  
11              
12 4     4   21 use warnings;
  4         9  
  4         176  
13              
14              
15             our $VERSION = '0.200';
16              
17 4     4   2079 use RDF::RDFa::Generator::HTML::Head;
  4         13  
  4         159  
18 4     4   1919 use RDF::RDFa::Generator::HTML::Hidden;
  4         43  
  4         210  
19 4     4   2280 use RDF::RDFa::Generator::HTML::Pretty;
  4         10  
  4         151  
20 4     4   67 use Carp;
  4         10  
  4         1335  
21              
22              
23             =head1 DESCRIPTION
24              
25             =head2 Constructor
26              
27             =over 4
28              
29             =item C<< $gen = RDF::RDFa::Generator->new(style => $style, %options) >>
30              
31             Creates a new generator object. $style is one of the following case-sensitive strings:
32             'HTML::Head' (the default), 'HTML::Hidden' or 'HTML::Pretty'. You can also construct
33             an object like this:
34              
35             $gen = RDF::RDFa::Generator::HTML::Head->new(%options);
36              
37             Options include:
38              
39             =over 4
40              
41             =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.
42              
43             =item * B<data_context> - if non-null, an L<Attean> Blank or IRI object or an L<RDF::Trine::Node> which indicates the context (named graph) containing the data to generate RDFa for.
44              
45             =item * B<namespacemap> - a L<URI::NamespaceMap> object containing preferred CURIE prefixes. This is the preferred method, see note below.
46              
47             =item * B<namespaces> - a {prefix=>uri} hashref of preferred CURIE prefixes.
48              
49             =item * B<ns> - a {uri=>prefix} hashref of preferred CURIE prefixes. DEPRECATED - use B<namespaces> instead.
50              
51             =item * B<prefix_attr> - use the @prefix attribute for CURIE prefixes (RDFa 1.1 only). Boolean, defaults to false.
52              
53             =item * B<safe_xml_literals> - prevents XML literals from injecting arbitrary XHTML into the output. Boolean, B<defaults to FALSE>.
54              
55             =item * B<title> - assign a <title> element for generated XHTML documents.
56              
57             =item * B<version> - set generated RDFa version. Valid values are '1.0' (the default) or '1.1'.
58              
59             =back
60              
61             =back
62              
63             =cut
64              
65             sub new
66             {
67 15     15 1 522996 my ($class, %opts) = @_;
68 15   100     114 my $implementation = sprintf('%s::%s', __PACKAGE__, $opts{'style'} || 'HTML::Head');
69 15         131 return $implementation->new(%opts);
70             }
71              
72             =head2 Public Methods
73              
74             =over 4
75              
76             =item C<< $gen->create_document($model, %opts) >>
77              
78             Creates a new RDFa file containing triples. $model is an RDF::Trine::Model object
79             providing the triples. Returns an XML::LibXML::Document object suitable
80             for serializing using its C<toString> method.
81              
82             If you're planning on serving the RDFa with the text/html media type, then
83             it is recommended that you use HTML::HTML5::Writer to serialize the
84             document rather than C<toString>.
85              
86             Can also be called as a class method:
87              
88             $document = RDF::RDFa::Generator->create_document($model)
89             # Same as:
90             # $document = RDF::RDFa::Generator->new->create_document($model)
91              
92             Options can also be passed as a HASH. This is typically used for style-specific options.
93              
94             =cut
95              
96             sub create_document
97             {
98 0     0 1   my $proto = shift;
99 0 0         my $self = (ref $proto) ? $proto : $proto->new;
100 0           return $self->create_document(@_);
101             }
102              
103             =item C<< $gen->inject_document($document, $model) >>
104              
105             Injects an existing document with triples. $document is an XML::LibXML::Document
106             to inject, or a well-formed XML string. $model is an RDF::Trine::Model object providing
107             the triples. Returns an XML::LibXML::Document object suitable
108             for serializing using its C<toString> method.
109              
110             See C<create_document> for information about serving the RDFa with the
111             text/html media type.
112              
113             Can also be called as a class method. See C<create_document> for details.
114              
115             =cut
116              
117             sub inject_document
118             {
119 0     0 1   my $proto = shift;
120 0 0         my $self = (ref $proto) ? $proto : $proto->new;
121 0           return $self->inject_document(@_);
122             }
123              
124             =item C<< $gen->nodes($model) >>
125              
126             Provides triple-laden XML::LibXML::Elements to be added to a document.
127             $model is an RDF::Trine::Model object providing the triples. If called in
128             list context, returns a list of XML::LibXML::Element objects which can be
129             added to a document; otherwise returns an XML::LibXML::NodeList containing
130             a list of such elements.
131              
132             Can also be called as a class method. See C<create_document> for details.
133              
134             The HTML::Pretty generator can be passed a couple of additional options:
135              
136             $gen->nodes($model, notes_heading=>'Additional Info', notes=>\@notes);
137              
138             The notes are a list of RDF::RDFa::Generator::HTML::Pretty::Note objects
139             which are added as notes to the end of each subject's data.
140              
141             =cut
142              
143             sub nodes
144             {
145 0     0 1   my $proto = shift;
146 0 0         my $self = (ref $proto) ? $proto : $proto->new;
147 0           return $self->nodes(@_);
148             }
149              
150             =back
151              
152             =head1 UPGRADING TO 0.200
153              
154             The recommended upgrade path is to migrate your application to use
155             L<Attean> rather than L<RDF::Trine> as your RDF library. If that is
156             not an option, you may continue to use L<RDF::Trine>, by using a
157             compatibility layer. If you are using this module directly, to
158             upgrade from earlier releases, you would simply add
159              
160             use RDF::TrineX::Compatibility::Attean;
161              
162             alongside the import of this module. It is in a separate distribution
163             that needs to be installed. If you use the L<RDF::Trine::Serializer>
164             methods, you should instead use L<RDF::Trine::Serializer::RDFa>.
165              
166             =head1 NOTE
167              
168             Version 0.200 introduced a large number of changes to be compatible
169             with both L<Attean> and L<RDF::Trine>. Some of these were
170             backwards-incompatible, some were to support new features, such as the
171             use of L<URI::NamespaceMap>.
172              
173             =head2 Backwards-incompatible changes
174              
175             The methods C<serialize_model_to_file>, C<serialize_model_to_string>,
176             C<serialize_iterator_to_file> and C<serialize_iterator_to_string> that
177             were provided for compatibility with the L<RDF::Trine::Serializer>
178             interface have been moved to a module L<RDF::Trine::Serializer::RDFa>
179             that has to be installed separately to use this with L<RDF::Trine>.
180              
181             C<data_context> previously accepted a plain-text string URI. Now, it
182             requires an appropriate object, as documented.
183              
184             Since RDF 1.1 abandons untyped literals, this module also ceases to
185             emit them.
186              
187             =head2 Namespace mappings
188              
189             The way namespace mappings are handled have been rewritten. Now, the
190             preferred method to add them is to pass an L<URI::NamespaceMap> object
191             to C<namespacemap>. This will override any other options.
192              
193             The namespace mappings for the following prefixes will always be
194             added: C<rdfa>, C<rdf>, C<rdfs> and C<xsd>.
195              
196             If L<URI::NamespaceMap> is not used, but C<namespaces> is given as a
197             hashref of prefix-URI pairs, the pairs will be added. If neither are
198             given, all mappings from L<RDF::NS::Curated>, which includes all if
199             RDFa Initial Context will be added. Finally, any pairs from the
200             deprecated C<ns> option will be added, but a warning will be emitted.
201              
202             =cut
203              
204             sub serialize_model_to_string {
205 0     0 0   croak 'serialize_model_to_string have been to moved RDF::Trine::Serializer::RDFa';
206             }
207              
208             sub serialize_model_to_file {
209 0     0 0   croak 'serialize_model_to_file have been to moved RDF::Trine::Serializer::RDFa';
210             }
211              
212             sub serialize_iterator_to_string {
213 0     0 0   croak 'serialize_iterator_to_string have been to moved RDF::Trine::Serializer::RDFa';
214             }
215              
216             sub serialize_iterator_to_file {
217 0     0 0   croak 'serialize_iterator_to_string have been to moved RDF::Trine::Serializer::RDFa';
218             }
219              
220             1;
221              
222             __END__
223              
224             =head1 BUGS
225              
226             Please report any bugs to L<https://github.com/kjetilk/p5-rdf-rdfa-generator/issues>.
227              
228             =head1 SEE ALSO
229              
230             You may want to use the framework-specific frontends: L<RDF::Trine::Serializer::RDFa> or L<AtteanX::Serializer::RDFa>.
231              
232             Other relevant modules:
233              
234             L<HTML::HTML5::Writer>, L<XML::LibXML>, L<RDF::RDFa::Parser>, L<RDF::Trine>,
235             L<URI::NamespaceMap>, L<Attean>.
236              
237             =head1 AUTHOR
238              
239             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
240              
241             Kjetil Kjernsmo E<lt>kjetilk@cpan.orgE<gt>.
242              
243             =head1 COPYRIGHT AND LICENCE
244              
245             Copyright (C) 2010 by Toby Inkster, 2017, 2018 Kjetil Kjernsmo
246              
247             This library is free software; you can redistribute it and/or modify
248             it under the same terms as Perl itself, either Perl version 5.8 or,
249             at your option, any later version of Perl 5 you may have available.
250              
251             =head2 Icons
252              
253             RDF::RDFa::Generator::HTML::Pretty uses the FamFamFam Silk icons;
254             see L<http://famfamfam.com/lab/icons/silk/>.
255              
256             =cut
257