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 handleMatch(self, m): # a single line represents a row tr = etree.Element('tr') # chunks between pipes represent cells for t in m.group(3).split('|'): if len(t) >= 2 and t.startswith('*') and t.endswith('*'): # if a cell is bounded by asterisks, it is a <th> td = etree.Element('th') t = t[1:-1] else: # otherwise it is a <td> td = etree.Element('td') # add text ot inline section, later it will be # processed by core td.text = t tr.append(td) tr.tail = "\n" return tr
def handleMatch(self, m): div = etree.Element("div") div.set("class", "figure") img = etree.SubElement(div, "img") src_parts = m.group(4).split() if src_parts: src = src_parts[0] if src[0] == "<" and src[-1] == ">": src = src[1:-1] img.set('src', self.sanitize_url(src)) else: img.set('src', "") if len(src_parts) > 1: title = dequote(" ".join(src_parts[1:])) img.set('title', title) truealt = m.group(2) img.set('alt', truealt) caption = etree.SubElement(div, "div") caption.set("class", "caption") caption.text = "__Figure " + m.group(3) + ".__ " + truealt return div
def make_link(md, rel, target, label): a = etree.Element('a') a.set('href', target) if rel: a.set('rel', rel) a.text = label or target return a
def create_slide(self, buf, i): cont = etree.Element('div') cont.set('class', 'slide') cont.set('id', str(i)) i += 1 for b in buf: cont.append(b) return cont
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 create_source(self, root): if root.tag == 'img' and str(root.get('src'))[:4] == 'http': src = etree.Element('cite') src.text = 'Quelle: ' + str(root.get('src')) src.set('class', 'source') root.append(src) for c in root: c = self.create_source(c) return root
def parseDocument(self, lines): """Parse a markdown string into an ElementTree.""" # Create a ElementTree from the lines root = etree.Element("div") buffer = [] for line in lines: if line.startswith("#"): self.parseChunk(root, buffer) buffer = [line] else: buffer.append(line) self.parseChunk(root, buffer) return etree.ElementTree(root)
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
def run(self, root): root = self.create_source(root) root = self.create_effects(root) i = 1 nodes = [] buf = [] for c in root: if c.tag in ('h1', 'h2') and len(buf) > 0: nodes.append(self.create_slide(buf, i)) buf = [] i += 1 buf.append(c) if len(buf) > 0: nodes.append(self.create_slide(buf, i)) slide = etree.Element('div') for n in nodes: slide.append(n) return slide
def run(self, doc): div = etree.Element("div") div.attrib["class"] = "alert alert-info" last_li = None # Add title to the div if self.config["title"][0]: header = etree.SubElement(div, "span") header.attrib["class"] = "toctitle" header.text = self.config["title"][0] level = 0 list_stack = [div] header_rgx = re.compile("[Hh][123456]") # Get a list of id attributes used_ids = [] for c in doc.getiterator(): if "id" in c.attrib: used_ids.append(c.attrib["id"]) for (p, c) in self.iterparent(doc): if not c.text: continue # To keep the output from screwing up the # validation by putting a <div> inside of a <p> # we actually replace the <p> in its entirety. # We do not allow the marker inside a header as that # would causes an enless loop of placing a new TOC # inside previously generated TOC. if c.text.find( self.config["marker"][0]) > -1 and not header_rgx.match( c.tag): for i in range(len(p)): if p[i] == c: p[i] = div break if header_rgx.match(c.tag): tag_level = int(c.tag[-1]) while tag_level < level: list_stack.pop() level -= 1 if tag_level > level: newlist = etree.Element("ul") if last_li: last_li.append(newlist) else: list_stack[-1].append(newlist) list_stack.append(newlist) level += 1 # Do not override pre-existing ids if not "id" in c.attrib: id = self.config["slugify"][0](c.text) if id in used_ids: ctr = 1 while "%s_%d" % (id, ctr) in used_ids: ctr += 1 id = "%s_%d" % (id, ctr) used_ids.append(id) c.attrib["id"] = id else: id = c.attrib["id"] # List item link, to be inserted into the toc div last_li = etree.Element("li") link = etree.SubElement(last_li, "a") link.text = c.text link.attrib["href"] = '#' + id if int(self.config["anchorlink"][0]): anchor = etree.SubElement(c, "a") anchor.text = c.text anchor.attrib["href"] = "#" + id anchor.attrib["class"] = "toclink" c.text = "" list_stack[-1].append(last_li)
def handleMatch(self, m): abbr = etree.Element('abbr') abbr.text = m.group('abbr') abbr.set('title', self.title) return abbr