File: XMDP.pm

package info (click to toggle)
libhtml-microformats-perl 0.105-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,324 kB
  • ctags: 1,019
  • sloc: perl: 14,121; makefile: 25
file content (194 lines) | stat: -rw-r--r-- 4,346 bytes parent folder | download | duplicates (4)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
=head1 NAME

HTML::Microformats::Format::XMDP - the XMDP microformat

=head1 SYNOPSIS

 use HTML::Microformats;
 use LWP::Simple qw[get];
 use RDF::TrineShortcuts;
 
 my $uri  = 'http://microformats.org/profile/hcard';
 my $html = get($uri);
 my $doc  = HTML::Microformats->new_document($html, $uri);
 $doc->assume_all_profiles;
 
 my @xmdp_objects = $doc->objects('XMDP');
 foreach my $xo (@xmdp_objects)
 {
   print $xo->serialise_model(
       as         => 'Turtle',
       namespaces => {
           rdfs  => 'http://www.w3.org/2000/01/rdf-schema#',
           hcard => 'http://microformats.org/profile/hcard#',
           },
       );
   print "########\n\n";
 }

=head1 DESCRIPTION

HTML::Microformats::Format::XMDP inherits from HTML::Microformats::Format. See the
base class definition for a description of property getter/setter methods,
constructors, etc.

HTML::Microformats::Format::XMDP also inherits from HTML::Microformats::Format::XOXO, and
the C<data> method returns the same structure.

=cut

package HTML::Microformats::Format::XMDP;

use base qw(HTML::Microformats::Format::XOXO);
use strict qw(subs vars); no warnings;
use 5.010;

use Object::AUTHORITY;

BEGIN {
	$HTML::Microformats::Format::XMDP::AUTHORITY = 'cpan:TOBYINK';
	$HTML::Microformats::Format::XMDP::VERSION   = '0.105';
}

sub new
{
	my $class = shift;
	my $self  = $class->SUPER::new(@_);
	
	return $self;
}

sub format_signature
{
	return {
		'root'         => ['profile'] ,
		'classes'      => [] ,
		'rdf:type'     => [] ,
		'rdf:property' => {} ,
		}
}

sub profiles
{
	return qw(http://gmpg.org/xmdp/1);
}

sub add_to_model
{
	my $self  = shift;
	my $model = shift;
	
	$self->SUPER::add_to_model($model);

	while (my ($term, $data) = each %{ $self->data })
	{
		$self->_add_term_to_model($model, $term, $data);
	}
	
	return $self;
}

sub _add_term_to_model
{
	my ($self, $model, $term, $data) = @_;
	
	my $rdfs  = 'http://www.w3.org/2000/01/rdf-schema#';
	
	my $ident = RDF::Trine::Node::Blank->new(
		substr($self->context->make_bnode, 2));
	if (defined $data->{'id'})
	{
		$ident = RDF::Trine::Node::Resource->new(
			$self->context->uri('#'.$data->{'id'}));
	}
	
	$model->add_statement(RDF::Trine::Statement->new(
		$ident,
		RDF::Trine::Node::Resource->new("${rdfs}label"),
		$self->_make_literal($term),
		));

	$model->add_statement(RDF::Trine::Statement->new(
		$ident,
		RDF::Trine::Node::Resource->new("${rdfs}isDefinedBy"),
		$self->id(1),
		));

	foreach my $item (@{$data->{'items'}})
	{
		$model->add_statement(RDF::Trine::Statement->new(
			$ident,
			RDF::Trine::Node::Resource->new("${rdfs}comment"),
			$self->_make_literal($item->{'text'}),
			))
			if defined $item->{'text'};

		if ($item->{'rel'} =~ /^(help|glossary)$/ && defined $item->{'url'})
		{
			$model->add_statement(RDF::Trine::Statement->new(
				$ident,
				RDF::Trine::Node::Resource->new("http://www.w3.org/1999/xhtml/vocab#".lc $1),
				RDF::Trine::Node::Resource->new($item->{'url'}),
				));
		}
		
		while (my ($child_term, $child_data) = each %{ $item->{'properties'} })
		{
			my $child_ident = $self->_add_term_to_model($model, $child_term, $child_data);
			$model->add_statement(RDF::Trine::Statement->new(
				$ident,
				RDF::Trine::Node::Resource->new("${rdfs}seeAlso"),
				$child_ident,
				));
		}
	}
		
	return $ident;
}

1;

=head1 MICROFORMAT

HTML::Microformats::Format::XMDP supports XMDP as described at
L<http://gmpg.org/xmdp/>.

=head1 RDF OUTPUT

Data is returned using RDFS.

=head1 BUGS

A limitation is that for any E<lt>ddE<gt> element with
E<lt>dlE<gt> children, only the first such E<lt>dlE<gt>
is looked at. This means that the XFN 1.1 profile document
is only partially parsable; most other microformat profile
document can be properly parsed though.

Please report any bugs to L<http://rt.cpan.org/>.

=head1 SEE ALSO

L<HTML::Microformats::Format>,
L<HTML::Microformats>.

=head1 AUTHOR

Toby Inkster E<lt>tobyink@cpan.orgE<gt>.

=head1 COPYRIGHT AND LICENCE

Copyright 2008-2012 Toby Inkster

This library is free software; you can redistribute it and/or modify it
under the same terms as Perl 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.


=cut