Ejemplo n.º 1
0
    def link(self, node, entering):
        attrs = self.attrs(node)
        if entering:
            if not (self.options.get("safe") and potentially_unsafe(node.destination)):
                attrs.append(["href", escape_xml(node.destination, True)])

            if node.title:
                attrs.append(["title", escape_xml(node.title, True)])

            self.tag("a", attrs)
        else:
            self.tag("/a")
Ejemplo n.º 2
0
 def renderCodeBlock(self, event, node, attrs, entering):
     info_words = re.split(r'\s+', node.info) if node.info else []
     if len(info_words) > 0 and len(info_words[0]) > 0:
         attrs.append([
             'class',
             'language-' + escape_xml(info_words[0], True)
         ])
     self.cr()
     self.out(tag('pre') + tag('code', attrs))
     self.out(escape_xml(node.literal, False))
     self.out(tag('/code') + tag('/pre'))
     self.cr()
Ejemplo n.º 3
0
    def link(self, node, entering):
        attrs = self.attrs(node)
        if entering:
            if not (self.options.get('safe') and
                    potentially_unsafe(node.destination)):
                attrs.append(['href', escape_xml(node.destination, True)])

            if node.title:
                attrs.append(['title', escape_xml(node.title, True)])

            self.tag('a', attrs)
        else:
            self.tag('/a')
Ejemplo n.º 4
0
    def link(self, node, entering):
        attrs = self.attrs(node)
        if entering:
            if not (self.options.get('safe')
                    and potentially_unsafe(node.destination)):
                attrs.append(['href', escape_xml(node.destination, True)])

            if node.title:
                attrs.append(['title', escape_xml(node.title, True)])

            self.tag('a', attrs)
        else:
            self.tag('/a')
Ejemplo n.º 5
0
 def renderLink(self, event, node, attrs, entering):
     if entering:
         if not (self.options.get('safe') and
                 potentially_unsafe(node.destination)):
             attrs.append([
                 'href',
                 escape_xml(node.destination, True)
             ])
         if node.title:
             attrs.append(['title', escape_xml(node.title, True)])
         self.out(tag('a', attrs))
     else:
         self.out(tag('/a'))
Ejemplo n.º 6
0
 def image(self, node, entering):
     if entering:
         if self.disable_tags == 0:
             if self.options.get("safe") and potentially_unsafe(node.destination):
                 self.lit('<img src="" alt="')
             else:
                 self.lit('<img src="' + escape_xml(node.destination, True) + '" alt="')
         self.disable_tags += 1
     else:
         self.disable_tags -= 1
         if self.disable_tags == 0:
             if node.title:
                 self.lit('" title="' + escape_xml(node.title, True))
             self.lit('" />')
Ejemplo n.º 7
0
 def image(self, node, entering):
     if entering:
         if self.disable_tags == 0:
             if self.options.get('safe') and \
                potentially_unsafe(node.destination):
                 self.lit('<img src="" alt="')
             else:
                 self.lit('<img src="' +
                          escape_xml(node.destination, True) + '" alt="')
         self.disable_tags += 1
     else:
         self.disable_tags -= 1
         if self.disable_tags == 0:
             if node.title:
                 self.lit('" title="' + escape_xml(node.title, True))
             self.lit('" />')
Ejemplo n.º 8
0
 def renderImage(self, event, node, attrs, entering):
     if entering:
         if self.disable_tags == 0:
             if self.options.get('safe') and \
                potentially_unsafe(node.destination):
                 self.out('<img src="" alt="')
             else:
                 self.out(
                     '<img src="{0}" alt="'.format(
                         escape_xml(node.destination, True)))
         self.disable_tags += 1
     else:
         self.disable_tags -= 1
         if self.disable_tags == 0:
             if node.title:
                 self.out('" title="' +
                          escape_xml(node.title, True))
             self.out('" />')
Ejemplo n.º 9
0
    def code_block(self, node, entering):
        info_words = node.info.split() if node.info else []
        attrs = self.attrs(node)
        if len(info_words) > 0 and len(info_words[0]) > 0:
            attrs.append(["class", "language-" + escape_xml(info_words[0], True)])

        self.cr()
        self.tag("pre")
        self.tag("code", attrs)
        self.out(node.literal)
        self.tag("/code")
        self.tag("/pre")
        self.cr()
Ejemplo n.º 10
0
    def code_block(self, node, entering):
        info_words = node.info.split() if node.info else []
        attrs = self.attrs(node)
        if len(info_words) > 0 and len(info_words[0]) > 0:
            attrs.append(['class', 'language-' +
                          escape_xml(info_words[0], True)])

        self.cr()
        self.tag('pre')
        self.tag('code', attrs)
        self.out(node.literal)
        self.tag('/code')
        self.tag('/pre')
        self.cr()
Ejemplo n.º 11
0
    def code_block(self, node, entering):
        info_words = node.info.split() if node.info else []
        attrs = self.attrs(node)
        if len(info_words) > 0 and len(info_words[0]) > 0:
            attrs.append(
                ['class', 'language-' + escape_xml(info_words[0], True)])

        self.cr()
        self.tag('pre')
        self.tag('code', attrs)
        self.out(node.literal)
        self.tag('/code')
        self.tag('/pre')
        self.cr()
Ejemplo n.º 12
0
    def renderNodes(self, block):
        walker = block.walker()
        self.buf = ""
        self.last_out = "\n"
        self.disable_tags = 0

        event = walker.nxt()
        while event is not None:
            entering = event["entering"]
            node = event["node"]

            attrs = []
            if self.options.get("sourcepos"):
                pos = node.sourcepos
                if pos:
                    attrs.push(["data-sourcepos", pos[0][0] + ":" + pos[0][1] + "-" + pos[1][0] + ":" + pos[1][1]])

            if node.t == "Text":
                self.out(escape_xml(node.literal, False))
            elif node.t == "Softbreak":
                self.out(self.softbreak)
            elif node.t == "Hardbreak":
                self.out(tag("br", [], True))
                self.cr()
            elif node.t == "Emph":
                self.out(tag("em" if entering else "/em"))
            elif node.t == "Strong":
                self.out(tag("strong" if entering else "/strong"))
            elif node.t == "HtmlInline":
                if self.options.get("safe"):
                    self.out("<!-- raw HTML omitted -->")
                else:
                    self.out(node.literal)
            elif node.t == "CustomInline":
                if entering and node.on_enter:
                    self.out(node.on_enter)
                elif not entering and node.on_exit:
                    self.out(node.on_exit)
            elif node.t == "Link":
                if entering:
                    if not (self.options.get("safe") and potentially_unsafe(node.destination)):
                        attrs.append(["href", escape_xml(node.destination, True)])
                    if node.title:
                        attrs.append(["title", escape_xml(node.title, True)])
                    self.out(tag("a", attrs))
                else:
                    self.out(tag("/a"))
            elif node.t == "Image":
                if entering:
                    if self.disable_tags == 0:
                        if self.options.get("safe") and potentially_unsafe(node.destination):
                            self.out('<img src="" alt="')
                        else:
                            self.out('<img src="{}" alt="'.format(escape_xml(node.destination, True)))
                    self.disable_tags += 1
                else:
                    self.disable_tags -= 1
                    if self.disable_tags == 0:
                        if node.title:
                            self.out('" title="' + escape_xml(node.title, True))
                        self.out('" />')
            elif node.t == "Code":
                self.out(tag("code") + escape_xml(node.literal, False) + tag("/code"))
            elif node.t == "Document":
                pass
            elif node.t == "Paragraph":
                grandparent = node.parent.parent
                if grandparent is not None and grandparent.t == "List" and grandparent.list_data.get("tight"):
                    pass
                else:
                    if entering:
                        self.cr()
                        self.out(tag("p", attrs))
                    else:
                        self.out(tag("/p"))
                        self.cr()
            elif node.t == "BlockQuote":
                if entering:
                    self.cr()
                    self.out(tag("blockquote", attrs))
                    self.cr()
                else:
                    self.cr()
                    self.out(tag("/blockquote"))
                    self.cr()
            elif node.t == "Item":
                if entering:
                    self.out(tag("li", attrs))
                else:
                    self.out(tag("/li"))
                    self.cr()
            elif node.t == "List":
                tagname = "ul" if node.list_data["type"] == "Bullet" else "ol"
                if entering:
                    try:
                        start = node.list_data["start"]
                    except KeyError:
                        start = None
                    if start is not None and start != 1:
                        attrs.append(["start", str(start)])
                    self.cr()
                    self.out(tag(tagname, attrs))
                    self.cr()
                else:
                    self.cr()
                    self.out(tag("/" + tagname))
                    self.cr()
            elif node.t == "Heading":
                tagname = "h" + str(node.level)
                if entering:
                    self.cr()
                    self.out(tag(tagname, attrs))
                else:
                    self.out(tag("/" + tagname))
                    self.cr()
            elif node.t == "CodeBlock":
                info_words = re.split(r"\s+", node.info) if node.info else []
                if len(info_words) > 0 and len(info_words[0]) > 0:
                    attrs.append(["class", "language-" + escape_xml(info_words[0], True)])
                self.cr()
                self.out(tag("pre") + tag("code", attrs))
                self.out(escape_xml(node.literal, False))
                self.out(tag("/code") + tag("/pre"))
                self.cr()
            elif node.t == "HtmlBlock":
                if self.options.get("safe"):
                    self.out("<!-- raw HTML omitted -->")
                else:
                    self.out(str(node.literal))
                self.cr()
            elif node.t == "CustomBlock":
                self.cr()
                if entering and node.on_enter:
                    self.out(node.on_enter)
                elif not entering and node.on_exit:
                    self.out(node.on_exit)
                self.cr()
            elif node.t == "ThematicBreak":
                self.cr()
                self.out(tag("hr", attrs, True))
                self.cr()
            else:
                raise ValueError("Unknown node type {}".format(node.t))
            event = walker.nxt()
        return self.buf
Ejemplo n.º 13
0
    def renderNodes(self, block):
        walker = block.walker()
        self.buf = ''
        self.last_out = '\n'
        self.disable_tags = 0

        event = walker.nxt()
        while event is not None:
            entering = event['entering']
            node = event['node']

            attrs = []
            if self.options.get('sourcepos'):
                pos = node.sourcepos
                if pos:
                    attrs.push([
                        'data-sourcepos', pos[0][0] + ':' + pos[0][1] + '-' +
                        pos[1][0] + ':' + pos[1][1]
                    ])

            if node.t == 'Text':
                self.out(escape_xml(node.literal, False))
            elif node.t == 'Softbreak':
                self.out(self.softbreak)
            elif node.t == 'Hardbreak':
                self.out(tag('br', [], True))
                self.cr()
            elif node.t == 'Emph':
                self.out(tag('em' if entering else '/em'))
            elif node.t == 'Strong':
                self.out(tag('strong' if entering else '/strong'))
            elif node.t == 'HtmlInline':
                if self.options.get('safe'):
                    self.out('<!-- raw HTML omitted -->')
                else:
                    self.out(node.literal)
            elif node.t == 'CustomInline':
                if entering and node.on_enter:
                    self.out(node.on_enter)
                elif not entering and node.on_exit:
                    self.out(node.on_exit)
            elif node.t == 'Link':
                if entering:
                    if not (self.options.get('safe')
                            and potentially_unsafe(node.destination)):
                        attrs.append(
                            ['href',
                             escape_xml(node.destination, True)])
                    if node.title:
                        attrs.append(['title', escape_xml(node.title, True)])
                    self.out(tag('a', attrs))
                else:
                    self.out(tag('/a'))
            elif node.t == 'Image':
                if entering:
                    if self.disable_tags == 0:
                        if self.options.get('safe') and \
                           potentially_unsafe(node.destination):
                            self.out('<img src="" alt="')
                        else:
                            self.out('<img src="{}" alt="'.format(
                                escape_xml(node.destination, True)))
                    self.disable_tags += 1
                else:
                    self.disable_tags -= 1
                    if self.disable_tags == 0:
                        if node.title:
                            self.out('" title="' +
                                     escape_xml(node.title, True))
                        self.out('" />')
            elif node.t == 'Code':
                self.out(
                    tag('code') + escape_xml(node.literal, False) +
                    tag('/code'))
            elif node.t == 'Document':
                pass
            elif node.t == 'Paragraph':
                grandparent = node.parent.parent
                if grandparent is not None and \
                   grandparent.t == 'List' and \
                   grandparent.list_data.get('tight'):
                    pass
                else:
                    if entering:
                        self.cr()
                        self.out(tag('p', attrs))
                    else:
                        self.out(tag('/p'))
                        self.cr()
            elif node.t == 'BlockQuote':
                if entering:
                    self.cr()
                    self.out(tag('blockquote', attrs))
                    self.cr()
                else:
                    self.cr()
                    self.out(tag('/blockquote'))
                    self.cr()
            elif node.t == 'Item':
                if entering:
                    self.out(tag('li', attrs))
                else:
                    self.out(tag('/li'))
                    self.cr()
            elif node.t == 'List':
                tagname = 'ul' if node.list_data['type'] == 'Bullet' else 'ol'
                if entering:
                    try:
                        start = node.list_data['start']
                    except KeyError:
                        start = None
                    if start is not None and start != 1:
                        attrs.append(['start', str(start)])
                    self.cr()
                    self.out(tag(tagname, attrs))
                    self.cr()
                else:
                    self.cr()
                    self.out(tag('/' + tagname))
                    self.cr()
            elif node.t == 'Heading':
                tagname = 'h' + str(node.level)
                if entering:
                    self.cr()
                    self.out(tag(tagname, attrs))
                else:
                    self.out(tag('/' + tagname))
                    self.cr()
            elif node.t == 'CodeBlock':
                info_words = re.split(r'\s+', node.info) if node.info else []
                if len(info_words) > 0 and len(info_words[0]) > 0:
                    attrs.append([
                        'class', 'language-' + escape_xml(info_words[0], True)
                    ])
                self.cr()
                self.out(tag('pre') + tag('code', attrs))
                self.out(escape_xml(node.literal, False))
                self.out(tag('/code') + tag('/pre'))
                self.cr()
            elif node.t == 'HtmlBlock':
                if self.options.get('safe'):
                    self.out('<!-- raw HTML omitted -->')
                else:
                    self.out(str(node.literal))
                self.cr()
            elif node.t == 'CustomBlock':
                self.cr()
                if entering and node.on_enter:
                    self.out(node.on_enter)
                elif not entering and node.on_exit:
                    self.out(node.on_exit)
                self.cr()
            elif node.t == 'ThematicBreak':
                self.cr()
                self.out(tag('hr', attrs, True))
                self.cr()
            else:
                raise ValueError('Unknown node type {}'.format(node.t))
            event = walker.nxt()
        return self.buf
Ejemplo n.º 14
0
    def renderNodes(self, block):
        walker = block.walker()
        self.buf = ''
        self.last_out = '\n'
        self.disable_tags = 0

        event = walker.nxt()
        while event is not None:
            entering = event['entering']
            node = event['node']

            attrs = []
            if self.options.get('sourcepos'):
                pos = node.sourcepos
                if pos:
                    attrs.push([
                        'data-sourcepos',
                        pos[0][0] + ':' + pos[0][1] + '-' +
                        pos[1][0] + ':' + pos[1][1]])

            if node.t == 'Text':
                self.out(escape_xml(node.literal, False))
            elif node.t == 'Softbreak':
                self.out(self.softbreak)
            elif node.t == 'Hardbreak':
                self.out(tag('br', [], True))
                self.cr()
            elif node.t == 'Emph':
                self.out(tag('em' if entering else '/em'))
            elif node.t == 'Strong':
                self.out(tag('strong' if entering else '/strong'))
            elif node.t == 'HtmlInline':
                if self.options.get('safe'):
                    self.out('<!-- raw HTML omitted -->')
                else:
                    self.out(node.literal)
            elif node.t == 'CustomInline':
                if entering and node.on_enter:
                    self.out(node.on_enter)
                elif not entering and node.on_exit:
                    self.out(node.on_exit)
            elif node.t == 'Link':
                if entering:
                    if not (self.options.get('safe') and
                            potentially_unsafe(node.destination)):
                        attrs.append([
                            'href',
                            escape_xml(node.destination, True)
                        ])
                    if node.title:
                        attrs.append(['title', escape_xml(node.title, True)])
                    self.out(tag('a', attrs))
                else:
                    self.out(tag('/a'))
            elif node.t == 'Image':
                if entering:
                    if self.disable_tags == 0:
                        if self.options.get('safe') and \
                           potentially_unsafe(node.destination):
                            self.out('<img src="" alt="')
                        else:
                            self.out(
                                '<img src="{0}" alt="'.format(
                                    escape_xml(node.destination, True)))
                    self.disable_tags += 1
                else:
                    self.disable_tags -= 1
                    if self.disable_tags == 0:
                        if node.title:
                            self.out('" title="' +
                                     escape_xml(node.title, True))
                        self.out('" />')
            elif node.t == 'Code':
                self.out(
                    tag('code') +
                    escape_xml(node.literal, False) +
                    tag('/code'))
            elif node.t == 'Document':
                pass
            elif node.t == 'Paragraph':
                grandparent = node.parent.parent
                if grandparent is not None and \
                   grandparent.t == 'List' and \
                   grandparent.list_data.get('tight'):
                    pass
                else:
                    if entering:
                        self.cr()
                        self.out(tag('p', attrs))
                    else:
                        self.out(tag('/p'))
                        self.cr()
            elif node.t == 'BlockQuote':
                if entering:
                    self.cr()
                    self.out(tag('blockquote', attrs))
                    self.cr()
                else:
                    self.cr()
                    self.out(tag('/blockquote'))
                    self.cr()
            elif node.t == 'Item':
                if entering:
                    self.out(tag('li', attrs))
                else:
                    self.out(tag('/li'))
                    self.cr()
            elif node.t == 'List':
                tagname = 'ul' if node.list_data['type'] == 'Bullet' else 'ol'
                if entering:
                    try:
                        start = node.list_data['start']
                    except KeyError:
                        start = None
                    if start is not None and start != 1:
                        attrs.append(['start', str(start)])
                    self.cr()
                    self.out(tag(tagname, attrs))
                    self.cr()
                else:
                    self.cr()
                    self.out(tag('/' + tagname))
                    self.cr()
            elif node.t == 'Heading':
                tagname = 'h' + str(node.level)
                if entering:
                    self.cr()
                    self.out(tag(tagname, attrs))
                else:
                    self.out(tag('/' + tagname))
                    self.cr()
            elif node.t == 'CodeBlock':
                info_words = re.split(r'\s+', node.info) if node.info else []
                if len(info_words) > 0 and len(info_words[0]) > 0:
                    attrs.append([
                        'class',
                        'language-' + escape_xml(info_words[0], True)
                    ])
                self.cr()
                self.out(tag('pre') + tag('code', attrs))
                self.out(escape_xml(node.literal, False))
                self.out(tag('/code') + tag('/pre'))
                self.cr()
            elif node.t == 'HtmlBlock':
                if self.options.get('safe'):
                    self.out('<!-- raw HTML omitted -->')
                else:
                    self.out(str(node.literal))
                self.cr()
            elif node.t == 'CustomBlock':
                self.cr()
                if entering and node.on_enter:
                    self.out(node.on_enter)
                elif not entering and node.on_exit:
                    self.out(node.on_exit)
                self.cr()
            elif node.t == 'ThematicBreak':
                self.cr()
                self.out(tag('hr', attrs, True))
                self.cr()
            else:
                raise ValueError('Unknown node type {0}'.format(node.t))
            event = walker.nxt()
        return self.buf
Ejemplo n.º 15
0
 def renderCode(self, event, node, attrs, entering):
     self.out(
         tag('code') +
         escape_xml(node.literal, False) +
         tag('/code'))
Ejemplo n.º 16
0
 def out(self, s):
     self.lit(escape_xml(s, False))
Ejemplo n.º 17
0
 def renderText(self, event, node, attrs, entering):
     self.out(escape_xml(node.literal, False))
Ejemplo n.º 18
0
 def escape(self, text, preserve_entities):
     escaped = escape_xml(text, preserve_entities)
     return INTERNAL_LINK.sub('{\g<1>}', escaped)
Ejemplo n.º 19
0
 def escape(self, text, preserve_entities):
     return escape_xml(text, preserve_entities)
Ejemplo n.º 20
0
 def out(self, s):
     self.lit(escape_xml(s, False))