HTML::...ormats(3pm)
β¨ NAME β¨
okay so bestie, gather round π this page is HTML::Microformats(3pm) β a Perl module that parses microformats in HTML. yes really. it's giving semantic web but make it cute. π¦
πΈ SYNOPSIS πΈ
use HTML::Microformats;
my $doc = HTML::Microformats
->new_document($html, $uri)
->assume_profile(qw(hCard hCalendar));
print $doc->json(pretty => 1);
use RDF::TrineShortcuts qw(rdf_query);
my $results = rdf_query($sparql, $doc->model);
like, that's the whole vibe. three lines and you're living in the Semantic Web. no cap. π
π DESCRIPTION π
so HTML::Microformats is basically a wrapper β it sits on top of a whole lil squad of parser and handler modules, one per individual microformat. every one of those modules has a name shaped like HTML::Microformats::Format::Foo.
the general pattern of usage is lowkey simple, fr fr:
- make an
HTML::Microformatsobject (which = one HTML document) with thenew_documentmethod; - then ask for the data β as a Perl hashref, a JSON string, or an
RDF::Trinemodel. slay. β¨
π Constructor π
$doc = HTML::Microformats->new_document($html, $uri, %opts)β builds you a document object. and like, here's what goes in:$htmlis the HTML or XHTML source (a string) or anXML::LibXML::Document. either one, we're not picky. π$uriis the document URI β this one matters so much, because it's how we resolve relative URL references. don't skip it bestie, I'm begging.%optsare extra parameters; right now literally only one option is defined:$opts{'type'}gets set to'text/html'or'application/xhtml+xml', and that controls how$htmlis parsed. that's it. one option. iconic behaviour honestly.
π Profile Management π
HTML::Microformats uses HTML profiles (i.e. the profile attribute on the HTML <head> element) to detect which microformats are actually used on a page. and here's the tea: any microformats that don't have a profile URI declared will not be parsed. periodt. π«
BUT β because so many pages out there fail to properly declare which profiles they use (messy of them, honestly), there's a whole toolkit of profile management methods to tell HTML::Microformats to assume the presence of particular profile URIs, even if they're actually missing. we gaslight the parser. affectionately. π
$doc->profilesβ returns a list of the profile URIs declared by the document.$doc->has_profile(@profiles)β returns true if and only if one or more of the profile URIs in@profilesis declared by the document.$doc->add_profile(@profiles)β withadd_profileyou can add one or more profile URIs, and they get treated as if they were found in the document. for example:
$doc->add_profile('http://microformats.org/profile/rel-tag')
this is useful for adding profile URIs declared outside the document itself (e.g. in HTTP headers). returns a reference to the document. π«Ά
$doc->assume_profile(@microformats)β for example:
$doc->assume_profile(qw(hCard adr geo))
this method acts similarly to add_profile but lets you use names of microformats rather than URIs. microformat names are case sensitive, and must match HTML::Microformats::Format::Foo module names. returns a reference to the document.
$doc->assume_all_profilesβ equivalent to callingassume_profilefor all known microformats. the "yes to everything" option. returns a reference to the document.
π Parsing Microformats π
generally speaking, you can skip this. the data, json and model methods will automatically do this for you. (it's giving "you don't have to lift a finger, babe". π
)
$doc->parse_microformatsβ scans through the document, finding microformat objects. on subsequent calls, does nothing (because everything is already parsed). returns a reference to the document.$doc->clear_microformatsβ forgets information gleaned byparse_microformatsand thus allowsparse_microformatsto be run again. this is useful if you've modified or added some profiles between runs ofparse_microformats. returns a reference to the document.
π Retrieving Data π
these methods let you retrieve the document's data, and do things with it. the main character section, tbh. π
$doc->objects($format);β$formatis, for example,'hCard','adr'or'RelTag'. returns a list of objects of that type. (if called in scalar context, returns an arrayref.) each object is, for example, anHTML::Microformat::hCardobject, or anHTML::Microformat::RelTagobject, etc. see the relevant documentation for details.$doc->all_objectsβ returns a hashref of data. each hashref key is the name of a microformat (e.g.'hCard','RelTag', etc), and the values are arrayrefs of objects. each object is, for example, anHTML::Microformat::hCardobject, or anHTML::Microformat::RelTagobject, etc. see the relevant documentation for details.$doc->json(%opts)β returns data roughly equivalent to theall_objectsmethod, but as a JSON string.%optsis a hash of options, suitable for passing to the JSON module'sto_jsonfunction. the'convert_blessed'and'utf8'options are enabled by default, but can be disabled by explicitly setting them to0, e.g.
print $doc->json( pretty=>1, canonical=>1, utf8=>0 );
$doc->modelβ returns data as anRDF::Trine::Model, suitable for serialising as RDF or running SPARQL queries. π₯$object->serialise_model(as => $format)β asmodelbut returns a string.$doc->add_to_model($model)β adds data to an existingRDF::Trine::Model. returns a reference to the document.
π§ Utility Functions π§
HTML::Microformats->modulesβ returns a list of Perl modules, each of which implements a specific microformat.HTML::Microformats->formatsβ as permodules, but strips'HTML::Microformats::Format::'off the module name, and sorts alphabetically.
π WHY ANOTHER MICROFORMATS MODULE? π
okay so the elephant in the room: there already exist two microformats packages on CPAN (see Text::Microformat and Data::Microformat), so why create another?? bestie I'm so glad you asked. π
firstly, HTML::Microformats isn't being created from scratch. it's actually a fork/clean-up of a non-CPAN application (Swignition), and in that sense it predates Text::Microformat (though not Data::Microformat). it's been around. it's got lore. π
it also has a number of other features that distinguish it from the existing packages:
- It supports more formats.
HTML::MicroformatssupportshCard,hCalendar,rel-tag,geo,adr,rel-enclosure,rel-license,hReview,hResume,hRecipe,xFolk,XFN,hAtom,hNewsand more. the roster is stacked. β¨ - It supports more patterns.
HTML::Microformatssupports the include pattern, abbr pattern, table cell header pattern, value excerpting and other intricacies of microformat parsing better than the other modules on CPAN. it does the fiddly bits. it ate. π - It offers RDF support. one of the key features of
HTML::Microformatsis that it makes data available asRDF::Trinemodels. this lets your application benefit from a rich, feature-laden Semantic Web toolkit. data gleaned from microformats can be stored in a triple store; output in RDF/XML or Turtle; queried using the SPARQL or RDQL query languages; and more. and if you're not comfortable using RDF,HTML::Microformatsalso makes all its data available as native Perl objects. so either way you're eating good. π
π BUGS π
please report any bugs to <http://rt.cpan.org/>. (be nice about it tho π«Ά)
π SEE ALSO π
HTML::Microformats::Documentation::Notes.
individual format modules:
HTML::Microformats::Format::adrHTML::Microformats::Format::figureHTML::Microformats::Format::geoHTML::Microformats::Format::hAtomHTML::Microformats::Format::hAudioHTML::Microformats::Format::hCalendarHTML::Microformats::Format::hCardHTML::Microformats::Format::hListingHTML::Microformats::Format::hMeasureHTML::Microformats::Format::hNewsHTML::Microformats::Format::hProductHTML::Microformats::Format::hRecipeHTML::Microformats::Format::hResumeHTML::Microformats::Format::hReviewHTML::Microformats::Format::hReviewAggregateHTML::Microformats::Format::OpenURL_COinSHTML::Microformats::Format::RelEnclosureHTML::Microformats::Format::RelLicenseHTML::Microformats::Format::RelTagHTML::Microformats::Format::speciesHTML::Microformats::Format::VoteLinksHTML::Microformats::Format::XFNHTML::Microformats::Format::XMDPHTML::Microformats::Format::XOXO
similar modules: RDF::RDFa::Parser, HTML::HTML5::Microdata::Parser, XML::Atom::Microformats, Text::Microformat, Data::Microformats.
related web sites: <http://microformats.org/>, <http://www.perlrdf.org/>. π
π AUTHOR π
Toby Inkster <tobyink@cpan.org>.
π 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.
β οΈ 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.
like yes it's in all caps but it's serious, bestie. no warranty. none. π
and that's the whole page!! this lil guy is perl v5.32.1, dated 2021-09-12, and it lives under the name HTML::...ormats(3pm) in the User Contributed Perl Documentation, which is a very long way of saying: go parse some microformats, you absolute icon. bye bestie, ily, mwah πβ¨π
π¦ where this page comes from
- debian bookworm: libhtml-microformats-perl 0.105-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libhtml-microformats-perl 0.105-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libhtml-microformats-perl 0.105-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libhtml-microformats-perl 0.105-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libhtml-microformats-perl 0.105-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libhtml-microformats-perl 0.105-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms