本文整理汇总了Python中pygments.highlight函数的典型用法代码示例。如果您正苦于以下问题:Python highlight函数的具体用法?Python highlight怎么用?Python highlight使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了highlight函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pygments2xpre
def pygments2xpre(s, language="python"):
"Return markup suitable for XPreformatted"
try:
from pygments import highlight
from pygments.formatters import HtmlFormatter
except ImportError:
return s
from pygments.lexers import get_lexer_by_name
l = get_lexer_by_name(language)
h = HtmlFormatter()
# XXX: Does not work in Python 2, since pygments creates non-unicode
# outpur snippets.
# from io import StringIO
from six import StringIO
out = StringIO()
highlight(s, l, h, out)
styles = [
(cls, style.split(";")[0].split(":")[1].strip())
for cls, (style, ttype, level) in h.class2style.items()
if cls and style and style.startswith("color:")
]
from reportlab.lib.pygments2xpre import _2xpre
return _2xpre(out.getvalue(), styles)
示例2: send_message
def send_message(self, mess):
if self.demo_mode:
print(self.md_ansi.convert(mess.body))
else:
bar = '\n╌╌[{mode}]' + ('╌' * 60)
super().send_message(mess)
print(bar.format(mode='MD '))
if ANSI:
print(highlight(mess.body, self.md_lexer, self.terminal_formatter))
else:
print(mess.body)
print(bar.format(mode='HTML'))
html = self.md_html.convert(mess.body)
if ANSI:
print(highlight(html, self.html_lexer, self.terminal_formatter))
else:
print(html)
print(bar.format(mode='TEXT'))
print(self.md_text.convert(mess.body))
print(bar.format(mode='IM '))
print(self.md_im.convert(mess.body))
if ANSI:
print(bar.format(mode='ANSI'))
print(self.md_ansi.convert(mess.body))
print(bar.format(mode='BORDERLESS'))
print(self.md_borderless_ansi.convert(mess.body))
print('\n\n')
示例3: highlightBlock
def highlightBlock(self, text):
"""Takes a block, applies format to the document.
according to what's in it.
"""
# I need to know where in the document we are,
# because our formatting info is global to
# the document
cb = self.currentBlock()
p = cb.position()
# The \n is not really needed, but sometimes
# you are in an empty last block, so your position is
# **after** the end of the document.
text=unicode(self.document().toPlainText())+'\n'
# Yes, re-highlight the whole document.
# There **must** be some optimizacion possibilities
# but it seems fast enough.
highlight(text,self.lexer,self.formatter)
# Just apply the formatting to this block.
# For titles, it may be necessary to backtrack
# and format a couple of blocks **earlier**.
for i in range(len(unicode(text))):
try:
self.setFormat(i,1,self.formatter.data[p+i])
except IndexError:
pass
# I may need to do something about this being called
# too quickly.
self.tstamp=time.time()
示例4: testExternalMultipleMemFuncs
def testExternalMultipleMemFuncs( self ):
className = "ClassName"
funcName = "FuncName"
funcName2 = "FuncName2"
code = """
int %s::%s (int count, char* mess ) { {} {} }
int %s::%s (int count, char* mess ) { }
""" % ( className, funcName, className, funcName2 )
highlight( code, self.lexer, self.formatter)
myRTags = FakeRTags()
test = CppSemantics( myRTags )
for t,v,num in self.formatter.current_line:
test.Feed( t,v,num )
self.assertEqual( myRTags.GetClass()[0].name, className,
"Class Name did not match %s" % myRTags.GetClass()[0].name )
mf = myRTags.GetClass()[0].GetElements()[0]
self.assertEqual( len(myRTags.GetClass()), 1,
"Too many classes (%d) created." % len(myRTags.GetClass()) )
self.assertEqual( mf['method'][0].GetName(), funcName,
"Function Name %s did not match %s" % (mf['method'][0].GetName(), funcName) )
self.assertEqual( test.state, 'start',
"Semantics not in correct state '%s'" % ( test.state ))
示例5: print_action_exception
def print_action_exception(e):
if isinstance(e.inner_exception, (ExecCommandFailed, QueryException)):
print_exception(e.inner_exception)
else:
print '-'*79
print highlight(e.traceback, PythonTracebackLexer(), Formatter())
print '-'*79
示例6: pygments2xpre
def pygments2xpre(s, language="python"):
"Return markup suitable for XPreformatted"
try:
from pygments import highlight
from pygments.formatters import HtmlFormatter
except ImportError:
return s
from pygments.lexers import get_lexer_by_name
rconv = lambda x: x
if isPy3:
out = getStringIO()
else:
if isUnicode(s):
s = asBytes(s)
rconv = asUnicode
out = getBytesIO()
l = get_lexer_by_name(language)
h = HtmlFormatter()
highlight(s,l,h,out)
styles = [(cls, style.split(';')[0].split(':')[1].strip())
for cls, (style, ttype, level) in h.class2style.items()
if cls and style and style.startswith('color:')]
return rconv(_2xpre(out.getvalue(),styles))
示例7: testMemFuncs
def testMemFuncs( self ):
className = "myclassname"
funcName = "myFuncName"
code = """
class %s {
int %s (int count, char* mess ) { {} {} }
};
"""% ( className, funcName )
highlight( code, self.lexer, self.formatter)
myRTags = FakeRTags()
test = CppSemantics( myRTags )
for t,v,num in self.formatter.current_line:
test.Feed( t,v,num )
self.assertEqual( myRTags.GetClass()[0].name, className,
"Class Name did not match %s" % myRTags.GetClass()[0].name )
mf = myRTags.GetClass()[0].GetElements()[0]
self.assertEqual( mf['method'][0].GetName(), funcName,
"Function Name %s did not match %s" % (mf['method'][0].GetName(), funcName) )
self.assertEqual( test.state, 'start',
"Semantics not in correct state '%s'" % ( test.state ))
示例8: pcat
def pcat(filename, target='ipython'):
code = read_file_or_url(filename)
HTML_TEMPLATE = """<style>
{}
</style>
{}
"""
from pygments.lexers import get_lexer_for_filename
lexer = get_lexer_for_filename(filename, stripall=True)
from pygments.formatters import HtmlFormatter, TerminalFormatter
from pygments import highlight
try:
assert(target=='ipython')
from IPython.display import HTML, display
from pygments.formatters import HtmlFormatter
formatter = HtmlFormatter(linenos=True, cssclass="source")
html_code = highlight(code, lexer, formatter)
css = formatter.get_style_defs()
html = HTML_TEMPLATE.format(css, html_code)
htmlres = HTML(html)
return htmlres
except Exception as e:
print(e)
pass
formatter = TerminalFormatter()
output = highlight(code,lexer,formatter)
print(output)
示例9: template
def template(template_id):
"""
The view where we display the result
in syntax-highlighted HTML and CSS
"""
template = Template.query.filter(Template.id == long(template_id)).first()
if not template:
return "The requested template doesn't exist", 404
hashtml = len(template.html.strip()) > 0
cssperma = 'http://%s/static/cssserve/%s.css'%(SITE_DOMAIN, str(template.id))
pygmented_css_link = highlight('<link rel="stylesheet" type="text/css" href="%s">'%cssperma,
CssLexer(),
HtmlFormatter(style = 'bw',
linenos = 'table'))
return render_template('saved_template.html',
template = template,
pygmented_css_link_code = pygmented_css_link,
pygmented_html_code = highlight(template.html,
HtmlLexer(),
HtmlFormatter(style = 'bw',
linenos = 'table')),
pygmented_css_code = highlight(template.css,
CssLexer(),
HtmlFormatter(style = 'bw',
linenos = 'table')),
pygments_style = HtmlFormatter(style = 'bw').get_style_defs('.highlight'),
hashtml = hashtml,
)
示例10: format_codechunks
def format_codechunks(self, chunk):
from pygments import highlight
from pygments.lexers import PythonLexer, TextLexer, PythonConsoleLexer
# from IPythonLexer import IPythonLexer
from pygments.formatters import LatexFormatter
chunk["content"] = highlight(
chunk["content"],
PythonLexer(),
LatexFormatter(verboptions="frame=single,fontsize=\small, xleftmargin=0.5em"),
)
if len(chunk["result"].strip()) > 0 and chunk["results"] == "verbatim":
if chunk["term"]:
chunk["result"] = highlight(
chunk["result"],
PythonLexer(),
LatexFormatter(verboptions="frame=single,fontsize=\small, xleftmargin=0.5em"),
)
else:
chunk["result"] = highlight(
chunk["result"],
TextLexer(),
LatexFormatter(verboptions="frame=leftline,fontsize=\small, xleftmargin=0.5em"),
)
return PwebFormatter.format_codechunks(self, chunk)
示例11: pretty_print
def pretty_print (src, out):
""" `src' is a filepath to be formatted. `out' is a file object
to be written. """
f = os.path.basename(src)
ext = os.path.splitext(src)[1]
if ext != "json":
s = open(src).read()
if ext == "json":
s = json.dumps(json.load(open(src)), indent=2)
elif ext == "":
# is executable script?
if os.access(f, os.X_OK) and s.startswith("#!"):
# this is a script without extention. read shebang
shebang = s.split("\n", 1)[0]
try:
interpreter = shebang.split("env ")[1]
except IndexError:
interpreter = os.path.basename(shebang[2:])
ext = _interpreter2ext(interpreter)
f += "." + ext
# colorful!
try:
lexer = pygments.lexers.get_lexer_for_filename(f)
except pygments.util.ClassNotFound:
lexer = pygments.lexers.get_lexer_for_mimetype("text/plain")
fmt = pygments.formatters.Terminal256Formatter(encoding="utf-8")
pygments.highlight(s, lexer, fmt, out)
示例12: inner
def inner():
assert re.match(r'^[a-zA-Z._-]+$', script)
exec_path = "scripts/" + script + ".py"
cmd = ["python3", "-u", exec_path] # -u: don't buffer output
error = False
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
for line in proc.stdout:
yield highlight(line, BashLexer(), HtmlFormatter())
# Maybe there is more stdout after an error...
for line in proc.stderr:
error = True
yield highlight(line, BashLexer(), HtmlFormatter())
if error:
yield "<script>parent.stream_error()</script>"
else:
yield "<script>parent.stream_success()</script>"
示例13: pretty_print_body
def pretty_print_body(fmt, body):
try:
if fmt.lower() == 'json':
d = json.loads(body.strip())
s = json.dumps(d, indent=4, sort_keys=True)
print pygments.highlight(s, JsonLexer(), TerminalFormatter())
elif fmt.lower() == 'form':
qs = repeatable_parse_qs(body)
for k, v in qs.all_pairs():
s = Colors.GREEN
s += '%s: ' % urllib.unquote(k)
s += Colors.ENDC
s += urllib.unquote(v)
print s
elif fmt.lower() == 'text':
print body
elif fmt.lower() == 'xml':
import xml.dom.minidom
xml = xml.dom.minidom.parseString(body)
print pygments.highlight(xml.toprettyxml(), XmlLexer(), TerminalFormatter())
else:
raise PappyException('"%s" is not a valid format' % fmt)
except PappyException as e:
raise e
except:
raise PappyException('Body could not be parsed as "%s"' % fmt)
示例14: get
def get(self):
self.values["project"] = "http://www.proven-corporation.com/software/app-engine-console/"
if self.values["subpage"] == "usage":
for exampleNum in range(len(self.examples)):
key = "example%d" % (exampleNum + 1)
val = util.trim(self.examples[exampleNum])
val = pygments.highlight(val, self.resultLexer, self.outputFormatter).strip()
self.values[key] = val
elif self.values["subpage"] == "integration":
self.values["example1"] = pygments.highlight(
util.trim(
"""
def is_dev():
import os
return os.environ['SERVER_SOFTWARE'].startswith('Dev')
"""
),
self.pythonLexer,
self.outputFormatter,
).strip()
self.values["example2"] = pygments.highlight(
util.trim(
"""
>>> is_dev()
True
"""
),
self.resultLexer,
self.outputFormatter,
).strip()
示例15: pretty_print
def pretty_print (src, s, out, with_formatter, ext=None):
""" `src' is a filepath to be formatted. `out' is a file object
to be written."""
if ext is None:
ext = guess_ext_by_filename(src)
if ext == "":
ext = guess_ext_by_contents(s)
# format
if with_formatter:
f = _load_formatter(ext)
if f is not None:
ext,s = f.format(s)
# highlight
h = _load_highlighter(ext)
if h is None:
try:
lexer = pygments.lexers.get_lexer_by_name(ext)
except pygments.util.ClassNotFound:
lexer = pygments.lexers.get_lexer_for_mimetype("text/plain")
fmt = pygments.formatters.Terminal256Formatter(encoding="utf-8")
pygments.highlight(s, lexer, fmt, out)
else:
h.highlight(out, s)
out.close()