本文整理汇总了Python中pygments.lexers.guess_lexer_for_filename函数的典型用法代码示例。如果您正苦于以下问题:Python guess_lexer_for_filename函数的具体用法?Python guess_lexer_for_filename怎么用?Python guess_lexer_for_filename使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了guess_lexer_for_filename函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: highlight_code
def highlight_code(path, src, div=False, **kwargs):
src = decode_charset_to_unicode(src)
try:
if path.endswith(('.html', '.mako')):
lexer = MakoHtmlLexer(encoding='utf-8')
elif path.endswith('.ptl'):
lexer = PythonLexer(encoding='utf-8')
elif path.endswith('.md'):
lexer = RstLexer(encoding='utf-8')
else:
if path.endswith(IGNORE_FILE_EXTS):
src = 'Hmm.., this is binary file.'
lexer = guess_lexer_for_filename(path, src)
lexer.encoding = 'utf-8'
lexer.stripnl = False
except ClassNotFound:
# no code highlight
lexer = TextLexer(encoding='utf-8')
if div:
formatter = _CodeHtmlFormatter
else:
formatter = HtmlFormatter
src = highlight(src, lexer, formatter(linenos=True,
lineanchors='L',
anchorlinenos=True,
encoding='utf-8',
**kwargs))
return src
示例2: _detect_lang_name
def _detect_lang_name(self, subject, paste_content):
lexer = None
if '.' in subject:
if subject[-1] == ')':
if ' (' in subject:
name = subject.split(' (')[0]
elif '(' in subject:
name = subject.split('(')[0]
else:
name = subject
else:
name = subject
if name.split('.')[-1] in self.recognized_extenstions:
try:
lexer = guess_lexer_for_filename(name, paste_content, encoding='utf-8')
except (ClassNotFound, ImportError):
pass
if lexer is None and len(paste_content) >= 20:
try:
lexer = guess_lexer(paste_content, encoding='utf-8')
except (ClassNotFound, ImportError):
pass
if lexer is None:
try:
lexer = get_lexer_by_name('text', encoding='utf-8')
except (ClassNotFound, ImportError) as e:
self.log(self.logger.WARNING, '%s: %s' % (subject, e))
return ''
return lexer.aliases[0]
示例3: show_submission_source_view
def show_submission_source_view(request, contest_id, submission_id):
submission = get_submission_or_404(request, contest_id, submission_id, ProgramSubmission)
raw_source = submission.source_file.read()
raw_source, decode_error = decode_str(raw_source)
filename = submission.source_file.file.name
is_source_safe = False
try:
lexer = guess_lexer_for_filename(filename, raw_source)
formatter = HtmlFormatter(linenos=True, line_number_chars=3, cssclass="syntax-highlight")
formatted_source = highlight(raw_source, lexer, formatter)
formatted_source_css = HtmlFormatter().get_style_defs(".syntax-highlight")
is_source_safe = True
except ClassNotFound:
formatted_source = raw_source
formatted_source_css = ""
download_url = reverse(
"download_submission_source", kwargs={"contest_id": request.contest.id, "submission_id": submission_id}
)
return TemplateResponse(
request,
"programs/source.html",
{
"source": formatted_source,
"css": formatted_source_css,
"is_source_safe": is_source_safe,
"download_url": download_url,
"decode_error": decode_error,
},
)
示例4: _generate_preview_html
def _generate_preview_html(self, data):
"""Return the first few truncated lines of the text file."""
from reviewboard.diffviewer.chunk_generator import \
NoWrapperHtmlFormatter
charset = self.mimetype[2].get('charset', 'ascii')
try:
text = data.decode(charset)
except UnicodeDecodeError:
logging.error('Could not decode text file attachment %s using '
'charset "%s"',
self.attachment.pk, charset)
text = data.decode('utf-8', 'replace')
try:
lexer = guess_lexer_for_filename(self.attachment.filename, text)
except ClassNotFound:
lexer = TextLexer()
lines = highlight(text, lexer, NoWrapperHtmlFormatter()).splitlines()
return ''.join([
'<pre>%s</pre>' % line
for line in lines[:self.TEXT_CROP_NUM_HEIGHT]
])
示例5: get_highlighted_code
def get_highlighted_code(name, code, type="terminal"):
"""
If pygments are available on the system
then returned output is colored. Otherwise
unchanged content is returned.
"""
import logging
try:
import pygments
pygments
except ImportError:
return code
from pygments import highlight
from pygments.lexers import guess_lexer_for_filename, ClassNotFound
from pygments.formatters import TerminalFormatter
try:
lexer = guess_lexer_for_filename(name, code)
formatter = TerminalFormatter()
content = highlight(code, lexer, formatter)
except ClassNotFound:
logging.debug("Couldn't guess Lexer, will not use pygments.")
content = code
return content
示例6: process
def process(context):
"""Main routine for metrics."""
metrics = {}
# import all the needed metric modules
metric_modules = __import_metric_modules(context['include_metrics'])
# instantiate all the desired metric classes
metric_instance = __instantiate_metric(metric_modules, context)
cm = ComputeMetrics(metric_instance, context)
# main loop
for i, in_file in enumerate(context['in_file_names']):
# print 'file %i: %s' % (i, in_file)
try:
cm.reset()
fin = open(os.path.join(context['base'], in_file), 'r')
code = ''.join(fin.readlines())
fin.close()
# define lexographical scanner to use for this run
try:
lex = guess_lexer_for_filename(in_file, code, encoding='guess')
# encoding is 'guess', chardet', 'utf-8'
except:
pass
else:
token_list = lex.get_tokens(code) # parse code
metrics[in_file] = {}
metrics[in_file].update(cm(token_list))
metrics[in_file]['language'] = lex.name # provide language
except IOError, e:
sys.stderr.writelines(str(e) + " -- Skipping input file.\n\n")
示例7: show_submission_source_view_unsafe
def show_submission_source_view_unsafe(request, submission_id, source_file,
download_url):
raw_source, decode_error = decode_str(source_file.read())
filename = source_file.file.name
is_source_safe = False
try:
lexer = guess_lexer_for_filename(
filename,
raw_source
)
formatter = HtmlFormatter(linenos=True, line_number_chars=3,
cssclass='syntax-highlight')
formatted_source = highlight(raw_source, lexer, formatter)
formatted_source_css = HtmlFormatter() \
.get_style_defs('.syntax-highlight')
is_source_safe = True
except ClassNotFound:
formatted_source = raw_source
formatted_source_css = ''
return TemplateResponse(request, 'programs/source.html', {
'source': formatted_source,
'css': formatted_source_css,
'is_source_safe': is_source_safe,
'download_url': download_url,
'decode_error': decode_error,
'submission_id': submission_id
})
示例8: get_highlighted
def get_highlighted(self, filename, hl_lines=None):
"""Get the highlighted version of a file."""
hl_lines = sorted(hl_lines or [])
st = os.stat(filename)
key = '%s-%d-%s-%s' % (filename, int(st.st_mtime),
CACHE_SERIAL, hl_lines)
key = os.path.join(self.cache_dir,
hashlib.sha1(key).hexdigest() + '.html.gz')
try:
with gzip.open(key) as keyfile:
return keyfile.read()
except IOError:
with open(filename) as infile:
file_data = infile.read()
try:
lexer = lexers.guess_lexer_for_filename(filename, file_data)
except pygments.util.ClassNotFound:
try:
lexer = lexers.guess_lexer(file_data)
except pygments.util.ClassNotFound:
lexer = lexers.TextLexer()
highlight = pygments.highlight(
file_data, lexer, formatters.HtmlFormatter(
hl_lines=hl_lines, linenos='table', lineanchors='line',
anchorlinenos=True))
with gzip.open(key, 'w') as keyfile:
keyfile.write(highlight.encode('utf-8'))
return highlight
示例9: programas
def programas(request, arquivo):
erros = False
codigo = ''
nome = 'Erro!'
caminho = os.path.join(programas_dir, arquivo)
if os.path.isfile(caminho):
try:
with open(caminho) as programa:
texto = programa.read().decode('utf-8')
except IOError:
erros = True
else:
nome = os.path.basename(arquivo)
#Se não conseguir adivinhar a linguagem do programa exibe texto
try:
lexer = guess_lexer_for_filename(arquivo, texto)
except ValueError:
try:
lexer = guess_lexer(texto)
except ValueError:
lexer = TextLexer
# linenos pode ser inline, table, True ou ''
# replace corrige problema com {% spaceless %}
codigo = highlight(texto, lexer, HtmlFormatter(linenos=False)).replace('\n', '<br>\n')
else:
erros = True
return render_to_response('programas.html',
{'erros': erros, 'nome': nome, 'codigo': codigo},
context_instance=RequestContext(request))
示例10: _highlight_syntax
def _highlight_syntax(self):
"""If we have pygments, syntax highlight the file."""
if self.syntax_format:
stringified_text = \
''.join(str(s.decode("utf-8")) for s in self.read_lines())
if self.syntax_format is not True:
lexer = get_lexer_by_name(self.syntax_format)
elif self.file_name:
try:
lexer = \
guess_lexer_for_filename(
self.file_name, stringified_text)
except TypeError:
# XXX pygments py3 incompatibility workaround; fixed in tip
lexer = guess_lexer(stringified_text)
else:
lexer = guess_lexer(stringified_text)
highlighted_text = \
highlight(
stringified_text, lexer,
TerminalFormatter(bg="dark")).split('\n')
line_num = 1
try:
for line in highlighted_text:
self._lines[
self._find_line_index(line_num)].update_highlight(line)
line_num += 1
except KeyError:
# our conversions to strings sometimes adds a trailing line
pass
示例11: show_submission_source_view
def show_submission_source_view(request, contest_id, submission_id):
submission = get_object_or_404(ProgramSubmission, id=submission_id)
if contest_id != submission.problem_instance.contest_id:
raise Http404
check_submission_access(request, submission)
raw_source = submission.source_file.read()
filename = submission.source_file.file.name
is_source_safe = True
try:
lexer = guess_lexer_for_filename(
filename,
raw_source
)
formatter = HtmlFormatter(linenos=True, cssclass='syntax-highlight')
formatted_source = highlight(raw_source, lexer, formatter)
formatted_source_css = HtmlFormatter().get_style_defs('.syntax-highlight')
except ClassNotFound:
formatted_source = raw_source
formatted_source_css = ''
is_source_safe = False
return TemplateResponse(request, 'programs/source.html', {
'source': formatted_source,
'css': formatted_source_css,
'is_source_safe': is_source_safe
})
示例12: __init__
def __init__(self, parent, lexer=None, filename="a.txt"):
super(GenericHighlighter, self).__init__(parent=parent, lexer=lexer)
self._document = self.document()
self._formatter = HtmlFormatter()
self._lexer = guess_lexer_for_filename(filename, "")
print(self._lexer)
self.set_style('default')
示例13: get_lexer_for_filename
def get_lexer_for_filename(filename, text="", **options):
"""Gets a lexer from a filename (usually via the filename extension).
This mimics the behavior of ``pygments.lexers.get_lexer_for_filename()``
and ``pygments.lexers.guess_lexer_for_filename()``.
"""
if CACHE is None:
load_or_build()
exts = CACHE["lexers"]["exts"]
fname = os.path.basename(filename)
key = fname if fname in exts else os.path.splitext(fname)[1]
if key in exts:
modname, clsname = exts[key]
mod = importlib.import_module(modname)
cls = getattr(mod, clsname)
lexer = cls(**options)
else:
# couldn't find lexer in cache, fallback to the hard way
import inspect
from pygments.lexers import guess_lexer_for_filename
lexer = guess_lexer_for_filename(filename, text, **options)
# add this filename to the cache for future use
cls = type(lexer)
mod = inspect.getmodule(cls)
exts[fname] = (mod.__name__, cls.__name__)
write_cache(cache_filename())
return lexer
示例14: to_html
def to_html(self):
"""Convert file document to html"""
source_dir = get_source_directory()
css_path = source_dir + "printing.css"
fin = open(self.path, 'r')
code = fin.read()
#cmd = "source-highlight -n --style-css-file {0} --tab 2 -f html -i {1}".format(css_path, self.path)
#p = Popen(cmd.split(), shell=False, stdout=PIPE)
file_path_name = os.path.relpath(self.path, get_current_directory())
html_string = """<h1>File: {0}</h1>
<h3>Created By: {1}, Date: {2}</h3>
""".format(file_path_name,
self.username,
str(self.modifytime))
lexer = guess_lexer_for_filename('test.py', code, stripall=True)
linenos = False
if self.args.linenumbers == True:
linenos = 'inline'
formatter = HtmlFormatter(style='bw', linenos=linenos)
html_string += highlight(code, lexer, formatter)
fin.close()
return html_string
示例15: handleMatch
def handleMatch(self, match):
params = match.group('params') or ''
rel_include_path = match.group('path')
source_dir = os.path.dirname(self.source_path)
include_path = os.path.join(source_dir, rel_include_path)
try:
with open(include_path) as include_file:
file_text = include_file.read()
except IOError as e:
raise IOError('Markdown file {0} tried to include file {1}, got '
'{2}'.format(self.source_path,
rel_include_path,
e.strerror))
include_text = choose_include_text(file_text, params, self.source_path)
if not include_text:
raise TaskError('Markdown file {0} tried to include file {1} but '
'filtered out everything'.format(self.source_path,
rel_include_path))
el = markdown.util.etree.Element('div')
el.set('class', 'md-included-snippet')
try:
lexer = guess_lexer_for_filename(include_path, file_text)
except ClassNotFound:
# e.g., ClassNotFound: no lexer for filename u'BUILD' found
if 'BUILD' in include_path:
lexer = PythonLexer()
else:
lexer = TextLexer() # the boring plain-text lexer
html_snippet = highlight(include_text,
lexer,
HtmlFormatter(cssclass='codehilite'))
el.text = html_snippet
return el