diff options
author | wiemann <wiemann@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2007-06-29 03:29:36 +0000 |
---|---|---|
committer | wiemann <wiemann@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2007-06-29 03:29:36 +0000 |
commit | 098c62623c8bf93b53bccd2271ac5fcf9bcff52a (patch) | |
tree | 93ac054362ae84df851894b82667ba028c39fd0a /sandbox/code-block-directive/docs/pygments_code_block_directive.py.htm | |
parent | 9a939fa37e565573abb76951de00cfcca027020f (diff) | |
download | docutils-098c62623c8bf93b53bccd2271ac5fcf9bcff52a.tar.gz |
renamed .html to .htm to avoid collisions with the docutils-update website updater script; it was sending me email because the code-block directive caused error messages; this is kindof a hack, but it should work for now; Guenther, if the changed extensions are a problem for you, please feel free to revert (or drop me a line)
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@5305 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'sandbox/code-block-directive/docs/pygments_code_block_directive.py.htm')
-rw-r--r-- | sandbox/code-block-directive/docs/pygments_code_block_directive.py.htm | 263 |
1 files changed, 263 insertions, 0 deletions
diff --git a/sandbox/code-block-directive/docs/pygments_code_block_directive.py.htm b/sandbox/code-block-directive/docs/pygments_code_block_directive.py.htm new file mode 100644 index 000000000..ba7364dbe --- /dev/null +++ b/sandbox/code-block-directive/docs/pygments_code_block_directive.py.htm @@ -0,0 +1,263 @@ +<?xml version="1.0" encoding="iso-8859-1" ?> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> +<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> +<meta name="generator" content="Docutils 0.4.1: http://docutils.sourceforge.net/" /> +<title></title> +<meta name="author" content="a Pygments author|contributor; Felix Wiemann; Guenter Milde" /> +<meta name="date" content="2007-06-08" /> +<meta name="copyright" content="This module has been placed in the public domain." /> +<link rel="stylesheet" href="../../../../lib/python2.4/site-packages/docutils/writers/html4css1/html4css1.css" type="text/css" /> +</head> +<body> +<div class="document"> +<table class="docinfo" frame="void" rules="none"> +<col class="docinfo-name" /> +<col class="docinfo-content" /> +<tbody valign="top"> +<tr><th class="docinfo-name">Author:</th> +<td>a Pygments author|contributor; Felix Wiemann; Guenter Milde</td></tr> +<tr><th class="docinfo-name">Date:</th> +<td>2007-06-08</td></tr> +<tr><th class="docinfo-name">Copyright:</th> +<td>This module has been placed in the public domain.</td></tr> +</tbody> +</table> +<!-- #!/usr/bin/python --> +<p>This is a merge of <a class="reference" href="http://pygments.org/docs/rstdirective/">Using Pygments in ReST documents</a> from the <a class="reference" href="http://pygments.org/">pygments</a> +documentation, and a <a class="reference" href="http://article.gmane.org/gmane.text.docutils.user/3689">proof of concept</a> by Felix Wiemann.</p> +<table border="1" class="docutils"> +<colgroup> +<col width="14%" /> +<col width="86%" /> +</colgroup> +<tbody valign="top"> +<tr><td>2007-06-01</td> +<td>Removed redundancy from class values.</td> +</tr> +<tr><td>2007-06-04</td> +<td>Merge of successive tokens of same type +(code taken from pygments.formatters.others).</td> +</tr> +<tr><td>2007-06-05</td> +<td>Separate docutils formatter script +Use pygments' CSS class names (like the html formatter) +allowing the use of pygments-produced style sheets.</td> +</tr> +<tr><td>2007-06-07</td> +<td>Merge in the formatting of the parsed tokens +(misnamed as docutils_formatter) as class DocutilsInterface</td> +</tr> +<tr><td>2007-06-08</td> +<td>Failsave implementation (fallback to a standard literal block +if pygments not found)</td> +</tr> +</tbody> +</table> +<div class="system-message"> +<p class="system-message-title">System Message: ERROR/3 (<tt class="docutils">/usr/src/docutils-svn/sandbox/code-block-directive/pygments_code_block_directive.py.txt</tt>, line 23)</p> +<p>Unknown directive type "code-block".</p> +<pre class="literal-block"> +.. code-block:: + + """Define and register a code-block directive using pygments + """ + +</pre> +</div> +<div class="section"> +<h1><a id="requirements" name="requirements">Requirements</a></h1> +<div class="system-message"> +<p class="system-message-title">System Message: ERROR/3 (<tt class="docutils">/usr/src/docutils-svn/sandbox/code-block-directive/pygments_code_block_directive.py.txt</tt>, line 31)</p> +<p>Unknown directive type "code-block".</p> +<pre class="literal-block"> +.. code-block:: + + from docutils import nodes + from docutils.parsers.rst import directives + try: + import pygments + from pygments.lexers import get_lexer_by_name + from pygments.formatters.html import _get_ttype_class + except ImportError: + pass + + + +</pre> +</div> +</div> +<div class="section"> +<h1><a id="customisation" name="customisation">Customisation</a></h1> +<p>Do not insert inline nodes for the following tokens. +(You could add e.g. Token.Punctuation like <tt class="docutils literal"><span class="pre">['',</span> <span class="pre">'p']</span></tt>.)</p> +<div class="system-message"> +<p class="system-message-title">System Message: ERROR/3 (<tt class="docutils">/usr/src/docutils-svn/sandbox/code-block-directive/pygments_code_block_directive.py.txt</tt>, line 50)</p> +<p>Unknown directive type "code-block".</p> +<pre class="literal-block"> +.. code-block:: + + unstyled_tokens = [''] + +</pre> +</div> +</div> +<div class="section"> +<h1><a id="docutilsinterface" name="docutilsinterface">DocutilsInterface</a></h1> +<p>This interface class combines code from +pygments.formatters.html and pygments.formatters.others.</p> +<p>It does not require anything of docutils and could also become a part of +pygments</p> +<div class="system-message"> +<p class="system-message-title">System Message: ERROR/3 (<tt class="docutils">/usr/src/docutils-svn/sandbox/code-block-directive/pygments_code_block_directive.py.txt</tt>, line 63)</p> +<p>Unknown directive type "code-block".</p> +<pre class="literal-block"> +.. code-block:: + + class DocutilsInterface(object): + """Parse `code` string and yield "classified" tokens. + + Arguments + + code -- string of source code to parse + language -- formal language the code is written in. + + Merge subsequent tokens of the same token-type. + + Yields the tokens as ``(ttype_class, value)`` tuples, + where ttype_class is taken from pygments.token.STANDARD_TYPES and + corresponds to the class argument used in pygments html output. + + """ + + def __init__(self, code, language): + self.code = code + self.language = language + + def lex(self): + # Get lexer for language (use text as fallback) + try: + lexer = get_lexer_by_name(self.language) + except ValueError: + # info: "no pygments lexer for %s, using 'text'"%self.language + lexer = get_lexer_by_name('text') + return pygments.lex(self.code, lexer) + + + def join(self, tokens): + """join subsequent tokens of same token-type + """ + tokens = iter(tokens) + (lasttype, lastval) = tokens.next() + for ttype, value in tokens: + if ttype is lasttype: + lastval += value + else: + yield(lasttype, lastval) + (lasttype, lastval) = (ttype, value) + yield(lasttype, lastval) + + def __iter__(self): + """parse code string and yield "clasified" tokens + """ + try: + tokens = self.lex() + except IOError: + print "INFO: Pygments lexer not found, using fallback" + # TODO: write message to INFO + yield ('', self.code) + return + + for ttype, value in self.join(tokens): + yield (_get_ttype_class(ttype), value) + + + +</pre> +</div> +</div> +<div class="section"> +<h1><a id="code-block-directive" name="code-block-directive">code_block_directive</a></h1> +<div class="system-message"> +<p class="system-message-title">System Message: ERROR/3 (<tt class="docutils">/usr/src/docutils-svn/sandbox/code-block-directive/pygments_code_block_directive.py.txt</tt>, line 127)</p> +<p>Unknown directive type "code-block".</p> +<pre class="literal-block"> +.. code-block:: + + def code_block_directive(name, arguments, options, content, lineno, + content_offset, block_text, state, state_machine): + """parse and classify content of a code_block + """ + language = arguments[0] + # create a literal block element and set class argument + code_block = nodes.literal_block(classes=["code-block", language]) + + # parse content with pygments and add to code_block element + for cls, value in DocutilsInterface(u'\n'.join(content), language): + 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] + + +</pre> +</div> +</div> +<div class="section"> +<h1><a id="register-directive" name="register-directive">Register Directive</a></h1> +<div class="system-message"> +<p class="system-message-title">System Message: ERROR/3 (<tt class="docutils">/usr/src/docutils-svn/sandbox/code-block-directive/pygments_code_block_directive.py.txt</tt>, line 151)</p> +<p>Unknown directive type "code-block".</p> +<pre class="literal-block"> +.. code-block:: + + code_block_directive.arguments = (1, 0, 1) + code_block_directive.content = 1 + directives.register_directive('code-block', code_block_directive) + +</pre> +</div> +</div> +<div class="section"> +<h1><a id="test-output" name="test-output">Test output</a></h1> +<p>If called from the command line, call the docutils publisher to render the +input</p> +<div class="system-message"> +<p class="system-message-title">System Message: ERROR/3 (<tt class="docutils">/usr/src/docutils-svn/sandbox/code-block-directive/pygments_code_block_directive.py.txt</tt>, line 169)</p> +<p>Unknown directive type "code-block".</p> +<pre class="literal-block"> +.. code-block:: + + 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) + + + +</pre> +</div> +</div> +</div> +<div class="footer"> +<hr class="footer" /> +<a class="reference" href="pygments_code_block_directive.py.txt">View document source</a>. +Generated on: 2007-06-13. + +</div> +</body> +</html> |