-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassemble.py
74 lines (61 loc) · 1.92 KB
/
assemble.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
#!/usr/bin/env python3
import jinja2
import glob
import os
import re
latex_jinja_env = jinja2.Environment(
block_start_string='\BLOCK{',
block_end_string='}',
variable_start_string='\VAR{',
variable_end_string='}',
comment_start_string='\#{',
comment_end_string='}',
line_statement_prefix='%%',
line_comment_prefix='%#',
trim_blocks=True,
autoescape=False,
loader=jinja2.FileSystemLoader(os.path.abspath('.'))
)
## Add in the list below, the Notebook latex files to become chapters, in the desired order.
chapters = [
'Capitulo_1.tex',
]
def render(files):
"""
Render book template
"""
template = latex_jinja_env.get_template('Book/Book_master.tex')
with open('Book/book.tex', 'w') as f:
f.write(template.render(chapters=files))
def move_directories():
"""
Move directories to the Book directory
:return:
"""
for f in glob.glob('*'):
if f == 'Book':
continue
if os.path.isdir(f):
print(f)
os.system(r"rm -rf 'Book/{}' && mv -f '{}'/ 'Book/{}'".format(f, f, f))
def chop_files(files):
"""
Chop the files keeping only the document block
"""
pattern = r'\\title{([\w\s]+)}'
for n, fn in enumerate(files):
with open(fn, 'r') as inp:
text = inp.read()
try:
chapter_title = re.findall(pattern, text)[0]
except IndexError as exc:
print(exc, re.findall(pattern, text))
chapter_title = 'Chapter'
body = text.split(r'\maketitle')[1].split(r'\end{document}')[0]
with open('Book/chapter_{}.tex'.format(n), 'w') as out:
out.write(r'\chapter{' + chapter_title + '}\n' + body)
if __name__ == "__main__":
move_directories()
chop_files(chapters)
chapter_files = [f.split('/')[1].split('.tex')[0] for f in glob.glob('Book/chapter*.tex')]
render(chapter_files)