This repository has been archived by the owner on Feb 14, 2022. It is now read-only.
/
pyLatex.py
95 lines (74 loc) · 2.25 KB
/
pyLatex.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#coding=utf8
"""
File: pyLatex.py
Author: Li-Yufei
Create: 2014-04-17 20:43:37 by Li-Yufei
License:
MIT: http://en.wikipedia.org/wiki/MIT_License
Discription:
"""
import re
from commands import getstatusoutput
re_exp = re.compile('({%\s*?(\w+)\s*?%})')
re_exp_title = re.compile('\\\\title{\s*([^}]+?)\s*}')
class pyLatex():
def __init__(self):
self.doctype = 'report'
self.template = './templates/'
self.output = './output/'
self.title = 'Default'
self.template_file = None
self.template_text = None
def render(self, fname = 'default.tex', params = {}):
if fname is not None and type(params) is dict:
# Open template file
try:
self.template_file = open(self.template + fname, "r")
except:
print "Failed to open tex template file"
self.template_text = self.template_file.read()
# Replace template tag
for item in re_exp.findall(self.template_text):
if item[1] in params.keys():
self.template_text = self.template_text.replace(item[0], params[item[1]])
# Parse the title of LaTex document
try:
self.title = (re_exp_title.search(self.template_text).groups())[0]
except:
print "Uneable template, please check your \\title."
# Render to the latex template
try:
tmp = open(self.output + unicode(self.title, "utf8") + '.tex', "w")
tmp.write(self.template_text)
tmp.close()
except:
print "Error to write!"
print self.title + " is success for render to template."
return 0
# Use "pdflatex" command to compile latex document
# If you want to make title, please call it twice
def compile(self):
cmd = 'pdflatex -output-directory ' + self.output + " " + self.output + self.title + '.tex'
status, output = getstatusoutput(cmd)
if status is 0:
print "compile successfully."
else:
print output
def chapter(self, title=None):
if title is not None:
rtext = '\n\\chapter{ ' + title + ' }\n'
return rtext
else:
return ''
def section(self, title=None):
if title is not None:
rtext = '\n\\section{ ' + title + ' }\n'
return rtext
else:
return ''
def quote(self, text=''):
rtext = '\n\\begin{quote}\n' + text + '\n\\end{quote}\n'
return rtext
def verbatim(self, text=''):
rtxt = '\n\\begin{verbatim}\n' + text + '\n\\end{verbatim}\n'
return rtext