Exemple #1
0
def safemarkdown(text, div=True):
    from contrib.markdown import markdown
    if text:
        # increase escaping of &, < and > once
        text = text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;") 
        text = wrap_urls(text)

        try:
            text = markdown(text)
        except RuntimeError:
            text = "<p><em>Comment Broken</em></p>"
        #wipe malicious javascript
        text = jscript_url.sub('', text)
        def href_handler(m):
            x = m.group(1).replace('&amp;', '&')
            if c.cname:
                return '<a target="_top" href="%s"' % x
            else:
                return '<a href="%s"' % x
        def code_handler(m):
            l = m.group(1)
            return '<code>%s</code>' % l.replace('&amp;','&')
        #unescape double escaping in links
        def inner_a_handler(m):
            l = m.group(1)
            return '>%s</a>' % l.replace('&amp;','&')
        # remove the "&" escaping in urls
        text = href_re.sub(href_handler, text)
        text = code_re.sub(code_handler, text)
        text = a_re.sub(inner_a_handler, text)
        return MD_START + text + MD_END if div else text
Exemple #2
0
def safemarkdown(text, nofollow=False, target=None):
    from contrib.markdown import markdown
    if text:
        # increase escaping of &, < and > once
        text = text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
        #wrap urls in "<>" so that markdown will handle them as urls
        text = r_url.sub(r'<\1>', text)
        try:
            text = markdown(text)
        except RuntimeError:
            text = "<p><em>Comment Broken</em></p>"
        #remove images
        text = img.sub('', text)
        #wipe malicious javascript
        text = jscript_url.sub('', text)
        def href_handler(m):
            url = m.group(1).replace('&amp;', '&')
            link = '<a href="%s"' % url

            if target:
                link += ' target="%s"' % target
            elif c.cname:
                link += ' target="_top"'

            if nofollow:
                link += ' rel="nofollow"'
            return link
        def code_handler(m):
            l = m.group(1)
            return '<code>%s</code>' % l.replace('&amp;','&')
        #unescape double escaping in links
        def inner_a_handler(m):
            l = m.group(1)
            return '>%s</a>' % l.replace('&amp;','&')
        # remove the "&" escaping in urls
        text = href_re.sub(href_handler, text)
        text = code_re.sub(code_handler, text)
        text = a_re.sub(inner_a_handler, text)
        text = fix_url.sub(r'\1', text)
        return SC_OFF + '<div class="md">' + text + '</div>' + SC_ON
Exemple #3
0
def safemarkdown(text):
    from contrib.markdown import markdown
    if text:
        # increase escaping of &, < and > once
        text = text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;") 
        #wrap urls in "<>" so that markdown will handle them as urls        
        text = r_url.sub(r'<\1>', text)
        try:
            text = markdown(text)
        except RuntimeError:
            text = "<p><em>Comment Broken</em></p>"
        #wipe malicious javascript
        text = jscript_url.sub('', text)
        def href_handler(m):
            return '<a href="%s"' % m.group(1).replace('&amp;', '&')
        def code_handler(m):
            l = m.group(1)
            return '<code>%s</code>' % l.replace('&amp;','&')
        # remove the "&" escaping in urls
        text = href_re.sub(href_handler, text)
        text = code_re.sub(code_handler, text)
        return MD_START + text + MD_END
def py_markdown(text, nofollow=False, target=None):
    # increase escaping of &, < and > once
    text = text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")

    #wrap urls in "<>" so that markdown will handle them as urls
    text = r_url.sub(r'<\1>', text)

    text = markdown(text)

    text = img.sub('', text)  #remove images
    # remove the "&" escaping in urls
    text = code_re.sub(code_handler, text)
    text = a_re.sub(inner_a_handler, text)

    #remove images
    text = img.sub('', text)

    #wipe malicious javascript
    text = jscript_url.sub('', text)

    # remove the "&" escaping in urls
    def href_handler(m):
        url = m.group(1).replace('&amp;', '&')
        link = '<a href="%s"' % url

        if target:
            link += ' target="%s"' % target

        if nofollow:
            link += ' rel="nofollow"'

        return link

    text = href_re.sub(href_handler, text)
    text = code_re.sub(code_handler, text)
    text = a_re.sub(inner_a_handler, text)
    text = fix_url.sub(r'\1', text)

    return text
Exemple #5
0
def py_markdown(text, nofollow=False, target=None):
    # increase escaping of &, < and > once
    text = text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")

    #wrap urls in "<>" so that markdown will handle them as urls
    text = r_url.sub(r'<\1>', text)

    text = markdown(text)

    text = img.sub('', text) #remove images
    # remove the "&" escaping in urls
    text = code_re.sub(code_handler, text)
    text = a_re.sub(inner_a_handler, text)

    #remove images
    text = img.sub('', text)

    #wipe malicious javascript
    text = jscript_url.sub('', text)

    # remove the "&" escaping in urls
    def href_handler(m):
        url = m.group(1).replace('&amp;', '&')
        link = '<a href="%s"' % url

        if target:
            link += ' target="%s"' % target

        if nofollow:
            link += ' rel="nofollow"'

        return link

    text = href_re.sub(href_handler, text)
    text = code_re.sub(code_handler, text)
    text = a_re.sub(inner_a_handler, text)
    text = fix_url.sub(r'\1', text)

    return text
Exemple #6
0
def safemarkdown(text, div=True):
    from contrib.markdown import markdown
    if text:
        # increase escaping of &, < and > once
        text = text.replace("&", "&amp;").replace("<",
                                                  "&lt;").replace(">", "&gt;")
        text = wrap_urls(text)

        try:
            text = markdown(text)
        except RuntimeError:
            text = "<p><em>Comment Broken</em></p>"
        #wipe malicious javascript
        text = jscript_url.sub('', text)

        def href_handler(m):
            x = m.group(1).replace('&amp;', '&')
            if c.cname:
                return '<a target="_top" href="%s"' % x
            else:
                return '<a href="%s"' % x

        def code_handler(m):
            l = m.group(1)
            return '<code>%s</code>' % l.replace('&amp;', '&')

        #unescape double escaping in links
        def inner_a_handler(m):
            l = m.group(1)
            return '>%s</a>' % l.replace('&amp;', '&')

        # remove the "&" escaping in urls
        text = href_re.sub(href_handler, text)
        text = code_re.sub(code_handler, text)
        text = a_re.sub(inner_a_handler, text)
        return MD_START + text + MD_END if div else text