From 0784ae4285103bb3098e482fc4f915b977fdf6db Mon Sep 17 00:00:00 2001 From: milde Date: Fri, 8 Jun 2007 16:05:01 +0000 Subject: New sandbox project for development of a code-block directive git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@5229 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- .../docs/pygments_code_block_directive.py.html | 137 +++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 sandbox/code-block-directive/docs/pygments_code_block_directive.py.html (limited to 'sandbox/code-block-directive/docs/pygments_code_block_directive.py.html') diff --git a/sandbox/code-block-directive/docs/pygments_code_block_directive.py.html b/sandbox/code-block-directive/docs/pygments_code_block_directive.py.html new file mode 100644 index 000000000..15af90a8c --- /dev/null +++ b/sandbox/code-block-directive/docs/pygments_code_block_directive.py.html @@ -0,0 +1,137 @@ + + + + + + + + + + + + + +
+ +++ + + + + + + + +
Author:a Pygments author|contributor; Felix Wiemann; Guenter Milde
Date:$Date$
Copyright:This module has been placed in the public domain.
+ +

This is a merge of Using Pygments in ReST documents from the pygments +documentation, and a proof of concept by Felix Wiemann.

+ ++++ + + + + + + + + + + + +
2007-06-01Removed redundancy from class values.
2007-06-04Merge of successive tokens of same type +(code taken from pygments.formatters.others).
2007-06-05Separate docutils formatter script +Use pygments' CSS class names (like the html formatter) +allowing the use of pygments-produced style sheets.
+
+"""Define and register a code-block directive using pygments
+"""
+
+
+

Requirements

+
+from docutils import nodes
+from docutils.parsers.rst import directives
+import pygments
+from pygments.lexers import get_lexer_by_name
+from pygments_docutils_formatter import DocutilsFormatter
+
+
+
+

Customisation

+

Do not insert inline nodes for the following tokens. +(You could add e.g. Token.Punctuation like ['', 'p'].)

+
+unstyled_tokens = ['']
+
+
+
+

code_block_directive

+
+def code_block_directive(name, arguments, options, content, lineno,
+                       content_offset, block_text, state, state_machine):
+    language = arguments[0]
+    # create a literal block element and set class argument
+    code_block = nodes.literal_block(raw_content=content,
+                                     classes=["code-block", language])
+    # Get lexer for language (use text as fallback)
+    try:
+        lexer = get_lexer_by_name(language)
+    except ValueError:
+        lexer = get_lexer_by_name('text')
+
+    # parse content with pygments
+    tokens = list(pygments.lex(u'\n'.join(content), lexer))
+
+    for cls, value in DocutilsFormatter(tokens):
+        if cls in unstyled_tokens:
+            # insert as Text to decrease the verbosity of the output.
+            code_block += nodes.Text(value, value)
+        else:
+            code_block += nodes.inline(value, value, classes=[cls])
+
+    return [code_block]
+
+
+
+

Register Directive

+
+code_block_directive.arguments = (1, 0, 1)
+code_block_directive.content = 1
+directives.register_directive('code-block', code_block_directive)
+
+
+
+

Test output

+

If called from the command line, call the docutils publisher to render the +input:

+
+if __name__ == '__main__':
+    from docutils.core import publish_cmdline, default_description
+    description = "code-block directive test output" + default_description
+    try:
+        import locale
+        locale.setlocale(locale.LC_ALL, '')
+    except:
+        pass
+    # Uncomment the desired output format:
+    publish_cmdline(writer_name='pseudoxml', description=description)
+    # publish_cmdline(writer_name='xml', description=description)
+    # publish_cmdline(writer_name='html', description=description)
+    # publish_cmdline(writer_name='latex', description=description)
+    # publish_cmdline(writer_name='newlatex2e', description=description)
+
+
+
+ + + -- cgit v1.2.1