microformats2 parsing specification

(Difference between revisions)

Jump to: navigation, search
(parse an element for class microformats: make it explicit that e-* h-* re-uses the e-* { value:"" } structure (insetad of creating another level of { } structure) - per question/point from kylewm)
(microformats2-parsing-brainstorming#more_information_for_alternates part 1, add rel-urls key, update parsed rels example)
Line 12: Line 12:
=== parse a document for microformats ===
=== parse a document for microformats ===
To parse a document for microformats, follow the HTML parsing rules and do the following:
To parse a document for microformats, follow the HTML parsing rules and do the following:
-
* start with an empty JSON "items" array and "rels" hash:  
+
* start with an empty JSON "items" array and "rels" & "rel-urls" hashes:  
<source lang=javascript>
<source lang=javascript>
{
{
  "items": [],
  "items": [],
-
  "rels": {}
+
  "rels": {},
 +
"rel-urls": {}
}
}
</source>
</source>
* parse the root element for class microformats, adding to the JSON items array accordingly
* parse the root element for class microformats, adding to the JSON items array accordingly
-
* parse all hyperlink (<code>&lt;link> &lt;a></code>) elements for rel microformats, adding to the JSON rels hash accordingly
+
* parse all hyperlink (<code>&lt;link> &lt;a></code>) elements for rel microformats, adding to the JSON rels & rel-urls hashes accordingly
* return the resulting JSON
* return the resulting JSON
Parsers may simultaneously parse the document for both class and rel microformats (e.g. in a single tree traversal).
Parsers may simultaneously parse the document for both class and rel microformats (e.g. in a single tree traversal).
Line 131: Line 132:
*** "hreflang": the value of the "hreflang" attribute
*** "hreflang": the value of the "hreflang" attribute
*** "type": the value of the "type" attribute
*** "type": the value of the "type" attribute
 +
*** "title": the value of the "title" attribute
 +
*** "text": the text content of the element if any
* end if
* end if
-
 
-
Proposed: [[microformats2-parsing-brainstorming#more_information_for_alternates|more information for alternates]]
 
-
* In addition to media, hreflang, type etc. add the following when parsing alternates:
 
-
** "title": the value of the "title" attribute, as seen in link alternates for feeds, for example
 
-
** "textContent": the text content of the element if any, e.g. as seen in the example below <blockquote><code>"textContent": "French mobile homepage"</code></blockquote>
 
Line 162: Line 160:
     "in-reply-to": [ "http://example.com/1", "http://example.com/2" ]  
     "in-reply-to": [ "http://example.com/1", "http://example.com/2" ]  
   },
   },
 +
  "rel-urls": {
 +
    "http://example.com/a": {
 +
      "rels": ["author"],
 +
      "text": "author a"
 +
    },
 +
    "http://example.com/b": {
 +
      "rels": ["author"],
 +
      "text": "author b"
 +
    },
 +
    "http://example.com/1": {
 +
      "rels": ["in-reply-to"],
 +
      "text": "post 1"
 +
    },
 +
    "http://example.com/2": {
 +
      "rels": ["in-reply-to"],
 +
      "text": "post 2"
 +
    },
 +
    "http://example.com/fr": {
 +
      "rels": ["alternate", "home"],
 +
      "media": "handheld",
 +
      "hreflang": "fr",
 +
      "text": "French mobile homepage"
 +
    }
 +
  }
   "alternates": [{
   "alternates": [{
     "url": "http://example.com/fr",  
     "url": "http://example.com/fr",  
     "rel": "home",  
     "rel": "home",  
     "media": "handheld",  
     "media": "handheld",  
-
     "hreflang": "fr"  
+
     "hreflang": "fr",
 +
    "text": "French mobile homepage"
   }]
   }]
}
}

Revision as of 04:34, 29 May 2015


Tantek Çelik (Editor)


microformats2 is a simple, open format for marking up data in HTML. The microformats2 parsing specification describes how to implement a microformats2 parser.

One of the goals of microformats2 is to greatly simplify parsing of microformats, in particular, by making parsing independent of any one vocabulary. This specification documents the microformats2 parsing algorithm for doing so.

Per CC0, to the extent possible under law, the editors have waived all copyright and related or neighboring rights to this work. In addition, as of 2017-11-20, the editors have made this specification available under the Open Web Foundation Agreement Version 1.0.

Contents

algorithm

parse a document for microformats

To parse a document for microformats, follow the HTML parsing rules and do the following:

{
 "items": [],
 "rels": {},
 "rel-urls": {}
}

Parsers may simultaneously parse the document for both class and rel microformats (e.g. in a single tree traversal).

parse an element for class microformats

To parse an element for class microformats:

parse an element for properties

parsing a p- property

To parse an element for a p-x property value:

parsing a u- property

To parse an element for a u-x property value:

parsing a dt- property

To parse an element for a dt-x property value:

parsing an e- property

To parse an element for a e-x property value:

parsing for implied properties

To imply properties: (where h-x is the root microformat element being parsed)

Note: The same markup for a property should not be causing that property to occur in both a microformat and one embedded inside - such a property should only be showing up on one of them. The parsing algorithm has details to prevent that, such as the :not[.h-*] tests above.

parse a hyperlink element for rel microformats

To parse a hyperlink element for rel microformats: (where * is the hyperlink element)


rel parse examples

Here are some examples to show how parsed rels may be reflected into the JSON (empty items key).

E.g. parsing this markup:

<a rel="author" href="http://example.com/a">author a</a>
<a rel="author" href="http://example.com/b">author b</a>
<a rel="in-reply-to" href="http://example.com/1">post 1</a>
<a rel="in-reply-to" href="http://example.com/2">post 2</a>
<a rel="alternate home"
   href="http://example.com/fr"
   media="handheld"
   hreflang="fr">French mobile homepage</a>

Would generate this JSON:

{
  "items": [],
  "rels": { 
    "author": [ "http://example.com/a", "http://example.com/b" ],
    "in-reply-to": [ "http://example.com/1", "http://example.com/2" ] 
  },
  "rel-urls": {
    "http://example.com/a": {
      "rels": ["author"], 
      "text": "author a"
    },
    "http://example.com/b": {
      "rels": ["author"], 
      "text": "author b"
    },
    "http://example.com/1": {
      "rels": ["in-reply-to"], 
      "text": "post 1"
    },
    "http://example.com/2": {
      "rels": ["in-reply-to"], 
      "text": "post 2"
    },
    "http://example.com/fr": {
      "rels": ["alternate", "home"],
      "media": "handheld", 
      "hreflang": "fr", 
      "text": "French mobile homepage"
    }
  }
  "alternates": [{
     "url": "http://example.com/fr", 
     "rel": "home", 
     "media": "handheld", 
     "hreflang": "fr",
     "text": "French mobile homepage"
  }]
}

Another parse output example can be found here:

what do the CSS selector expressions mean

This section is non-normative.

Use SelectORacle to expand any of the above CSS selector expressions into longform English prose.

Exception:

note HTML parsing rules

This section is non-normative.

microformats2 parsers are expected to follow HTML parsing rules, which includes for example:

questions

See the FAQ:

issues

See the issues page:

implementations

Main article: microformats2#Implementations

There are open source microformats2 parsers available for Javascript, node.js, PHP, Ruby and Python.

test suite

See:

Ports to/for other languages encouraged.

see also

Categories

microformats2 parsing specification was last modified: Wednesday, December 31st, 1969

Views