Commits

Takayuki KONDO committed 9161407

add "roles" extensions

  • Participants
  • Parent commits a2945dc

Comments (0)

Files changed (3)

 # If extensions (or modules to document with autodoc) are in another directory,
 # add these directories to sys.path here. If the directory is relative to the
 # documentation root, use os.path.abspath to make it absolute, like shown here.
-#sys.path.insert(0, os.path.abspath('.'))
+sys.path.insert(0, os.path.abspath('exts'))
 
 # -- General configuration -----------------------------------------------------
 
 # Add any Sphinx extension module names here, as strings. They can be extensions
 # coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
 extensions = []
+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;" }
+
 
 # Add any paths that contain templates here, relative to this directory.
 templates_path = ['_templates']
 
 # How to display URL addresses: 'footnote', 'no', or 'inline'.
 #texinfo_show_urls = 'footnote'
+
+
+
+

File exts/conf.py

+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;" }

File exts/sphinxcontrib_roles.py

+# -*- 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)