我使用Sphinx制作一个包含代码示例的网站。
我成功使用.. code-block
指令来获取语法高亮。
但我无法使用此代码获得内联语法高亮显示:
.. role:: bash(code)
:language: bash
Test inline: :bash:`export FOO="bar"`.
.. code-block:: bash
export FOO="bar"
产生此输出的,即块代码为:
时未突出显示的内联代码
问题在于,为内联代码生成的HTML包含长类名,而不包含代码块。 这是输出HTML(为了可读性而缩进):
<p>Test inline:
<tt class="code bash docutils literal">
<span class="name builtin">
<span class="pre">export</span>
</span>
<span class="name variable">
<span class="pre">FOO</span>
</span>
<span class="operator">
<span class="pre">=</span>
</span>
<span class="literal string double">
<span class="pre">"bar"</span>
</span>
</tt>.
</p>
<p>Test code-block:</p>
<div class="highlight-bash">
<div class="highlight">
<pre>
<span class="nb">export </span>
<span class="nv">FOO</span>
<span class="o">=</span>
<span class="s2">"bar"</span>
</pre>
</div>
</div>
非常感谢任何帮助。
答案 0 :(得分:4)
找到一个更好的(仅限狮身人面像)解决方案:在sphinx/builders/html.py
中找到一条线
from docutils.core import Publisher
并将其更改为:
from docutils.core import Publisher
def process_programmatic_settings(self, settings_spec,
settings_overrides,
config_section):
if self.settings is None:
defaults = (settings_overrides or {}).copy()
# Propagate exceptions by default when used programmatically:
defaults.setdefault('traceback', True)
defaults.setdefault('syntax_highlight', 'short') # ADDED THIS LINE
self.get_settings(settings_spec=settings_spec,
config_section=config_section,
**defaults)
Publisher.process_programmatic_settings = process_programmatic_settings
此解决方案比以前更好:因为它不会使css规则的数量增加一倍,也不会修改docutils。
但理想的解决方案只会改变conf.py
。所以还有很大的改进空间。
答案 1 :(得分:3)
好的我使用了这个解决方法:我生成一个包含短名称和长名称的css文件。 我仍然对“好”的答案感兴趣。
#!/usr/bin/env python
"""Generate a css file thanks to pygments that will contain both short
and long class names."""
import subprocess
import sys
PYGMENTIZE = 'pygmentize'
def parse_command_line():
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-s', '--style', default='colorful')
parser.add_argument('-p', '--prefix', default='.highlight')
return parser.parse_args()
def pygmentize(style, prefix='.highlight'):
cmd = '{0} -f html -S {1} -a {2}'.format(PYGMENTIZE, style, prefix)
# This will fail if pygmentize does not exist.
try:
p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
except OSError:
print >> sys.stderr, '{0}: command not found'.format(PYGMENTIZE)
exit(1)
out, err = p.communicate()
if p.returncode != 0:
exit(p.returncode)
return out
def main():
args = parse_command_line()
style = args.style
prefix = args.prefix
# Print new css header.
header = """\
/*
* This is pygment css style {0} generated with
* {1}
*/""".format(style, ' '.join(sys.argv))
print header
# Parse pygmentize output.
# Find long names based on comments.
content = pygmentize(style, prefix)
s = content.splitlines()
out = ''
for line in s:
start = line.find("/* ") + 3
end = line.find(" */")
# if line has a comment
if start != 2:
comment = line[start:end]
name = '.' + comment.lower()
arg = line[line.find('{ '): start - 4]
out += '%(prefix)s %(name)s %(arg)s\n' % vars()
print content
print out
if __name__ == '__main__':
main()
答案 2 :(得分:3)
SELECT
r.id, r.building_id AS zID,z.name AS zName,
r.user_id AS kID, k.Name AS kName,
expence_id AS uID,u.name AS uName,u.price AS uPrice,
IIF(u.unique=1,u.price,k.kvadratura * u.price) AS Balance,
r.year,r.month,
DENSE_RANK() OVER(Partition by r.building_id ORDER BY r.user_id) AS Counter
FROM
Bill r
INNER JOIN Expences u ON r.usluga_id = u.id
INNER JOIN Building z ON r.zgrada_id = z.id
INNER JOIN User k ON r.korisnik_id = k.id
WHERE r.building_id = 7
是一个普通的docutils设置,可以在docutils.conf
中设置。如果放在Sphinx的configuration directory(syntax_highlight
所在的位置),Sphinx也会尊重此文件:
conf.py
这比修补[restructuredtext parser]
syntax_highlight = short
或docutils
代码或创建长名称CSS文件要好得多。
答案 3 :(得分:2)
当狮身人面像主题有static/pygments.css
时 - 文件不会被覆盖。所以我只保留包含短名称和长名称的文件(我在emacs中使用regexp获得):
.highlight .hll { background-color: #ffffcc }
/* .highlight { background: #eeffcc; } */
.highlight .c { color: #408090; font-style: italic } /* comment */
.highlight .comment { color: #408090; font-style: italic }
.highlight .err { border: 1px solid #ff0000 } /* error */
.highlight .error { border: 1px solid #ff0000 }
.highlight .k { color: #007020; font-weight: bold } /* keyword */
.highlight .keyword { color: #007020; font-weight: bold }
.highlight .o { color: #666666 } /* operator */
.highlight .operator { color: #666666 }
.highlight .cm { color: #408090; font-style: italic } /* comment.multiline */
.highlight .comment.multiline { color: #408090; font-style: italic }
.highlight .cp { color: #007020 } /* comment.preproc */
.highlight .comment.preproc { color: #007020 }
.highlight .c1 { color: #408090; font-style: italic } /* comment.single */
.highlight .comment.single { color: #408090; font-style: italic }
.highlight .cs { color: #408090; background-color: #fff0f0 } /* comment.special */
.highlight .comment.special { color: #408090; background-color: #fff0f0 }
.highlight .gd { color: #a00000 } /* generic.deleted */
.highlight .generic.deleted { color: #a00000 }
.highlight .ge { font-style: italic } /* generic.emph */
.highlight .generic.emph { font-style: italic }
.highlight .gr { color: #ff0000 } /* generic.error */
.highlight .generic.error { color: #ff0000 }
.highlight .gh { color: #000080; font-weight: bold } /* generic.heading */
.highlight .generic.heading { color: #000080; font-weight: bold }
.highlight .gi { color: #00a000 } /* generic.inserted */
.highlight .generic.inserted { color: #00a000 }
.highlight .go { color: #333333 } /* generic.output */
.highlight .generic.output { color: #333333 }
.highlight .gp { color: #c65d09; font-weight: bold } /* generic.prompt */
.highlight .generic.prompt { color: #c65d09; font-weight: bold }
.highlight .gs { font-weight: bold } /* generic.strong */
.highlight .generic.strong { font-weight: bold }
.highlight .gu { color: #800080; font-weight: bold } /* generic.subheading */
.highlight .generic.subheading { color: #800080; font-weight: bold }
.highlight .gt { color: #0044dd } /* generic.traceback */
.highlight .generic.traceback { color: #0044dd }
.highlight .kc { color: #007020; font-weight: bold } /* keyword.constant */
.highlight .keyword.constant { color: #007020; font-weight: bold }
.highlight .kd { color: #007020; font-weight: bold } /* keyword.declaration */
.highlight .keyword.declaration { color: #007020; font-weight: bold }
.highlight .kn { color: #007020; font-weight: bold } /* keyword.namespace */
.highlight .keyword.namespace { color: #007020; font-weight: bold }
.highlight .kp { color: #007020 } /* keyword.pseudo */
.highlight .keyword.pseudo { color: #007020 }
.highlight .kr { color: #007020; font-weight: bold } /* keyword.reserved */
.highlight .keyword.reserved { color: #007020; font-weight: bold }
.highlight .kt { color: #902000 } /* keyword.type */
.highlight .keyword.type { color: #902000 }
.highlight .m { color: #208050 } /* literal.number */
.highlight .literal.number { color: #208050 }
.highlight .s { color: #4070a0 } /* literal.string */
.highlight .literal.string { color: #4070a0 }
.highlight .na { color: #4070a0 } /* name.attribute */
.highlight .name.attribute { color: #4070a0 }
.highlight .nb { color: #007020 } /* name.builtin */
.highlight .name.builtin { color: #007020 }
.highlight .nc { color: #0e84b5; font-weight: bold } /* name.class */
.highlight .name.class { color: #0e84b5; font-weight: bold }
.highlight .no { color: #60add5 } /* name.constant */
.highlight .name.constant { color: #60add5 }
.highlight .nd { color: #555555; font-weight: bold } /* name.decorator */
.highlight .name.decorator { color: #555555; font-weight: bold }
.highlight .ni { color: #d55537; font-weight: bold } /* name.entity */
.highlight .name.entity { color: #d55537; font-weight: bold }
.highlight .ne { color: #007020 } /* name.exception */
.highlight .name.exception { color: #007020 }
.highlight .nf { color: #06287e } /* name.function */
.highlight .name.function { color: #06287e }
.highlight .nl { color: #002070; font-weight: bold } /* name.label */
.highlight .name.label { color: #002070; font-weight: bold }
.highlight .nn { color: #0e84b5; font-weight: bold } /* name.namespace */
.highlight .name.namespace { color: #0e84b5; font-weight: bold }
.highlight .nt { color: #062873; font-weight: bold } /* name.tag */
.highlight .name.tag { color: #062873; font-weight: bold }
.highlight .nv { color: #bb60d5 } /* name.variable */
.highlight .name.variable { color: #bb60d5 }
.highlight .ow { color: #007020; font-weight: bold } /* operator.word */
.highlight .operator.word { color: #007020; font-weight: bold }
.highlight .w { color: #bbbbbb } /* text.whitespace */
.highlight .text.whitespace { color: #bbbbbb }
.highlight .mf { color: #208050 } /* literal.number.float */
.highlight .literal.number.float { color: #208050 }
.highlight .mh { color: #208050 } /* literal.number.hex */
.highlight .literal.number.hex { color: #208050 }
.highlight .mi { color: #208050 } /* literal.number.integer */
.highlight .literal.number.integer { color: #208050 }
.highlight .mo { color: #208050 } /* literal.number.oct */
.highlight .literal.number.oct { color: #208050 }
.highlight .sb { color: #4070a0 } /* literal.string.backtick */
.highlight .literal.string.backtick { color: #4070a0 }
.highlight .sc { color: #4070a0 } /* literal.string.char */
.highlight .literal.string.char { color: #4070a0 }
.highlight .sd { color: #4070a0; font-style: italic } /* literal.string.doc */
.highlight .literal.string.doc { color: #4070a0; font-style: italic }
.highlight .s2 { color: #4070a0 } /* literal.string.double */
.highlight .literal.string.double { color: #4070a0 }
.highlight .se { color: #4070a0; font-weight: bold } /* literal.string.escape */
.highlight .literal.string.escape { color: #4070a0; font-weight: bold }
.highlight .sh { color: #4070a0 } /* literal.string.heredoc */
.highlight .literal.string.heredoc { color: #4070a0 }
.highlight .si { color: #70a0d0; font-style: italic } /* literal.string.interpol */
.highlight .literal.string.interpol { color: #70a0d0; font-style: italic }
.highlight .sx { color: #c65d09 } /* literal.string.other */
.highlight .literal.string.other { color: #c65d09 }
.highlight .sr { color: #235388 } /* literal.string.regex */
.highlight .literal.string.regex { color: #235388 }
.highlight .s1 { color: #4070a0 } /* literal.string.single */
.highlight .literal.string.single { color: #4070a0 }
.highlight .ss { color: #517918 } /* literal.string.symbol */
.highlight .literal.string.symbol { color: #517918 }
.highlight .bp { color: #007020 } /* name.builtin.pseudo */
.highlight .name.builtin.pseudo { color: #007020 }
.highlight .vc { color: #bb60d5 } /* name.variable.class */
.highlight .name.variable.class { color: #bb60d5 }
.highlight .vg { color: #bb60d5 } /* name.variable.global */
.highlight .name.variable.global { color: #bb60d5 }
.highlight .vi { color: #bb60d5 } /* name.variable.instance */
.highlight .name.variable.instance { color: #bb60d5 }
.highlight .il { color: #208050 } /* literal.number.integer.long */
.highlight .literal.number.integer.long { color: #208050 }
我遇到了另一个问题 - 我正在使用bootstrap主题,它也定义了label
......
答案 4 :(得分:1)
这比previous解决方法更好:
更改docuitls的默认--syntax-highlight
选项。在
docutils/parsers/rst/__init__.py
查找
('Token name set for parsing code with Pygments: one of '
'"long", "short", or "none (no parsing)". Default is "short".',
['--syntax-highlight'],
{'choices': ['long', 'short', 'none'],
'default': 'long', 'metavar': '<format>'}),
并将默认值设为short
:
'default': 'short', 'metavar': '<format>'}),
在sphnix中突出显示内联代码的方法是注册一个新角色:
.. role:: py(code)
:language: py
:class: highlight
这个构造来自docutils,而不是来自sphinx。然后可以更改docutils的默认值,以获得所需的输出(短类)。更好的解决方案是在sphinx中在类的init期间设置值。但发生这种情况可能并不容易找到。
此解决方案比previous好得多,因为它不会使css规则的数量加倍,以匹配代码的颜色。
答案 5 :(得分:0)
可以通过在sphinxcontrib.inlinesyntaxhighlight
中添加conf.py
扩展程序来解决此问题:
extensions = [ 'sphinxcontrib.inlinesyntaxhighlight' ]
# use language set by highlight directive if no language is set by role
inline_highlight_respect_highlight = False
# use language set by highlight directive if no role is set
inline_highlight_literals = False
Sphinx文档:http://sphinxcontrib-inlinesyntaxhighlight.readthedocs.io/en/latest/
此处有扩展名: https://bitbucket.org/klorenz/sphinxcontrib-inlinesyntaxhighlight