# Copyright Ulrik Sverdrup , 2009 # # Released under the terms of the GNU GPL version 2 (or any later version) # import os import sys import uuid from docutils import nodes from docutils.parsers.rst import directives, roles, states from docutils.parsers.rst import Directive def shortcut_role(role, rawtext, text, lineno, inliner, options={}, content=[]): if not options.has_key('base'): msg = inliner.reporter.error( 'No base is associated with this shortcut: "%s".\n' 'Instead, use the "role" directive to create a new role with ' 'an associated base.' % role, line=lineno) prb = inliner.problematic(rawtext, rawtext, msg) return [prb], [msg] base = options['base'] roles.set_classes(options) ref = base + text basename = os.path.basename(text) node = nodes.reference(rawtext, basename, refuri=ref, **options) return [node], [] shortcut_role.options = {'base': directives.uri} roles.register_canonical_role('shortcut', shortcut_role) file_shortcut = roles.CustomRole("file", shortcut_role, options={'base': "http://kaizer.se/publicfiles/"}) roles.register_canonical_role('file', file_shortcut) lp_shortcut = roles.CustomRole("LP", shortcut_role, options={'base': "http://bugs.launchpad.net/kupfer/+bug/"}) roles.register_canonical_role('LP', lp_shortcut) class Toggle(Directive): """ A reStructuredText directive for ikiwiki Toggle/Toggleable A really ugly test/hack for now, but it works. """ required_arguments = 1 optional_arguments = 0 final_argument_whitespace = True option_spec = {} has_content = True node_class = nodes.compound #node_class = nodes.container def run(self): self.assert_has_content() text = '\n'.join(self.content) # Create the admonition node, to be populated by `nested_parse`. admonition_node = self.node_class(rawsource=text) id_ = "toggle.%s" % uuid.uuid4() title = self.arguments[0] toggle = '%s' begin = '
' end = '
' rawtoggle = nodes.raw(self.arguments[0], toggle % (id_, title), format="html") rawbegin = nodes.raw(self.arguments[0], begin % id_, format="html") admonition_node += rawtoggle admonition_node += rawbegin # Parse the directive contents. self.state.nested_parse(self.content, self.content_offset, admonition_node) rawend = nodes.raw(self.arguments[0], end, format="html") admonition_node += rawend return [admonition_node] directives.register_directive("toggle", Toggle) ############################################################ # /usr/share/doc/python-pygments/examples/rst-directive.py # ############################################################ """ The Pygments reStructuredText directive ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This fragment is a Docutils_ 0.4 directive that renders source code (to HTML only, currently) via Pygments. To use it, adjust the options below and copy the code into a module that you import on initialization. The code then automatically registers a ``sourcecode`` directive that you can use instead of normal code blocks like this:: .. sourcecode:: python My code goes here. If you want to have different code styles, e.g. one with line numbers and one without, add formatters with their names in the VARIANTS dict below. You can invoke them instead of the DEFAULT one by using a directive option:: .. sourcecode:: python :linenos: My code goes here. Look at the `directive documentation`_ to get all the gory details. .. _Docutils: http://docutils.sf.net/ .. _directive documentation: http://docutils.sourceforge.net/docs/howto/rst-directives.html :copyright: 2007 by Georg Brandl. :license: BSD, see LICENSE for more details. """ # Options # ~~~~~~~ # Set to True if you want inline CSS styles instead of classes INLINESTYLES = True from pygments.formatters import HtmlFormatter # The default formatter DEFAULT = HtmlFormatter(noclasses=INLINESTYLES) # Add name -> formatter pairs for every variant you want to use VARIANTS = { # 'linenos': HtmlFormatter(noclasses=INLINESTYLES, linenos=True), } from docutils import nodes from docutils.parsers.rst import directives from pygments import highlight from pygments.lexers import get_lexer_by_name, TextLexer def pygments_directive(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): try: lexer = get_lexer_by_name(arguments[0]) except ValueError: # no lexer found - use the text one instead of an exception lexer = TextLexer() # take an arbitrary option if more than one is given formatter = options and VARIANTS[options.keys()[0]] or DEFAULT parsed = highlight(u'\n'.join(content), lexer, formatter) return [nodes.raw('', parsed, format='html')] pygments_directive.arguments = (1, 0, 1) pygments_directive.content = 1 pygments_directive.options = dict([(key, directives.flag) for key in VARIANTS]) directives.register_directive('sourcecode', pygments_directive)