Misc usage and design notes
The internals of HTML::Microformats are pretty complicated - best to steer clear of them. Here are three usage patterns that mostly avoid dealing with the internals:
Parse a page and use it as a single \s-1RDF\s0 graph. A page can be parsed into an RDF::Trine::Model and queried using \s-1SPARQL\s0.
use HTML::Microformats; use LWP::Simple qw[get]; use RDF::Query; my $page = 'http://example.net/'; my $graph = HTML::Microformats ->new_document(get($page), $page) ->assume_all_profiles ->parse_microformats ->model; my $query = RDF::Query->new(<<SPARQL); PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT DISTINCT ?friendname ?friendpage WHERE { <$page> ?p ?friendpage . ?person foaf:name ?friendname ; foaf:page ?friendpage . FILTER ( isURI(?friendpage) && isLiteral(?friendname) && regex(str(?p), "^http://vocab.sindice.com/xfn#(.+)-hyperlink") ) } SPARQL my $results = $query->execute($graph); while (my $result = $results->next) { printf("%s <%s>\n", $result->{friendname}->literal_value, $result->{friendpage}->uri, ); }
Use the data method on each object. The \*(C`data\*(C' method on microformat objects returns a hashref of useful data. use HTML::Microformats; use LWP::Simple qw[get];
my $page = 'http://example.net/'; my @xfn_objs = HTML::Microformats ->new_document(get($page), $page) ->assume_all_profiles ->parse_microformats ->objects('XFN');
while (my $xfn = shift @xfn_objs) { printf("%s <%s>\n", $xfn->data->{title}, $xfn->data->{href}, ); } (If you're wondering why the second example's simpler it's because it returns somewhat dumber data.)
Convert to other formats. Various microformat objects have \*(C`to_foo\*(C' methods allowing the data to be exported in various formats.. use HTML::Microformats; use LWP::Simple qw[get];
my $page = 'http://example.net/'; my @hcards = HTML::Microformats ->new_document(get($page), $page) ->assume_all_profiles ->parse_microformats ->objects('hCard');
print $_->to_vcard foreach @hcards; Methods available are:
\*(C`to_vcard\*(C' (hCard objects) Exports as vCard 3.0.
\*(C`to_vcard4\*(C' (hCard objects) Exports as vCard 4.0.
\*(C`to_vcard4_xml\*(C' (hCard objects) Exports as vCard \s-1XML\s0.
\*(C`to_icalendar\*(C' (hCalendar, hEvent, hTodo, hFreebusy, hAlarm and hEntry objects) Exports as iCalendar.
\*(C`to_atom\*(C' (hAtom and hEntry objects) Exports as Atom 1.0.
\*(C`to_kml\*(C' (geo objects) Exports as \s-1KML\s0 2.0.
\*(C`serialialise_model(as => $format)\*(C' (all microformat objects) Exports as \s-1RDF\s0, serialised as $format. (Format can be '\s-1RDFXML\s0', 'Turtle', 'NTriples', '\s-1RDFJSON\s0'.)
The \*(C`get_foo\*(C', \*(C`set_foo\*(C', \*(C`add_foo\*(C', \*(C`clear_foo\*(C' methods defined in HTML::Microformats::Format work unreliably and are poorly documented. You're better off using the \*(C`data\*(C' method and inspecting the returned structure for the data you need. This will be fixed in the future.
There are several parts of the code which are incredibly complicated and desperately need refactoring. This will be done at some point, so don't rely too much on their current behaviour.
\*(C`stringify\*(C' and \*(C`_stringify_helper\*(C' in HTML::Microformats::Utilities. The whole of HTML::Microformats::Mixin::Parser.
HTML::Microformats.
Toby Inkster <[email protected]>.
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.