本文整理汇总了Python中sphinx.highlighting.PygmentsBridge.highlight_block方法的典型用法代码示例。如果您正苦于以下问题:Python PygmentsBridge.highlight_block方法的具体用法?Python PygmentsBridge.highlight_block怎么用?Python PygmentsBridge.highlight_block使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sphinx.highlighting.PygmentsBridge
的用法示例。
在下文中一共展示了PygmentsBridge.highlight_block方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_set_formatter
# 需要导入模块: from sphinx.highlighting import PygmentsBridge [as 别名]
# 或者: from sphinx.highlighting.PygmentsBridge import highlight_block [as 别名]
def test_set_formatter():
PygmentsBridge.html_formatter = MyFormatter
try:
bridge = PygmentsBridge('html')
ret = bridge.highlight_block('foo\n', 'python')
assert ret == 'foo\n'
finally:
PygmentsBridge.html_formatter = HtmlFormatter
示例2: test_trim_doctest_flags
# 需要导入模块: from sphinx.highlighting import PygmentsBridge [as 别名]
# 或者: from sphinx.highlighting.PygmentsBridge import highlight_block [as 别名]
def test_trim_doctest_flags():
PygmentsBridge.html_formatter = MyFormatter
try:
bridge = PygmentsBridge('html', trim_doctest_flags=True)
ret = bridge.highlight_block('>>> 1+2 # doctest: SKIP\n3\n', 'pycon')
assert ret == '>>> 1+2 \n3\n'
finally:
PygmentsBridge.html_formatter = HtmlFormatter
示例3: test_default_highlight
# 需要导入模块: from sphinx.highlighting import PygmentsBridge [as 别名]
# 或者: from sphinx.highlighting.PygmentsBridge import highlight_block [as 别名]
def test_default_highlight(logger):
bridge = PygmentsBridge('html')
# default: highlights as python3
ret = bridge.highlight_block('print "Hello sphinx world"', 'default')
assert ret == ('<div class="highlight"><pre><span></span><span class="nb">print</span> '
'<span class="s2">"Hello sphinx world"</span>\n</pre></div>\n')
# default: fallbacks to none if highlighting failed
ret = bridge.highlight_block('reST ``like`` text', 'default')
assert ret == '<div class="highlight"><pre><span></span>reST ``like`` text\n</pre></div>\n'
# python3: highlights as python3
ret = bridge.highlight_block('print "Hello sphinx world"', 'python3')
assert ret == ('<div class="highlight"><pre><span></span><span class="nb">print</span> '
'<span class="s2">"Hello sphinx world"</span>\n</pre></div>\n')
# python3: raises error if highlighting failed
ret = bridge.highlight_block('reST ``like`` text', 'python3')
logger.warning.assert_called_with('Could not lex literal_block as "%s". '
'Highlighting skipped.', 'python3',
type='misc', subtype='highlighting_failure',
location=None)
示例4: test_default_highlight
# 需要导入模块: from sphinx.highlighting import PygmentsBridge [as 别名]
# 或者: from sphinx.highlighting.PygmentsBridge import highlight_block [as 别名]
def test_default_highlight():
bridge = PygmentsBridge('html')
# default: highlights as python3
ret = bridge.highlight_block('print "Hello sphinx world"', 'default')
assert ret == ('<div class="highlight"><pre><span></span><span class="nb">print</span> '
'<span class="s2">"Hello sphinx world"</span>\n</pre></div>\n')
# default: fallbacks to none if highlighting failed
ret = bridge.highlight_block('reST ``like`` text', 'default')
assert ret == '<div class="highlight"><pre><span></span>reST ``like`` text\n</pre></div>\n'
# python3: highlights as python3
ret = bridge.highlight_block('print "Hello sphinx world"', 'python3')
assert ret == ('<div class="highlight"><pre><span></span><span class="nb">print</span> '
'<span class="s2">"Hello sphinx world"</span>\n</pre></div>\n')
# python3: raises error if highlighting failed
try:
ret = bridge.highlight_block('reST ``like`` text', 'python3')
assert False, "highlight_block() does not raise any exceptions"
except ErrorToken:
pass # raise parsing error
示例5: test_add_lexer
# 需要导入模块: from sphinx.highlighting import PygmentsBridge [as 别名]
# 或者: from sphinx.highlighting.PygmentsBridge import highlight_block [as 别名]
def test_add_lexer(app):
app.add_lexer('test', MyLexer())
bridge = PygmentsBridge('html')
ret = bridge.highlight_block('ab', 'test')
assert '<span class="n">a</span>b' in ret
示例6: test_lexer_options
# 需要导入模块: from sphinx.highlighting import PygmentsBridge [as 别名]
# 或者: from sphinx.highlighting.PygmentsBridge import highlight_block [as 别名]
def test_lexer_options():
bridge = PygmentsBridge('html')
ret = bridge.highlight_block('//comment', 'php', opts={'startinline': True})
assert '<span class="c1">//comment</span>' in ret
示例7: HTMLTranslator
# 需要导入模块: from sphinx.highlighting import PygmentsBridge [as 别名]
# 或者: from sphinx.highlighting.PygmentsBridge import highlight_block [as 别名]
#.........这里部分代码省略.........
node, 'div', CLASS=('admonition ' + name)))
if name and name != 'seealso':
node.insert(0, nodes.title(name, self.language.labels[name]))
self.set_first_last(node)
def visit_seealso(self, node):
self.visit_admonition(node, 'seealso')
def depart_seealso(self, node):
self.depart_admonition(node)
# overwritten for docutils 0.4
if hasattr(BaseTranslator, 'start_tag_with_title'):
def visit_section(self, node):
# the 0.5 version, to get the id attribute in the <div> tag
self.section_level += 1
self.body.append(self.starttag(node, 'div', CLASS='section'))
def visit_title(self, node):
# don't move the id attribute inside the <h> tag
BaseTranslator.visit_title(self, node, move_ids=0)
# overwritten
def visit_literal_block(self, node):
if node.rawsource != node.astext():
# most probably a parsed-literal block -- don't highlight
return BaseTranslator.visit_literal_block(self, node)
lang = self.highlightlang
linenos = node.rawsource.count('\n') >= self.highlightlinenothreshold - 1
if node.has_key('language'):
# code-block directives
lang = node['language']
if node.has_key('linenos'):
linenos = node['linenos']
self.body.append(self.highlighter.highlight_block(node.rawsource,
lang, linenos))
raise nodes.SkipNode
def visit_doctest_block(self, node):
self.visit_literal_block(node)
# overwritten
def visit_literal(self, node):
if len(node.children) == 1 and \
node.children[0] in ('None', 'True', 'False'):
node['classes'].append('xref')
self.body.append(self.starttag(node, 'tt', '', CLASS='docutils literal'))
self.protect_literal_text += 1
def depart_literal(self, node):
self.protect_literal_text -= 1
self.body.append('</tt>')
def visit_productionlist(self, node):
self.body.append(self.starttag(node, 'pre'))
names = []
for production in node:
names.append(production['tokenname'])
maxlen = max(len(name) for name in names)
for production in node:
if production['tokenname']:
lastname = production['tokenname'].ljust(maxlen)
self.body.append(self.starttag(production, 'strong', ''))
self.body.append(lastname + '</strong> ::= ')
else:
self.body.append('%s ' % (' '*len(lastname)))
production.walkabout(self)
self.body.append('\n')
示例8: HTMLTranslator
# 需要导入模块: from sphinx.highlighting import PygmentsBridge [as 别名]
# 或者: from sphinx.highlighting.PygmentsBridge import highlight_block [as 别名]
#.........这里部分代码省略.........
def visit_seealso(self, node):
self.visit_admonition(node, 'seealso')
def depart_seealso(self, node):
self.depart_admonition(node)
# overwritten (args/kwds due to docutils 0.4/0.5 incompatibility)
def visit_title(self, node, *args, **kwds):
# if we have a section we do our own processing in order
# to have ids in the hN-tags and not in additional a-tags
if isinstance(node.parent, nodes.section):
h_level = self.section_level + self.initial_header_level - 1
if node.parent.get('ids'):
attrs = {'ids': node.parent['ids']}
else:
attrs = {}
self.body.append(self.starttag(node, 'h%d' % h_level, '', **attrs))
self.context.append('</h%d>\n' % h_level)
else:
BaseTranslator.visit_title(self, node, *args, **kwds)
# overwritten
def visit_literal_block(self, node):
if node.rawsource != node.astext():
# most probably a parsed-literal block -- don't highlight
return BaseTranslator.visit_literal_block(self, node)
lang = self.highlightlang
linenos = node.rawsource.count('\n') >= self.highlightlinenothreshold - 1
if node.has_key('language'):
# code-block directives
lang = node['language']
if node.has_key('linenos'):
linenos = node['linenos']
self.body.append(self.highlighter.highlight_block(node.rawsource,
lang, linenos))
raise nodes.SkipNode
def visit_doctest_block(self, node):
self.visit_literal_block(node)
# overwritten
def visit_literal(self, node):
if len(node.children) == 1 and \
node.children[0] in ('None', 'True', 'False'):
node['classes'].append('xref')
BaseTranslator.visit_literal(self, node)
def visit_productionlist(self, node):
self.body.append(self.starttag(node, 'pre'))
names = []
for production in node:
names.append(production['tokenname'])
maxlen = max(len(name) for name in names)
for production in node:
if production['tokenname']:
self.body.append(self.starttag(production, 'strong', ''))
self.body.append(production['tokenname'].ljust(maxlen) +
'</strong> ::= ')
lastname = production['tokenname']
else:
self.body.append('%s ' % (' '*len(lastname)))
production.walkabout(self)
self.body.append('\n')
self.body.append('</pre>\n')
raise nodes.SkipNode
def depart_productionlist(self, node):
示例9: test_add_lexer
# 需要导入模块: from sphinx.highlighting import PygmentsBridge [as 别名]
# 或者: from sphinx.highlighting.PygmentsBridge import highlight_block [as 别名]
def test_add_lexer(app, status, warning):
app.add_lexer("test", MyLexer())
bridge = PygmentsBridge("html")
ret = bridge.highlight_block("ab", "test")
assert '<span class="n">a</span>b' in ret
示例10: HTMLTranslator
# 需要导入模块: from sphinx.highlighting import PygmentsBridge [as 别名]
# 或者: from sphinx.highlighting.PygmentsBridge import highlight_block [as 别名]
#.........这里部分代码省略.........
node.insert(0, nodes.title(name, admonitionlabels[name]))
self.set_first_last(node)
def visit_seealso(self, node):
self.visit_admonition(node, "seealso")
def depart_seealso(self, node):
self.depart_admonition(node)
# overwritten for docutils 0.4
if hasattr(BaseTranslator, "start_tag_with_title"):
def visit_section(self, node):
# the 0.5 version, to get the id attribute in the <div> tag
self.section_level += 1
self.body.append(self.starttag(node, "div", CLASS="section"))
def visit_title(self, node):
# don't move the id attribute inside the <h> tag
BaseTranslator.visit_title(self, node, move_ids=0)
# overwritten
def visit_literal_block(self, node):
if node.rawsource != node.astext():
# most probably a parsed-literal block -- don't highlight
return BaseTranslator.visit_literal_block(self, node)
lang = self.highlightlang
linenos = node.rawsource.count("\n") >= self.highlightlinenothreshold - 1
if node.has_key("language"):
# code-block directives
lang = node["language"]
if node.has_key("linenos"):
linenos = node["linenos"]
highlighted = self.highlighter.highlight_block(node.rawsource, lang, linenos)
starttag = self.starttag(node, "div", suffix="", CLASS="highlight-%s" % lang)
self.body.append(starttag + highlighted + "</div>\n")
raise nodes.SkipNode
def visit_doctest_block(self, node):
self.visit_literal_block(node)
# overwritten
def visit_literal(self, node):
if len(node.children) == 1 and node.children[0] in ("None", "True", "False"):
node["classes"].append("xref")
self.body.append(self.starttag(node, "tt", "", CLASS="docutils literal"))
self.protect_literal_text += 1
def depart_literal(self, node):
self.protect_literal_text -= 1
self.body.append("</tt>")
def visit_productionlist(self, node):
self.body.append(self.starttag(node, "pre"))
names = []
for production in node:
names.append(production["tokenname"])
maxlen = max(len(name) for name in names)
for production in node:
if production["tokenname"]:
lastname = production["tokenname"].ljust(maxlen)
self.body.append(self.starttag(production, "strong", ""))
self.body.append(lastname + "</strong> ::= ")
else:
self.body.append("%s " % (" " * len(lastname)))
production.walkabout(self)