def insert_annotation(self, annotation_element=None, after=None, body=None, creator=None, date=None): if annotation_element is None: annotation_element = odf_create_annotation(body, creator=creator, date=date) else: # XXX clone or modify the argument? if body: annotation_element.set_annotation_body(body) if creator: annotation_element.set_dc_creator(creator) if date: annotation_element.set_dc_date(date) annotation_element.check_validity() if type(after) is unicode: self._insert(annotation_element, after=after) elif isinstance(after, odf_element): after.insert(annotation_element, FIRST_CHILD) else: self.insert(annotation_element, FIRST_CHILD)
def insert_annotation(self, annotation_element=None, after=None, body=None, creator=None, date=None): if annotation_element is None: annotation_element = odf_create_annotation(body, creator=creator, date=date) else: # XXX clone or modify the argument? if body: annotation_element.set_annotation_body(body) if creator: annotation_element.set_dc_creator(creator) if date: annotation_element.set_dc_date(date) annotation_element.check_validity() if type(after) is unicode: self._insert(annotation_element, after=after) elif isinstance(after, odf_element): after.insert_element(annotation_element, FIRST_CHILD) else: self.insert_element(annotation_element, FIRST_CHILD)
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