Ejemplo n.º 1
0
    def visit_reference(self, node):
        refuri = node.get('refuri', '')
        if refuri.startswith('<<') and refuri.endswith('>>'):  # moin macro
            macro_name = refuri[2:-2].split('(')[0]
            if macro_name == "TableOfContents":
                arguments = refuri[2:-2].split('(')[1][:-1].split(',')
                node = moin_page.table_of_content()
                self.open_moin_page_node(node)
                if arguments and arguments[0]:
                    node.set(moin_page.outline_level, arguments[0])
                return
            if macro_name == "Include":
                # include macros are expanded by include.py similar to transclusions
                # rst include handles only wiki pages and does not support additional arguments like moinwiki
                arguments = refuri[2:-2].split('(')[1][:-1].split(',')
                link = Iri(scheme='wiki.local', path=arguments)
                node = xinclude.include(
                    attrib={
                        xinclude.href: link,
                        moin_page.alt: refuri,
                        moin_page.content_type: 'x-moin/macro;name=' +
                        macro_name,
                    })
                self.open_moin_page_node(node)
                return
            try:
                arguments = refuri[2:-2].split('(')[1][:-1]
            except IndexError:
                arguments = ''  # <<DateTime>>

            self.open_moin_page_node(
                moin_page.inline_part(
                    attrib={
                        moin_page.content_type:
                        "x-moin/macro;name={0}".format(macro_name)
                    }))
            if arguments:
                self.open_moin_page_node(moin_page.arguments())
                self.open_moin_page_node(arguments)
                self.close_moin_page_node()
                self.close_moin_page_node()
            return

        if not allowed_uri_scheme(refuri):
            self.visit_error(node)
            return
        if refuri == '':
            # build a link to a heading or an explicitly defined anchor
            refuri = Iri(scheme='wiki.local',
                         fragment=node.attributes['name'].replace(' ', '_'))
        self.open_moin_page_node(moin_page.a(attrib={xlink.href: refuri}))
Ejemplo n.º 2
0
    def _TableOfContents_repl(self, args, text, context_block):
        if not context_block:
            return text

        attrib = {}
        if args:
            try:
                level = int(args[0])
                assert 0 < level < 7
            except (ValueError, AssertionError):
                pass
            else:
                attrib[moin_page.outline_level] = str(level)

        return moin_page.table_of_content(attrib=attrib)
Ejemplo n.º 3
0
def postproc_text(markdown, text):
    """
    Removes HTML or XML character references and entities from a text string.

    :param text: The HTML (or XML) source text.
    :returns: The plain text, as a Unicode string, if necessary.
    """

    # http://effbot.org/zone/re-sub.htm#unescape-html

    if text is None:
        return None

    if text == '[TOC]':
        return moin_page.table_of_content(attrib={})

    for pp in markdown.postprocessors:
        text = pp.run(text)

    if text.startswith('<pre>') or text.startswith(
            '<div class="codehilite"><pre>'):
        # TODO: with markdown 3.0.0 indented code is somehow escaped twice - see issue #707
        text = text.replace('&amp;lt;',
                            '&lt;').replace('&amp;gt;', '&gt;').replace(
                                '&amp;amp;', '&amp;')
        return text

    def fixup(m):
        text = m.group(0)
        if text[:2] == "&#":
            # character reference
            try:
                if text[:3] == "&#x":
                    return chr(int(text[3:-1], 16))
                else:
                    return chr(int(text[2:-1]))
            except ValueError:
                pass
        else:
            # named entity
            try:
                text = chr(name2codepoint[text[1:-1]])
            except KeyError:
                pass
        return text  # leave as is

    return re.sub(r"&#?\w+;", fixup, text)