本文整理汇总了Python中pygments.formatters.html.HtmlFormatter.get_style_defs方法的典型用法代码示例。如果您正苦于以下问题:Python HtmlFormatter.get_style_defs方法的具体用法?Python HtmlFormatter.get_style_defs怎么用?Python HtmlFormatter.get_style_defs使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygments.formatters.html.HtmlFormatter
的用法示例。
在下文中一共展示了HtmlFormatter.get_style_defs方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_request
# 需要导入模块: from pygments.formatters.html import HtmlFormatter [as 别名]
# 或者: from pygments.formatters.html.HtmlFormatter import get_style_defs [as 别名]
def process_request(self, req):
style = req.args['style']
try:
style_cls = get_style_by_name(style)
except ValueError as e:
raise HTTPNotFound(e)
parts = style_cls.__module__.split('.')
filename = resource_filename('.'.join(parts[:-1]), parts[-1] + '.py')
mtime = datetime.fromtimestamp(os.path.getmtime(filename), localtz)
last_modified = http_date(mtime)
if last_modified == req.get_header('If-Modified-Since'):
req.send_response(304)
req.end_headers()
return
formatter = HtmlFormatter(style=style_cls)
content = u'\n\n'.join([
formatter.get_style_defs('div.code pre'),
formatter.get_style_defs('table.code td')
]).encode('utf-8')
req.send_response(200)
req.send_header('Content-Type', 'text/css; charset=utf-8')
req.send_header('Last-Modified', last_modified)
req.send_header('Content-Length', len(content))
req.write(content)
示例2: _bbcodeAsHtml
# 需要导入模块: from pygments.formatters.html import HtmlFormatter [as 别名]
# 或者: from pygments.formatters.html.HtmlFormatter import get_style_defs [as 别名]
def _bbcodeAsHtml(self):
style = get_style_by_name('igor')
formatter = HtmlFormatter(style=style)
lexer = get_lexer_by_name("bbcode", stripall=True)
css = formatter.get_style_defs()
txt = highlight(self._bbcode, lexer, HtmlFormatter())
return "<style>%s</style>\n%s" % (css, txt)
示例3: prev_view_code
# 需要导入模块: from pygments.formatters.html import HtmlFormatter [as 别名]
# 或者: from pygments.formatters.html.HtmlFormatter import get_style_defs [as 别名]
def prev_view_code(self, code, laugange):
htmlFormatter = HtmlFormatter()
lexer = CppLexer()
if laugange == "Cpp":
lexer = CppLexer()
elif laugange == "CSharp":
lexer = CSharpLexer()
codeDiv = highlight(code, lexer, htmlFormatter)
codeCss = htmlFormatter.get_style_defs(".highlight")
#
html = """
<html>
<head>
<style type="text/css">
%s
</style>
</head>
<body>
%s
</body>
</html>
""" % (codeCss, codeDiv)
self.webView.setHtml(html)
self.setStyleSheet(codeCss)
# 输出文件测试验证
# ff = open('test.html', 'w')
# ff.write(html)
# ff.close()
pass
示例4: render_code
# 需要导入模块: from pygments.formatters.html import HtmlFormatter [as 别名]
# 或者: from pygments.formatters.html.HtmlFormatter import get_style_defs [as 别名]
def render_code(self):
formatter = HtmlFormatter(style='default', nowrap=True, classprefix='code%s-' % self.pk)
html = highlight(self.code, get_lexer_by_name(self.syntax), formatter)
css = formatter.get_style_defs()
# Included in a DIV, so the next item will be displayed below.
return _('<div class="code"><style type="text/css">%(css)s</style>\n<pre>%(html)s</pre></div>\n') % {'css':css, 'html':html}
示例5: style_defs
# 需要导入模块: from pygments.formatters.html import HtmlFormatter [as 别名]
# 或者: from pygments.formatters.html.HtmlFormatter import get_style_defs [as 别名]
def style_defs(cls):
"""
Return the CSS style definitions required
by the formatted snippet.
"""
formatter = HtmlFormatter()
formatter.style.highlight_color = cls.VIOLATION_COLOR
return formatter.get_style_defs()
示例6: highlight_paste
# 需要导入模块: from pygments.formatters.html import HtmlFormatter [as 别名]
# 或者: from pygments.formatters.html.HtmlFormatter import get_style_defs [as 别名]
def highlight_paste(paste, hl_lines):
'''Use pygments to syntax highlight a paste, returns by the way the CSS'''
lexer = get_lexer_by_name(paste.hl_alias)
formatter = HtmlFormatter(linenos=True, cssclass='source', hl_lines=hl_lines)
return (
highlight(paste.content, lexer, formatter),
formatter.get_style_defs('.source')
)
示例7: process_request
# 需要导入模块: from pygments.formatters.html import HtmlFormatter [as 别名]
# 或者: from pygments.formatters.html.HtmlFormatter import get_style_defs [as 别名]
def process_request(self, req):
# settings panel
if not 'style' in req.args:
req._no_pygments_stylesheet = True
styles = list(get_all_styles())
styles.sort(lambda a, b: cmp(a.lower(), b.lower()))
if req.method == 'POST':
style = req.args.get('new_style')
if style and style in styles:
req.session['pygments_style'] = style
output = self._highlight('html', self.EXAMPLE, False)
req.hdf['output'] = Markup(output)
req.hdf['current'] = req.session.get('pygments_style',
self.default_style)
req.hdf['styles'] = styles
req.hdf['pygments_path'] = self.env.href.pygments()
return 'pygments_settings.cs', None
# provide stylesheet
else:
style = req.args['style']
parts = style.__module__.split('.')
filename = resource_filename('.'.join(parts[:-1]), parts[-1] + '.py')
mtime = datetime.utcfromtimestamp(os.path.getmtime(filename))
last_modified = http_date(time.mktime(mtime.timetuple()))
if last_modified == req.get_header('If-Modified-Since'):
req.send_response(304)
req.end_headers()
return
formatter = HtmlFormatter(style=style)
content = u'\n\n'.join([
formatter.get_style_defs('div.code pre'),
formatter.get_style_defs('table.code td')
]).encode('utf-8')
req.send_response(200)
req.send_header('Content-Type', 'text/css; charset=utf-8')
req.send_header('Last-Modified', last_modified)
req.send_header('Content-Length', len(content))
req.write(content)
示例8: get
# 需要导入模块: from pygments.formatters.html import HtmlFormatter [as 别名]
# 或者: from pygments.formatters.html.HtmlFormatter import get_style_defs [as 别名]
def get(self, paste_id):
try:
aid = b64.num_decode(paste_id)
paste = Paste.get_by_id(aid)
content, lang = paste.content, paste.type
formatter = HtmlFormatter()
self.response.out.write(template.render("paste.html", {'css': formatter.get_style_defs('.highlight'),
'paste': highlight(content, get_lexer_by_name(lang), formatter)}))
except Exception:
self.response.set_status(404)
self.response.out.write(template.render("404.html", {}))
示例9: render_code
# 需要导入模块: from pygments.formatters.html import HtmlFormatter [as 别名]
# 或者: from pygments.formatters.html.HtmlFormatter import get_style_defs [as 别名]
def render_code(instance, style_name='default'):
# Some interesting options in the HtmlFormatter:
# - nowrap -> no wrap inside <pre>
# - classprefix -> prefix for the classnames
# - noclasses -> all inline styles.
#
# To get_style_defs(), you can pass a selector prefix.
#
style = styles.get_style_by_name(style_name)
formatter = HtmlFormatter(linenos=instance.linenumbers, style=style, nowrap=True, classprefix='code%s-' % instance.pk)
html = highlight(instance.code, get_lexer_by_name(instance.language), formatter)
css = formatter.get_style_defs()
# Included in a DIV, so the next item will be displayed below.
return '<div class="code"><style type="text/css">' + css + '</style>\n<pre>' + html + '</pre></div>\n'
示例10: export
# 需要导入模块: from pygments.formatters.html import HtmlFormatter [as 别名]
# 或者: from pygments.formatters.html.HtmlFormatter import get_style_defs [as 别名]
def export():
if not os.path.exists(PYGMENTS_PATH):
os.makedirs(PYGMENTS_PATH)
styles = list(get_all_styles())
for style in styles:
print("Generating CSS for %s" % style)
opts = {"style": style}
path = os.path.join(PYGMENTS_PATH, "%s.css" % style)
formatter = HtmlFormatter(**opts)
css_content = formatter.get_style_defs()
# little fix because pelican doesn't append background color.
css_content = css_content.replace(".hll", ".highlight")
with open(path, "w") as f:
f.write(css_content)
示例11: export
# 需要导入模块: from pygments.formatters.html import HtmlFormatter [as 别名]
# 或者: from pygments.formatters.html.HtmlFormatter import get_style_defs [as 别名]
def export():
if not os.path.exists(PYGMENTS_PATH):
os.makedirs(PYGMENTS_PATH)
styles = list(get_all_styles())
for style in styles:
print('Generating CSS for %s' % style)
opts = {
'style': style,
'noclasses': False,
'nobackground': False,
}
path = os.path.join(PYGMENTS_PATH, '%s.css' % style)
formatter = HtmlFormatter(**opts)
css_content = formatter.get_style_defs('.highlight')
with open(path, 'w') as f:
f.write(css_content)