diff options
| author | gbrandl <devnull@localhost> | 2009-01-05 20:22:30 +0100 |
|---|---|---|
| committer | gbrandl <devnull@localhost> | 2009-01-05 20:22:30 +0100 |
| commit | 5d46ac4e9e56bc4ce5ea3a5241fa243bf4e05e5e (patch) | |
| tree | df3c301170c2390a0234e3e3f60335202f5befdf /sphinx/theming.py | |
| parent | 9d7be98a2d69b979d8c1be5b408ba15c657b37b6 (diff) | |
| download | sphinx-5d46ac4e9e56bc4ce5ea3a5241fa243bf4e05e5e.tar.gz | |
Basic theme infrastructure.
Diffstat (limited to 'sphinx/theming.py')
| -rw-r--r-- | sphinx/theming.py | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/sphinx/theming.py b/sphinx/theming.py new file mode 100644 index 00000000..9493481c --- /dev/null +++ b/sphinx/theming.py @@ -0,0 +1,75 @@ +# -*- coding: utf-8 -*- +""" + sphinx.theming + ~~~~~~~~~~~~~~ + + Theming support for HTML builders. + + :copyright: 2007-2009 by the Sphinx team, see AUTHORS. + :license: BSD, see LICENSE for details. +""" + +import os +import ConfigParser +from os import path + +from sphinx.application import SphinxError + + +THEMECONF = 'theme.conf' + +class ThemeError(SphinxError): + category = 'Theme error' + + +class Theme(object): + """ + Represents the theme chosen in the configuration. + """ + @classmethod + def init_themes(cls, builder): + """Search all theme paths for available themes.""" + cls.themes = {} + + cls.themepath = list(builder.config.html_theme_path) + cls.themepath.append( + path.join(path.abspath(path.dirname(__file__)), 'themes')) + + for themedir in cls.themepath[::-1]: + themedir = path.join(builder.confdir, themedir) + if not path.isdir(themedir): + continue + for theme in os.listdir(themedir): + if not path.isfile(path.join(themedir, theme, THEMECONF)): + continue + cls.themes[theme] = path.join(themedir, theme) + + def __init__(self, name): + if name not in self.themes: + raise ThemeError('no theme named %r found' % name) + self.name = name + self.themedir = self.themes[name] + + self.themeconf = ConfigParser.RawConfigParser() + self.themeconf.read(path.join(self.themedir, THEMECONF)) + + inherit = self.themeconf.get('theme', 'inherit') + if inherit == 'none': + self.base = None + elif inherit not in self.themes: + raise ThemeError('no theme named %r found, inherited by %r' % + (inherit, name)) + else: + self.base = Theme(inherit) + + def get_dirchain(self): + """ + Return a list of theme directories, beginning with this theme's, + then the base theme's, then that one's base theme's, etc. + """ + chain = [self.themedir] + base = self.base + while base is not None: + chain.append(base.themedir) + base = base.base + return chain |
