Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
__init__.py83 linesDownload Raw Back to pygments
1"""2    Pygments3    ~~~~~~~~4 5    Pygments is a syntax highlighting package written in Python.6 7    It is a generic syntax highlighter for general use in all kinds of software8    such as forum systems, wikis or other applications that need to prettify9    source code. Highlights are:10 11    * a wide range of common languages and markup formats is supported12    * special attention is paid to details, increasing quality by a fair amount13    * support for new languages and formats are added easily14    * a number of output formats, presently HTML, LaTeX, RTF, SVG, all image15      formats that PIL supports, and ANSI sequences16    * it is usable as a command-line tool and as a library17    * ... and it highlights even Brainfuck!18 19    The `Pygments master branch`_ is installable with ``easy_install Pygments==dev``.20 21    .. _Pygments master branch:22       https://github.com/pygments/pygments/archive/master.zip#egg=Pygments-dev23 24    :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.25    :license: BSD, see LICENSE for details.26"""27from io import StringIO, BytesIO28 29__version__ = '2.18.0'30__docformat__ = 'restructuredtext'31 32__all__ = ['lex', 'format', 'highlight']33 34 35def lex(code, lexer):36    """37    Lex `code` with the `lexer` (must be a `Lexer` instance)38    and return an iterable of tokens. Currently, this only calls39    `lexer.get_tokens()`.40    """41    try:42        return lexer.get_tokens(code)43    except TypeError:44        # Heuristic to catch a common mistake.45        from pygments.lexer import RegexLexer46        if isinstance(lexer, type) and issubclass(lexer, RegexLexer):47            raise TypeError('lex() argument must be a lexer instance, '48                            'not a class')49        raise50 51 52def format(tokens, formatter, outfile=None):  # pylint: disable=redefined-builtin53    """54    Format ``tokens`` (an iterable of tokens) with the formatter ``formatter``55    (a `Formatter` instance).56 57    If ``outfile`` is given and a valid file object (an object with a58    ``write`` method), the result will be written to it, otherwise it59    is returned as a string.60    """61    try:62        if not outfile:63            realoutfile = getattr(formatter, 'encoding', None) and BytesIO() or StringIO()64            formatter.format(tokens, realoutfile)65            return realoutfile.getvalue()66        else:67            formatter.format(tokens, outfile)68    except TypeError:69        # Heuristic to catch a common mistake.70        from pygments.formatter import Formatter71        if isinstance(formatter, type) and issubclass(formatter, Formatter):72            raise TypeError('format() argument must be a formatter instance, '73                            'not a class')74        raise75 76 77def highlight(code, lexer, formatter, outfile=None):78    """79    This is the most high-level highlighting function. It combines `lex` and80    `format` in one function.81    """82    return format(lex(code, lexer), formatter, outfile)83 
codekingpro/portable-devtools · Team Ai