pyRdfa.transform.OpenID

Simple transfomer: handle OpenID elements. Ie: an openid namespace is added and the usual 'link' elements for openid are exchanged against a namespaced version.

@summary: OpenID transformer module. @requires: U{RDFLib packagehttp://rdflib.net} @organization: U{World Wide Web Consortiumhttp://www.w3.org} @author: U{Ivan Herman} @license: This software is available for use under the U{W3C® SOFTWARE NOTICE AND LICENSE} @contact: Ivan Herman, ivan@w3.org @var OPENID_NS: the OpenID URI used in the package

 1# -*- coding: utf-8 -*-
 2"""
 3Simple transfomer: handle OpenID elements. Ie: an openid namespace is added and the usual
 4'link' elements for openid are exchanged against a namespaced version.
 5
 6@summary: OpenID transformer module.
 7@requires: U{RDFLib package<http://rdflib.net>}
 8@organization: U{World Wide Web Consortium<http://www.w3.org>}
 9@author: U{Ivan Herman<a href="http://www.w3.org/People/Ivan/">}
10@license: This software is available for use under the
11U{W3C® SOFTWARE NOTICE AND LICENSE<href="http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231">}
12@contact: Ivan Herman, ivan@w3.org
13@var OPENID_NS: the OpenID URI used in the package
14"""
15
16"""
17$Id: OpenID.py,v 1.4 2012-01-18 14:16:44 ivan Exp $
18$Date: 2012-01-18 14:16:44 $
19"""
20
21OPENID_NS = "http://xmlns.openid.net/auth#"
22
23
24def OpenID_transform(html, options, _state):
25    """
26    Replace C{openid.XXX} type C{@rel} attribute values in C{<link>} elements by C{openid:XXX}. The openid URI is also
27    added to the top level namespaces with the C{openid:} local name.
28
29    @param html: a DOM node for the top level html element
30    @param options: invocation options
31    @type options: L{Options<pyRdfa.options>}
32    @param state: top level execution state
33    @type state: L{State<pyRdfa.state>}
34    """
35    from ..host import HostLanguage
36    if not(options.host_language in [HostLanguage.xhtml, HostLanguage.html5, HostLanguage.xhtml5]):
37        return
38
39    # the head element is necessary; to be sure, the namespaces are set
40    # on that level only
41    head = None
42    try:
43        head = html.getElementsByTagName("head")[0]
44    except:
45        # no head....
46        return
47
48    foundOpenId = False
49    for link in html.getElementsByTagName("link"):
50        if link.hasAttribute("rel"):
51            rel = link.getAttribute("rel")
52            newProp = ""
53            for n in rel.strip().split():
54                if n.startswith("openid."):
55                    newProp += " " + n.replace("openid.", "openid:")
56                    foundOpenId = True
57                else:
58                    newProp += " " + n
59            link.setAttribute("rel",newProp.strip())
60
61    # Add the OpenId namespace if necessary
62    if foundOpenId and not head.hasAttribute("xmlns:openid"):
63        head.setAttributeNS("", "xmlns:openid", OPENID_NS)
OPENID_NS = 'http://xmlns.openid.net/auth#'
def OpenID_transform(html, options, _state):
25def OpenID_transform(html, options, _state):
26    """
27    Replace C{openid.XXX} type C{@rel} attribute values in C{<link>} elements by C{openid:XXX}. The openid URI is also
28    added to the top level namespaces with the C{openid:} local name.
29
30    @param html: a DOM node for the top level html element
31    @param options: invocation options
32    @type options: L{Options<pyRdfa.options>}
33    @param state: top level execution state
34    @type state: L{State<pyRdfa.state>}
35    """
36    from ..host import HostLanguage
37    if not(options.host_language in [HostLanguage.xhtml, HostLanguage.html5, HostLanguage.xhtml5]):
38        return
39
40    # the head element is necessary; to be sure, the namespaces are set
41    # on that level only
42    head = None
43    try:
44        head = html.getElementsByTagName("head")[0]
45    except:
46        # no head....
47        return
48
49    foundOpenId = False
50    for link in html.getElementsByTagName("link"):
51        if link.hasAttribute("rel"):
52            rel = link.getAttribute("rel")
53            newProp = ""
54            for n in rel.strip().split():
55                if n.startswith("openid."):
56                    newProp += " " + n.replace("openid.", "openid:")
57                    foundOpenId = True
58                else:
59                    newProp += " " + n
60            link.setAttribute("rel",newProp.strip())
61
62    # Add the OpenId namespace if necessary
63    if foundOpenId and not head.hasAttribute("xmlns:openid"):
64        head.setAttributeNS("", "xmlns:openid", OPENID_NS)

Replace C{openid.XXX} type C{@rel} attribute values in C{} elements by C{openid:XXX}. The openid URI is also added to the top level namespaces with the C{openid:} local name.

@param html: a DOM node for the top level html element @param options: invocation options @type options: L{Options<pyRdfa.options>} @param state: top level execution state @type state: L{State<pyRdfa.state>}