forked from rougier/scientific-visualization-book
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rst2latex.py
executable file
·52 lines (39 loc) · 1.53 KB
/
rst2latex.py
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
#!/usr/bin/env python3
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
"""
A customized front end to the Docutils Publisher, producing LaTeX with valid codeblocks using the listings package.
"""
try:
import locale
locale.setlocale(locale.LC_ALL, '')
except:
pass
from docutils.core import publish_cmdline
from docutils.parsers.rst import directives, Directive
from docutils import nodes
class CodeBlock(Directive):
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = False
option_spec = {}
has_content = True
rstlang_to_listingslang = {
'text': '{}'
}
def run(self):
language = self.rstlang_to_listingslang.get(self.arguments[0], self.arguments[0])
content = '\n'.join(self.content)
latex = '\\begin{{lstlisting}}[language={}]\n{}\n\\end{{lstlisting}}'.format(language, content)
return [nodes.raw('', latex, format='latex')]
description = ('Generates LaTeX documents from standalone reStructuredText '
'sources. '
'Reads from <source> (default is stdin) and writes to '
'<destination> (default is stdout). See '
'<http://docutils.sourceforge.net/docs/user/latex.html> for '
'the full reference.')
for directive_name in ('code', 'code-block'):
directives.register_directive(directive_name, CodeBlock)
publish_cmdline(writer_name='latex', description=description)