def test_insert_element_first_child(self): element = odf_create_element( '<office:text><text:p/><text:p/></office:text>') child = odf_create_element('<text:h/>') element.insert(child, FIRST_CHILD) self.assertEqual(element.serialize(), '<office:text><text:h/><text:p/><text:p/></office:text>')
def test_insert_element_last_child(self): element = odf_create_element( '<office:text><text:p/><text:p/></office:text>') child = odf_create_element('<text:h/>') element.append(child) self.assertEqual(element.serialize(), '<office:text><text:p/><text:p/><text:h/></office:text>')
def test_insert_element_last_child(self): element = odf_create_element( '<office:text><text:p/><text:p/></office:text>') child = odf_create_element('<text:h/>') element.append(child) self.assertEqual( element.serialize(), '<office:text><text:p/><text:p/><text:h/></office:text>')
def test_append_element(self): element = odf_create_element("<root/>") element.append(u"f") element.append(u"oo1") element.append(odf_create_element("<a/>")) element.append(u"f") element.append(u"oo2") self.assertEqual(element.serialize(), "<root>foo1<a/>foo2</root>")
def test_insert_element_prev_sibling(self): root = odf_create_element( '<office:text><text:p/><text:p/></office:text>') element = root.get_elements('//text:p')[0] sibling = odf_create_element('<text:h/>') element.insert(sibling, PREV_SIBLING) self.assertEqual(root.serialize(), '<office:text><text:h/><text:p/><text:p/></office:text>')
def test_insert_element_first_child(self): element = odf_create_element( '<office:text><text:p/><text:p/></office:text>') child = odf_create_element('<text:h/>') element.insert(child, FIRST_CHILD) self.assertEqual( element.serialize(), '<office:text><text:h/><text:p/><text:p/></office:text>')
def test_append_element(self): element = odf_create_element("text:p") element.append(u"f") element.append(u"oo1") element.append(odf_create_element("text:line-break")) element.append(u"f") element.append(u"oo2") self.assertEqual(element.serialize(), "<text:p>foo1<text:line-break/>foo2</text:p>")
def test_insert_element_prev_sibling(self): root = odf_create_element( '<office:text><text:p/><text:p/></office:text>') element = root.get_elements('//text:p')[0] sibling = odf_create_element('<text:h/>') element.insert(sibling, PREV_SIBLING) self.assertEqual( root.serialize(), '<office:text><text:h/><text:p/><text:p/></office:text>')
def __init__(self, **options): Formatter.__init__(self, **options) # buffer regex for tab/space splitting for block code self.whitespace_re = re.compile(u'( {2,}|\t)', re.UNICODE) # create a dict of (start, end) tuples that wrap the # value of a token so that we can use it in the format # method later self.styles = {} # we iterate over the `_styles` attribute of a style item # that contains the parsed style values. for token, style in self.style: root_elem = None curr_elem = None # a style item is a tuple in the following form: # colors are readily specified in hex: 'RRGGBB' if style['color']: root_elem = curr_elem = odf_create_element('text:span') # pylint: disable=maybe-no-member curr_elem.set_style('md2odp-TColor%s' % style['color']) if style['bold']: span = odf_create_element('text:span') # pylint: disable=maybe-no-member span.set_style('md2odp-TBold') if root_elem is None: root_elem = curr_elem = span else: curr_elem.append(span) curr_elem = span if style['italic']: span = odf_create_element('text:span') # pylint: disable=maybe-no-member span.set_style('md2odp-TItalic') if root_elem is None: root_elem = curr_elem = span else: curr_elem.append(span) curr_elem = span if style['underline']: span = odf_create_element('text:span') # pylint: disable=maybe-no-member span.set_style('md2odp-TUnderline') if root_elem is None: root_elem = curr_elem = span else: curr_elem.append(span) curr_elem = span self.styles[token] = (root_elem, curr_elem)
def test_register_family(self): register_element_class('office:dummy3', self.dummy_element, family='graphics') element = odf_create_element('<office:dummy3/>') self.assert_(type(element) is odf_element) element = odf_create_element('<office:dummy3 ' 'style:family="graphics"/>') self.assert_(type(element) is self.dummy_element) element = odf_create_element('<office:dummy4 ' 'style:family="graphics"/>') self.assert_(type(element) is odf_element)
def test_insert_link_simple(self): paragraph = odf_create_element('<text:p>toto tata titi</text:p>') paragraph.insert_link("http://", from_="tata", to="tata") expected = ('<text:p>toto ' '<text:a xlink:href="http://">tata</text:a> ' 'titi</text:p>') self.assertEqual(paragraph.serialize(), expected)
def set_title(self, text): """ text -- str """ elt_title = self.get_element('chart:title') if elt_title is None: elt_title = odf_create_element('chart:title') self.append(elt_title) elt_title.set_text_content(text)
def set_stock_range_line(self, style=None): elt = self.get_element("chart:stock-range-line") if elt is None: elt = odf_create_element("chart:stock-range-line") self.append(elt) if style is not None: elt.set_attribute("chart:style-name", style)
def set_stock_gain_marker(self, style=None): elt = self.get_element("chart:stock-gain-marker") if elt is None: elt = odf_create_element("chart:stock-gain-marker") self.append(elt) if style is not None: elt.set_attribute("chart:style-name", style)
def set_legend_style(self, style): """ style -- str """ elt_legend = self.get_element('chart:legend') if elt_legend is None: elt_legend = odf_create_element('chart:legend') self.append(elt_legend) elt_legend.set_attribute('chart:style-name', style)
def set_legend_alignment(self, align): """ align -- 'start' 'center' 'end' """ elt_legend = self.get_element('chart:legend') if elt_legend is None: elt_legend = odf_create_element('chart:legend') self.append(elt_legend) elt_legend.set_attribute('chart:legend-align', align)
def set_footer_style(self, style): """ style -- str """ elt_footer = self.get_element('chart:footer') if elt_footer is None: elt_footer = odf_create_element('chart:footer') self.append(elt_footer) elt_footer.set_attribute('chart:style-name', style)
def set_footer(self, text): """ test -- str """ elt_footer = self.get_element('chart:footer') if elt_footer is None: elt_footer = odf_create_element('chart:footer') self.append(elt_footer) elt_footer.set_text_content(text)
def set_title_style(self, style): """ style -- str """ elt_title = self.get_element('chart:title') if elt_title is None: elt_title = odf_create_element('chart:title') self.append(elt_title) elt_title.set_attribute('chart:style-name', style)
def set_subtitle_style(self, style): """ style -- str """ elt_subtitle = self.get_element('chart:subtitle') if elt_subtitle is None: elt_subtitle = odf_create_element('chart:subtitle') self.append(elt_subtitle) elt_subtitle.set_attribute('chart:style-name', style)
def test_insert_link_simple(self): paragraph = odf_create_element('<text:p>toto tata titi</text:p>') paragraph.insert_link("http://", from_=u"tata", to=u"tata") expected = ('<text:p>toto ' '<text:a xlink:href="http://">tata</text:a> ' 'titi</text:p>') self.assertEqual(paragraph.serialize(), expected)
def odf_create_chart(chart_class, size=('10cm', '10cm'), title=None, subtitle=None, legend_position=None): """ Create a chart structure for basic use Arguments class -- 'line' 'area' 'circle' 'ring' 'scatter' 'radar' 'bar' 'stock' 'bubble' 'surface' 'gant' size -- (str, str) title -- str subtitle -- str legend_position -- None 'start' 'end' 'top' 'bottom' 'top-start' 'bottom-start' 'top-end' 'bottom-end' """ element = odf_create_element('chart:chart') element.set_class(chart_class) element.set_size(size[0], size[1]) if title is not None: element.set_title(title) if subtitle is not None: element.set_subtitle(subtitle) if legend_position is not None: element.set_legend(legend_position) return element
def test_delete_self_5(self): element = odf_create_element( '<text:p><tag>x</tag><text:span/>keep</text:p>') child = element.get_element('//text:span') child.delete() self.assertEqual(element.serialize(), '<text:p><tag>x</tag>keep</text:p>')
def emphasis(self, text): span = odf_create_element('text:span') # pylint: disable=maybe-no-member span.set_style('md2odp-TextEmphasisStyle') for elem in text.get(): span.append(elem) return ODFPartialTree.from_metrics_provider([span], self)
def set_footer_position(self, x, y): """ arguments -- str """ elt_footer = self.get_element('chart:footer') if elt_footer is None: elt_footer = odf_create_element('chart:footer') self.append(elt_footer) elt_footer.set_attribute('svg:x', x) elt_footer.set_attribute('svg:y', y)
def set_subtitle_position(self, x, y): """ arguments -- str """ elt_subtitle = self.get_element('chart:subtitle') if elt_subtitle is None: elt_subtitle = odf_create_element('chart:subtitle') self.append(elt_subtitle) elt_subtitle.set_attribute('svg:x', x) elt_subtitle.set_attribute('svg:y', y)
def test_pretty_serialize(self): # With pretty = True element = odf_create_element('<root><a>spam</a><b/></root>') serialized = element.serialize(pretty=True) expected = ('<root>\n' ' <a>spam</a>\n' ' <b/>\n' '</root>\n') self.assertEqual(serialized, expected)
def set_legend(self, position): """ position -- 'start' 'end' 'top' 'bottom' 'top-start' 'bottom-start' 'top-end' 'bottom-end' """ elt_legend = self.get_element('chart:legend') if elt_legend is None: elt_legend = odf_create_element('chart:legend') self.append(elt_legend) elt_legend.set_attribute('chart:legend-position', position)
def block_code(self, code, language=None): if language == 'Comment': para = odf_create_paragraph(style=u'md2odp-NoteText') notes = odf_create_element('presentation:notes') # no lang given, use plain monospace formatting for elem in handle_whitespace(code): if isinstance(elem, basestring): span = odf_create_element('text:span') span.set_text(elem) para.append(span) else: para.append(elem) frame = odf_create_text_frame( para, presentation_style=u'md2odp-OutlineText', size=(u'18cm', u'25cm'), position=(u'2cm', u'2cm'), presentation_class=u'title') notes.append(frame) return ODFPartialTree.from_metrics_provider([notes], self) elif language is not None: # explicit lang given, use syntax highlighting para = odf_create_paragraph(style=u'md2odp-ParagraphCodeStyle') lexer = get_lexer_by_name(language) for span in self.formatter.format(lexer.get_tokens(code)): para.append(span) else: # no lang given, use plain monospace formatting para = odf_create_paragraph(style=u'md2odp-ParagraphCodeStyle') for elem in handle_whitespace(code): if isinstance(elem, basestring): span = odf_create_element('text:span') span.set_text(elem) para.append(span) else: para.append(elem) return ODFPartialTree.from_metrics_provider([para], self)
def block_quote(self, text): para = odf_create_paragraph(style=u'md2odp-ParagraphQuoteStyle') span = odf_create_element('text:span') span.set_text(u'“') para.append(span) span = odf_create_element('text:span') for elem in text.get(): span.append(elem) para.append(span) span = odf_create_element('text:span') span.set_text(u'”') para.append(span) # pylint: disable=maybe-no-member para.set_span(u'md2odp-TextQuoteStyle', regex=u'“') para.set_span(u'md2odp-TextQuoteStyle', regex=u'”') return ODFPartialTree.from_metrics_provider([para], self)
def codespan(self, text): span = odf_create_element('text:span') # pylint: disable=maybe-no-member span.set_style('md2odp-TextCodeStyle') if isinstance(text, basestring): span.set_text(unicode(text)) else: for elem in text.get(): span.append(elem) return ODFPartialTree.from_metrics_provider([span], self)
def _set_attribute_in_properties(self, properties, attribute_name, value): """ properties - str attribute_name - str value - str """ p = self.get_element(properties) if p is None: p = odf_create_element(properties) self.append(p) p.set_attribute(attribute_name, value)
def test_insert_link_horrible(self): paragraph = odf_create_element('<text:p>toto ' '<text:span>tata titi</text:span>' ' tutu </text:p>') paragraph.insert_link("http://", from_=u"titi", to=u"tutu") expected = ('<text:p>toto <text:span>tata </text:span>' '<text:a xlink:href="http://">' '<text:span>titi</text:span> tutu' '</text:a> ' '</text:p>') self.assertEqual(paragraph.serialize(), expected)
def test_tail(self): data = (u"<text:p>Le Père Noël a une " u"<text:span>moustache</text:span> rouge.</text:p>") paragraph = odf_create_element(data) paragraph.set_span(u"highlight", regex=u"rouge") expected = ('<text:p>Le Père Noël a une ' '<text:span>moustache</text:span> ' '<text:span ' 'text:style-name="highlight">rouge</text:span>.' '</text:p>') self.assertEqual(paragraph.serialize(), expected)
def test_insert_link_horrible(self): paragraph = odf_create_element('<text:p>toto ' '<text:span>tata titi</text:span>' ' tutu </text:p>') paragraph.insert_link("http://", from_="titi", to="tutu") expected = ('<text:p>toto <text:span>tata </text:span>' '<text:a xlink:href="http://">' '<text:span>titi</text:span> tutu' '</text:a> ' '</text:p>') self.assertEqual(paragraph.serialize(), expected)
def test_insert_link_complex(self): paragraph = odf_create_element('<text:p>toto ' '<text:span> tata </text:span> ' 'titi</text:p>') paragraph.insert_link("http://", from_=u"tata", to=u"titi") expected = ('<text:p>toto <text:span> </text:span>' '<text:a xlink:href="http://">' '<text:span>tata </text:span> titi' '</text:a>' '</text:p>') self.assertEqual(paragraph.serialize(), expected)
def paragraph(self, text): # images? insert as standalone frame, no inline img if isinstance(text.get()[0], odf_frame): return text else: # yes, this seem broadly illogical. but most 'paragraphs' # actually end up being parts of tables, quotes, list items # etc, which might not always permit text:p span = odf_create_element('text:span') for elem in text.get(): span.append(elem) return ODFPartialTree.from_metrics_provider([span], self)
def set_floor(self, width=None, style=None): """ arguments -- str """ floor = self.get_element("chart:floor") if floor is None: floor = odf_create_element('chart:floor') self.append(floor) if width is not None: floor.set_attribute("svg:width", width) if style is not None: floor.set_attribute("chart:style-name", style)
def set_chart_series_domain(self, values, cell_range): """ cell_range -- str like 'Sheet1.A1:Sheet.A10' """ series = \ self.get_element("chart:series[@chart:values-cell-range-address='" + values + "']") if series is not None: domain = odf_create_element('chart:domain') domain.set_attribute("table:cell-range-address", cell_range) series.append(domain) else: raise "Series Element not found"
def set_wall(self, width=None, style=None): """ arguments -- str """ wall = self.get_element("chart:wall") if wall is None: wall = odf_create_element('chart:wall') self.append(wall) if width is not None: wall.set_attribute("svg:width", width) if style is not None: wall.set_attribute("chart:style-name", style)