| 1 |
|
| 2 |
""" |
| 3 |
Transformer sub-package for the pyRdfa package. It contains modules with transformer functions; each may be |
| 4 |
invoked 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 |
| 11 |
U{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 |
|
| 21 |
|
| 22 |
def 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 |
|
| 43 |
|
| 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 |
|
| 62 |
def 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 |
|
| 89 |
def 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) |
| 118 |
|