File: docutilsupport.py

package info (click to toggle)
python-pysqlite2 2.3.2-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 756 kB
  • ctags: 751
  • sloc: ansic: 3,351; python: 2,071; makefile: 46
file content (82 lines) | stat: -rw-r--r-- 2,874 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python

import SilverCity
import docutils.parsers.rst
import StringIO

def code_block( name, arguments, options, content, lineno,
             content_offset, block_text, state, state_machine ):
  """
  The code-block directive provides syntax highlighting for blocks
  of code.  It is used with the the following syntax::
  
  .. code-block:: CPP
     
    #include <iostream>
    
    int main( int argc, char* argv[] )
    {
      std::cout << "Hello world" << std::endl;
    }
    
  The directive requires the name of a language supported by SilverCity
  as its only argument.  All code in the indented block following
  the directive will be colourized.  Note that this directive is only
  supported for HTML writers.

  The directive can also be told to include a source file directly::

  .. code-block::
     :language: Python
     :source-file: ../myfile.py

  You cannot both specify a source-file and include code directly.
  """

  try:
    language = arguments[0]
  except IndexError:
    language = options['language']

  if content and 'source-file' in options:
    error = state_machine.reporter.error( "You cannot both specify a source-file and include code directly.",
                                          docutils.nodes.literal_block(block_text,block_text), line=lineno)
    return [error]

  if not content:
    try:
      content = [line.rstrip() for line in file(options['source-file'])]
    except KeyError:
      # source-file was not specified
      pass
    except IOError:
      error = state_machine.reporter.error( "Could not read file %s."%options['source-file'],
                                            docutils.nodes.literal_block(block_text,block_text), line=lineno)
      return [error]
  try:
    module = getattr(SilverCity, language)
    generator = getattr(module, language+"HTMLGenerator")
  except AttributeError:
    error = state_machine.reporter.error( "No SilverCity lexer found "
      "for language '%s'." % language, 
      docutils.nodes.literal_block(block_text, block_text), line=lineno )
    return [error]
  io = StringIO.StringIO()
  generator().generate_html( io, '\n'.join(content) )
  html = '<div class="code-block">\n%s\n</div>\n' % io.getvalue()
  raw = docutils.nodes.raw('',html, format = 'html')
  return [raw]

#code_block.arguments = (1,0,0)
code_block.arguments = (0,2,1)
code_block.options = {'language' : docutils.parsers.rst.directives.unchanged,
                      'source-file' : docutils.parsers.rst.directives.path,}
code_block.content = 1
  
# Simply importing this module will make the directive available.
docutils.parsers.rst.directives.register_directive( 'code-block', code_block )

if __name__ == "__main__":
  import docutils.core
  docutils.core.publish_cmdline(writer_name='html')