def test_parse_tag(): assert ("some-urn", "some-tag") == misc.parse_tag("{some-urn}some-tag") assert ( "urn:oasis:names:tc:opendocument:xmlns:office:1.0", "document-content", ) == misc.parse_tag( "{urn:oasis:names:tc:opendocument:xmlns:office:1.0}document-content")
def make_placeable(node, xml_space): _namespace, tag = misc.parse_tag(node.tag) if tag in _class_dictionary: klass, maker = _class_dictionary[tag] else: klass, maker = xliff.UnknownXML, make_unknown return maker(klass, node, xml_space)
def _process_children(dom_node, state): _namespace, tag = misc.parse_tag(dom_node.tag) children = [find_translatable_dom_nodes(child, state) for child in dom_node] # Flatten a list of lists into a list of elements children = [child for child_list in children for child in child_list] if len(children) > 1: intermediate_translatable = Translatable(tag, state.xpath_breadcrumb.xpath, dom_node, children) return [intermediate_translatable] else: return children
def find_translatable_dom_nodes(dom_node, state, process_func=process_translatable): # For now, we only want to deal with XML elements. # And we want to avoid processing instructions, which # are XML elements (in the inheritance hierarchy). if not isinstance(dom_node, etree._Element) or isinstance(dom_node, etree._ProcessingInstruction): return [] namespace, tag = misc.parse_tag(dom_node.tag) with parse_status_set(namespace, tag, state): if (namespace, tag) not in state.no_translate_content_elements: return process_func(dom_node, state) else: return _process_children(dom_node, state, process_func)
def find_translatable_dom_nodes(dom_node, state, process_func=process_translatable): # For now, we only want to deal with XML elements. # And we want to avoid processing instructions, which # are XML elements (in the inheritance hierarchy). if (not isinstance(dom_node, etree._Element) or isinstance(dom_node, etree._ProcessingInstruction)): return [] namespace, tag = misc.parse_tag(dom_node.tag) with parse_status_set(namespace, tag, state): if (namespace, tag) not in state.no_translate_content_elements: return process_func(dom_node, state) else: return _process_children(dom_node, state, process_func)
def _process_children(dom_node, state, process_func): """Process an untranslatable DOM node. Since the node is untranslatable it just returns any translatable content present in its child nodes. """ children = [find_translatable_dom_nodes(child, state, process_func) for child in dom_node] # Flatten a list of lists into a list of elements children = [child for child_list in children for child in child_list] if len(children) > 1: _namespace, tag = misc.parse_tag(dom_node.tag) intermediate_translatable = Translatable(tag, state.xpath_breadcrumb.xpath, dom_node, children) return [intermediate_translatable] else: return children
def _retrieve_idml_placeables(dom_node, state): source = [] for child in dom_node: if not isinstance(child, etree._Element): continue if isinstance(child, etree._ProcessingInstruction): # TODO this probably won't be using the right xpath. source.append( Translatable("placeable", state.xpath_breadcrumb.xpath, child, [], False)) if child.tail is not None and child.tail.strip(): source.append(str(child.tail)) continue namespace, tag = misc.parse_tag(child.tag) with parse_status_set(namespace, tag, state): # Ensure we extract all the tags below ParagraphStyleRange as # placeables, independently of them being translatable or not. # state.is_inline = True nested_stuff = [] if child.text is not None and child.text.strip(): nested_stuff = [str(child.text)] nested_stuff.extend(_retrieve_idml_placeables(child, state)) source.append( Translatable( "placeable", state.xpath_breadcrumb.xpath, child, nested_stuff, state.is_inline, )) if child.tail is not None and child.tail.strip(): source.append(str(child.tail)) return source
def find_translatable_dom_nodes(dom_node, state): # For now, we only want to deal with XML elements. # And we want to avoid processing instructions, which # are XML elements (in the inheritance hierarchy). if not isinstance(dom_node, etree._Element) or \ isinstance(dom_node, etree._ProcessingInstruction): return [] namespace, tag = misc.parse_tag(dom_node.tag) @contextmanager def xpath_set(): state.xpath_breadcrumb.start_tag( compact_tag(state.nsmap, namespace, tag)) yield state.xpath_breadcrumb state.xpath_breadcrumb.end_tag() @contextmanager def placeable_set(): old_placeable_name = state.placeable_name state.placeable_name = tag yield state.placeable_name state.placeable_name = old_placeable_name @contextmanager def inline_set(): old_inline = state.is_inline if (namespace, tag) in state.inline_elements: state.is_inline = True else: state.is_inline = False yield state.is_inline state.is_inline = old_inline def with_block(xpath_breadcrumb, placeable_name, is_inline): if (namespace, tag) not in state.no_translate_content_elements: return _process_translatable(dom_node, state) else: return _process_children(dom_node, state) return with_(nested(xpath_set(), placeable_set(), inline_set()), with_block)
def find_translatable_dom_nodes(dom_node, state): # For now, we only want to deal with XML elements. # And we want to avoid processing instructions, which # are XML elements (in the inheritance hierarchy). if not isinstance(dom_node, etree._Element) or \ isinstance(dom_node, etree._ProcessingInstruction): return [] namespace, tag = misc.parse_tag(dom_node.tag) @contextmanager def xpath_set(): state.xpath_breadcrumb.start_tag(compact_tag(state.nsmap, namespace, tag)) yield state.xpath_breadcrumb state.xpath_breadcrumb.end_tag() @contextmanager def placeable_set(): old_placeable_name = state.placeable_name state.placeable_name = tag yield state.placeable_name state.placeable_name = old_placeable_name @contextmanager def inline_set(): old_inline = state.is_inline if (namespace, tag) in state.inline_elements: state.is_inline = True else: state.is_inline = False yield state.is_inline state.is_inline = old_inline def with_block(xpath_breadcrumb, placeable_name, is_inline): if (namespace, tag) not in state.no_translate_content_elements: return _process_translatable(dom_node, state) else: return _process_children(dom_node, state) return with_(nested(xpath_set(), placeable_set(), inline_set()), with_block)
def _retrieve_idml_placeables(dom_node, state): source = [] for child in dom_node: if not isinstance(child, etree._Element): continue if isinstance(child, etree._ProcessingInstruction): #TODO this probably won't be using the right xpath. source.append(Translatable(u"placeable", state.xpath_breadcrumb.xpath, child, [], False)) if child.tail is not None and child.tail.strip(): source.append(six.text_type(child.tail)) continue namespace, tag = misc.parse_tag(child.tag) with parse_status_set(namespace, tag, state): # Ensure we extract all the tags below ParagraphStyleRange as # placeables, independently of them being translatable or not. #state.is_inline = True nested_stuff = [] if child.text is not None and child.text.strip(): nested_stuff = [six.text_type(child.text)] nested_stuff.extend(_retrieve_idml_placeables(child, state)) source.append(Translatable(u"placeable", state.xpath_breadcrumb.xpath, child, nested_stuff, state.is_inline)) if child.tail is not None and child.tail.strip(): source.append(six.text_type(child.tail)) return source
def test_parse_tag(): assert (u'some-urn', u'some-tag') == \ misc.parse_tag(u'{some-urn}some-tag') assert (u'urn:oasis:names:tc:opendocument:xmlns:office:1.0', u'document-content') == \ misc.parse_tag(u'{urn:oasis:names:tc:opendocument:xmlns:office:1.0}document-content')