Пример #1
0
    def insert_annotation_end(self,
                              annotation_element,
                              before=None,
                              after=None,
                              position=0):
        """Insert an annotation end tag for an existing annotation. If some end
        tag already exists, replace it. Annotation end tag is set at the
        position defined by the regex (before or after).

        If content/before or after (regex) returns a group of matching
        positions, the position value is the index of matching place to use.

        Arguments:

            annotation_element -- annotation element (mandatory)

            before -- unicode regular expression or None

            after -- unicode regular expression or None

            position -- int
        """

        if annotation_element is None:
            raise ValueError
        if annotation_element.get_tag() != u'office:annotation':
            raise ValueError("Not a 'office:annotation' element")

        # remove existing end tag
        name = annotation_element.get_name()
        existing_end_tag = self.get_annotation_end(name=name)
        if existing_end_tag:
            existing_end_tag.delete()

        # create the end tag
        end_tag = odf_create_annotation_end(annotation_element)

        # Insert
        self._insert(end_tag,
                     before=before,
                     after=after,
                     position=position,
                     main_text=True)
        return end_tag
Пример #2
0
    def insert_annotation_end(self, annotation_element, before=None,
                          after=None, position=0):
        """Insert an annotation end tag for an existing annotation. If some end
        tag already exists, replace it. Annotation end tag is set at the
        position defined by the regex (before or after).

        If content/before or after (regex) returns a group of matching
        positions, the position value is the index of matching place to use.

        Arguments:

            annotation_element -- annotation element (mandatory)

            before -- unicode regular expression or None

            after -- unicode regular expression or None

            position -- int
        """

        if annotation_element is None:
            raise ValueError
        if annotation_element.get_tag() != u'office:annotation':
            raise ValueError("Not a 'office:annotation' element")

        # remove existing end tag
        name = annotation_element.get_name()
        existing_end_tag = self.get_annotation_end(name=name)
        if existing_end_tag:
            existing_end_tag.delete()

        # create the end tag
        end_tag = odf_create_annotation_end(annotation_element)

        # Insert
        self._insert(end_tag, before=before, after=after,
                     position=position, main_text=True)
        return end_tag
Пример #3
0
    def insert_annotation(self, annotation_element=None, before=None,
                          after=None, position=0, content=None,
                          body=None, creator=None, date=None):
        """Insert an annotation, at the position defined by the regex (before,
        after, content) or by positionnal argument (position). If content is
        provided, the annotation covers the full content regex. Else, the
        annotation is positionned either 'before' or 'after' provided regex.

        If content is an odf element (ie: paragraph, span, ...), the full inner
        content is covered by the annotation (of the position just after if
        content is a single empty tag).

        If content/before or after exists (regex) and return a group of matching
        positions, the position value is the index of matching place to use.

        annotation_element can contain a previously created annotation, else
        the annotation is created from the body, creator and optional date
        (current date by default).

        Arguments:

            annotation_element -- annotation element or name

            before -- unicode regular expression or None

            after -- unicode regular expression or None

            content -- unicode regular expression or None, or odf_element

            position -- int or tuple of int

            body -- unicode or odf_element

            creator -- unicode

            date -- datetime
        """

        if annotation_element is None:
            annotation_element = odf_create_annotation(body, creator=creator,
                                                       date=date, parent=self)
        else:
            # XXX clone or modify the argument?
            if body:
                annotation_element.set_body(body)
            if creator:
                annotation_element.set_dc_creator(creator)
            if date:
                annotation_element.set_dc_date(date)
        annotation_element.check_validity()

        # special case: content is an odf element (ie: a paragraph)
        if isinstance(content, odf_element):
            if content.is_empty():
                content.insert(annotation_element, xmlposition=NEXT_SIBLING)
                return annotation_element
            content.insert(annotation_element, start=True)
            annotation_end = odf_create_annotation_end(annotation_element)
            content.append(annotation_end)
            return annotation_element

        # special case
        if isinstance(after, odf_element):
            after.insert(annotation_element, FIRST_CHILD)
            return annotation_element

        # With "content" => automatically insert a "start" and an "end"
        # bookmark
        if (before is None and after is None and content is not None
                and type(position) is int):
            # Start tag
            self._insert(annotation_element, before=content, position=position,
                         main_text=True)
            # End tag
            annotation_end = odf_create_annotation_end(annotation_element)
            self._insert(annotation_end, after=content, position=position,
                         main_text=True)
            return annotation_element

        # With "(int, int)" =>  automatically insert a "start" and an "end"
        # bookmark
        if (before is None and after is None and content is None
                and type(position) is tuple):
            # Start
            self._insert(annotation_element, position=position[0],
                         main_text=True)
            # End
            annotation_end = odf_create_annotation_end(annotation_element)
            self._insert(annotation_end, position=position[1], main_text=True)
            return annotation_element

        # Without "content" nor "position"
        if content is not None or type(position) is not int:
            raise ValueError("bad arguments")

        # Insert
        self._insert(annotation_element, before=before, after=after,
                     position=position, main_text=True)
        return annotation_element
Пример #4
0
    def insert_annotation(self,
                          annotation_element=None,
                          before=None,
                          after=None,
                          position=0,
                          content=None,
                          body=None,
                          creator=None,
                          date=None):
        """Insert an annotation, at the position defined by the regex (before,
        after, content) or by positionnal argument (position). If content is
        provided, the annotation covers the full content regex. Else, the
        annotation is positionned either 'before' or 'after' provided regex.

        If content is an odf element (ie: paragraph, span, ...), the full inner
        content is covered by the annotation (of the position just after if
        content is a single empty tag).

        If content/before or after exists (regex) and return a group of matching
        positions, the position value is the index of matching place to use.

        annotation_element can contain a previously created annotation, else
        the annotation is created from the body, creator and optional date
        (current date by default).

        Arguments:

            annotation_element -- annotation element or name

            before -- unicode regular expression or None

            after -- unicode regular expression or None

            content -- unicode regular expression or None, or odf_element

            position -- int or tuple of int

            body -- unicode or odf_element

            creator -- unicode

            date -- datetime
        """

        if annotation_element is None:
            annotation_element = odf_create_annotation(body,
                                                       creator=creator,
                                                       date=date,
                                                       parent=self)
        else:
            # XXX clone or modify the argument?
            if body:
                annotation_element.set_body(body)
            if creator:
                annotation_element.set_dc_creator(creator)
            if date:
                annotation_element.set_dc_date(date)
        annotation_element.check_validity()

        # special case: content is an odf element (ie: a paragraph)
        if isinstance(content, odf_element):
            if content.is_empty():
                content.insert(annotation_element, xmlposition=NEXT_SIBLING)
                return annotation_element
            content.insert(annotation_element, start=True)
            annotation_end = odf_create_annotation_end(annotation_element)
            content.append(annotation_end)
            return annotation_element

        # special case
        if isinstance(after, odf_element):
            after.insert(annotation_element, FIRST_CHILD)
            return annotation_element

        # With "content" => automatically insert a "start" and an "end"
        # bookmark
        if (before is None and after is None and content is not None
                and type(position) is int):
            # Start tag
            self._insert(annotation_element,
                         before=content,
                         position=position,
                         main_text=True)
            # End tag
            annotation_end = odf_create_annotation_end(annotation_element)
            self._insert(annotation_end,
                         after=content,
                         position=position,
                         main_text=True)
            return annotation_element

        # With "(int, int)" =>  automatically insert a "start" and an "end"
        # bookmark
        if (before is None and after is None and content is None
                and type(position) is tuple):
            # Start
            self._insert(annotation_element,
                         position=position[0],
                         main_text=True)
            # End
            annotation_end = odf_create_annotation_end(annotation_element)
            self._insert(annotation_end, position=position[1], main_text=True)
            return annotation_element

        # Without "content" nor "position"
        if content is not None or type(position) is not int:
            raise ValueError("bad arguments")

        # Insert
        self._insert(annotation_element,
                     before=before,
                     after=after,
                     position=position,
                     main_text=True)
        return annotation_element