Package translate :: Package storage :: Package xml_extract :: Module generate
[hide private]
[frames] | no frames]

Module generate

source code

Functions [hide private]
 
_get_tag_arrays(dom_node)
Return a dictionary indexed by child tag names, where each tag is associated with an array of all the child nodes with matching the tag name, in the order in which they appear as children of dom_node.
source code
 
apply_translations(dom_node, unit_node, do_translate) source code
 
reduce_dom_tree(f, dom_node, *state) source code
 
find_dom_root(parent_dom_node, dom_node) source code
 
find_placeable_dom_tree_roots(unit_node)
For an inline placeable, find the root DOM node for the placeable in its parent.
source code
 
_map_source_dom_to_doc_dom(unit_node, source_dom_node)
Creating a mapping from the DOM nodes in source_dom_node which correspond to placeables, with DOM nodes in the XML document template (this information is obtained from unit_node).
source code
 
_map_target_dom_to_source_dom(source_dom_node, target_dom_node)
Associate placeables in source_dom_node and target_dom_node which have the same 'id' attributes.
source code
 
_build_target_dom_to_doc_dom(unit_node, source_dom, target_dom) source code
 
_get_translated_node(target_node, target_dom_to_doc_dom)
Convenience function to get node corresponding to 'target_node' and to assign the tail text of 'target_node' to this node.
source code
 
_build_translated_dom(dom_node, target_node, target_dom_to_doc_dom)
Use the "shape" of 'target_node' (which is a DOM tree) to insert nodes into the DOM tree rooted at 'dom_node'.
source code
 
replace_dom_text(make_parse_state)
Return a function:
source code

Imports: etree, base, accepts, IsCallable, Any, misc, extract, unit_tree, XmlNamer


Function Details [hide private]

_get_tag_arrays(dom_node)

source code 

Return a dictionary indexed by child tag names, where each tag is associated with an array of all the child nodes with matching the tag name, in the order in which they appear as children of dom_node.

>>> xml = '<a><b></b><c></c><b></b><d/></a>'
>>> element = etree.fromstring(xml)
>>> get_tag_arrays(element)
{'b': [<Element a at 84df144>, <Element a at 84df148>], 'c': [<Element a at 84df120>], 'd': [<Element a at 84df152>]}
Decorators:
  • @accepts(etree._Element)

apply_translations(dom_node, unit_node, do_translate)

source code 
Decorators:
  • @accepts(etree._Element, unit_tree.XPathTree, extract.Translatable, IsCallable())

reduce_dom_tree(f, dom_node, *state)

source code 
Decorators:
  • @accepts(IsCallable(), etree._Element, vargs= [Any()])

find_dom_root(parent_dom_node, dom_node)

source code 
Decorators:
  • @accepts(etree._Element, etree._Element)

find_placeable_dom_tree_roots(unit_node)

source code 

For an inline placeable, find the root DOM node for the placeable in its parent.

Consider the diagram. In this pseudo-ODF example, there is an inline span element. However, the span is contained in other tags (which we never process). When splicing the template DOM tree (that is, the DOM which comes from the XML document we're using to generate a translated XML document), we'll need to move DOM sub-trees around and we need the roots of these sub-trees:

   <p> This is text \/                <- Paragraph containing an inline placeable
                    <blah>            <- Inline placeable's root (which we want to find)
                    ...               <- Any number of intermediate DOM nodes
                    <span> bold text  <- The inline placeable's Translatable 
                                         holds a reference to this DOM node    
Decorators:
  • @accepts(extract.Translatable)

_map_source_dom_to_doc_dom(unit_node, source_dom_node)

source code 

Creating a mapping from the DOM nodes in source_dom_node which correspond to placeables, with DOM nodes in the XML document template (this information is obtained from unit_node). We are interested in DOM nodes in the XML document template which are the roots of placeables. See the diagram below, as well as find_placeable_dom_tree_roots.

XLIFF Source (below):

   <source>This is text <g> bold text</g> and a footnote<x/></source> 
                        /                                 \________
                       /                                           \ 
   <p>This is text<blah>...<span> bold text</span>...</blah> and <note>...</note></p>

Input XML document used as a template (above)

In the above diagram, the XLIFF source DOM node <g> is associated with the XML document DOM node <blah>, whereas the XLIFF source DOM node <x> is associated with the XML document DOM node <note>.

Decorators:
  • @accepts(extract.Translatable, etree._Element)

_map_target_dom_to_source_dom(source_dom_node, target_dom_node)

source code 

Associate placeables in source_dom_node and target_dom_node which have the same 'id' attributes.

We're using XLIFF placeables. The XLIFF standard requires that placeables have unique ids. The id of a placeable is never modified, which means that even if placeables are moved around in a translation, we can easily associate placeables from the source text with placeables in the target text.

This function does exactly that.

Decorators:
  • @accepts(etree._Element, etree._Element)

_get_translated_node(target_node, target_dom_to_doc_dom)

source code 

Convenience function to get node corresponding to 'target_node' and to assign the tail text of 'target_node' to this node.

Decorators:
  • @accepts(etree._Element, {etree._Element: etree._Element})

_build_translated_dom(dom_node, target_node, target_dom_to_doc_dom)

source code 

Use the "shape" of 'target_node' (which is a DOM tree) to insert nodes into the DOM tree rooted at 'dom_node'.

The mapping 'target_dom_to_doc_dom' is used to map nodes from 'target_node' to nodes which much be inserted into dom_node.

Decorators:
  • @accepts(etree._Element, etree._Element, {etree._Element: etree._Element})

replace_dom_text(make_parse_state)

source code 

Return a function:

   action: etree_Element x base.TranslationUnit -> None

which takes a dom_node and a translation unit. The dom_node is rearranged according to rearrangement of placeables in unit.target (relative to their positions in unit.source).

Decorators:
  • @accepts(IsCallable())