示例#1
0
def hl_code(code, name, user_data):
    style: Style = user_data.get('style') or default_style

    formatter = find_formatter_class(style.format)

    css_style = ('pre.code{{ font-family: {}; }}'.format(style.font) +
                 'td.linenos{ '
                 'background-color: rgba(240, 240, 240, 0.11); }')

    scheme:pygments.style.Style = get_style_by_name(style.color_scheme)
    rgb = getrgb(scheme.background_color)[:3]
    lineno_bg = (*rgb, 20)

    highlighted = highlight(code,
                            PythonLexer(),
                            formatter(style=style.color_scheme,
                                      linenos = True,
                                      font_name=style.font,
                                      fontfamily=style.font,
                                      full=True,
                                      line_number_bg=lineno_bg,
                                      cssstyles=css_style))

    if style.format == 'html':
        highlighted = highlighted.replace(
            'background-color: #f0f0f0;',
            'background-color: rgba(240, 240, 240, 0.06);'
        )

    if isinstance(highlighted, str):
        highlighted = highlighted.encode()
    img = BytesIO(highlighted)
    img.name = 'code_{}.{}'.format(name, style.format)

    return img
示例#2
0
def pretty_dumps(json_dict):
    """Format a json dictionary to a colorful and indented string."""
    if json_dict is None:
        return "None"
    formatted_json = json.dumps(json_dict, indent=4)
    return highlight(formatted_json,
                     lexers.find_lexer_class_by_name("JSON")(),
                     formatters.find_formatter_class("terminal")())
示例#3
0
def _print_help(what, name):
    try:
        if what == "lexer":
            cls = find_lexer_class(name)
            print("Help on the %s lexer:" % cls.name)
            print(dedent(cls.__doc__))
        elif what == "formatter":
            cls = find_formatter_class(name)
            print("Help on the %s formatter:" % cls.name)
            print(dedent(cls.__doc__))
        elif what == "filter":
            cls = find_filter_class(name)
            print("Help on the %s filter:" % name)
            print(dedent(cls.__doc__))
    except AttributeError:
        print("%s not found!" % what, file=sys.stderr)
示例#4
0
def _print_help(what, name):
    try:
        if what == 'lexer':
            cls = find_lexer_class(name)
            print "Help on the %s lexer:" % cls.name
            print dedent(cls.__doc__)
        elif what == 'formatter':
            cls = find_formatter_class(name)
            print "Help on the %s formatter:" % cls.name
            print dedent(cls.__doc__)
        elif what == 'filter':
            cls = find_filter_class(name)
            print "Help on the %s filter:" % name
            print dedent(cls.__doc__)
    except AttributeError:
        print >>sys.stderr, "%s not found!" % what
示例#5
0
文件: cmdline.py 项目: axil/blog
def _print_help(what, name):
    try:
        if what == 'lexer':
            cls = get_lexer_by_name(name)
            print("Help on the %s lexer:" % cls.name)
            print(dedent(cls.__doc__))
        elif what == 'formatter':
            cls = find_formatter_class(name)
            print("Help on the %s formatter:" % cls.name)
            print(dedent(cls.__doc__))
        elif what == 'filter':
            cls = find_filter_class(name)
            print("Help on the %s filter:" % name)
            print(dedent(cls.__doc__))
        return 0
    except (AttributeError, ValueError):
        print("%s not found!" % what, file=sys.stderr)
        return 1
示例#6
0
def _print_help(what, name):
    try:
        if what == 'lexer':
            cls = get_lexer_by_name(name)
            print("Help on the %s lexer:" % cls.name)
            print(dedent(cls.__doc__))
        elif what == 'formatter':
            cls = find_formatter_class(name)
            print("Help on the %s formatter:" % cls.name)
            print(dedent(cls.__doc__))
        elif what == 'filter':
            cls = find_filter_class(name)
            print("Help on the %s filter:" % name)
            print(dedent(cls.__doc__))
        return 0
    except (AttributeError, ValueError):
        print("%s not found!" % what, file=sys.stderr)
        return 1
示例#7
0
def view_crawler_app(request, crawler_id):
    crawler_item = Crawler.objects.get(id=crawler_id)
    crawler_app = crawler_item.crawler_app

    try:
        code_text = ''
        file_object = open(crawler_app, 'r')
        for line in file_object:
            code_text = code_text + line
    finally:
        file_object.close()

    highted_code = highlight(code_text,
                             lexers.find_lexer_class_by_name('python')(),
                             formatters.find_formatter_class('html')())

    model = {'code_text': highted_code}

    return render(request, 'html/crawler_app_view.html', model)
License: [BSD](http://www.opensource.org/licenses/bsd-license.php)
"""
from __future__ import absolute_import
from __future__ import unicode_literals
import re
from markdown import Extension
from markdown.treeprocessors import Treeprocessor
from markdown import util as md_util
import copy
from collections import OrderedDict
try:
    from pygments import highlight
    from pygments.lexers import get_lexer_by_name, guess_lexer
    from pygments.formatters import find_formatter_class
    HtmlFormatter = find_formatter_class('html')
    pygments = True
except ImportError:  # pragma: no cover
    pygments = False
try:
    from markdown.extensions.codehilite import CodeHiliteExtension
except Exception:  # pragma: no cover
    CodeHiliteExtension = None

CODE_WRAP = '<pre%s><code%s>%s</code></pre>'
CLASS_ATTR = ' class="%s"'
DEFAULT_CONFIG = {
    'use_pygments': [
        True, 'Use Pygments to highlight code blocks. '
        'Disable if using a JavaScript library. '
        'Default: True'
示例#9
0
License: [BSD](http://www.opensource.org/licenses/bsd-license.php)
"""
from __future__ import absolute_import
from __future__ import unicode_literals
from markdown import Extension
from markdown.treeprocessors import Treeprocessor
from markdown import util as md_util
import copy
from collections import OrderedDict
import re
try:
    from pygments import highlight
    from pygments.lexers import get_lexer_by_name, guess_lexer
    from pygments.formatters import find_formatter_class
    HtmlFormatter = find_formatter_class('html')
    pygments = True
except ImportError:  # pragma: no cover
    pygments = False
try:
    from markdown.extensions.codehilite import CodeHiliteExtension
except Exception:  # pragma: no cover
    CodeHiliteExtension = None

CODE_WRAP = '<pre%s><code%s>%s</code></pre>'
CLASS_ATTR = ' class="%s"'
DEFAULT_CONFIG = {
    'use_pygments': [
        True,
        'Use Pygments to highlight code blocks. '
        'Disable if using a JavaScript library. '