Skip to content

Instantly share code, notes, and snippets.

@tk0miya
Created April 26, 2012 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tk0miya/2495330 to your computer and use it in GitHub Desktop.
Save tk0miya/2495330 to your computer and use it in GitHub Desktop.
Define original roles and styles in Sphinx docs
sys.path += ['.']
extensions += ['sphinxcontrib_roles']
# configuration case.1: define roles as list (define only roles)
roles = ['strike', 'red']
# configuration case.2: define roles as dict (define roles and its style on HTML)
roles = {'strike': "text-decoration: line-through;",
'red': "color: red;" }
# -*- coding: utf-8 -*-
import os
from docutils.parsers.rst import roles
def _define_role(name):
base_role = roles.generic_custom_role
role = roles.CustomRole(name, base_role, {'class': [name]}, [])
roles.register_local_role(name, role)
def on_builder_inited(app):
for name in app.builder.config.roles:
_define_role(name)
def on_html_collect_pages(app):
if isinstance(app.builder.config.roles, dict) and app.builder.config.roles:
cssdir = os.path.join(app.builder.outdir, '_static')
cssfile = os.path.join(cssdir, 'roles.css')
if not os.path.exists(cssdir):
os.makedirs(cssdir)
fd = open(cssfile, 'wt')
for name, style in app.builder.config.roles.items():
fd.write("span.%s { %s }\n" % (name, style))
fd.close()
return ()
def html_page_context(app, pagename, templatename, context, doctree):
if isinstance(app.builder.config.roles, dict) and app.builder.config.roles:
if 'css_files' in context:
context['css_files'].append('_static/roles.css')
def setup(app):
app.add_config_value('roles', [], 'html')
app.connect("builder-inited", on_builder_inited)
app.connect("html-collect-pages", on_html_collect_pages)
app.connect("html-page-context", html_page_context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment