forked from SchnitzlerJ/latex-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
executable file
·117 lines (93 loc) · 3.68 KB
/
build.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3
# coding=utf-8
# -*- coding: utf-8 -*-
"""
All imports
"""
# Argument parsing
import argparse
# Proper regular expression building and matching
import re
# OS specific things like file handling
import os
# Handle the current build time in the final document
import datetime
# Nicer way of handling paths in python
import pathlib
def build():
"""
This is the main function that builds the given file
:return:
"""
parser = argparse.ArgumentParser(description='Build the given TeX class.')
parser.add_argument('cls', metavar='c', nargs='+', type=str,
help='LaTeX class to compile')
parser.add_argument('-d', '--dest', dest='destination', type=str,
help='Set build destination folder.', default='build', action='store')
# Parse the arguments given
args = parser.parse_args()
# Loop over each given cls file
[process_file(f, dest=args.destination) for f in args.cls]
def process_file(f, dest='build'):
"""
Process the given file
:param str c: Filename of the file to process
:param str dest: Destination directory name or path
:return void:
"""
# Open the source file
ps = pathlib.Path(f).absolute()
# Open the target file
pd = pathlib.Path(dest + os.sep + ps.name).absolute()
# Open the file
try:
with ps.open('r', encoding='utf-8') as s:
# Read the whole file
lns = s.readlines()
# Then process all lines
cnt = process_lines(lns)
# Make sure we have the directory of the containing file, too
pd.parent.absolute().mkdir(parents=True, exist_ok=True)
# Write the content to the new target file
pd.write_text(''.join(cnt), encoding='utf-8')
except BaseException as err:
raise ReferenceError('Error processing file \'{}\''.format(f)) from err
def process_lines(lns, replace_inc=None, replace_time=None):
"""
Process lines of the given file
:param list lns: An array of file lines
:return str: The processed file content
"""
# Loop over each line of the file and return the new list
return [process_line(l, replace_inc, replace_time) for l in lns]
def process_line(l, replace_inc=None, replace_time=None):
"""
Process a single line of text
:param str l: The line to process and look for \input, \include, or \filedate
:param re replace_inc: Regular expression to use to match the line of \include or \input
:param re replace_time: Regular expression to use to match the line of \filedate
:return str: The processed or original line
"""
# Build the replacers if not given
if replace_inc is None:
replace_inc = re.compile('\\\\(input|include)\\{(?P<incfile>.*)\\}')
if replace_time is None:
replace_time = re.compile('\\\\filedate\\{(?P<filedate>.*)\\}')
# Search for an include directive in the current line
inc_found = replace_inc.search(l)
# Search for a filedate directive in the current line
time_found = replace_time.search(l)
# Found an include directive to replace?
if inc_found:
# Get the file that should be injected
inc_file = pathlib.Path('src' + os.sep + inc_found.group('incfile') + '.tex')
# If the file exists, then process it
if inc_file.exists():
return ''.join(process_lines(inc_file.open('r', encoding='utf-8').readlines(), replace_inc, replace_time))
# Found a filedate directive to replace
elif time_found:
return l.replace(time_found.group('filedate'), datetime.datetime.utcnow().strftime('%Y/%m/%d'))
# Return the current line
return l
if __name__ == '__main__':
build()