pyRdfa.transform
Transformer sub-package for the pyRdfa package. It contains modules with transformer functions; each may be invoked by pyRdfa to transform the dom tree before the "real" RDfa processing.
@summary: RDFa Transformer package
@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
1# -*- coding: utf-8 -*- 2""" 3Transformer sub-package for the pyRdfa package. It contains modules with transformer functions; each may be 4invoked by pyRdfa to transform the dom tree before the "real" RDfa processing. 5 6@summary: RDFa Transformer package 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""" 13 14""" 15$Id: __init__.py,v 1.8 2012/06/12 11:47:19 ivan Exp $ 16$Date: 2012/06/12 11:47:19 $ 17""" 18__version__ = "3.0" 19 20# Here are the transfomer functions that are to be performed for all RDFa files, no matter what 21 22def top_about(root, options, state): 23 """ 24 @param root: a DOM node for the top level element 25 @param options: invocation options 26 @type options: L{Options<pyRdfa.options>} 27 @param state: top level execution state 28 @type state: L{State<pyRdfa.state>} 29 """ 30 def set_about(node): 31 if has_one_of_attributes(node, "rel", "rev"): 32 if not has_one_of_attributes(top, "about", "src"): 33 node.setAttribute("about","") 34 else: 35 if not has_one_of_attributes(node, "href", "resource", "about", "src"): 36 node.setAttribute("about","") 37 38 from ..host import HostLanguage 39 from ..utils import has_one_of_attributes 40 41 if not has_one_of_attributes(root, "about"): 42 # The situation is a bit complicated: if a @resource is present without anything else, then it sets 43 # the subject, ie, should be accepted... 44 if has_one_of_attributes(root, "resource", "href", "src"): 45 if has_one_of_attributes(root, "rel", "rev","property"): 46 root.setAttribute("about","") 47 else: 48 root.setAttribute("about","") 49 50 if options.host_language in [ HostLanguage.xhtml, HostLanguage.html5, HostLanguage.xhtml5 ]: 51 if state.rdfa_version >= "1.1": 52 pass 53 else: 54 for top in root.getElementsByTagName("head"): 55 if not has_one_of_attributes(top, "href", "resource", "about", "src"): 56 set_about(top) 57 for top in root.getElementsByTagName("body"): 58 if not has_one_of_attributes(top, "href", "resource", "about", "src"): 59 set_about(top) 60 61 62def empty_safe_curie(node, options, state): 63 """ 64 Remove the attributes whose value is an empty safe curie. It also adds an 'artificial' flag, ie, an 65 attribute (called 'emptysc') into the node to signal that there _is_ an attribute with an ignored 66 safe curie value. The name of the attribute is 'about_pruned' or 'resource_pruned'. 67 68 @param node: a DOM node for the top level element 69 @param options: invocation options 70 @type options: L{Options<pyRdfa.options>} 71 @param state: top level execution state 72 @type state: L{State<pyRdfa.state>} 73 """ 74 def prune_safe_curie(node,name): 75 if node.hasAttribute(name): 76 av = node.getAttribute(name) 77 if av == '[]': 78 node.removeAttribute(name) 79 node.setAttribute(name+'_pruned','') 80 msg = "Attribute @%s uses an empty safe CURIE; the attribute is ignored" % name 81 options.add_warning(msg, node=node) 82 83 prune_safe_curie(node, "about") 84 prune_safe_curie(node, "resource") 85 for n in node.childNodes: 86 if n.nodeType == node.ELEMENT_NODE: 87 empty_safe_curie(n, options, state) 88 89def vocab_for_role(node, options, state): 90 """ 91 The value of the @role attribute (defined separately in the U{Role Attribute Specification Lite<http://www.w3.org/TR/role-attribute/#using-role-in-conjunction-with-rdfa>}) should be as if a @vocab value to the 92 XHTML vocabulary was defined for it. This method turns all terms in role attributes into full URI-s, so that 93 this would not be an issue for the run-time. 94 95 @param node: a DOM node for the top level element 96 @param options: invocation options 97 @type options: L{Options<pyRdfa.options>} 98 @param state: top level execution state 99 @type state: L{State<pyRdfa.state>} 100 """ 101 from ..termorcurie import termname, XHTML_URI 102 103 def handle_role(node): 104 if node.hasAttribute("role"): 105 old_values = node.getAttribute("role").strip().split() 106 new_values = "" 107 for val in old_values: 108 if termname.match(val): 109 new_values += XHTML_URI + val + ' ' 110 else: 111 new_values += val + ' ' 112 node.setAttribute("role", new_values.strip()) 113 114 handle_role(node) 115 for n in node.childNodes: 116 if n.nodeType == node.ELEMENT_NODE: 117 vocab_for_role(n, options, state)
23def top_about(root, options, state): 24 """ 25 @param root: a DOM node for the top level element 26 @param options: invocation options 27 @type options: L{Options<pyRdfa.options>} 28 @param state: top level execution state 29 @type state: L{State<pyRdfa.state>} 30 """ 31 def set_about(node): 32 if has_one_of_attributes(node, "rel", "rev"): 33 if not has_one_of_attributes(top, "about", "src"): 34 node.setAttribute("about","") 35 else: 36 if not has_one_of_attributes(node, "href", "resource", "about", "src"): 37 node.setAttribute("about","") 38 39 from ..host import HostLanguage 40 from ..utils import has_one_of_attributes 41 42 if not has_one_of_attributes(root, "about"): 43 # The situation is a bit complicated: if a @resource is present without anything else, then it sets 44 # the subject, ie, should be accepted... 45 if has_one_of_attributes(root, "resource", "href", "src"): 46 if has_one_of_attributes(root, "rel", "rev","property"): 47 root.setAttribute("about","") 48 else: 49 root.setAttribute("about","") 50 51 if options.host_language in [ HostLanguage.xhtml, HostLanguage.html5, HostLanguage.xhtml5 ]: 52 if state.rdfa_version >= "1.1": 53 pass 54 else: 55 for top in root.getElementsByTagName("head"): 56 if not has_one_of_attributes(top, "href", "resource", "about", "src"): 57 set_about(top) 58 for top in root.getElementsByTagName("body"): 59 if not has_one_of_attributes(top, "href", "resource", "about", "src"): 60 set_about(top)
@param root: a DOM node for the top level element @param options: invocation options @type options: L{Options<pyRdfa.options>} @param state: top level execution state @type state: L{State<pyRdfa.state>}
63def empty_safe_curie(node, options, state): 64 """ 65 Remove the attributes whose value is an empty safe curie. It also adds an 'artificial' flag, ie, an 66 attribute (called 'emptysc') into the node to signal that there _is_ an attribute with an ignored 67 safe curie value. The name of the attribute is 'about_pruned' or 'resource_pruned'. 68 69 @param node: a DOM node for the top level element 70 @param options: invocation options 71 @type options: L{Options<pyRdfa.options>} 72 @param state: top level execution state 73 @type state: L{State<pyRdfa.state>} 74 """ 75 def prune_safe_curie(node,name): 76 if node.hasAttribute(name): 77 av = node.getAttribute(name) 78 if av == '[]': 79 node.removeAttribute(name) 80 node.setAttribute(name+'_pruned','') 81 msg = "Attribute @%s uses an empty safe CURIE; the attribute is ignored" % name 82 options.add_warning(msg, node=node) 83 84 prune_safe_curie(node, "about") 85 prune_safe_curie(node, "resource") 86 for n in node.childNodes: 87 if n.nodeType == node.ELEMENT_NODE: 88 empty_safe_curie(n, options, state)
Remove the attributes whose value is an empty safe curie. It also adds an 'artificial' flag, ie, an attribute (called 'emptysc') into the node to signal that there _is_ an attribute with an ignored safe curie value. The name of the attribute is 'about_pruned' or 'resource_pruned'.
@param node: a DOM node for the top level element @param options: invocation options @type options: L{Options<pyRdfa.options>} @param state: top level execution state @type state: L{State<pyRdfa.state>}
90def vocab_for_role(node, options, state): 91 """ 92 The value of the @role attribute (defined separately in the U{Role Attribute Specification Lite<http://www.w3.org/TR/role-attribute/#using-role-in-conjunction-with-rdfa>}) should be as if a @vocab value to the 93 XHTML vocabulary was defined for it. This method turns all terms in role attributes into full URI-s, so that 94 this would not be an issue for the run-time. 95 96 @param node: a DOM node for the top level element 97 @param options: invocation options 98 @type options: L{Options<pyRdfa.options>} 99 @param state: top level execution state 100 @type state: L{State<pyRdfa.state>} 101 """ 102 from ..termorcurie import termname, XHTML_URI 103 104 def handle_role(node): 105 if node.hasAttribute("role"): 106 old_values = node.getAttribute("role").strip().split() 107 new_values = "" 108 for val in old_values: 109 if termname.match(val): 110 new_values += XHTML_URI + val + ' ' 111 else: 112 new_values += val + ' ' 113 node.setAttribute("role", new_values.strip()) 114 115 handle_role(node) 116 for n in node.childNodes: 117 if n.nodeType == node.ELEMENT_NODE: 118 vocab_for_role(n, options, state)
The value of the @role attribute (defined separately in the U{Role Attribute Specification Litehttp://www.w3.org/TR/role-attribute/#using-role-in-conjunction-with-rdfa}) should be as if a @vocab value to the XHTML vocabulary was defined for it. This method turns all terms in role attributes into full URI-s, so that this would not be an issue for the run-time.
@param node: a DOM node for the top level element @param options: invocation options @type options: L{Options<pyRdfa.options>} @param state: top level execution state @type state: L{State<pyRdfa.state>}