示例#1
0
def paste_show(key):
    paste = Paste.find(key)
    lexer_name = flask.request.args.get('lang', None)
    try:
        if lexer_name:
            lexer = get_lexer_by_name(lexer_name, stripall=True)
        else:
            lexer = guess_lexer(paste.content)
    except pygments.util.ClassNotFound:
        lexer_name = 'raw'

    if lexer_name == 'raw':
        return flask.Response(paste.content,
                              content_type='text/plain; charset=utf-8')

    with_lines = 'lineno' in flask.request.args

    # JSON is detected as C by default. So this does not work on autodetection
    if lexer.name == 'JSON':
        paste.to_json()

    formatter = HtmlFormatter(linenos=with_lines, cssclass="source")
    html = highlight(paste.content, lexer, formatter)
    stylename = 'css/pygments/{}.css'.format(
            flask.request.args.get('style', 'tango'))
    return flask.render_template('source_code.html', html=html,
                                 stylename=stylename)
示例#2
0
文件: paste.py 项目: husio/bytebin
def paste_show(key):
    paste = Paste.find(key)
    if paste.one_use == 'True':
        paste.delete()
    lexer_name = flask.request.args.get('lang', paste.lang)
    if lexer_name in (None, '', 'raw'):
        return flask.Response(paste.content,
                              content_type='text/plain; charset=utf-8')

    try:
        lexer = pygments.lexers.get_lexer_by_name(lexer_name, stripall=True)
    except pygments.util.ClassNotFound:
        return 'language "{}" not supported'.format(lexer_name), 400

    with_lines = 'lineno' in flask.request.args

    formatter = pygments.formatters.HtmlFormatter(linenos=with_lines,
                                                cssclass="source")
    html = pygments.highlight(paste.content, lexer, formatter)
    stylename = 'css/pygments/{}.css'.format(
            flask.request.args.get('style', 'tango'))
    return flask.render_template('source_code.html', html=html,
                                 stylename=stylename)
示例#3
0
文件: paste.py 项目: husio/bytebin
def paste_delete(key):
    paste = Paste.find(key)
    paste.delete()
    return "Successfully deleted", 204