def makeFootnotesDiv(self, root): """ Return div of footnotes as et Element. """ if not self.footnotes.keys(): return None div = etree.Element("div") div.set('class', 'footnote') hr = etree.SubElement(div, "hr") ol = etree.SubElement(div, "ol") for id in self.footnotes.keys(): li = etree.SubElement(ol, "li") li.set("id", self.makeFootnoteId(id)) self.parser.parseChunk(li, self.footnotes[id]) backlink = etree.Element("a") backlink.set("href", "#" + self.makeFootnoteRefId(id)) backlink.set("rev", "footnote") backlink.set("title", "Jump back to footnote %d in the text" % \ (self.footnotes.index(id)+1)) backlink.text = FN_BACKLINK_TEXT if li.getchildren(): node = li[-1] if node.tag == "p": node.text = node.text + NBSP_PLACEHOLDER node.append(backlink) else: p = etree.SubElement(li, "p") p.append(backlink) return div
def run(self, parent, blocks): """ Parse a table block and build table. """ block = blocks.pop(0).split('\n') header = block[:2] rows = block[2:] # Get format type (bordered by pipes or not) border = False if header[0].startswith('|'): border = True # Get alignment of columns align = [] for c in self._split_row(header[1], border): if c.startswith(':') and c.endswith(':'): align.append('center') elif c.startswith(':'): align.append('left') elif c.endswith(':'): align.append('right') else: align.append(None) # Build table table = etree.SubElement(parent, 'table') thead = etree.SubElement(table, 'thead') self._build_row(header[0], thead, align, border) tbody = etree.SubElement(table, 'tbody') for row in rows: self._build_row(row, tbody, align, border)
def _build_row(self, row, parent, align, border): """ Given a row of text, build table cells. """ tr = etree.SubElement(parent, 'tr') tag = 'td' if parent.tag == 'thead': tag = 'th' cells = self._split_row(row, border) # We use align here rather than cells to ensure every row # contains the same number of columns. for i, a in enumerate(align): c = etree.SubElement(tr, tag) try: c.text = cells[i].strip() except IndexError: c.text = "" if a: c.set('align', a)
def handleMatch(self, m): sup = etree.Element("sup") a = etree.SubElement(sup, "a") id = m.group(2) sup.set('id', self.footnotes.makeFootnoteRefId(id)) a.set('href', '#' + self.footnotes.makeFootnoteId(id)) a.set('rel', 'footnote') a.text = str(self.footnotes.footnotes.index(id) + 1) return sup
def run(self, parent, blocks): block = blocks.pop(0) m = self.RE.search(block) terms = [l.strip() for l in block[:m.start()].split('\n') if l.strip()] d, theRest = self.detab(block[m.end():]) if d: d = '%s\n%s' % (m.group(2), d) else: d = m.group(2) #import ipdb; ipdb.set_trace() sibling = self.lastChild(parent) if not terms and sibling.tag == 'p': # The previous paragraph contains the terms state = 'looselist' terms = sibling.text.split('\n') parent.remove(sibling) # Aquire new sibling sibling = self.lastChild(parent) else: state = 'list' if sibling and sibling.tag == 'dl': # This is another item on an existing list dl = sibling if len(dl) and dl[-1].tag == 'dd' and len(dl[-1]): state = 'looselist' else: # This is a new list dl = etree.SubElement(parent, 'dl') # Add terms for term in terms: dt = etree.SubElement(dl, 'dt') dt.text = term # Add definition self.parser.state.set(state) dd = etree.SubElement(dl, 'dd') self.parser.parseBlocks(dd, [d]) self.parser.state.reset() if theRest: blocks.insert(0, theRest)
def run(self, root): rss = etree.Element("rss") rss.set("version", "2.0") channel = etree.SubElement(rss, "channel") for tag, text in (("title", self.ext.getConfig("TITLE")), ("link", self.ext.getConfig("URL")), ("description", None)): element = etree.SubElement(channel, tag) element.text = text for child in root: if child.tag in ["h1", "h2", "h3", "h4", "h5"]: heading = child.text.strip() item = etree.SubElement(channel, "item") link = etree.SubElement(item, "link") link.text = self.ext.getConfig("URL") title = etree.SubElement(item, "title") title.text = heading guid = ''.join([x for x in heading if x.isalnum()]) guidElem = etree.SubElement(item, "guid") guidElem.text = guid guidElem.set("isPermaLink", "false") elif child.tag in ["p"]: try: description = etree.SubElement(item, "description") except UnboundLocalError: # Item not defined - moving on pass else: if len(child): content = "\n".join( [etree.tostring(node) for node in child]) else: content = child.text pholder = self.markdown.htmlStash.store("<![CDATA[ %s]]>" % content) description.text = pholder return rss