Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 67 lines (46 sloc) 1.849 kb
e4576f9 [svn r3559] r4003@delle: sbehnel | 2008-04-18 21:39:50 +0200
scoder authored
1 #!/usr/bin/python
2
3 # Testing:
4 # python rest2latex.py objectify.txt > latex/objectify.tex
5
6 """
7 A minimal front end to the Docutils Publisher, producing LaTeX with
8 some syntax highlighting.
9 """
10
11 # Set to True if you want inline CSS styles instead of classes
12 INLINESTYLES = False
13
14
15 try:
16 import locale
17 locale.setlocale(locale.LC_ALL, '')
18 except:
19 pass
20
21 # set up Pygments
22
23 from pygments.formatters import LatexFormatter
24
25 # The default formatter
26 DEFAULT = LatexFormatter()
27
28 # Add name -> formatter pairs for every variant you want to use
29 VARIANTS = {
30 # 'linenos': HtmlFormatter(noclasses=INLINESTYLES, linenos=True),
31 }
32
33
34 from docutils import nodes
35 from docutils.parsers.rst import directives
36
37 from pygments import highlight
38 from pygments.lexers import get_lexer_by_name, TextLexer
39
40 def pygments_directive(name, arguments, options, content, lineno,
41 content_offset, block_text, state, state_machine):
42 try:
43 lexer = get_lexer_by_name(arguments[0])
44 except ValueError, e:
45 # no lexer found - use the text one instead of an exception
46 lexer = TextLexer()
47 # take an arbitrary option if more than one is given
48 formatter = options and VARIANTS[options.keys()[0]] or DEFAULT
49 parsed = highlight(u'\n'.join(content), lexer, formatter)
50 return [nodes.raw('', parsed, format='latex')]
51
52 pygments_directive.arguments = (1, 0, 1)
53 pygments_directive.content = 1
54 pygments_directive.options = dict([(key, directives.flag) for key in VARIANTS])
55
56 directives.register_directive('sourcecode', pygments_directive)
57
58
59 # run the generation
60
61 from docutils.core import publish_cmdline, default_description
62
63 description = ('Generates LaTeX documents from standalone reStructuredText '
64 'sources. ' + default_description)
65
66 publish_cmdline(writer_name='latex2e', description=description)
Something went wrong with that request. Please try again.