cleverdevil / microformats2

Validate Microformats2 JSON with JSON Schema, and perform Post Type Discovery

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

microformats2

A Python package for validating JSON-encoded Microformats2 using JSON Schema.

Usage

Validating MF2 JSON:

import microformats2

mf2 = { 
    "type": ["h-event"],
    "properties": {
        "name": ["IndieWebCamp 2012"],
        "url": ["http://indiewebcamp.com/2012"],
        "start": ["2012-06-30"],
        "end": ["2012-07-01"],
        "location": [{
            "value": "Geoloqi",
            "type": ["h-card"],
            "properties": {
                "name": ["Geoloqi"],
                "org": ["Geoloqi"],
                "url": ["http://geoloqi.com/"],
                "street-address": ["920 SW 3rd Ave. Suite 400"],
                "locality": ["Portland"],
                "region": ["Oregon"]
            }
        }]
    }
}

microformats2.validate(mf2)

Get the schema for a particular microformat:

import microformats2
import json

print(json.dumps(microformats2.schema_for('h-entry'), indent=2))

For those of you not using Python, who just want a raw JSON schema file to use, refer to schema.json which was generated using this command:

python tools/generate.py > schema.json

Determine the post type for a MF2 JSON entry, using the Post Type Discovery guidelines from the W3C.

import microformats2

mf2 = {
    "type": [
        "h-entry"
    ],
    "properties": {
        "name": [
            "Microformats are amazing"
        ],
        "author": [
            {
                "value": "W. Developer",
                "type": [
                    "h-card"
                ],
                "properties": {
                    "name": [
                        "W. Developer"
                    ],
                    "url": [
                        "http://example.com"
                    ]
                }
            }
        ],
        "published": [
            "2013-06-13 12:00:00"
        ],
        "summary": [
            "In which I extoll the virtues of using microformats."
        ],
        "content": [
            {
                "value": "Blah blah blah",
                "html": "<p>Blah blah blah</p>"
            }
        ]
    }
}

assert microformats2.get_post_type(mf2) == microformats2.PostTypes.article

Convert MF2 JSON to JF2:

jf2 = microformats2.to_jf2(mf2)
assert jf2 == {
    "type": "entry",
    "name": "Microformats are amazing",
    "author": {
        "type": "card",
        "name": "W. Developer",
        "url": "http://example.com"
    },
    "published": "2013-06-13 12:00:00",
    "summary": "In which I extoll the virtues of using microformats.",
    "content": "Blah blah blah"
}

About

Validate Microformats2 JSON with JSON Schema, and perform Post Type Discovery


Languages

Language:Python 100.0%