How to use the markdown.util.etree.SubElement function in Markdown

To help you get started, we’ve selected a few Markdown examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dragondjf / QMarkdowner / markdown / extensions / def_list.py View on Github external
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)
github Python-Markdown / markdown / markdown / extensions / toc.py View on Github external
def build_etree_ul(toc_list, parent):
            ul = etree.SubElement(parent, "ul")
            for item in toc_list:
                # List item link, to be inserted into the toc div
                li = etree.SubElement(ul, "li")
                link = etree.SubElement(li, "a")
                link.text = item.get('name', '')
                link.attrib["href"] = '#' + item.get('id', '')
                if item['children']:
                    build_etree_ul(item['children'], li)
            return ul
github firm1 / zest-writer / src / markdown / extensions / grid_tables.py View on Github external
def _render_rows(self, table, parent):
        """
        Renders all rows in a table into 'tr' elements, and all cells into all
        'td' elements.
        """
        header_cell_tag = 'th'
        body_cell_tag = 'td'
        rendered = []
        if table.has_header:
            header_subparent = etree.SubElement(parent, 'thead')
            body_subparent = etree.SubElement(parent, 'tbody')
        else:
            header_subparent = body_subparent = etree.SubElement(parent, 'tbody')
        for row in table.get_all_rows():
            if table.has_header and row.is_header:
                subparent = header_subparent
            else:
                subparent = body_subparent
            if len(list(row.get_all_cells())) != 0:
                tr = etree.SubElement(subparent, 'tr')
            for cell in row.get_all_cells():
                if not cell in rendered:
                    if row.is_header:
                        cell_element = etree.SubElement(tr, header_cell_tag)
                    else:
                        cell_element = etree.SubElement(tr, body_cell_tag)
                    rendered.append(cell)
github zulip / zulip / zerver / lib / bugdown / __init__.py View on Github external
if media_item['type'] != 'photo':
                    continue

                # Find the image size that is smaller than
                # TWITTER_MAX_IMAGE_HEIGHT px tall or the smallest
                size_name_tuples = list(media_item['sizes'].items())
                size_name_tuples.sort(reverse=True,
                                      key=lambda x: x[1]['h'])
                for size_name, size in size_name_tuples:
                    if size['h'] < self.TWITTER_MAX_IMAGE_HEIGHT:
                        break

                media_url = '%s:%s' % (media_item['media_url_https'], size_name)
                img_div = markdown.util.etree.SubElement(tweet, 'div')
                img_div.set('class', 'twitter-image')
                img_a = markdown.util.etree.SubElement(img_div, 'a')
                img_a.set('href', media_item['url'])
                img_a.set('target', '_blank')
                img_a.set('title', media_item['url'])
                img = markdown.util.etree.SubElement(img_a, 'img')
                img.set('src', media_url)

            return tweet
        except Exception:
            # We put this in its own try-except because it requires external
            # connectivity. If Twitter flakes out, we don't want to not-render
            # the entire message; we just want to not show the Twitter preview.
            bugdown_logger.warning(traceback.format_exc())
            return None
github tomchristie / mkautodoc / mkautodoc / extension.py View on Github external
# Eg: `(a, b='default', **kwargs)``
        signature = inspect.signature(item)

        bracket_elem = etree.SubElement(signature_elem, "span")
        bracket_elem.text = "("
        bracket_elem.set("class", "autodoc-punctuation")

        if signature.parameters:
            for param, is_last in last_iter(get_params(signature)):
                param_elem = etree.SubElement(signature_elem, "em")
                param_elem.text = param
                param_elem.set("class", "autodoc-param")

                if not is_last:
                    comma_elem = etree.SubElement(signature_elem, "span")
                    comma_elem.text = ", "
                    comma_elem.set("class", "autodoc-punctuation")

        bracket_elem = etree.SubElement(signature_elem, "span")
        bracket_elem.text = ")"
        bracket_elem.set("class", "autodoc-punctuation")
github jdittrich / figureAltCaption / figureAltCaption.py View on Github external
def run(self, parent, blocks): # how to process the block?
        raw_block = blocks.pop(0)
        captionText = self.CAPTION_RE.search(raw_block).group('caption') # Get the caption text

        # create figure
        figure = etree.SubElement(parent, 'figure')

        # render image in figure
        figure.text = raw_block

        # create caption
        figcaptionElem = etree.SubElement(figure,'figcaption')
        figcaptionElem.text = captionText #no clue why the text itself turns out as html again and not raw. Anyhow, it suits me, the blockparsers annoyingly wrapped everything into <p>.
</p>
github cozy / cozy.github.io / markdown_sphinxjs / markdown_sphinxjs / __init__.py View on Github external
def make_definition_node(ancestor, definition, path):
    div = etree.SubElement(ancestor, "div")
    div.attrib["class"] = "markdown-sphinxjs-description"

    name = etree.SubElement(div, "h4")
    name.text = "%s.%s(%s) => %s" % (
        definition["memberof"],
        definition["name"],
        ", ".join(definition["meta"]["code"]["paramnames"]),
        definition["returns"][0]["type"]["names"][0]
    )
    p = etree.SubElement(div, "p")
    p.text = definition["description"]
    param_table = etree.SubElement(div, "table")
    param_head = etree.SubElement(param_table, "thead")
    head_row = etree.SubElement(param_table, "tr")
    name = etree.SubElement(head_row, "th")
    name.text = 'Parameter'
github akngs / ecogwiki / markdownext / md_tables.py View on Github external
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('style', 'text-align: %s;' % a)
github TeleMidia / nclcomposer / doc / _old / mdx_grid_tables.py View on Github external
def _render_as_block(self, parent, text):
        """
        Renders a table as a block of text instead of a table. This isn't done
        correctly, since the serialized items are serialized again, but I'll
        fix this later.
        """
        trans_table = [(' ', '&nbsp;'), ('&lt;', '&lt;'), ('&gt;', '&gt;'), ('&amp;', '&amp;')]
        for from_char, to_char in trans_table:
            text = text.replace(from_char, to_char)
        div = etree.SubElement(parent, 'div')
        div.set('class', 'grid-table-error')
        div.text = text
github dellsystem / wikinotes / mdx / mdx_wiki_footnotes.py View on Github external
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")