]> code.delx.au - gnu-emacs-elpa/blob - doc/compile-doc.py
more document
[gnu-emacs-elpa] / doc / compile-doc.py
1 #!/usr/bin/python
2 # Compile document to HTML use docutils.
3
4 # ========================================
5 # Pygments syntax highlighting
6 # ========================================
7 from pygments.formatters import HtmlFormatter
8
9 # Set to True if you want inline CSS styles instead of classes
10 INLINESTYLES = True
11
12 from pygments.formatters import HtmlFormatter
13
14 # The default formatter
15 DEFAULT = HtmlFormatter(noclasses=INLINESTYLES)
16
17 # Add name -> formatter pairs for every variant you want to use
18 VARIANTS = {
19 # 'linenos': HtmlFormatter(noclasses=INLINESTYLES, linenos=True),
20 }
21
22 from docutils import nodes
23 from docutils.parsers.rst import directives
24
25 from pygments import highlight
26 from pygments.lexers import get_lexer_by_name, TextLexer
27
28 def pygments_directive(name, arguments, options, content, lineno,
29 content_offset, block_text, state, state_machine):
30 try:
31 lexer = get_lexer_by_name(arguments[0])
32 except ValueError:
33 # no lexer found - use the text one instead of an exception
34 lexer = TextLexer()
35 # take an arbitrary option if more than one is given
36 formatter = options and VARIANTS[options.keys()[0]] or DEFAULT
37 parsed = highlight(u'\n'.join(content), lexer, formatter)
38 return [nodes.raw('', parsed, format='html')]
39
40 pygments_directive.arguments = (1, 0, 1)
41 pygments_directive.content = 1
42 pygments_directive.options = dict([(key, directives.flag) for key in VARIANTS])
43
44 directives.register_directive('sourcecode', pygments_directive)
45
46
47 # ========================================
48 # Command line processing
49 # ========================================
50 from docutils.core import publish_cmdline, default_description
51
52
53 description = ('Generates (X)HTML documents from standalone reStructuredText '
54 'sources. ' + default_description)
55
56 publish_cmdline(writer_name='html', description=description)