summaryrefslogtreecommitdiff
path: root/sandbox/code-block-directive/docs/pygments_code_block_directive.py.htm
blob: ba7364dbe9f103acf9aa6a817b7f4629a0c5353f (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
<?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 &quot;code-block&quot;.</p>
<pre class="literal-block">
.. code-block::

  &quot;&quot;&quot;Define and register a code-block directive using pygments
  &quot;&quot;&quot;

</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 &quot;code-block&quot;.</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 &quot;code-block&quot;.</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 &quot;code-block&quot;.</p>
<pre class="literal-block">
.. code-block::

  class DocutilsInterface(object):
      &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;

      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: &quot;no pygments lexer for %s, using 'text'&quot;%self.language
              lexer = get_lexer_by_name('text')
          return pygments.lex(self.code, lexer)


      def join(self, tokens):
          &quot;&quot;&quot;join subsequent tokens of same token-type
          &quot;&quot;&quot;
          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):
          &quot;&quot;&quot;parse code string and yield &quot;clasified&quot; tokens
          &quot;&quot;&quot;
          try:
              tokens = self.lex()
          except IOError:
              print &quot;INFO: Pygments lexer not found, using fallback&quot;
              # 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 &quot;code-block&quot;.</p>
<pre class="literal-block">
.. code-block::

  def code_block_directive(name, arguments, options, content, lineno,
                         content_offset, block_text, state, state_machine):
      &quot;&quot;&quot;parse and classify content of a code_block
      &quot;&quot;&quot;
      language = arguments[0]
      # create a literal block element and set class argument
      code_block = nodes.literal_block(classes=[&quot;code-block&quot;, 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 &quot;code-block&quot;.</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 &quot;code-block&quot;.</p>
<pre class="literal-block">
.. code-block::

  if __name__ == '__main__':
      from docutils.core import publish_cmdline, default_description
      description = &quot;code-block directive test output&quot; + 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>