本文整理汇总了Python中pygments.lexers.guess_lexer方法的典型用法代码示例。如果您正苦于以下问题:Python lexers.guess_lexer方法的具体用法?Python lexers.guess_lexer怎么用?Python lexers.guess_lexer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygments.lexers
的用法示例。
在下文中一共展示了lexers.guess_lexer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: guess_language
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def guess_language(raw: str, filename: Optional[str] = None) -> str:
options = {'stripnl': True}
# Guess a lexer based on filename and raw text first
if filename:
try:
return guess_lexer_for_filename(filename, raw, **options).aliases[0]
except (ValueError, IndexError):
pass
# If that didn't work guess lexer just by looking at the raw text
try:
language = guess_lexer(raw, **options).aliases[0]
except (ValueError, IndexError):
# If no lexer was detected, fallback to plain text.
return 'text'
# These are odd lexers that match far too often, so exclude them.
if language in GUESS_LANG_IGNORES:
return 'text'
# Finally check for language overrides and return
return GUESS_LANG_OVERRIDES.get(language, language)
示例2: test_guess_c_lexer
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def test_guess_c_lexer():
code = '''
#include <stdio.h>
#include <stdlib.h>
int main(void);
int main(void) {
uint8_t x = 42;
uint8_t y = x + 1;
/* exit 1 for success! */
return 1;
}
'''
lexer = guess_lexer(code)
assert isinstance(lexer, CLexer)
示例3: get_lexer
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def get_lexer(self, src, language):
"""Get the Pygments lexer."""
if language:
language, lexer_options = self.get_extended_language(language)
else:
lexer_options = {}
# Try and get lexer by the name given.
try:
lexer = get_lexer_by_name(language, **lexer_options)
except Exception:
lexer = None
if lexer is None:
if self.guess_lang:
try:
lexer = guess_lexer(src)
except Exception: # pragma: no cover
pass
if lexer is None:
lexer = get_lexer_by_name('text')
return lexer
示例4: lang_for_block
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def lang_for_block(source, lang):
if lang in ('py', 'python'):
if source.startswith('>>>'):
# interactive session
return 'pycon'
else:
# maybe Python -- try parsing it
if try_parse(source):
return 'python'
else: # Guess
return lang_for_block(source, 'guess')
elif lang in ('python3', 'py3') and source.startswith('>>>'):
# for py3, recognize interactive sessions, but do not try parsing...
return 'pycon3'
elif lang == 'guess':
try:
# return 'python'
lexer = guess_lexer(source)
return lexer.aliases[0]
except Exception:
return None
else:
return lang
示例5: highlight_file
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def highlight_file(self, linenos=True, style='default'):
""" Highlight the input file, and return HTML as a string. """
try:
lexer = lexers.get_lexer_for_filename(self.input_file)
except pygments.util.ClassNotFound:
# Try guessing the lexer (file type) later.
lexer = None
try:
formatter = formatters.HtmlFormatter(
linenos=linenos,
style=style,
full=True)
except pygments.util.ClassNotFound:
logging.error("\nInvalid style name: {}\nExpecting one of:\n \
{}".format(style, "\n ".join(sorted(styles.STYLE_MAP))))
sys.exit(1)
try:
with open(self.input_file, "r") as f:
content = f.read()
try:
lexer = lexer or lexers.guess_lexer(content)
except pygments.util.ClassNotFound:
# No lexer could be guessed.
lexer = lexers.get_lexer_by_name("text")
except EnvironmentError as exread:
fmt = "\nUnable to read file: {}\n{}"
logging.error(fmt.format(self.input_file, exread))
sys.exit(2)
return pygments.highlight(content, lexer, formatter)
示例6: render_fenced_code
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def render_fenced_code(self, element):
code = element.children[0].children
options = CodeHiliteRendererMixin.options.copy()
options.update(_parse_extras(getattr(element, "extra", None)))
if element.lang:
try:
lexer = get_lexer_by_name(element.lang, stripall=True)
except ClassNotFound:
lexer = guess_lexer(code)
else:
lexer = guess_lexer(code)
formatter = html.HtmlFormatter(**options)
return highlight(code, lexer, formatter)
示例7: set_lexer
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def set_lexer(self):
"""
Try to detect the lexer by filename if it fails
then try to guess the lex by shebang statement.
The shebang statement should be placed in the first
20 lines of the file.
"""
try:
self.lexer = get_lexer_for_filename(self.area.filename, '')
except Exception as e:
self.lexer = guess_lexer(self.area.get('1.0', '20.0'))
示例8: render_block_code
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def render_block_code(self, token):
code = token.children[0].content
lexer = get_lexer(token.language) if token.language else guess_lexer(code)
return highlight(code, lexer, self.formatter)
示例9: _highlight_code
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def _highlight_code(self, match):
code = match.group(1)
try:
if self.body_lexer:
lexer = get_lexer_by_name(self.body_lexer)
else:
lexer = guess_lexer(code.strip())
except ClassNotFound:
lexer = get_lexer_by_name('text')
for item in lexer.get_tokens_unprocessed(code):
yield item
示例10: test_get_lexers
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [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
示例11: highlight
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def highlight(html: str) -> str:
soup = BeautifulSoup(html, "html5lib")
for code in soup.find_all("code"):
if not code.parent.name == "pre":
continue
lexer = guess_lexer(code.text)
tag = BeautifulSoup(
phighlight(code.text, lexer, _FORMATTER), "html5lib"
).body.next
pre = code.parent
pre.replaceWith(tag)
out = soup.body
out.name = "div"
return str(out)
示例12: detect_language_from_file
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def detect_language_from_file(file_path):
if os.path.exists(file_path):
try:
with open(file_path, 'r') as file:
code = file.read()
language = guess_lexer(code)
if language.name == 'Objective-C':
return language.name
else:
return "MATLAB"
except:
return None
示例13: _default_lexer
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def _default_lexer(self):
d = self.declaration
if d.language:
return lexers.find_lexer_class_by_name(d.language)()
return lexers.guess_lexer(d.source)
示例14: syntax_hl
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def syntax_hl(src, lang=None, guess_lang=False, inline=False, code_wrap=False):
"""Highlight."""
css_class = 'highlight'
src = src.strip('\n')
try:
lexer = get_lexer_by_name(lang)
except ValueError:
try:
if guess_lang:
lexer = guess_lexer(src)
else:
lexer = get_lexer_by_name('text')
except ValueError:
lexer = get_lexer_by_name('text')
if inline:
formatter = SublimeInlineHtmlFormatter(
cssclass=css_class
)
elif code_wrap:
formatter = SublimeWrapBlockFormatter(
cssclass=css_class
)
else:
formatter = SublimeBlockFormatter(
cssclass=css_class
)
return highlight(src, lexer, formatter)
示例15: hilite
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def hilite(self):
"""
Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with
optional line numbers. The output should then be styled with css to
your liking. No styles are applied by default - only styling hooks
(i.e.: <span class="k">).
returns : A string of html.
"""
self.src = self.src.strip('\n')
if self.lang is None:
self._getLang()
if pygments:
try:
lexer = get_lexer_by_name(self.lang)
except ValueError:
try:
if self.guess_lang:
lexer = guess_lexer(self.src)
else:
lexer = TextLexer()
except ValueError:
lexer = TextLexer()
formatter = HtmlFormatter(linenos=self.linenos,
cssclass=self.css_class,
style=self.style,
noclasses=self.noclasses)
return highlight(self.src, lexer, formatter)
else:
# just escape and build markup usable by JS highlighting libs
txt = self.src.replace('&', '&')
txt = txt.replace('<', '<')
txt = txt.replace('>', '>')
txt = txt.replace('"', '"')
classes = []
if self.lang:
classes.append('language-%s' % self.lang)
if self.linenos:
classes.append('linenums')
class_str = ''
if classes:
class_str = ' class="%s"' % ' '.join(classes)
return '<pre class="%s"><code%s>%s</code></pre>\n'% \
(self.css_class, class_str, txt)