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
|
package RDF::Trine::Serializer::RDFa;
use 5.010001;
use strict;
use warnings;
use base qw(RDF::Trine::Serializer);
use RDF::RDFa::Generator;
use RDF::TrineX::Compatibility::Attean;
our $AUTHORITY = 'cpan:KJETILK';
our ($VERSION);
BEGIN {
$VERSION = '0.101';
$RDF::Trine::Serializer::serializer_names{ 'rdfa' } = __PACKAGE__;
$RDF::Trine::Serializer::format_uris{ 'http://www.w3.org/ns/formats/RDFa' } = __PACKAGE__;
foreach my $type (qw(application/xhtml+xml text/html)) {
$RDF::Trine::Serializer::media_types{ $type } = __PACKAGE__;
}
}
sub new {
my ($class, %args) = @_;
my $opts = $args{generator_options};
delete $args{generator_options};
my $gen = RDF::RDFa::Generator->new(%args);
my $self = bless( { gen => $gen, opts => $opts }, $class);
return $self;
}
sub serialize_model_to_string {
my ($self, $model) = @_;
return $self->{gen}->create_document($model, %{$self->{opts}})->toString;
}
sub serialize_model_to_file {
my ($self, $fh, $model) = @_;
print {$fh} $self->{gen}->create_document($model, %{$self->{opts}})->toString;
}
sub serialize_iterator_to_string {
my ($self, $iter) = @_;
my $model = RDF::Trine::Model->temporary_model;
while (my $st = $iter->next) {
$model->add_statement($st);
}
return $self->serialize_model_to_string($model);
}
sub serialize_iterator_to_file {
my ($self, $fh, $iter) = @_;
my $model = RDF::Trine::Model->temporary_model;
while (my $st = $iter->next) {
$model->add_statement($st);
}
return $self->serialize_model_to_file($fh, $model);
}
1;
__END__
=pod
=encoding utf-8
=head1 NAME
RDF::Trine::Serializer::RDFa - RDFa Serializer for RDF::Trine
=head1 SYNOPSIS
use RDF::Trine::Serializer;
my $s = RDF::Trine::Serializer->new('RDFa', style => 'HTML::Hidden');
my $string = $s->serialize_model_to_string($model);
=head1 DESCRIPTION
The L<RDF::Trine::Serializer> class provides an API for serializing
RDF graphs to strings and files. This subclass provides RDFa
serialization via L<RDF::RDFa::Generator>.
It is intended that this module will replace the RDF::Trine
compatibility methods in L<RDF::RDFa::Generator>, which are now
deprecated. This is done to allow both L<RDF::Trine> and L<Attean> to
use it, but not require them as dependencies.
=head1 METHODS
Beyond the methods documented below, this class inherits methods from the
L<RDF::Trine::Serializer> class.
=over
=item C<< new >>
Returns a new RDFa serializer object. It can any arguments are passed
on to L<RDF::RDFa::Generator>, see it's documentation for
details. This includes a C<style> argument that names a module that
formats the output. In addition, a C<generator_options> argument may
be passed. This is passed to the generator's C<create_document>
methods as options, and are typically used for style-specific
configuration.
=item C<< serialize_model_to_file ( $fh, $model ) >>
Serializes the C<$model> to RDFa, printing the results to the supplied
filehandle C<<$fh>>.
=item C<< serialize_model_to_string ( $model ) >>
Serializes the C<$model> to RDFa, returning the result as a string.
=item C<< serialize_iterator_to_file ( $file, $iter ) >>
Serializes the iterator to RDFa, printing the results to the supplied
filehandle C<<$fh>>.
=item C<< serialize_iterator_to_string ( $iter ) >>
Serializes the iterator to RDFa, returning the result as a string.
=back
=head1 BUGS
Please report any bugs to
L<https://github.com/kjetilk/p5-rdf-trine-serializer-rdfa/issues>.
=head1 SEE ALSO
L<RDF::RDFa::Generator>, L<RDF::Trine>, L<Attean>
=head1 ACKNOWLEDGEMENTS
This module is mostly a straightforward port with substantial
cutnpaste from L<RDF::RDFa::Generator> and L<RDF::Trine> by Toby
Inkster and Gregory Todd Williams respectively.
=head1 AUTHOR
Kjetil Kjernsmo E<lt>kjetilk@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2017, 2018, 2019 by Kjetil Kjernsmo.
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.
|