Exemplo n.º 1
0
 def __init__(self,
              stripnl=False,
              stripall=False,
              ensurenl=True,
              tabsize=tabideal,
              encoding=default_encoding):
     """Initialize the Python syntax highlighter."""
     Python3Lexer.__init__(self,
                           stripnl=stripnl,
                           stripall=stripall,
                           ensurenl=ensurenl,
                           tabsize=tabsize,
                           encoding=default_encoding)
     self.original_add_filter, self.add_filter = self.add_filter, lenient_add_filter
Exemplo n.º 2
0
Arquivo: lain.py Projeto: 5l1v3r1/lain
def printh(input):
    stdout.write("\x1b[0;35m")
    stdout.write("│ ")
    stdout.write("\x1b[0;33m")
    stdout.write(
        highlight(input, Python3Lexer(), Terminal256Formatter(
            style="friendly"))[:-1].replace("# ", "") + "\n")
Exemplo n.º 3
0
def main():
    """The main function."""
    expr = parser_factory(dummy_styler)
    pph = PPHighlighter(parser_factory)
    lexer = Python3Lexer()
    formatter = Terminal256Formatter()

    # Generate the test data
    random.seed(0)
    data = [[random.randrange(1000) for _ in range(100)] for _ in range(100)]
    s = str(data)

    # Perform the benchmarks
    t1 = time.perf_counter()
    for _ in range(N_RUNS):
        result = expr.parseString(s, parseAll=True)[0]
    t2 = time.perf_counter()
    for _ in range(N_RUNS):
        fragments = pph.highlight(s)
    t3 = time.perf_counter()
    for _ in range(N_RUNS):
        highlight(s, lexer, formatter)
    t4 = time.perf_counter()

    # Verify the results
    assert data == result
    assert s == ''.join(text for _, text in fragments)

    # Display the results
    print('Input string size: {} chars'.format(len(s)))
    print('Parsing completed in {:.3f}ms'.format((t2 - t1) / N_RUNS * 1000))
    print('Highlighting completed in {:.3f}ms'.format(
        (t3 - t2) / N_RUNS * 1000))
    print('Pygments highlighting completed in {:.3f}ms'.format(
        (t4 - t3) / N_RUNS * 1000))
Exemplo n.º 4
0
async def coder_print(event):
    if not event.reply_to_msg_id:
        return await eod(event, "`Reply to a file or message!`", time=5)
    msg = await event.get_reply_message()
    if msg.document:
        a = await event.client.download_media(await event.get_reply_message(),
                                              "ncode.png")
        with open(a, "r") as s:
            c = s.read()
    else:
        a = None
        c = msg.text
    pygments.highlight(
        c,
        Python3Lexer(),
        ImageFormatter(line_numbers=True),
        "result.png",
    )
    res = await event.client.send_message(
        event.chat_id,
        "**Pasting this code on my page...**",
        reply_to=event.reply_to_msg_id,
    )
    await event.client.send_file(event.chat_id,
                                 "result.png",
                                 force_document=True,
                                 reply_to=event.reply_to_msg_id)
    await res.delete()
    await event.delete()
    if a:
        os.remove(a)
    os.remove("result.png")
Exemplo n.º 5
0
    def main(new_width=n):
        # attempt to open image from user-input
        d = down
        try:
            image = PIL.Image.open(d)
        except:
            pass

        # convert image to ascii
        new_image_data = pixels_to_ascii(grayify(resize_image(image)))

        # format
        pixel_count = len(new_image_data)
        ascii_image = "\n".join([
            new_image_data[index:(index + new_width)]
            for index in range(0, pixel_count, new_width)
        ])

        # save result to "ascii_image.txt"
        with open("ascii_image.txt", "w") as f:
            f.write(ascii_image)
        s = open("ascii_image.txt", 'r')
        c = s.read()
        s.close()
        pygments.highlight(
            f"{c}", Python3Lexer(),
            ImageFormatter(font_name="DejaVu Sans Mono", line_numbers=False),
            "ascii.png")
        imgs = "ascii.png"
        shvm = PIL.Image.open(imgs)
        sh1, vam = image.size
        img = shvm.resize((int(sh1), int(vam)))
        img.save("asci.png", format="PNG", optimize=True)
    def __init__(self, file_path):
        super(CodeViewer, self).__init__()

        self.setWindowTitle(f'Code: [{file_path}]')
        self.setStyleSheet("font: 10pt \"MS Shell Dlg 2\";")
        self.resize(800, 600)

        self.ui_text = QTextEdit(self)
        self.ui_text.setReadOnly(True)
        self.ui_text.setAcceptRichText(True)

        self.ui_layout = QVBoxLayout(self)
        self.ui_layout.addWidget(self.ui_text)
        self.setLayout(self.ui_layout)

        code = Path(file_path).read_text()

        try:
            from pygments import highlight
            from pygments.lexers import Python3Lexer
            from pygments.formatters import HtmlFormatter
            code_lexer = Python3Lexer()
            formatter = HtmlFormatter(style='colorful')
            self.ui_text.document().setDefaultStyleSheet(
                formatter.get_style_defs())
            html = highlight(code, code_lexer, formatter)
            self.ui_text.setText(html)

        except ModuleNotFoundError:
            self.ui_text.setText(code)
            print(
                'No "pygments" module found. Please install pygments to enable code highlighting.'
            )
            print('   pip install pygments')
Exemplo n.º 7
0
def print_args():
    """Prints out all command-line parameters."""
    bg = terminal_bg()
    if bg is True:
        style = 'xcode'
    elif bg is False:
        style = 'monokai'

    pprint = print_
    try:
        if bg is not None:
            import pygments
            from pygments.lexers import Python3Lexer
            from pygments.formatters import Terminal256Formatter
            pprint = partial(pygments.highlight,
                             lexer=Python3Lexer(),
                             formatter=Terminal256Formatter(style=style),
                             outfile=sys.stdout)
    except ImportError:
        pass
    print_('Parameters:')
    for key in sorted(ARGS):
        v = repr(getattr(ARGS, key))
        print_('% 14s: ' % key, end='')
        pprint(v)
    print_()
Exemplo n.º 8
0
async def coder_print(event):
    a = await event.client.download_media(await event.get_reply_message(),
                                          Var.TEMP_DOWNLOAD_DIRECTORY)
    s = open(a, 'r')
    c = s.read()
    s.close()
    pygments.highlight(
        f"{c}", Python3Lexer(),
        ImageFormatter(font_name="DejaVu Sans Mono", line_numbers=True),
        "out.png")
    res = await event.client.send_message(
        event.chat_id,
        "**Pasting this code on my page pls weit🤓...**",
        reply_to=event.reply_to_msg_id)
    await event.client.send_file(event.chat_id,
                                 "out.png",
                                 force_document=True,
                                 reply_to=event.reply_to_msg_id)
    await event.client.send_file(event.chat_id,
                                 "out.png",
                                 force_document=False,
                                 reply_to=event.reply_to_msg_id)
    await res.delete()
    await event.delete()
    os.remove(a)
    os.remove('out.png')
Exemplo n.º 9
0
async def coder_print(event):
    cmd = event.text
    a = await event.get_reply_message()
    coder = ""
    if len(cmd) > 7:
        coder = " ".join(cmd[7:])
    elif event.reply_to_msg_id and len(cmd) == 6:
        coder = a.message
    elif len(cmd) == 6:
        await event.reply("`No text Given`")
        await asyncio.sleep(2)
        await event.delete()
        return
    pygments.highlight(
        f"{coder}",
        Python3Lexer(),
        ImageFormatter(font_name="LiberationMono-Regular.ttf",
                       line_numbers=True),
        "out.png",
    )
    await event.client.send_file(event.chat_id,
                                 "out.png",
                                 force_document=False)
    await event.delete()
    os.remove("out.png")
Exemplo n.º 10
0
async def coder_print(event):
    input = event.pattern_match.group(1)
    a = await event.client.download_media(await event.get_reply_message(),
                                          Var.TEMP_DOWNLOAD_DIRECTORY)
    s = open(a, 'r')
    c = s.read()
    s.close()
    pygments.highlight(
        f"{c}", Python3Lexer(),
        ImageFormatter(font_name="DejaVu Sans Mono", line_numbers=True),
        "out.png")
    if 'doc' in input:
        await event.client.send_file(event.chat_id,
                                     "out.png",
                                     force_document=True)
        await event.delete()
        os.remove(a)
        os.remove('out.png')
    else:
        await event.client.send_file(event.chat_id,
                                     "out.png",
                                     force_document=False)
        await event.delete()
        os.remove(a)
        os.remove('out.png')
Exemplo n.º 11
0
def printargs():
    """Prints out all command-line parameters."""
    switch = {
        BGColor.LIGHT: 'xcode',
        BGColor.DARK: 'vim',
        BGColor.UNKNOWN: 'default'
    }
    style = switch[terminal_bg()]
    pprint = print
    try:
        import pygments
        from pygments.lexers import Python3Lexer
        from pygments.formatters import Terminal256Formatter
        pprint = partial(pygments.highlight,
                         lexer=Python3Lexer(),
                         formatter=Terminal256Formatter(style=style),
                         outfile=sys.stdout)
    except ImportError:
        pass
    print('Parameters:')
    for key in sorted(ARGS):
        v = repr(getattr(ARGS, key))
        print('% 16s: ' % key, end='')
        pprint(v)
    print()
def render_example(site, kw, in_name, out_name):
    """Render a single .py file to HTML with formatting.

    Parameters
    ==========
    site:
        An instance of a Nikola site, available in any plugin as ``self.site``
    kw:
        A dictionary of keywords for this task
    in_name:
        The file to be rendered, as an instance of pathlib.Path
    out_name:
        A pathlib.Path instance pointing to the rendered output file

    """
    code = highlight(
        in_name.read_bytes(), Python3Lexer(), utils.NikolaPygmentsHTML(in_name.name)
    )

    title = in_name.name

    permalink = out_name.relative_to(kw["output_folder"])
    source_link = permalink.stem  # remove '.html'
    context = {
        "code": code,
        "title": title,
        "permalink": str(permalink),
        "lang": kw["default_lang"],
        "description": title,
        "source_link": source_link,
        "pagekind": ["example"],
    }
    site.render_template("examples.tmpl", str(out_name), context)
Exemplo n.º 13
0
async def mkc(event):
    a = await event.client.download_media(await event.get_reply_message(),
                                          Config.TMP_DOWNLOAD_DIRECTORY)
    s = open(a, "r")
    c = s.read()
    s.close()
    pygments.highlight(
        f"{c}",
        Python3Lexer(),
        ImageFormatter(font_name="LiberationMono-Regular.ttf",
                       line_numbers=True),
        "out.png",
    )
    res = await event.client.send_message(
        event.chat_id,
        "**Pasting this code on my page...**",
        reply_to=event.reply_to_msg_id,
    )
    await event.client.send_file(event.chat_id,
                                 "out.png",
                                 force_document=True,
                                 reply_to=event.reply_to_msg_id)
    await event.client.send_file(event.chat_id,
                                 "out.png",
                                 force_document=False,
                                 reply_to=event.reply_to_msg_id)
    await res.delete()
    await event.delete()
    os.remove(a)
    os.remove("out.png")
Exemplo n.º 14
0
 def __init__(self, filter=None):
     self._formatter = NullFormatter()
     self._lexer = Python3Lexer(ensurenl=False, stripnl=False)
     self._lexer.add_filter("tokenmerge")
     if filter is None:
         filter = ConverterFilter()
     self._lexer.add_filter(filter)
Exemplo n.º 15
0
def parsekeywordpairs(signature):
    tokens = Python3Lexer().get_tokens(signature)
    preamble = True
    stack = []
    substack = []
    parendepth = 0
    for token, value in tokens:
        if preamble:
            if token is Token.Punctuation and value == "(":
                preamble = False
            continue

        if token is Token.Punctuation:
            if value in ("(", "{", "["):
                parendepth += 1
            elif value in (")", "}", "]"):
                parendepth -= 1
            elif value == ":" and parendepth == -1:
                # End of signature reached
                break
            if (value == "," and parendepth == 0) or (value == ")"
                                                      and parendepth == -1):
                stack.append(substack)
                substack = []
                continue

        if value and (parendepth > 0 or value.strip()):
            substack.append(value)

    return {item[0]: "".join(item[2:]) for item in stack if len(item) >= 3}
Exemplo n.º 16
0
def exe_run(args):
    expath = os.path.join(settings.installdir, "examples", "**", "*.py")
    exfiles = [f for f in glob.glob(expath, recursive=True)]
    f2search = os.path.basename(args.run).lower()
    matching = [s for s in exfiles if (f2search in os.path.basename(s).lower() and "__" not in s)]
    matching = list(sorted(matching))
    nmat = len(matching)
    if nmat == 0:
        printc("No matching example found containing string:", args.run, c=1)
        printc(" Current installation directory is:", settings.installdir, c=1)
        exit(1)

    if nmat > 1:
        printc("\nSelect one of", nmat, "matching scripts:", c='y', italic=1)
        args.full_screen=True # to print out the one line description

    if args.full_screen: # -f option not to dump the full code but just the first line
        for mat in matching[:25]:
            printc(os.path.basename(mat).replace('.py',''), c='y', italic=1, end=' ')
            with open(mat) as fm:
                lline = ''.join(fm.readlines(60))
                lline = lline.replace('\n',' ').replace('\'','').replace('\"','').replace('-','')
                line = lline[:56] #cut
                if line.startswith('from'): line=''
                if line.startswith('import'): line=''
                if len(lline) > len(line):
                    line += '..'
                if len(line)>5:
                    printc('-', line,  c='y', bold=0, italic=1)
                else:
                    print()

    if nmat>25:
        printc('...', c='y')

    if nmat > 1:
        exit(0)

    if not args.full_screen: # -f option not to dump the full code
        with open(matching[0]) as fm:
            code = fm.read()
        code = "#"*80 + "\n" + code + "\n"+ "#"*80

        try:
            from pygments import highlight
            from pygments.lexers import Python3Lexer
            from pygments.formatters import Terminal256Formatter
            # from pygments.styles import STYLE_MAP
            # print(STYLE_MAP.keys())
            result = highlight(code, Python3Lexer(), Terminal256Formatter(style='zenburn'))
            print(result, end='')

        except:
            printc(code, italic=1, bold=0)
            printc("To colorize code try:  pip install Pygments")
        # print()

    printc("("+matching[0]+")", c='y', bold=0, italic=1)
    os.system('python3 ' + matching[0])
Exemplo n.º 17
0
    def get_tokens_unprocessed(self, text):
        pylexer = Python3Lexer(**self.options)
        tblexer = Python3TracebackLexer(**self.options)
        if ">>>" not in text:
            if (text.startswith('Traceback (most recent call last):') or
                        re.match(r'^  File "[^"]+", line \d+\n$', text)):
                yield from tblexer.get_tokens_unprocessed(text)
            else:
                yield from pylexer.get_tokens_unprocessed(text)
            return

        curcode = ''
        insertions = []
        curtb = ''
        tbindex = 0
        tb = 0

        def do_current_code():
            nonlocal curcode
            nonlocal insertions
            if curcode:
                for item in do_insertions(insertions,
                                          pylexer.get_tokens_unprocessed(curcode)):
                    yield item
                curcode = ''
                insertions = []

        section = ""
        code = list(line_re.finditer(text))
        while code:
            match = code.pop(0)
            line = match.group()
            if line.startswith(">>>"):
                insertions = []
                insertions.append((0,
                                   [(0, Generic.Prompt, line[:4])]))
                section = line[4:]
                secindex = match.start()
                while code and code[0].group().startswith("   "):
                    line = code.pop(0).group()
                    if not line.strip():
                        break
                    insertions.append((len(section),
                                        [(0, Generic.Prompt, line[:4])]))
                    section += line[4:]
                for i, t, v in do_insertions(insertions,
                                            pylexer.get_tokens_unprocessed(section)):
                    yield secindex+i, t, v
            elif line.startswith('Traceback (most recent call last):') or re.match(r' *File "[^"]+", line \d+\n$', line):
                tb = line
                tbindex = match.start()
                while code and not code[0].group().startswith(">>>"):
                    tb += code.pop(0).group()
                for i, t, v in tblexer.get_tokens_unprocessed(tb):
                    yield tbindex+i, t, v
            else:
                yield match.start(), Generic.Output, line
    def __init__(self, parent, lexer=None):
        super().__init__(parent)

        self._document = self.document()
        self._formatter = HtmlFormatter(nowrap=True)
        self.set_style('default')
        if lexer is not None:
            self._lexer = lexer
        else:
            self._lexer = Python3Lexer()
Exemplo n.º 19
0
 def _funcname_and_argnum(cls, line):
     """Parse out the current function name and arg from a line of code."""
     # each list in stack:
     # [full_expr, function_expr, arg_number, opening]
     # arg_number may be a string if we've encountered a keyword
     # argument so we're done counting
     stack = [["", "", 0, ""]]
     try:
         for (token, value) in Python3Lexer().get_tokens(line):
             if token is Token.Punctuation:
                 if value in "([{":
                     stack.append(["", "", 0, value])
                 elif value in ")]}":
                     full, _, _, start = stack.pop()
                     expr = start + full + value
                     stack[-1][1] += expr
                     stack[-1][0] += expr
                 elif value == ",":
                     try:
                         stack[-1][2] += 1
                     except TypeError:
                         stack[-1][2] = ""
                     stack[-1][1] = ""
                     stack[-1][0] += value
                 elif value == ":" and stack[-1][3] == "lambda":
                     expr = stack.pop()[0] + ":"
                     stack[-1][1] += expr
                     stack[-1][0] += expr
                 else:
                     stack[-1][1] = ""
                     stack[-1][0] += value
             elif (token is Token.Number or token in Token.Number.subtypes
                   or token is Token.Name or token in Token.Name.subtypes
                   or token is Token.Operator and value == "."):
                 stack[-1][1] += value
                 stack[-1][0] += value
             elif token is Token.Operator and value == "=":
                 stack[-1][2] = stack[-1][1]
                 stack[-1][1] = ""
                 stack[-1][0] += value
             elif token is Token.Number or token in Token.Number.subtypes:
                 stack[-1][1] = value
                 stack[-1][0] += value
             elif token is Token.Keyword and value == "lambda":
                 stack.append([value, "", 0, value])
             else:
                 stack[-1][1] = ""
                 stack[-1][0] += value
         while stack[-1][3] in "[{":
             stack.pop()
         _, _, arg_number, _ = stack.pop()
         _, func, _, _ = stack.pop()
         return func, arg_number
     except IndexError:
         return None, None
Exemplo n.º 20
0
	def __init__(self, *args, **kwargs):
		super(TextBox, self).__init__(*args, **kwargs)
		self.current_file = ''
		self.modified = False # Not implemented, denotes that current file isn't saved
		self.current_lexer = Python3Lexer()
		self.filetypes = [tuple(i) for i in settings['filetypes']]
		self.stats = StringVar()
		self.stats.set(f"{lang['stats'][0]}: {self.index(INSERT)}; {lang['stats'][1]}: {int(self.index('end').split('.')[0]) - 1}; {lang['stats'][2]}: {len(self.get('1.0', 'end')) - 1}")
		self.config(font='Verdana 12 normal')
		self.binds()
		self.tag_configs()
		self.focus()
Exemplo n.º 21
0
def _highlight(code, filename):
    try:
        import pygments
    except ImportError:
        return code

    from pygments.lexers import Python3Lexer, YamlLexer
    from pygments.formatters import Terminal256Formatter

    lexer = Python3Lexer() if filename.endswith(".py") else YamlLexer()
    code = pygments.highlight(code, lexer, Terminal256Formatter(style="monokai"))
    return code
Exemplo n.º 22
0
def dump_color(obj):
    # source: https://gist.github.com/EdwardBetts/0814484fdf7bbf808f6f
    from pygments import highlight

    # Module name actually exists, but pygments loads things in a strange manner
    from pygments.lexers import Python3Lexer  # pylint: disable=no-name-in-module
    from pygments.formatters.terminal256 import (Terminal256Formatter)  # pylint: disable=no-name-in-module

    for attr in dir(obj):
        if hasattr(obj, attr):
            obj_data = "obj.%s = %s" % (attr, getattr(obj, attr))
            print(highlight(obj_data, Python3Lexer(), Terminal256Formatter()))
Exemplo n.º 23
0
 def restoreText(self, text):
     """Takes the text and displays it in python style syntax. Unfortunately
     this is a pretty clunky way of doing it, but it wouldn't work otherwise
     """
     pos = self.textCursor().position()
     text = highlight(text, Python3Lexer(), self.formatter)
     textSplit = text.split("</pre>")
     textSplit[0] = textSplit[0].strip()
     text = "</pre>".join(textSplit)
     self.setHtml(text)
     for i in range(pos):
         self.moveCursor(QG.QTextCursor.NextCharacter)
Exemplo n.º 24
0
def next_token_inside_string(code_string, inside_string):
    """Given a code string s and an initial state inside_string, return
    whether the next token will be inside a string or not."""
    for token, value in Python3Lexer().get_tokens(code_string):
        if token is Token.String:
            value = value.lstrip("bBrRuU")
            if value in ('"""', "'''", '"', "'"):
                if not inside_string:
                    inside_string = value
                elif value == inside_string:
                    inside_string = False
    return inside_string
Exemplo n.º 25
0
 def render(self, size, focus=False):
     (maxcol,) = size
     #num_pudding = maxcol / len(self.text.encode('utf-8'))
     cursor = None
     if focus:
         cursor = self.get_cursor_coords(size)
     tokens = Python3Lexer().get_tokens(self.text.encode('utf-8'))
     logger.info(tokens)
     color_attribute = self.color_attribute(tokens)
     logger.info(color_attribute)
     #import rpdb; rpdb.Rpdb().set_trace()
     logger.info(self.text.encode('utf-8'))
     return urwid.TextCanvas([self.text.encode('utf-8')], attr=[color_attribute], cursor=cursor, maxcol=maxcol)
Exemplo n.º 26
0
 def setup(self, pdb):  # pylint:disable=W0621
     """Override pdbpp mappings and colors."""
     # See https://github.com/antocuni/pdb/issues/36
     pdb_class = pdb.__class__
     # Aliases
     pdb_class.do_l = pdb_class.do_longlist
     pdb_class.do_ll = pdb_class.do_list
     pdb_class.do_st = pdb_class.do_sticky
     pdb_class.do_ev = pdb_class.do_edit
     pdb_class.do_ip = pdb_class.do_interact
     # Colors
     pdb_class._lexer = Python3Lexer()
     pdb_class._fmt = TerminalTrueColorFormatter(style=OneDarkish)
Exemplo n.º 27
0
 def parse(self, start='1.0'):
     data = self.get(start, 'end')
     while data and '\n' == data[0]:
         start = self.index('%s+1c' % start)
         data = data[1:]
     self.mark_set('range_start', start)
     for t in self._syntax_highlighting_tags:
         self.tag_remove(t, start, "range_start +%ic" % len(data))
     for token, content in lex(data, Python3Lexer()):
         self.mark_set("range_end", "range_start + %ic" % len(content))
         for t in token.split():
             self.tag_add(str(t), "range_start", "range_end")
         self.mark_set("range_start", "range_end")
Exemplo n.º 28
0
async def paste_img(event):
    "To paste text to image."
    reply_to = await reply_id(event)
    d_file_name = None
    catevent = await edit_or_reply(event, "`Pasting the text on image`")
    input_str = event.pattern_match.group(1)
    reply = await event.get_reply_message()
    ext = re.findall(r"-f", input_str)
    extension = None
    try:
        extension = ext[0].replace("-", "")
        input_str = input_str.replace(ext[0], "").strip()
    except IndexError:
        extension = None
    text_to_print = ""
    if input_str:
        text_to_print = input_str
    if text_to_print == "" and reply.media:
        mediatype = media_type(reply)
        if mediatype == "Document":
            d_file_name = await event.client.download_media(
                reply, Config.TEMP_DIR)
            with open(d_file_name, "r") as f:
                text_to_print = f.read()
    if text_to_print == "":
        if reply.text:
            text_to_print = reply.raw_text
        else:
            return await edit_delete(
                catevent,
                "`Either reply to text/code file or reply to text message or give text along with command`",
            )
    pygments.highlight(
        text_to_print,
        Python3Lexer(),
        ImageFormatter(font_name="DejaVu Sans Mono", line_numbers=True),
        "out.png",
    )
    try:
        await event.client.send_file(
            event.chat_id,
            "out.png",
            force_document=bool(extension),
            reply_to=reply_to,
        )
        await catevent.delete()
        os.remove("out.png")
        if d_file_name is not None:
            os.remove(d_file_name)
    except Exception as e:
        await edit_delete(catevent, f"**Error:**\n`{str(e)}`", time=10)
Exemplo n.º 29
0
    def get_code(self, *args, **kwargs):
        name = args[0]
        if any([name.startswith(x) for x in self._prompt.shebangs]):
            name = name[1:]

        callback = self._commands[name]
        source = _inspect.getsourcelines(callback)[0]
        source = ''.join(source)

        if not pygments:
            return source
        else:
            return pygments.highlight(source, Python3Lexer(),
                                      Terminal256Formatter(style='vim'))
Exemplo n.º 30
0
def format_code(code):
    pp = Python3Lexer()
    tokens = pp.get_tokens(code)
    formatted = ""
    for token, string in tokens:
        updated = False
        for span_class, checker in tokenizer_map.items():
            if checker(token):
                formatted += f'<span class="token {span_class}">{string}</span>'
                updated = True
                break
        if not updated:
            formatted += string
    return formatted
Exemplo n.º 31
0
 def __init__(self, stripnl=False, stripall=False, ensurenl=True, tabsize=tabideal, encoding=default_encoding):
     """Initialize the Python syntax highlighter."""
     Python3Lexer.__init__(self, stripnl=stripnl, stripall=stripall, ensurenl=ensurenl, tabsize=tabsize, encoding=default_encoding)
     self.original_add_filter, self.add_filter = self.add_filter, lenient_add_filter