summaryrefslogtreecommitdiff
path: root/sandbox/code-block-directive/docs/pygments_code_block_directive-bunt.py.html
blob: 78bb809e9c6a380e1926acc4995b833eaf9d6e95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?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." />
<style type="text/css">

/* Stylesheet for pygments enhanced reStructured Text */
/* ================================================== */

/* :Author:    Guenter Milde */
/* :Copyright: 2007 G. Milde */
/*             This stylesheet is released under the GPL v. 2 or later */

/* This stylesheet provides syntax highlight for documents generated with a */
/* pygments_ enhanced reStructured Text -> html converter. */

/* Import the default docutils style sheet */
/* --------------------------------------- */
/* :: */

@import url("/stylesheets/html4css1.css");         

/* Indent the code block */
/* --------------------- */

/* Content copied from the `html4css1.css` rule for literal blocks. */
/* Selector adapted to the output of Pygments_. :: */

div.highlight {
  margin-left: 2em ;
  margin-right: 2em ;
  background-color: #eeeeee
  }


/* Colour code blocks */
/* ------------------ */

/* Pygments_ has an option to generate stylesheets for html and latex. */
/* The following code is generated with the command */
/* `pygmentize -S default -f html > pygments-default.css`:: */

.c { color: #008800; font-style: italic } /* Comment */
.err { border: 1px solid #FF0000 } /* Error */
.k { color: #AA22FF; font-weight: bold } /* Keyword */
.o { color: #666666 } /* Operator */
.cm { color: #008800; font-style: italic } /* Comment.Multiline */
.cp { color: #008800 } /* Comment.Preproc */
.c1 { color: #008800; font-style: italic } /* Comment.Single */
.gd { color: #A00000 } /* Generic.Deleted */
.ge { font-style: italic } /* Generic.Emph */
.gr { color: #FF0000 } /* Generic.Error */
.gh { color: #000080; font-weight: bold } /* Generic.Heading */
.gi { color: #00A000 } /* Generic.Inserted */
.go { color: #808080 } /* Generic.Output */
.gp { color: #000080; font-weight: bold } /* Generic.Prompt */
.gs { font-weight: bold } /* Generic.Strong */
.gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.gt { color: #0040D0 } /* Generic.Traceback */
.kc { color: #AA22FF; font-weight: bold } /* Keyword.Constant */
.kd { color: #AA22FF; font-weight: bold } /* Keyword.Declaration */
.kp { color: #AA22FF } /* Keyword.Pseudo */
.kr { color: #AA22FF; font-weight: bold } /* Keyword.Reserved */
.kt { color: #AA22FF; font-weight: bold } /* Keyword.Type */
.m { color: #666666 } /* Literal.Number */
.s { color: #BB4444 } /* Literal.String */
.na { color: #BB4444 } /* Name.Attribute */
.nb { color: #AA22FF } /* Name.Builtin */
.nc { color: #0000FF } /* Name.Class */
.no { color: #880000 } /* Name.Constant */
.nd { color: #AA22FF } /* Name.Decorator */
.ni { color: #999999; font-weight: bold } /* Name.Entity */
.ne { color: #D2413A; font-weight: bold } /* Name.Exception */
.nf { color: #00A000 } /* Name.Function */
.nl { color: #A0A000 } /* Name.Label */
.nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
.nt { color: #008000; font-weight: bold } /* Name.Tag */
.nv { color: #B8860B } /* Name.Variable */
.ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
.mf { color: #666666 } /* Literal.Number.Float */
.mh { color: #666666 } /* Literal.Number.Hex */
.mi { color: #666666 } /* Literal.Number.Integer */
.mo { color: #666666 } /* Literal.Number.Oct */
.sb { color: #BB4444 } /* Literal.String.Backtick */
.sc { color: #BB4444 } /* Literal.String.Char */
.sd { color: #BB4444; font-style: italic } /* Literal.String.Doc */
.s2 { color: #BB4444 } /* Literal.String.Double */
.se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
.sh { color: #BB4444 } /* Literal.String.Heredoc */
.si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
.sx { color: #008000 } /* Literal.String.Other */
.sr { color: #BB6688 } /* Literal.String.Regex */
.s1 { color: #BB4444 } /* Literal.String.Single */
.ss { color: #B8860B } /* Literal.String.Symbol */
.bp { color: #AA22FF } /* Name.Builtin.Pseudo */
.vc { color: #B8860B } /* Name.Variable.Class */
.vg { color: #B8860B } /* Name.Variable.Global */
.vi { color: #B8860B } /* Name.Variable.Instance */
.il { color: #666666 } /* Literal.Number.Integer.Long */

/* .. _pygments:  http://pygments.org/ */

</style>
</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>
<!-- to get the syntax highlight in the html output of this file, convert with::

./rst2html-highlight - -stylesheet=data/pygments-default.css -->
<pre class="code-block python literal-block">
<span class="sd">&quot;&quot;&quot;Define and register a code-block directive using pygments
&quot;&quot;&quot;</span>

</pre>
<div class="section">
<h1><a id="requirements" name="requirements">Requirements</a></h1>
<pre class="code-block python literal-block">
<span class="k">from</span> <span class="nn">docutils</span> <span class="k">import</span> <span class="n">nodes</span>
<span class="k">from</span> <span class="nn">docutils.parsers.rst</span> <span class="k">import</span> <span class="n">directives</span>
<span class="k">try</span><span class="p">:</span>
    <span class="k">import</span> <span class="nn">pygments</span>
    <span class="k">from</span> <span class="nn">pygments.lexers</span> <span class="k">import</span> <span class="n">get_lexer_by_name</span>
    <span class="k">from</span> <span class="nn">pygments.formatters.html</span> <span class="k">import</span> <span class="n">_get_ttype_class</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="k">pass</span>

</pre>
</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>
<pre class="code-block python literal-block">
<span class="n">unstyled_tokens</span> <span class="o">=</span> <span class="p">[</span><span class="s">''</span><span class="p">]</span>

</pre>
</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>
<pre class="code-block python literal-block">
<span class="k">class</span> <span class="nc">DocutilsInterface</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Parse `code` string and yield &quot;classified&quot; 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.

    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">code</span><span class="p">,</span> <span class="n">language</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">code</span> <span class="o">=</span> <span class="n">code</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">language</span> <span class="o">=</span> <span class="n">language</span>

    <span class="k">def</span> <span class="nf">lex</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># Get lexer for language (use text as fallback)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">lexer</span> <span class="o">=</span> <span class="n">get_lexer_by_name</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">language</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="c"># info: &quot;no pygments lexer for %s, using 'text'&quot;%self.language</span>
            <span class="n">lexer</span> <span class="o">=</span> <span class="n">get_lexer_by_name</span><span class="p">(</span><span class="s">'text'</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">pygments</span><span class="o">.</span><span class="n">lex</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">,</span> <span class="n">lexer</span><span class="p">)</span>


    <span class="k">def</span> <span class="nf">join</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tokens</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;join subsequent tokens of same token-type
        &quot;&quot;&quot;</span>
        <span class="n">tokens</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">tokens</span><span class="p">)</span>
        <span class="p">(</span><span class="n">lasttype</span><span class="p">,</span> <span class="n">lastval</span><span class="p">)</span> <span class="o">=</span> <span class="n">tokens</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">ttype</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">tokens</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">ttype</span> <span class="ow">is</span> <span class="n">lasttype</span><span class="p">:</span>
                <span class="n">lastval</span> <span class="o">+=</span> <span class="n">value</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">yield</span><span class="p">(</span><span class="n">lasttype</span><span class="p">,</span> <span class="n">lastval</span><span class="p">)</span>
                <span class="p">(</span><span class="n">lasttype</span><span class="p">,</span> <span class="n">lastval</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="n">ttype</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
        <span class="k">yield</span><span class="p">(</span><span class="n">lasttype</span><span class="p">,</span> <span class="n">lastval</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;parse code string and yield &quot;clasified&quot; tokens
        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">tokens</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lex</span><span class="p">()</span>
        <span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;INFO: Pygments lexer not found, using fallback&quot;</span>
            <span class="c"># TODO: write message to INFO</span>
            <span class="k">yield</span> <span class="p">(</span><span class="s">''</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>
            <span class="k">return</span>

        <span class="k">for</span> <span class="n">ttype</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">tokens</span><span class="p">):</span>
            <span class="k">yield</span> <span class="p">(</span><span class="n">_get_ttype_class</span><span class="p">(</span><span class="n">ttype</span><span class="p">),</span> <span class="n">value</span><span class="p">)</span>

</pre>
</div>
<div class="section">
<h1><a id="code-block-directive" name="code-block-directive">code_block_directive</a></h1>
<pre class="code-block python literal-block">
<span class="k">def</span> <span class="nf">code_block_directive</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">arguments</span><span class="p">,</span> <span class="n">options</span><span class="p">,</span> <span class="n">content</span><span class="p">,</span> <span class="n">lineno</span><span class="p">,</span>
                       <span class="n">content_offset</span><span class="p">,</span> <span class="n">block_text</span><span class="p">,</span> <span class="n">state</span><span class="p">,</span> <span class="n">state_machine</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;parse and classify content of a code_block
    &quot;&quot;&quot;</span>
    <span class="n">language</span> <span class="o">=</span> <span class="n">arguments</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="c"># create a literal block element and set class argument</span>
    <span class="n">code_block</span> <span class="o">=</span> <span class="n">nodes</span><span class="o">.</span><span class="n">literal_block</span><span class="p">(</span><span class="n">classes</span><span class="o">=</span><span class="p">[</span><span class="s">&quot;code-block&quot;</span><span class="p">,</span> <span class="n">language</span><span class="p">])</span>

    <span class="c"># parse content with pygments and add to code_block element</span>
    <span class="k">for</span> <span class="n">cls</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">DocutilsInterface</span><span class="p">(</span><span class="n">u</span><span class="s">'</span><span class="se">\n</span><span class="s">'</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">content</span><span class="p">),</span> <span class="n">language</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">cls</span> <span class="ow">in</span> <span class="n">unstyled_tokens</span><span class="p">:</span>
            <span class="c"># insert as Text to decrease the verbosity of the output.</span>
            <span class="n">code_block</span> <span class="o">+=</span> <span class="n">nodes</span><span class="o">.</span><span class="n">Text</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">code_block</span> <span class="o">+=</span> <span class="n">nodes</span><span class="o">.</span><span class="n">inline</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">classes</span><span class="o">=</span><span class="p">[</span><span class="n">cls</span><span class="p">])</span>

    <span class="k">return</span> <span class="p">[</span><span class="n">code_block</span><span class="p">]</span>

</pre>
</div>
<div class="section">
<h1><a id="register-directive" name="register-directive">Register Directive</a></h1>
<pre class="code-block python literal-block">
<span class="n">code_block_directive</span><span class="o">.</span><span class="n">arguments</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">code_block_directive</span><span class="o">.</span><span class="n">content</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">directives</span><span class="o">.</span><span class="n">register_directive</span><span class="p">(</span><span class="s">'code-block'</span><span class="p">,</span> <span class="n">code_block_directive</span><span class="p">)</span>

</pre>
</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>
<pre class="code-block python literal-block">
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
    <span class="k">from</span> <span class="nn">docutils.core</span> <span class="k">import</span> <span class="n">publish_cmdline</span><span class="p">,</span> <span class="n">default_description</span>
    <span class="n">description</span> <span class="o">=</span> <span class="s">&quot;code-block directive test output&quot;</span> <span class="o">+</span> <span class="n">default_description</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">import</span> <span class="nn">locale</span>
        <span class="n">locale</span><span class="o">.</span><span class="n">setlocale</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">LC_ALL</span><span class="p">,</span> <span class="s">''</span><span class="p">)</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="c"># Uncomment the desired output format:</span>
    <span class="n">publish_cmdline</span><span class="p">(</span><span class="n">writer_name</span><span class="o">=</span><span class="s">'pseudoxml'</span><span class="p">,</span> <span class="n">description</span><span class="o">=</span><span class="n">description</span><span class="p">)</span>
    <span class="c"># publish_cmdline(writer_name='xml', description=description)</span>
    <span class="c"># publish_cmdline(writer_name='html', description=description)</span>
    <span class="c"># publish_cmdline(writer_name='latex', description=description)</span>
    <span class="c"># publish_cmdline(writer_name='newlatex2e', description=description)</span>

</pre>
</div>
</div>
<div class="footer">
<hr class="footer" />
Generated on: 2007-06-13.

</div>
</body>
</html>