def set_reference_mark_end(self, reference_mark, before=None, after=None, position=0): """Insert/move a reference_mark_end for an existing reference mark. If some end tag already exists, replace it. Reference 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: reference_mark -- reference element (mandatory) before -- unicode regular expression or None after -- unicode regular expression or None position -- int """ if reference_mark.get_tag() not in (u'text:reference-mark', u'text:reference-mark-start'): raise ValueError( "Not a 'text:reference-mark or text:reference-mark-start' element" ) name = reference_mark.get_name() if reference_mark.get_tag() == u'text:reference-mark': # change it to a range reference: reference_mark._set_tag_raw('text:reference-mark-start') existing_end_tag = self.get_reference_mark_end(name=name) if existing_end_tag: existing_end_tag.delete() # create the end tag end_tag = odf_create_reference_mark_end(name) # Insert self._insert(end_tag, before=before, after=after, position=position, main_text=True) return end_tag
def set_reference_mark_end(self, reference_mark, before=None, after=None, position=0): """Insert/move a reference_mark_end for an existing reference mark. If some end tag already exists, replace it. Reference 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: reference_mark -- reference element (mandatory) before -- unicode regular expression or None after -- unicode regular expression or None position -- int """ if reference_mark.get_tag() not in (u'text:reference-mark', u'text:reference-mark-start'): raise ValueError( "Not a 'text:reference-mark or text:reference-mark-start' element") name = reference_mark.get_name() if reference_mark.get_tag() == u'text:reference-mark': # change it to a range reference: reference_mark._set_tag_raw('text:reference-mark-start') existing_end_tag = self.get_reference_mark_end(name=name) if existing_end_tag: existing_end_tag.delete() # create the end tag end_tag = odf_create_reference_mark_end(name) # Insert self._insert(end_tag, before=before, after=after, position=position, main_text=True) return end_tag
def set_reference_mark(self, name, before=None, after=None, position=0, content=None): """Insert a reference mark, at the position defined by the regex (before, after, content) or by positionnal argument (position). If content is provided, the annotation covers the full range content regex (instances of odf_reference_mark_start and odf_reference_mark_end are created). Else, an instance of odf_reference_mark is positionned either 'before' or 'after' provided regex. If content is an odf element (ie: paragraph, span, ...), the full inner content is referenced (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. Name is mandatory and shall be unique in the document for the preference mark range. Arguments: name -- unicode before -- unicode regular expression or None after -- unicode regular expression or None, or odf_element content -- unicode regular expression or None, or odf_element position -- int or tuple of int Return: the created reference or reference start """ # special case: content is an odf element (ie: a paragraph) if isinstance(content, odf_element): if content.is_empty(): reference = odf_create_reference_mark(name) content.insert(reference, xmlposition=NEXT_SIBLING) return reference reference_start = odf_create_reference_mark_start(name) content.insert(reference_start, start=True) reference_end = odf_create_reference_mark_end(name) content.append(reference_end) return reference_start # With "content" => automatically insert a "start" and an "end" # reference if (before is None and after is None and content is not None and type(position) is int): # Start tag reference_start = odf_create_reference_mark_start(name) self._insert(reference_start, before=content, position=position, main_text=True) # End tag reference_end = odf_create_reference_mark_end(name) self._insert(reference_end, after=content, position=position, main_text=True) return reference_start # With "(int, int)" => automatically insert a "start" and an "end" if (before is None and after is None and content is None and type(position) is tuple): # Start reference_start = odf_create_reference_mark_start(name) self._insert(reference_start, position=position[0], main_text=True) # End reference_end = odf_create_reference_mark_end(name) self._insert(reference_end, position=position[1], main_text=True) return reference_start # Without "content" nor "position" if content is not None or type(position) is not int: raise ValueError("bad arguments") # Insert a positional reference mark reference = odf_create_reference_mark(name) self._insert(reference, before=before, after=after, position=position, main_text=True) return reference