-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatex_renderer.py
72 lines (51 loc) · 1.85 KB
/
latex_renderer.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import mistune
BEGIN = r'\begin{%s}'
END = r'\end{%s}'
enclose = lambda t, s: '\n%s\n%s\n%s\n' % (BEGIN % (t, ), s, END % (t, ))
class LatexRenderer(mistune.Renderer):
def block_code(self, code, lang):
return enclose('verbatim', code)
def block_quote(self, text):
return enclose('quotation', text)
def header(self, text, level, raw=None):
if level == 1:
self.title = text
return ''
elif level < 5:
return '\\' + 'sub' * (level - 2) + 'section' + '{%s}\n' % (text)
else:
return '\\' + 'sub' * (level - 5) + 'paragraph' + '{%s}\n' % (text)
def list(self, body, ordered=True):
return enclose('itemize', body)
def list_item(self, text):
return r'\item ' + text + '\n'
def paragraph(self, text):
return text + '\n\n'
def linebreak(self):
return '\n'
def emphasis(self, text):
return r'\textit{%s}' % (text, )
def double_emphasis(self, text):
return r'\textbf{%s}' % (text, )
def codespan(self, text):
return r'\texttt{%s}' % (text, )
def hrule(self):
return r'\hrulefill'
def footnotes(self, text):
print(text)
return text
def footnote_ref(self, key, index):
print(key, index)
return r'\footnotemark[%s]' % (key, )
def footnote_item(self, key, text):
return r'\footnotetext[%s]{%s}' % (key, text)
def reference(self, key):
return r'\cite{%s}' % (key, )
def image(self, src, title, text):
return '\n'.join([r'\begin{figure}',
r'\includegraphics{%s}' % (src, ),
r'\caption{%s}' % (title, ),
r'\label{%s}' % (text, ),
r'\end{figure}'])
def text(self, text):
return text.replace('&', '\&')