Example #1
0
    def set_span(self, style, regex=None, offset=None, length=0):
        """Apply the given style to text content matching the regex OR the
        positional arguments offset and length.

        Arguments:

            style -- style element or name

            regex -- unicode regular expression

            offset -- int

            length -- int
        """
        # XXX FIX ME cyclic import
        from span import odf_create_span

        if isinstance(style, odf_style):
            style = style.get_style_name()
        if offset:
            # XXX quickly hacking the offset
            text = self.get_text()
            if length:
                regex = text[offset:offset + length]
            else:
                regex = text[offset:]
            regex = escape(regex)
        if regex:
            pattern = compile(unicode(regex))
            for text in self.xpath('descendant::text()'):
                # Static information about the text node
                container = text.get_parent()
                wrapper = container.get_parent()
                is_text = text.is_text()
                # Group positions are calculated and static, so apply in
                # reverse order to preserve positions
                for group in reversed(list(pattern.finditer(text))):
                    start, end = group.span()
                    # Do not use the text node as it changes at each loop
                    if is_text:
                        text = container.get_text()
                    else:
                        text = container.get_tail()
                    before = text[:start]
                    match = text[start:end]
                    after = text[end:]
                    span = odf_create_span(match, style=style)
                    span.set_tail(after)
                    if is_text:
                        container.set_text(before)
                        # Insert as first child
                        container.insert(span, position=0)
                    else:
                        container.set_tail(before)
                        # Insert as next sibling
                        index = wrapper.index(container)
                        wrapper.insert(span, position=index + 1)
    def set_span(self, style, regex=None, offset=None, length=0):
        """Apply the given style to text content matching the regex OR the
        positional arguments offset and length.

        Arguments:

            style -- style element or name

            regex -- unicode regular expression

            offset -- int

            length -- int
        """
        # XXX FIX ME cyclic import
        from span import odf_create_span

        if isinstance(style, odf_style):
            style = style.get_style_name()
        if offset:
            # XXX quickly hacking the offset
            text = self.get_text()
            if length:
                regex = text[offset:offset + length]
            else:
                regex = text[offset:]
            regex = escape(regex)
        if regex:
            pattern = compile(unicode(regex))
            for text in self.xpath('descendant::text()'):
                # Static information about the text node
                container = text.get_parent()
                wrapper = container.get_parent()
                is_text = text.is_text()
                # Group positions are calculated and static, so apply in
                # reverse order to preserve positions
                for group in reversed(list(pattern.finditer(text))):
                    start, end = group.span()
                    # Do not use the text node as it changes at each loop
                    if is_text:
                        text = container.get_text()
                    else:
                        text = container.get_tail()
                    before = text[:start]
                    match = text[start:end]
                    after = text[end:]
                    span = odf_create_span(match, style=style)
                    span.set_tail(after)
                    if is_text:
                        container.set_text(before)
                        # Insert as first child
                        container.insert_element(span, position=0)
                    else:
                        container.set_tail(before)
                        # Insert as next sibling
                        index = wrapper.index(container)
                        wrapper.insert_element(span, position=index + 1)
Example #3
0
def _convert_style_like(node, context, style_name):
    # Create the span
    span = odf_create_span(style=style_name)
    context["top"].append(span)

    # Save the current top
    old_top = context["top"]

    # Convert
    context["top"] = span
    for child in node:
        convert_node(child, context)

    # And restore the top
    context["top"] = old_top
Example #4
0
def _convert_style_like(node, context, style_name):
    # Create the span
    span = odf_create_span(style=style_name)
    context['top'].append(span)
    push_convert_pop(node, span, context)
Example #5
0
def _convert_style_like(node, context, style_name):
    # Create the span
    span = odf_create_span(style=style_name)
    context['top'].append(span)
    push_convert_pop(node, span, context)