本文整理汇总了Python中pygments.lexers.get_lexer_for_mimetype方法的典型用法代码示例。如果您正苦于以下问题:Python lexers.get_lexer_for_mimetype方法的具体用法?Python lexers.get_lexer_for_mimetype怎么用?Python lexers.get_lexer_for_mimetype使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygments.lexers
的用法示例。
在下文中一共展示了lexers.get_lexer_for_mimetype方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: content_callback
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import get_lexer_for_mimetype [as 别名]
def content_callback(self, match):
content_type = getattr(self, 'content_type', None)
content = match.group()
offset = match.start()
if content_type:
from pygments.lexers import get_lexer_for_mimetype
possible_lexer_mimetypes = [content_type]
if '+' in content_type:
# application/calendar+xml can be treated as application/xml
# if there's not a better match.
general_type = re.sub(r'^(.*)/.*\+(.*)$', r'\1/\2',
content_type)
possible_lexer_mimetypes.append(general_type)
for i in possible_lexer_mimetypes:
try:
lexer = get_lexer_for_mimetype(i)
except ClassNotFound:
pass
else:
for idx, token, value in lexer.get_tokens_unprocessed(content):
yield offset + idx, token, value
return
yield offset, Text, content
示例2: content_callback
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import get_lexer_for_mimetype [as 别名]
def content_callback(self, match):
content_type = getattr(self, "content_type", None)
content = match.group()
offset = match.start()
if content_type:
from pygments.lexers import get_lexer_for_mimetype
try:
lexer = get_lexer_for_mimetype(content_type)
except ClassNotFound:
pass
else:
for idx, token, value in lexer.get_tokens_unprocessed(content):
yield offset + idx, token, value
return
yield offset, Text, content
示例3: set_lexer_from_filename
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import get_lexer_for_mimetype [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()
示例4: get_lexer
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import get_lexer_for_mimetype [as 别名]
def get_lexer(text, lang):
"""Tries to get lexer for the text whether the lang is provided or not"""
if lang:
try:
return get_lexer_by_name(lang, stripall=False)
except:
pass
return None
# No language provided, try to guess
mime = getMagicMimeFromBuffer(text.strip())
if mime:
try:
return get_lexer_for_mimetype(mime, stripall=False)
except:
pass
# The pygments data sometimes miss mime options provided by python magic
# library
if mime.startswith('text/'):
try:
return get_lexer_for_mimetype(mime.replace('text/',
'application/'),
stripall=False)
except:
pass
return None
# The start/end tags could be:
# @startuml / @enduml
# @startgantt / @endgantt
# @startsalt / @endsalt
# @startmindmap / @endmindmap
# @startwbs / @endwbs
# @startditaa / @endditaa
# @startjcckit / @endjcckit
示例5: content_callback
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import get_lexer_for_mimetype [as 别名]
def content_callback(self, match):
content_type = getattr(self, 'content_type', None)
content = match.group()
offset = match.start()
if content_type:
from pygments.lexers import get_lexer_for_mimetype
try:
lexer = get_lexer_for_mimetype(content_type)
except ClassNotFound:
pass
else:
for idx, token, value in lexer.get_tokens_unprocessed(content):
yield offset + idx, token, value
return
yield offset, Text, content
示例6: generate_content
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import get_lexer_for_mimetype [as 别名]
def generate_content(cls, attachment):
mime_type = attachment.file.content_type
lexer = cls.CUSTOM_LEXERS.get(mime_type)
if lexer is None:
lexer = get_lexer_for_mimetype(mime_type)
with attachment.file.open() as f:
html_formatter = HtmlFormatter(style='tango', linenos='inline', prestyles='mono')
html_code = highlight(f.read(), lexer, html_formatter)
css_code = html_formatter.get_style_defs('.highlight')
return render_template('previewer_code:pygments_preview.html', attachment=attachment,
html_code=html_code, css_code=css_code)
示例7: pretty_body
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import get_lexer_for_mimetype [as 别名]
def pretty_body(msg):
from .util import printable_data
to_ret = printable_data(msg.body, colors=False)
if 'content-type' in msg.headers:
try:
lexer = get_lexer_for_mimetype(msg.headers.get('content-type').split(';')[0])
to_ret = highlight(to_ret, lexer, TerminalFormatter())
except:
pass
return to_ret
示例8: fill_highlighted
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import get_lexer_for_mimetype [as 别名]
def fill_highlighted(self):
with DisableUpdates(self.htmlxml_widg):
self.highlighted_widg.setPlainText("")
if not self.data:
return
ct = self.headers.get('Content-Type').lower()
if ";" in ct:
ct = ct.split(";")[0]
try:
lexer = get_lexer_for_mimetype(ct)
highlighted = textedit_highlight(self.data, lexer)
except:
highlighted = printable_data(self.data)
self.highlighted_widg.setHtml(highlighted)
示例9: get_tokens_unprocessed
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import get_lexer_for_mimetype [as 别名]
def get_tokens_unprocessed(self, text):
try:
split = re.split(r"(?:\r\n|\n)(?:\r\n|\n)", text, 1)
if len(split) == 2:
h = split[0]
body = split[1]
else:
h = split[0]
body = ''
except Exception as e:
for v in self.tl.get_tokens_unprocessed(text):
yield v
raise e
for token in self.hl.get_tokens_unprocessed(h):
yield token
if len(body) > 0:
if len(body) <= self.max_len or self.max_len < 0:
second_parser = None
if "Content-Type" in h:
try:
ct = re.search("Content-Type: (.*)", h)
if ct is not None:
hval = ct.groups()[0]
mime = hval.split(";")[0]
second_parser = get_lexer_for_mimetype(mime)
except ClassNotFound:
pass
if second_parser is None:
yield (len(h), Token.Text, text[len(h):])
else:
for index, tokentype, value in second_parser.get_tokens_unprocessed(text[len(h):]):
yield (index + len(h), tokentype, value)
else:
yield (len(h), Token.Text, text[len(h):])
示例10: get_bodypart_tokens
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import get_lexer_for_mimetype [as 别名]
def get_bodypart_tokens(self, text):
# return if:
# * no content
# * no content type specific
# * content encoding is not readable
# * max recurrsion exceed
if not text.strip() or not self.content_type:
return [(0, Other, text)]
cte = self.content_transfer_encoding
if cte and cte not in {"8bit", "7bit", "quoted-printable"}:
return [(0, Other, text)]
if self.max_nested_level == 0:
return [(0, Other, text)]
# get lexer
try:
lexer = get_lexer_for_mimetype(self.content_type)
except ClassNotFound:
return [(0, Other, text)]
if isinstance(lexer, type(self)):
lexer.max_nested_level = self.max_nested_level - 1
return lexer.get_tokens_unprocessed(text)
示例11: test_get_lexers
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import get_lexer_for_mimetype [as 别名]
def test_get_lexers():
# test that the lexers functions work
for func, args in [(lexers.get_lexer_by_name, ("python",)),
(lexers.get_lexer_for_filename, ("test.py",)),
(lexers.get_lexer_for_mimetype, ("text/x-python",)),
(lexers.guess_lexer, ("#!/usr/bin/python3 -O\nprint",)),
(lexers.guess_lexer_for_filename, ("a.py", "<%= @foo %>"))
]:
x = func(opt='val', *args)
assert isinstance(x, lexers.PythonLexer)
assert x.options["opt"] == "val"
for cls, (_, lname, aliases, _, mimetypes) in lexers.LEXERS.items():
assert cls == lexers.find_lexer_class(lname).__name__
for alias in aliases:
assert cls == lexers.get_lexer_by_name(alias).__class__.__name__
for mimetype in mimetypes:
assert cls == lexers.get_lexer_for_mimetype(mimetype).__class__.__name__
try:
lexers.get_lexer_by_name(None)
except ClassNotFound:
pass
else:
raise Exception
示例12: set_lexer_from_mime_type
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import get_lexer_for_mimetype [as 别名]
def set_lexer_from_mime_type(self, mime, **options):
"""
Sets the pygments lexer from mime type.
:param mime: mime type
:param options: optional addtional options.
"""
self._lexer = get_lexer_for_mimetype(mime, **options)
_logger().debug('lexer for mimetype (%s): %r', mime, self._lexer)
示例13: fetch_lexer
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import get_lexer_for_mimetype [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()