Skip to content

adjunct.ogp

An intensely pragmatic implementation of the Open Graph Protocol.

This follows the specification rather than attempting to implement RDFa.

Theory of Operation

The metadata consists of a list of properties. A property may be unstructured (a string) or structured (a list of properties). A property may appear more than once. Properties has the form "ns:name", while a metadata field to be attached to a property (thus making it structured) has the form "ns:name:meta".

Metadata should be implemented as a multimap, but I'll be treating it as a map.

Example of prefix with multiple entries:

<body prefix="dc: http://purl.org/dc/terms/ schema: http://schema.org/">

If this is present: vocab="http://www.w3.org/2011/rdfa-context/rdfa-1.1", an initial context is set up consisting of the namespaces given on that page.

Property dataclass

Open Graph Property.

Attributes:

Name Type Description
type_ str

The property type (e.g., "title", "image", etc.).

value str

The property value.

metadata dict[str, str]

A mapping of metadata keys to values.

find(props, type_, value=None)

Find Open Graph properties by type and optional value.

Parameters:

Name Type Description Default
props Sequence[Property]

A sequence of Property instances to search.

required
type_ str

The property type to search for.

required
value str | None

An optional property value to match.

None

Returns:

Type Description
Iterable[Property]

An iterable of Property instances that match the specified type and value.

parse(properties)

Parse Open Graph properties from a list of name-value pairs.

Parameters:

Name Type Description Default
properties Collection[tuple[str, str]]

A collection of (name, value) pairs representing Open Graph properties.

required

Returns:

Type Description
list[Property]

The Property instances.

to_meta(props)

Convert Open Graph properties to HTML meta tags.

Parameters:

Name Type Description Default
props Property | Sequence[Property]

A single Property instance or a sequence of Property instances.

required

Returns:

Type Description
str

A string containing HTML meta tags representing the Open Graph properties.