def handle_whitespace(text): """Add ODF whitespace processing and ODF linebreak elements into multi-line text. Returns list with mixed unicode and odf elements.""" result = [] lines = text.split('\n') for index, line in enumerate(lines): # for every line for part in _whitespace_re.split(line): # split off tabulators and whitespace if part[:2] == ' ': # multiple spaces in ODF need markup result.append( odf_create_spaces(len(part))) elif part[:1] == '\t': # insert an actual tab result.append( odf_create_tabulation()) else: result.append( unicode(part)) # for all but the last line: add linebreak if index < len(lines)-1: result.append(odf_create_line_break()) return result
def convert_literal_block(node, context): paragraph = odf_create_paragraph(style="Preformatted_20_Text") context["top"].append_element(paragraph) # Convert for child in node: # Only text if child.tagname != "#text": warn('node "%s" not supported in literal block' % child.tagname) continue text = child.astext() tmp = [] spaces = 0 for c in text: if c == '\n': if tmp: tmp = u"".join(tmp) paragraph.append_element(tmp) tmp = [] spaces = 0 paragraph.append_element(odf_create_line_break()) elif c == '\r': continue elif c == ' ': spaces += 1 elif c == '\t': # Tab = 4 spaces spaces += 4 else: if spaces >= 2: if tmp: tmp = u"".join(tmp) paragraph.append_element(tmp) tmp = [] paragraph.append_element( odf_create_undividable_space(spaces)) spaces = 0 elif spaces == 1: tmp.append(' ') spaces = 0 tmp.append(c) if tmp: tmp = u"".join(tmp) paragraph.append_element(tmp)
def test_create_line_break(self): lb = odf_create_line_break() expected = ('<text:line-break/>') self.assertEqual(lb.serialize(), expected)
def linebreak(self): return ODFPartialTree.from_metrics_provider([odf_create_line_break()], self)