[Checkins] SVN: Sandbox/ulif/grok-reference-with-rest3/doc/build/grokdocs.py Add a LaTeX pygments directive.

Uli Fouquet uli at gnufix.de
Mon Feb 25 20:52:11 EST 2008


Log message for revision 84252:
  Add a LaTeX pygments directive.

Changed:
  U   Sandbox/ulif/grok-reference-with-rest3/doc/build/grokdocs.py

-=-
Modified: Sandbox/ulif/grok-reference-with-rest3/doc/build/grokdocs.py
===================================================================
--- Sandbox/ulif/grok-reference-with-rest3/doc/build/grokdocs.py	2008-02-26 01:32:42 UTC (rev 84251)
+++ Sandbox/ulif/grok-reference-with-rest3/doc/build/grokdocs.py	2008-02-26 01:52:09 UTC (rev 84252)
@@ -19,6 +19,9 @@
 import getopt
 from docutils import nodes
 from docutils.parsers.rst import directives
+from pygments import highlight
+from pygments.lexers import get_lexer_by_name, TextLexer
+from pygments.formatters import LatexFormatter
 from ulif.rest import pygments_directive
 import sphinx
 from sphinx.util.console import nocolor
@@ -50,6 +53,40 @@
 simple_directive.arguments = (1, 0, 1)
 simple_directive.content = 1
 
+
+LATEX_SETTINGS = {
+    'DEFAULT': LatexFormatter(),
+    'VARIANTS' : {
+       'linenos' : LatexFormatter(linenos=True),
+       'nolinenos' : LatexFormatter(linenos=False)
+       },
+    'FORMAT': 'latex',
+    }
+
+def pygments_latex_directive(name, arguments, options, content, lineno,
+                             content_offset, block_text, state, state_machine):
+    """A docutils directive that provides syntax highlighting for LaTeX.
+
+    This is needed to circumvent highlighting quirks when doing
+    non-HTML output. The pygments_directive delivers plain HTML, which
+    we must avoid when generating LaTeX for example.
+    """
+    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 LATEX_SETTINGS['VARIANTS'][
+        options.keys()[0]] or LATEX_SETTINGS['DEFAULT']
+    parsed = highlight(u'\n'.join(content), lexer, formatter)
+    return [nodes.raw('', parsed, format=LATEX_SETTINGS['FORMAT'])]
+
+pygments_latex_directive.arguments = (1, 0, 1)
+pygments_latex_directive.content = 1
+
+
+
 def usage(argv, msg=None, default_src=None, default_out=None):
     """Some hints for users.
 



More information about the Checkins mailing list