本文整理匯總了Python中pygments.formatters.html.HtmlFormatter方法的典型用法代碼示例。如果您正苦於以下問題:Python html.HtmlFormatter方法的具體用法?Python html.HtmlFormatter怎麽用?Python html.HtmlFormatter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pygments.formatters.html
的用法示例。
在下文中一共展示了html.HtmlFormatter方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: html
# 需要導入模塊: from pygments.formatters import html [as 別名]
# 或者: from pygments.formatters.html import HtmlFormatter [as 別名]
def html(self):
"""
Return an HTML representation of the snippet.
"""
formatter = HtmlFormatter(
cssclass=self.DIV_CSS_CLASS,
linenos=True,
linenostart=self._start_line,
hl_lines=self._shift_lines(
self._violation_lines,
self._start_line
),
lineanchors=self._src_filename
)
return pygments.format(self.src_tokens(), formatter)
示例2: block_code
# 需要導入模塊: from pygments.formatters import html [as 別名]
# 或者: from pygments.formatters.html import HtmlFormatter [as 別名]
def block_code(self, code, lang=None):
"""Rendering block level code. ``pre > code``.
:param code: text content of the code block.
:param lang: language of the given code.
"""
code = code.rstrip('\n') # 去掉尾部的換行符
# 如果沒有lang, 就返回代碼塊
if not lang:
code = mistune.escape(code)
return '<pre><code>%s\n</code></pre>\n' % code
# 給代碼加上高亮 例如: lang='python'的話
# ```python
# print('666')
# ```
try:
lexer = get_lexer_by_name(lang, stripall=True)
except ClassNotFound:
# 如果lang是不合法, 沒有匹配到, 就設置為python
lexer = get_lexer_by_name('python', stripall=True)
formatter = html.HtmlFormatter() # linenos=True
return highlight(code, lexer, formatter)
示例3: __init__
# 需要導入模塊: from pygments.formatters import html [as 別名]
# 或者: from pygments.formatters.html import HtmlFormatter [as 別名]
def __init__(self, parent, lexer=None):
super(MyHighlighter, self).__init__(parent)
self._document = self.document()
self._formatter = HtmlFormatter(nowrap=True)
self._lexer = lexer
self.set_style('paraiso-dark')
示例4: render_fenced_code
# 需要導入模塊: from pygments.formatters import html [as 別名]
# 或者: from pygments.formatters.html import HtmlFormatter [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)
示例5: __init__
# 需要導入模塊: from pygments.formatters import html [as 別名]
# 或者: from pygments.formatters.html import HtmlFormatter [as 別名]
def __init__(self, parent, lexer=None):
super(PygmentsHighlighter, self).__init__(parent)
self._document = self.document()
self._formatter = HtmlFormatter(nowrap=True)
self._lexer = lexer if lexer else PythonLexer()
self.set_style('default')
示例6: style_defs
# 需要導入模塊: from pygments.formatters import html [as 別名]
# 或者: from pygments.formatters.html import HtmlFormatter [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()
示例7: __init__
# 需要導入模塊: from pygments.formatters import html [as 別名]
# 或者: from pygments.formatters.html import HtmlFormatter [as 別名]
def __init__(self, parent, lexer=None):
super(PygmentsHighlighter, self).__init__(parent)
self._document = self.document()
self._formatter = HtmlFormatter(nowrap=True)
self.set_style('default')
if lexer is not None:
self._lexer = lexer
else:
if PY3:
self._lexer = Python3Lexer()
else:
self._lexer = PythonLexer()
示例8: handle_cli_output
# 需要導入模塊: from pygments.formatters import html [as 別名]
# 或者: from pygments.formatters.html import HtmlFormatter [as 別名]
def handle_cli_output(self, cli_process: QProcess):
output: QByteArray = cli_process.readAllStandardOutput()
message = output.data().decode('utf-8').strip()
if message.startswith('{') or message.startswith('['):
formatter = HtmlFormatter()
formatter.noclasses = True
formatter.linenos = False
formatter.nobackground = True
message = highlight(message, JsonLexer(), formatter)
self.output_area.insertHtml(message)
else:
self.output_area.append(message)
示例9: block_code
# 需要導入模塊: from pygments.formatters import html [as 別名]
# 或者: from pygments.formatters.html import HtmlFormatter [as 別名]
def block_code(self, code, lang):
if not lang:
return '\n<pre><code>%s</code></pre>\n' % \
mistune.escape(code)
lexer = get_lexer_by_name(lang, stripall=True)
formatter = html.HtmlFormatter(noclasses=True)
return highlight(code, lexer, formatter)
示例10: __init__
# 需要導入模塊: from pygments.formatters import html [as 別名]
# 或者: from pygments.formatters.html import HtmlFormatter [as 別名]
def __init__(self, document, lexer=None, color_scheme=None):
super(PygmentsSH, self).__init__(document, color_scheme=color_scheme)
self._pygments_style = self.color_scheme.name
self._style = None
self._formatter = HtmlFormatter(nowrap=True)
self._lexer = lexer if lexer else PythonLexer()
self._brushes = {}
self._formats = {}
self._init_style()
self._prev_block = None
示例11: block_code
# 需要導入模塊: from pygments.formatters import html [as 別名]
# 或者: from pygments.formatters.html import HtmlFormatter [as 別名]
def block_code(code, lang=None):
if not lang:
return "\n<pre><code>%s</code></pre>\n" % mistune.escape(code)
lexer = lexers.get_lexer_by_name(lang, stripall=True)
formatter = html.HtmlFormatter()
return pygments.highlight(code, lexer, formatter)
示例12: blockcode
# 需要導入模塊: from pygments.formatters import html [as 別名]
# 或者: from pygments.formatters.html import HtmlFormatter [as 別名]
def blockcode(self, text, lang):
if not lang:
return '\n<pre><code>{}</code></pre>\n'.format(houdini.escape_html(text.strip()))
lexer = get_lexer_by_name(lang)
formatter = HtmlFormatter()
return highlight(text, lexer, formatter)
示例13: save
# 需要導入模塊: from pygments.formatters import html [as 別名]
# 或者: from pygments.formatters.html import HtmlFormatter [as 別名]
def save(self, *args, **kwargs):
"""
Use the `pygments` library to create a highlighted HTML
representation of the code snippet.
"""
lexer = get_lexer_by_name(self.language)
linenos = self.linenos and 'table' or False
options = self.title and {'title': self.title} or {}
formatter = HtmlFormatter(style=self.style, linenos=linenos,
full=True, **options)
self.highlighted = highlight(self.code, lexer, formatter)
super(Snippet, self).save(*args, **kwargs)
示例14: transform_code_to_html
# 需要導入模塊: from pygments.formatters import html [as 別名]
# 或者: from pygments.formatters.html import HtmlFormatter [as 別名]
def transform_code_to_html(code, lang):
if not lang:
lang = "text"
return highlight(code, get_lexer_by_name(dict(LANG_REGULAR_NAME).get(lang, lang)), HtmlFormatter())
示例15: make_report
# 需要導入模塊: from pygments.formatters import html [as 別名]
# 或者: from pygments.formatters.html import HtmlFormatter [as 別名]
def make_report(self):
""" Makes a html report and saves it into the same folder where logs are stored.
"""
if self.eoexecutor.execution_stats is None:
raise RuntimeError('Cannot produce a report without running the executor first, check EOExecutor.run '
'method')
if os.environ.get('DISPLAY', '') == '':
plt.switch_backend('Agg')
try:
dependency_graph = self._create_dependency_graph()
except graphviz.backend.ExecutableNotFound as ex:
dependency_graph = None
warnings.warn("{}.\nPlease install the system package 'graphviz' (in addition "
"to the python package) to have the dependency graph in the final report!".format(ex),
Warning, stacklevel=2)
formatter = HtmlFormatter(linenos=True)
template = self._get_template()
html = template.render(dependency_graph=dependency_graph,
general_stats=self.eoexecutor.general_stats,
task_descriptions=self._get_task_descriptions(),
task_sources=self._render_task_sources(formatter),
execution_stats=self._render_execution_errors(formatter),
execution_logs=self.eoexecutor.execution_logs,
execution_names=self.eoexecutor.execution_names,
code_css=formatter.get_style_defs())
if not os.path.isdir(self.eoexecutor.report_folder):
os.mkdir(self.eoexecutor.report_folder)
with open(self.eoexecutor.get_report_filename(), 'w') as fout:
fout.write(html)