当前位置: 首页>>代码示例>>Python>>正文


Python special.TextLexer方法代码示例

本文整理汇总了Python中pygments.lexers.special.TextLexer方法的典型用法代码示例。如果您正苦于以下问题:Python special.TextLexer方法的具体用法?Python special.TextLexer怎么用?Python special.TextLexer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pygments.lexers.special的用法示例。


在下文中一共展示了special.TextLexer方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _parse_src

# 需要导入模块: from pygments.lexers import special [as 别名]
# 或者: from pygments.lexers.special import TextLexer [as 别名]
def _parse_src(cls, src_contents, src_filename):
        """
        Return a stream of `(token_type, value)` tuples
        parsed from `src_contents` (str)

        Uses `src_filename` to guess the type of file
        so it can highlight syntax correctly.
        """

        # Parse the source into tokens
        try:
            lexer = guess_lexer_for_filename(src_filename, src_contents)
        except ClassNotFound:
            lexer = TextLexer()

        # Ensure that we don't strip newlines from
        # the source file when lexing.
        lexer.stripnl = False

        return pygments.lex(src_contents, lexer) 
开发者ID:Bachmann1234,项目名称:diff_cover,代码行数:22,代码来源:snippets.py

示例2: format_data

# 需要导入模块: from pygments.lexers import special [as 别名]
# 或者: from pygments.lexers.special import TextLexer [as 别名]
def format_data(data):
    formatter = HtmlFormatter(cssclass="codehilite")

    if data is None:
        return data

    if isinstance(data, bool) or isinstance(data, int) or isinstance(data, float):
        return highlight(str(data), TextLexer(), formatter)
    elif isinstance(data, str):
        try:
            data = json.dumps(json.loads(data), indent=4, sort_keys=True)
            lexer = JsonLexer()
        except (ValueError, TypeError):
            lexer = TextLexer()
    elif isinstance(data, dict) or isinstance(data, list):
        data = json.dumps(data, indent=4, sort_keys=True)
        lexer = JsonLexer()
    else:
        lexer = TextLexer()

    lexer.stripall = True
    return highlight(data, lexer, formatter) 
开发者ID:ansible-community,项目名称:ara,代码行数:24,代码来源:pygments_highlights.py

示例3: set_mime_type

# 需要导入模块: from pygments.lexers import special [as 别名]
# 或者: from pygments.lexers.special import TextLexer [as 别名]
def set_mime_type(self, mime_type):
        """
        Update the highlighter lexer based on a mime type.

        :param mime_type: mime type of the new lexer to setup.
        """
        try:
            self.set_lexer_from_mime_type(mime_type)
        except ClassNotFound:
            _logger().exception('failed to get lexer from mimetype')
            self._lexer = TextLexer()
            return False
        except ImportError:
            # import error while loading some pygments plugins, the editor
            # should not crash
            _logger().warning('failed to get lexer from mimetype (%s)' %
                              mime_type)
            self._lexer = TextLexer()
            return False
        else:
            return True 
开发者ID:TuringApp,项目名称:Turing,代码行数:23,代码来源:pygments_sh.py

示例4: set_lexer_from_filename

# 需要导入模块: from pygments.lexers import special [as 别名]
# 或者: from pygments.lexers.special import TextLexer [as 别名]
def set_lexer_from_filename(self, filename):
        """
        Change the lexer based on the filename (actually only the extension is
        needed)

        :param filename: Filename or extension
        """
        self._lexer = None
        if filename.endswith("~"):
            filename = filename[0:len(filename) - 1]
        try:
            self._lexer = get_lexer_for_filename(filename)
        except (ClassNotFound, ImportError):
            print('class not found for url', filename)
            try:
                m = mimetypes.guess_type(filename)
                print(m)
                self._lexer = get_lexer_for_mimetype(m[0])
            except (ClassNotFound, IndexError, ImportError):
                self._lexer = get_lexer_for_mimetype('text/plain')
        if self._lexer is None:
            _logger().warning('failed to get lexer from filename: %s, using '
                              'plain text instead...', filename)
            self._lexer = TextLexer() 
开发者ID:TuringApp,项目名称:Turing,代码行数:26,代码来源:pygments_sh.py

示例5: view_file

# 需要导入模块: from pygments.lexers import special [as 别名]
# 或者: from pygments.lexers.special import TextLexer [as 别名]
def view_file(filename):
    """ Views file with syntax highlighting (if applicable)

    Args:
        filename (str): Full path to filename to render view response for.
    """
    folder = filename.split(_AEON_TOPDIR).pop().strip('/')
    filename = os.path.join(_AEON_TOPDIR, filename)
    try:
        with open(filename, 'r') as f:
            data = f.read()

            # lexer = guess_lexer_for_filename(filename, data)
            formatter = HtmlFormatter(linenos=True)
        try:
            lexer = get_lexer_for_filename(filename)
            code = highlight(data, lexer, formatter)
        except ClassNotFound:
            lexer = TextLexer()
            code = highlight(data, lexer, formatter)
        stat = os.stat(filename)
        return render_template('view.html', content=code, folder=folder, stat=stat, filename=filename)

    except (OSError, IOError) as e:
        code = e[0]
        reason = e[1]
        flash('Error: Could not view file {filename}: {reason} ({code})'.format(filename=filename, reason=reason,
                                                                                code=code), 'danger')
        return render_template('view.html') 
开发者ID:Apstra,项目名称:aeon-ztps,代码行数:31,代码来源:views.py

示例6: format_rtf

# 需要导入模块: from pygments.lexers import special [as 别名]
# 或者: from pygments.lexers.special import TextLexer [as 别名]
def format_rtf(t):
    tokensource = list(TextLexer().get_tokens(t))
    fmt = RtfFormatter()
    buf = StringIO()
    fmt.format(tokensource, buf)
    result = buf.getvalue()
    buf.close()
    return result 
开发者ID:pygments,项目名称:pygments,代码行数:10,代码来源:test_rtf_formatter.py

示例7: fetch_lexer

# 需要导入模块: from pygments.lexers import special [as 别名]
# 或者: from pygments.lexers.special import TextLexer [as 别名]
def fetch_lexer(
        source: str,
        language: str = None,
        filename: str = None,
        mime_type: str = None
) -> Lexer:
    """

    :param source:
    :param language:
    :param filename:
    :param mime_type:
    :return:
    """

    environ.abort_thread()

    try:
        if language:
            return get_lexer_by_name(language, stripall=True)
    except ClassNotFound:
        pass

    if filename:
        try:
            return get_lexer_for_filename(filename, stripall=True)
        except ClassNotFound:
            pass

        try:
            return guess_lexer_for_filename(filename, source, stripall=True)
        except ClassNotFound:
            pass

    try:
        if mime_type:
            return get_lexer_for_mimetype(mime_type, stripall=True)
    except ClassNotFound:
        pass

    try:
        return guess_lexer(source, stripall=True)
    except ClassNotFound:
        return TextLexer() 
开发者ID:sernst,项目名称:cauldron,代码行数:46,代码来源:syntax_highlighting.py

示例8: _pygments_highlight

# 需要导入模块: from pygments.lexers import special [as 别名]
# 或者: from pygments.lexers.special import TextLexer [as 别名]
def _pygments_highlight(source, output_formatter, language='ipython', metadata=None):
    """
    Return a syntax-highlighted version of the input source

    Parameters
    ----------
    source : str
        source of the cell to highlight
    output_formatter : Pygments formatter
    language : str
        language to highlight the syntax of
    metadata : NotebookNode cell metadata
        metadata of the cell to highlight
    """
    from pygments import highlight
    from pygments.lexers import get_lexer_by_name
    from pygments.util import ClassNotFound

    # If the cell uses a magic extension language,
    # use the magic language instead.
    if language.startswith('ipython') \
        and metadata \
        and 'magics_language' in metadata:

        language = metadata['magics_language']

    lexer = None
    if language == 'ipython2':
        try:
            from IPython.lib.lexers import IPythonLexer
        except ImportError:
            warn("IPython lexer unavailable, falling back on Python")
            language = 'python'
        else:
            lexer = IPythonLexer()
    elif language == 'ipython3':
        try:
            from IPython.lib.lexers import IPython3Lexer
        except ImportError:
            warn("IPython3 lexer unavailable, falling back on Python 3")
            language = 'python3'
        else:
            lexer = IPython3Lexer()

    if lexer is None:
        try:
            lexer = get_lexer_by_name(language, stripall=True)
        except ClassNotFound:
            warn("No lexer found for language %r. Treating as plain text." % language)
            from pygments.lexers.special import TextLexer
            lexer = TextLexer()


    return highlight(source, lexer, output_formatter) 
开发者ID:holzschu,项目名称:Carnets,代码行数:56,代码来源:highlight.py


注:本文中的pygments.lexers.special.TextLexer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。