本文整理汇总了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)
示例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)
示例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
示例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()
示例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')
示例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
示例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()
示例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)