Add Pygments style selection to theming.

This commit is contained in:
Georg Brandl
2009-01-10 19:14:01 +01:00
parent 13d9cddd1d
commit ec392f4888
8 changed files with 32 additions and 10 deletions

View File

@@ -66,7 +66,7 @@ release = version
show_authors = True
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'friendly'
#pygments_style = 'none'
# Options for HTML output

View File

@@ -78,6 +78,13 @@ class StandaloneHTMLBuilder(Builder):
if path.isfile(jsfile):
self.script_files.append('_static/translations.js')
# determine Pygments style and create the highlighter
if self.config.pygments_style is not None:
style = self.config.pygments_style
else:
style = self.theme.get_confstr('theme', 'pygments_style', 'none')
self.highlighter = PygmentsBridge('html', style)
def init_translator_class(self):
if self.config.html_translator_class:
self.translator_class = self.app.import_object(
@@ -376,7 +383,7 @@ class StandaloneHTMLBuilder(Builder):
ensuredir(path.join(self.outdir, '_static'))
# first, create pygments style file
f = open(path.join(self.outdir, '_static', 'pygments.css'), 'w')
f.write(PygmentsBridge('html', self.config.pygments_style).get_stylesheet())
f.write(self.highlighter.get_stylesheet())
f.close()
# then, copy translations JavaScript file
if self.config.language is not None:

View File

@@ -49,12 +49,15 @@ else:
Number: '#208050',
})
class NoneStyle(Style):
"""Style without any styling."""
lexers = dict(
none = TextLexer(),
python = PythonLexer(),
pycon = PythonConsoleLexer(),
# the python3 option exists as of Pygments 0.12, but it doesn't
# do any harm in previous versions
# the python3 option exists as of Pygments 1.0,
# but it doesn't do any harm in previous versions
pycon3 = PythonConsoleLexer(python3=True),
rest = RstLexer(),
c = CLexer(),
@@ -94,6 +97,8 @@ class PygmentsBridge(object):
return
if stylename == 'sphinx':
style = SphinxStyle
elif stylename == 'none':
style = NoneStyle
elif '.' in stylename:
module, stylename = stylename.rsplit('.', 1)
style = getattr(__import__(module, None, None, ['__name__']), stylename)

View File

@@ -1,4 +1,4 @@
[theme]
inherit = none
stylesheet = basic.css
pygments_style = sphinx
pygments_style = none

View File

@@ -1,3 +1,4 @@
[theme]
inherit = basic
stylesheet = default.css
pygments_style = sphinx

View File

@@ -1,3 +1,4 @@
[theme]
inherit = basic
stylesheet = sphinxdoc.css
stylesheet = sphinxdoc.css
pygments_style = friendly

View File

@@ -16,6 +16,7 @@ from os import path
from sphinx.application import SphinxError
NODEFAULT = object()
THEMECONF = 'theme.conf'
class ThemeError(SphinxError):
@@ -66,13 +67,21 @@ class Theme(object):
else:
self.base = Theme(inherit)
def get_confstr(self, section, name):
def get_confstr(self, section, name, default=NODEFAULT):
"""
Return the value for a theme configuration setting, searching the
base theme chain.
"""
try:
return self.themeconf.get(section, name)
except ConfigParser.NoOptionError:
if self.base is not None:
return self.base.get_confstr(section, name)
raise
if default is NODEFAULT:
raise ThemeError('setting %s.%s occurs in none of the '
'searched theme configs' % (section, name))
else:
return default
def get_dirchain(self):
"""

View File

@@ -17,7 +17,6 @@ from docutils import nodes
from docutils.writers.html4css1 import Writer, HTMLTranslator as BaseTranslator
from sphinx.locale import admonitionlabels, versionlabels
from sphinx.highlighting import PygmentsBridge
from sphinx.util.smartypants import sphinx_smarty_pants
try:
@@ -52,7 +51,7 @@ class HTMLTranslator(BaseTranslator):
def __init__(self, builder, *args, **kwds):
BaseTranslator.__init__(self, *args, **kwds)
self.highlighter = PygmentsBridge('html', builder.config.pygments_style)
self.highlighter = builder.highlighter
self.no_smarty = 0
self.builder = builder
self.highlightlang = builder.config.highlight_language