本文整理汇总了Python中pygments.lexers.TextLexer方法的典型用法代码示例。如果您正苦于以下问题:Python lexers.TextLexer方法的具体用法?Python lexers.TextLexer怎么用?Python lexers.TextLexer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygments.lexers
的用法示例。
在下文中一共展示了lexers.TextLexer方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import TextLexer [as 别名]
def __init__(self, enable_pretty=True):
QWidget.__init__(self)
layout = QVBoxLayout()
self.enable_pretty = enable_pretty
self.setLayout(layout)
self.layout().setSpacing(0)
self.layout().setContentsMargins(0, 0, 0, 0)
self.lexer = TextLexer()
self.textedit = QTextEdit()
self.textedit.setAcceptRichText(False)
doc = self.textedit.document()
font = doc.defaultFont()
font.setFamily("Courier New")
font.setPointSize(10)
doc.setDefaultFont(font)
doc.addResource(QTextDocument.ImageResource,
QUrl(self.byte_url),
HextEditor.byte_image)
self.textedit.focusInEvent = self.focus_in_event
self.textedit.focusOutEvent = self.focus_left_event
self.data = b''
self.pretty_mode = False
self.layout().addWidget(self.textedit)
示例2: pygment_html_render
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import TextLexer [as 别名]
def pygment_html_render(s, lexer=lexers.TextLexer):
"""Highlight text using a given Lexer"""
return highlight(s, lexer(), HtmlFormatter(linenos=True))
示例3: get_attr_renderer
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import TextLexer [as 别名]
def get_attr_renderer():
"""Return Dictionary containing different Pygements Lexers for Rendering & Highlighting"""
return {
'bash_command': lambda x: render(x, lexers.BashLexer),
'hql': lambda x: render(x, lexers.SqlLexer),
'sql': lambda x: render(x, lexers.SqlLexer),
'doc': lambda x: render(x, lexers.TextLexer),
'doc_json': lambda x: render(x, lexers.JsonLexer),
'doc_rst': lambda x: render(x, lexers.RstLexer),
'doc_yaml': lambda x: render(x, lexers.YamlLexer),
'doc_md': wrapped_markdown,
'python_callable': lambda x: render(get_python_source(x), lexers.PythonLexer),
}
示例4: get_lexer_for_file
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import TextLexer [as 别名]
def get_lexer_for_file(filename):
ext = os.path.splitext(filename)[1]
try:
lexer = lexers.get_lexer_for_filename(filename)
except lexers.ClassNotFound:
if ext == '.kv':
lexer = KivyLexer()
else:
lexer = lexers.TextLexer()
# print('found {} for {}'.format(lexer, filename))
return lexer
示例5: preformatted_emit
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import TextLexer [as 别名]
def preformatted_emit(self, node):
content = node.content
lines = content.split("\n")
if lines[0].startswith("#!code"):
lexer_name = lines[0].split()[1]
del lines[0]
else:
lexer_name = None
content = "\n".join(lines)
try:
lexer = get_lexer_by_name(lexer_name)
except ClassNotFound:
lexer = TextLexer()
return highlight(content, lexer, HtmlFormatter(cssclass="syntax")).strip()
示例6: run
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import TextLexer [as 别名]
def run(self, lines):
def repl(m):
try:
lexer = get_lexer_by_name(m.group(1))
except ValueError:
lexer = TextLexer()
code = highlight(m.group(2), lexer, self.formatter)
code = code.replace('\n\n', '\n \n').replace('\n', '<br />')
return '\n\n<div class="code">%s</div>\n\n' % code
joined_lines = "\n".join(lines)
joined_lines = self.pattern.sub(repl, joined_lines)
return joined_lines.split("\n")
示例7: run
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import TextLexer [as 别名]
def run(self):
self.assert_has_content()
try:
lexer = get_lexer_by_name(self.arguments[0])
except ValueError:
# no lexer found - use the text one instead of an exception
lexer = TextLexer()
# take an arbitrary option if more than one is given
formatter = self.options and VARIANTS[list(self.options)[0]] or DEFAULT
parsed = highlight(u'\n'.join(self.content), lexer, formatter)
return [nodes.raw('', parsed, format='html')]
示例8: setUp
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import TextLexer [as 别名]
def setUp(self):
self.log_printer = ListLogPrinter()
self.console_printer = ConsolePrinter(print_colored=False)
self.no_color = not self.console_printer.print_colored
self.file_diff_dict = {}
self.section = Section('t')
self.local_bears = OrderedDict([('default', [SomelocalBear]),
('test', [SomelocalBear])])
self.global_bears = OrderedDict([('default', [SomeglobalBear]),
('test', [SomeglobalBear])])
self.old_open_editor_applicable = OpenEditorAction.is_applicable
OpenEditorAction.is_applicable = staticmethod(
lambda *args: 'OpenEditorAction cannot be applied')
self.old_apply_patch_applicable = ApplyPatchAction.is_applicable
ApplyPatchAction.is_applicable = staticmethod(
lambda *args: 'ApplyPatchAction cannot be applied')
self.lexer = TextLexer()
self.lexer.add_filter(VisibleWhitespaceFilter(
spaces=True,
tabs=True,
tabsize=SpacingHelper.DEFAULT_TAB_WIDTH))
patcher = patch('coalib.results.result_actions.OpenEditorAction.'
'subprocess')
self.addCleanup(patcher.stop)
patcher.start()
示例9: highlight_text
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import TextLexer [as 别名]
def highlight_text(no_color, text, style, lexer=TextLexer()):
formatter = TerminalTrueColorFormatter(style=style)
if no_color:
formatter = TerminalTrueColorFormatter(style=NoColorStyle)
return highlight(text, lexer, formatter)[:-1]
示例10: run
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import TextLexer [as 别名]
def run(self, lines):
def repl(m):
try:
lexer = get_lexer_by_name(m.group(1))
except (ValueError, NameError):
lexer = TextLexer()
code = m.group(2).replace('\t', ' ')
code = pygments.highlight(code, lexer, self.formatter)
code = code.replace('\n\n', '\n \n').replace('\n', '<br />').replace('\\@', '@')
return '\n\n%s\n\n' % code
ret = self.pattern.sub(repl, "\n".join(lines))
return ret.split("\n")
示例11: hilite
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import TextLexer [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)
示例12: print_lines
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import TextLexer [as 别名]
def print_lines(console_printer,
file_dict,
sourcerange):
"""
Prints the lines between the current and the result line. If needed
they will be shortened.
:param console_printer: Object to print messages on the console.
:param file_dict: A dictionary containing all files as values with
filenames as key.
:param sourcerange: The SourceRange object referring to the related
lines to print.
"""
no_color = not console_printer.print_colored
for i in range(sourcerange.start.line, sourcerange.end.line + 1):
# Print affected file's line number in the sidebar.
console_printer.print(format_lines(lines='', line_nr=i, symbol='['),
color=FILE_LINES_COLOR,
end='')
line = file_dict[sourcerange.file][i - 1].rstrip('\n')
try:
lexer = get_lexer_for_filename(sourcerange.file)
except ClassNotFound:
lexer = TextLexer()
lexer.add_filter(VisibleWhitespaceFilter(
spaces=True, tabs=True,
tabsize=SpacingHelper.DEFAULT_TAB_WIDTH))
# highlight() combines lexer and formatter to output a ``str``
# object.
printed_chars = 0
if i == sourcerange.start.line and sourcerange.start.column:
console_printer.print(highlight_text(
no_color, line[:sourcerange.start.column - 1],
BackgroundMessageStyle, lexer), end='')
printed_chars = sourcerange.start.column - 1
if i == sourcerange.end.line and sourcerange.end.column:
console_printer.print(highlight_text(
no_color, line[printed_chars:sourcerange.end.column - 1],
BackgroundSourceRangeStyle, lexer), end='')
console_printer.print(highlight_text(
no_color, line[sourcerange.end.column - 1:],
BackgroundSourceRangeStyle, lexer), end='')
console_printer.print('')
else:
console_printer.print(highlight_text(
no_color, line[printed_chars:], BackgroundMessageStyle, lexer),
end='')
console_printer.print('')