File Coverage

blib/lib/AtteanX/Serializer/RDFa.pm
Criterion Covered Total %
statement 48 48 100.0
branch 2 2 100.0
condition n/a
subroutine 16 16 100.0
pod 2 2 100.0
total 68 68 100.0


line stmt bran cond sub pod time code
1             package AtteanX::Serializer::RDFa;
2              
3 3     3   4021787 use 5.010001;
  3         22  
4 3     3   17 use strict;
  3         6  
  3         64  
5 3     3   14 use warnings;
  3         4  
  3         166  
6              
7             our $AUTHORITY = 'cpan:KJETILK';
8             our $VERSION = '0.110';
9              
10 3     3   657 use Moo;
  3         12437  
  3         33  
11 3     3   3025 use Types::Standard qw(Str Maybe HashRef ConsumerOf);
  3         76892  
  3         31  
12 3     3   3667 use Encode qw(encode);
  3         15368  
  3         177  
13 3     3   21 use Scalar::Util qw(blessed);
  3         6  
  3         127  
14 3     3   572 use Attean;
  3         1998397  
  3         21  
15 3     3   110 use Attean::ListIterator;
  3         9  
  3         64  
16 3     3   14 use namespace::clean;
  3         6  
  3         26  
17 3     3   1081 use Attean::RDF qw(iri);
  3         7  
  3         28  
18 3     3   2558 use RDF::RDFa::Generator;
  3         141192  
  3         1360  
19              
20              
21             has 'canonical_media_type' => (is => 'ro', isa => Str, init_arg => undef, default => 'application/xhtml+xml');
22              
23             with 'Attean::API::TripleSerializer';
24             with 'Attean::API::AbbreviatingSerializer';
25              
26             has 'style' => (is => 'ro', isa => Maybe[Str]); # TODO: might be improved with OptList?
27              
28             has 'generator_options' => (is => 'ro', isa => HashRef, default => sub { return {} });
29              
30             has _opts => (is => 'rw', isa => HashRef, lazy => 1, builder => '_build_opts');
31              
32             sub _build_opts {
33 7     7   87 my $self = shift;
34 7 100       134 my $base = defined($self->base) ? $self->base->abs : undef;
35 7         1591 my %opts = (
36             style => $self->style,
37             namespacemap => $self->namespaces,
38             base => $base
39             );
40 7         113 return \%opts;
41             }
42              
43              
44             sub media_types {
45 1     1 1 688 return [qw(application/xhtml+xml text/html)];
46             }
47              
48 1     1 1 6459 sub file_extensions { return [qw(html xhtml)] };
49              
50             sub _make_document {
51 7     7   23 my ($self, $iter) = @_;
52 7         56 my $store = Attean->get_store('Memory')->new();
53 7         16185 $store->add_iter($iter->as_quads(iri('http://graph.invalid/')));
54 7         95320 my $model = Attean::QuadModel->new( store => $store );
55 7         2592 return RDF::RDFa::Generator->new(%{$self->_opts})->create_document($model, %{$self->generator_options});
  7         137  
  7         103052  
56             }
57              
58             sub serialize_iter_to_io {
59             my ($self, $io, $iter) = @_;
60             my $document = $self->_make_document($iter);
61             return $document->toFH($io);
62              
63             }
64              
65             sub serialize_iter_to_bytes {
66             my ($self, $iter) = @_;
67             my $document = $self->_make_document($iter);
68             return $document->toString;
69             }
70              
71             1;
72              
73             __END__
74              
75             =pod
76              
77             =encoding utf-8
78              
79             =head1 NAME
80              
81             AtteanX::Serializer::RDFa - RDFa Serializer for Attean
82              
83             =head1 SYNOPSIS
84              
85             use Attean;
86             use Attean::RDF qw(iri);
87             use URI::NamespaceMap;
88              
89             my $ser = Attean->get_serializer('RDFa')->new;
90             my $string = $ser->serialize_iter_to_bytes($iter);
91              
92             my $ns = URI::NamespaceMap->new( { ex => iri('http://example.org/') });
93             $ns->guess_and_add('foaf');
94             my $note = RDF::RDFa::Generator::HTML::Pretty::Note->new(iri('http://example.org/foo'), 'This is a Note');
95             my $ser = Attean->get_serializer('RDFa')->new(base => iri('http://example.org/'),
96             namespaces => $ns,
97             style => 'HTML::Pretty',
98             generator_options => { notes => [$note]});
99             $ser->serialize_iter_to_io($fh, $iter);
100              
101              
102              
103             =head1 DESCRIPTION
104              
105             This module can be used to serialize RDFa with several different
106             styles. It is implemented using L<Attean> to wrap around
107             L<RDF::RDFa::Generator>, which does the heavy lifting. It composes
108             L<Attean::API::TripleSerializer> and
109             L<Attean::API::AbbreviatingSerializer>.
110              
111             =head1 METHODS AND ATTRIBUTES
112              
113             =head2 Attributes
114              
115              
116             In addition to attributes required by L<Attean::API::TripleSerializer>
117             that should not be a concern to users, the following attributes can be
118             set:
119              
120             =over
121              
122              
123             =item C<< style >>
124              
125             This attribute sets the serialization style used by
126             L<RDF::RDFa::Generator>, see its documentation for details.
127              
128             =item C<< namespaces >>
129              
130             A HASH reference mapping prefix strings to L<URI::NamespaceMap>
131             objects. L<RDF::RDFa::Generator> will help manage this map, see its
132             documentation for details.
133              
134             =item C<< base >>
135              
136             An L<Attean::API::IRI> object representing the base against which
137             relative IRIs in the serialized data should be resolved. There is some
138             support in L<RDF::RDFa::Generator>, but currently, it doesn't do much.
139              
140             =item C<< generator_options >>
141              
142             A HASH reference that will be passed as options to
143             L<RDF::RDFa::Generator>'s C<create_document> method. This is typically
144             options that are specific to different styles, see synopsis for
145             example.
146              
147             =back
148              
149             =head2 Methods
150              
151             This implements four required methods:
152              
153             =over
154              
155             =item C<< serialize_iter_to_io( $fh, $iterator ) >>
156              
157             Serializes the elements from the L<Attean::API::Iterator> C<< $iterator >> to
158             the L<IO::Handle> object C<< $fh >>.
159              
160             =item C<< serialize_iter_to_bytes( $fh ) >>
161              
162             Serializes the elements from the L<Attean::API::Iterator> C<< $iterator >>
163             and returns the serialization as a UTF-8 encoded byte string.
164              
165             =item C<< media_types >> and C<< file_extensions >>
166              
167             Declares that HTML media types are used for the output of this module.
168              
169             =back
170              
171             =head1 BUGS
172              
173             Please report any bugs to
174             L<https://github.com/kjetilk/p5-atteanx-serializer-rdfa/issues>.
175              
176             =head1 SEE ALSO
177              
178             L<RDF::RDFa::Generator>, L<RDF::Trine::Serializer::RDFa>.
179              
180             =head1 TODO
181              
182             =over
183              
184             =item * The C<style> attribute may be implemented with better constraints.
185              
186             =item * Make the writers (i.e. the code actually writing the DOM) configurable.
187              
188             =back
189              
190             =head1 AUTHOR
191              
192             Kjetil Kjernsmo E<lt>kjetilk@cpan.orgE<gt>.
193              
194             =head1 COPYRIGHT AND LICENCE
195              
196             This software is copyright (c) 2017, 2018, 2019, 2021 by Kjetil Kjernsmo.
197              
198             This is free software; you can redistribute it and/or modify it under
199             the same terms as the Perl 5 programming language system itself.
200              
201              
202             =head1 DISCLAIMER OF WARRANTIES
203              
204             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
205             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
206             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
207