def mainpart(section, line, linelen): global paragraph if linelen > 0 and len(paragraph) == 0 and \ line.upper() == line and line.upper() != line.lower(): # Headlines are always upper case style = h1style l = cleantext(line) section.addElement(text.H(outlinelevel=1, stylename=h1style, text=l)) elif linelen >= FULLLINE: # In the middle of a paragraph paragraph.append(cleantext(line)) elif linelen == 0: # End of paragraph if len(paragraph) > 0: addparagraph(section) paragraph=[] elif linelen < FULLLINE and len(paragraph) > 0: # Short tail of paragraph paragraph.append(cleantext(line)) else: if line == title or line == title + " by " + creator: section.addElement(text.P( stylename=titlestyle, text=cleantext(line))) return if line == "by" or line == creator: section.addElement(text.P( stylename=subtitlestyle, text=cleantext(line))) return if len(paragraph) > 0: addparagraph(section) paragraph=[] section.addElement(text.P(stylename=textbodystyle, text=cleantext(line)))
def s_html_headline(self, tag, attrs): stylename = "Heading_20_%s" % tag[1] if stylename == "Heading_20_1" and self.doc.getStyleByName("Heading_20_1") is None: style = Style(name="Heading_20_1", displayname="Heading 1", family="paragraph", parentstylename="Heading", nextstylename="Text_20_body", attributes={'class':"text"}, defaultoutlinelevel=1) p = TextProperties(fontsize="115%", fontweight="bold", fontsizeasian="115%", fontweightasian="bold", fontsizecomplex="115%", fontweightcomplex="bold") style.addElement(p) self.doc.styles.addElement(style) if stylename == "Heading_20_2" and self.doc.getStyleByName("Heading_20_2") is None: style = Style(name="Heading_20_2", displayname="Heading 2", family="paragraph", parentstylename="Heading", nextstylename="Text_20_body", attributes={'class':"text"}, defaultoutlinelevel=2) p = TextProperties(fontsize="14pt", fontstyle="italic", fontweight="bold", fontsizeasian="14pt", fontstyleasian="italic", fontweightasian="bold", fontsizecomplex="14pt", fontstylecomplex="italic", fontweightcomplex="bold") style.addElement(p) self.doc.styles.addElement(style) if stylename == "Heading_20_3" and self.doc.getStyleByName("Heading_20_3") is None: style = Style(name="Heading_20_3", displayname="Heading 3", family="paragraph", parentstylename="Heading", nextstylename="Text_20_body", attributes={'class':"text"}, defaultoutlinelevel=3) p = TextProperties(fontsize="14pt", fontweight="bold", fontsizeasian="14pt", fontweightasian="bold", fontsizecomplex="14pt", fontweightcomplex="bold") style.addElement(p) self.doc.styles.addElement(style) e = text.H(stylename="Heading_20_%s" % tag[1], outlinelevel=tag[1]) self.curr.addElement(e) self.curr = e
def owriteArticle(self, a): self.references = [] # collect references title = a.caption log(u"processing article %s" % title) r = text.Section(stylename=style.sect, name=title) #, display="none") r.addElement( text.H(outlinelevel=1, stylename=style.ArticleHeader, text=title)) return r
def _render_section(self, level, ref_id, title, newpage=False): # docstring inherited from base class style = "h" + str(level) if newpage: style += "br" self.doc.text.addElement( text.H(text=title, outlinelevel=level, stylename=self.styles.get(style))) if level == 1: # page break after H1 self.doc.text.addElement(text.P(text='', stylename="Break"))
def _render_section(self, level, ref_id, title, newpage=False): """Produces new section in the output document. This method should also save section reference so that TOC can be later produced when :py:meth:`_render_toc` method is called. :param int level: Section level (1, 2, 3, etc.). :param str ref_id: Unique section identifier. :param str title: Printable section name. """ style = "h" + str(level) if newpage: style += "br" self.doc.text.addElement( text.H(text=title, outlinelevel=level, stylename=self.styles.get(style))) if level == 1: # page break after H1 self.doc.text.addElement(text.P(text='', stylename="Break"))
def owriteSection(self, obj): hXstyles = (style.h0,style.h1,style.h2,style.h3,style.h4,style.h5) # skip empty sections (as for eg References) hasDisplayContent = u"".join(x.getAllDisplayText().strip() for x in obj.children [1:]) \ or obj.getChildNodesByClass(advtree.ImageLink) # FIXME, add AdvancedNode.hasContent property enabled = False if enabled and not hasDisplayContent: # FIXME return SkipChildren() title = obj.children[0].getAllDisplayText() # = is level 0 as in article title = # == is level 1 as in mediawiki top level section == # getSectionLevel() == 1 for most outer section level level = 1 + obj.getSectionLevel() # min: 1+0 = 1 level = min(level, len(hXstyles)) hX = hXstyles[level-1] r = text.Section(stylename=style.sect, name=title) r.addElement(text.H(outlinelevel=level, stylename=hX, text=title)) obj.children = obj.children[1:] return r
def html2odf(e, ct=None, **ctargs): """ Convert a :mod:`etgen.html` element to an ODF text element. Most formats are not implemented. There's probably a better way to do this... :ct: the root element ("container"). If not specified, we create one. """ sections_counter = 1 #~ print "20120613 html2odf()", e.tag, e.text if ct is None: ct = text.P(**ctargs) #~ if e.tag in PTAGS: #~ oe = text.P(**ctargs) #~ else: #~ oe = text.P(**ctargs) #~ logger.info("20130201 %s", tostring(e)) #~ raise NotImplementedError("<%s> without container" % e.tag) if isinstance(e, six.string_types): ct.addText(e) #~ oe = text.Span() #~ oe.addText(e) #~ yield oe return ct if e.tag == 'ul': ct = text.List(stylename='podBulletedList') ctargs = dict(stylename='podBulletItem') #~ ctargs = dict() text_container = None if e.tag in ('b', 'strong'): #~ oe = text.Span(stylename='Bold Text') oe = text.Span(stylename='Strong Emphasis') elif e.tag == 'a': oe = text.Span(stylename='Strong Emphasis') #~ oe = text.Span(stylename='Bold Text') elif e.tag in ('i', 'em'): oe = text.Span(stylename='Emphasis') elif e.tag == 'span': oe = text.Span() elif e.tag == 'br': oe = text.LineBreak() elif e.tag == 'h1': """ <text:h text:style-name="Heading_20_1" text:outline-level="1"> """ oe = ct = text.H(stylename="Heading 1", outlinelevel=1) elif e.tag == 'h2': oe = ct = text.H(stylename="Heading 2", outlinelevel=2) elif e.tag == 'h3': oe = ct = text.H(stylename="Heading 3", outlinelevel=3) elif e.tag == 'div': oe = ct = text.Section(name="S" + str(sections_counter)) elif e.tag == 'img': return # ignore images elif e.tag == 'ul': oe = ct #~ elif e.tag in ('ul','ol'): #~ oe = text.List(stylename=e.tag.upper()) #~ ctargs = dict(stylename=e.tag.upper()+"_P") elif e.tag == 'li': #~ oe = ct oe = text.ListItem() text_container = text.P(**ctargs) oe.appendChild(text_container) elif e.tag in PTAGS: oe = ct #~ if ct.tagName == 'p': #~ oe = ct #~ else: #~ oe = text.P(**ctargs) else: logger.info("20130201 %s", tostring(e)) raise NotImplementedError("<%s> inside <%s>" % (e.tag, ct.tagName)) #~ oe = text.Span() if text_container is None: text_container = oe if e.text: text_container.addText(e.text) for child in e: #~ html2odf(child,oe) html2odf(child, text_container, **ctargs) #~ for oc in html2odf(child,oe): # ~ # oe.addElement(oc) #~ oe.appendChild(oc) #~ if not True: #~ if e.tail: #~ oe.addText(e.tail) if oe is not ct: ct.appendChild(oe) #~ yield oe #~ if True: if e.tail: #~ yield e.tail #~ yield text.Span(text=e.tail) #~ yield Text(e.tail) ct.addText(e.tail) return ct
def owriteChapter(self, obj): title = obj.caption item = text.Section(stylename=style.sect, name=title) item.addElement( text.H(outlinelevel=1, text=title, stylename=style.chapter)) return item
def put_header(input_text, style, place, outlinelevel): p = text.H(outlinelevel=outlinelevel, stylename=style) teletype.addTextToElement(p, input_text) place.addElement(p)
doc.styles.addElement(h3style) #boldstyle = style.Style(name="Bold",family="text") #boldstyle.addElement(style.TextProperties(attributes={'fontweight':"bold"})) #textdoc.automaticstyles.addElement(boldstyle) attrliststyle = style.Style(name="Attribute List",family="text") attrliststyle.addElement(style.TextProperties(fontstyle="italic")) doc.styles.addElement(attrliststyle) elmliststyle = style.Style(name="Element List",family="text") elmliststyle.addElement(style.TextProperties(fontstyle="italic")) doc.styles.addElement(elmliststyle) # Text h = text.H(outlinelevel=1, stylename=h1style, text="API for odfpy") textdoc.addElement(h) section = text.Section(name="modules") textdoc.addElement(section) def elmcmp(e1, e2): n1 = nsdict[e1[0]] n2 = nsdict[e2[0]] if cmp(n1,n2) != 0: return cmp(n1,n2) return cmp(e1[1], e2[1]) childkeys = grammar.allowed_children.keys() childkeys.sort(elmcmp) ns='' for element in childkeys: children = grammar.allowed_children[element]