-
Notifications
You must be signed in to change notification settings - Fork 0
/
cantoral_latex.py
292 lines (245 loc) · 10.8 KB
/
cantoral_latex.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from math import ceil
class line:
def __init__(self, raw, instrumental=False):
if instrumental:
self.space_between_chords = '\\qquad'
else:
self.space_between_chords = '\\,'
if len(raw) != 0 and raw[0] != '[':
raw = '[]'+raw
lst = [spt.split(']')
for spt in raw.split('[')] # Separate chords from text
# print(f'lst: {lst}')
lst.pop(0)
chords = [i[0] for i in lst] # Transpose algorithm
lyrics = [i[1] for i in lst] # Transpose algorithm
# print(f'chords: {chords}')
# print(f'lyrics: {lyrics}')
self.set_c_line(chords)
# Chord line table
self._l_line = self.set_line(lyrics)
def set_c_line(self, chords):
self._c_line = '&'.join(
[chord.replace('#', r'{\textsharp}').replace('b', r'{\flat}').replace('^',r'{\Major}').replace('_', r'{/}') for chord in chords])
def set_line(self, lst):
return '&'.join(lst)
def table(self):
return f'\\noindent\n\\begin{{tabular}}{{llllllllllll}}\n{self._c_line}\\\\\n{self._l_line}\n\\end{{tabular}}\n'
class double_line(line):
def __init__(self, raw):
raw = raw.replace('{', '')
self.add_class = ''
if list(raw)[0] != '[':
raw = '[]'+raw
lst = [spt.split(']')
for spt in raw.split('[')] # Separate chords from text
lst.pop(0)
for i in range(len(lst)):
app = lst[i].pop(1)
for a in app.split('|'):
lst[i].append(a)
for i in range(len(lst)):
# Add empty spaces in the second voice line
lst[i].append('') if len(lst[i]) != 3 else None
chords = [list(i) for i in zip(*lst)] # Transpose algorithm
self.set_c_line(chords)
self._l_line = self.set_line(lst[1])
self._second_line = self.set_line(lst[2])
def table(self):
return f'\\begin{{tabular}}{{llllllllllll}}\n{self._c_line}\n{self._l_line}\n{self._l_line}\n\\end{{tabular}}'
class Chorus:
def __init__(self, raw, part_id=''):
self.raw = raw.replace(' ', r'\,') # Spaces in html
self.id = (' ' + part_id) if part_id != '' else ''
self.color = 'chorus'
self.title = 'Coro'
self.repeat_counter = 1
def to_latex(self):
lines = [double_line(raw) if raw[0] == '{' else line(
raw) for raw in self.raw.split('\n') if len(raw) != 0]
latex = '\n'.join([lin.table() for lin in lines])
return f'\\noindent\n\\begin{{minipage}}{{\\columnwidth}}\n\\tit{{{self.title}}}{{{self.color}}}\n\\noindent\n{latex}\\end{{minipage}}\\\\\n'
class chorus:
def __init__(self, raw, part_id=''):
self.repeat_counter = 1
self.title = 'Coro'
def to_latex(self):
repeat_marker = f'\\,(\\texttimes{self.repeat_counter})' if self.repeat_counter > 1 else ''
latex = f'\\chorus{{{repeat_marker}}}'
return f'\n{latex}\n'
class verse:
def __init__(self, raw, part_id=''):
self.raw = raw.replace(' ', r'\,')
self.id = (' ' + part_id) if part_id != '' else ''
self.title = f'Estrofa{self.id}'
self.color = 'verse'
self.instrumental = False
self.repeat_counter = 1 if self.id == '' else 0
def to_latex(self):
repeat_marker = f'\\,(\\texttimes{self.repeat_counter})' if self.repeat_counter > 1 else ''
lines = [double_line(raw) if raw[0] == '{' else line(
raw, self.instrumental) for raw in self.raw.split('\n') if len(raw) != 0]
latex = '\n'.join([lin.table() for lin in lines])
return f'\\noindent\n\\begin{{minipage}}{{\\columnwidth}}\n\\tit{{{self.title}}}{{{self.color}}}{repeat_marker}\n\\noindent\n{latex}\\end{{minipage}}\\\\\n'
class general:
def __init__(self, raw, part_id=''):
self.raw = raw.replace(' ', r'\,')
self.klass = 'verse'
self.instrumental = False
def to_latex(self):
lines = [double_line(raw) if raw[0] == '{' else line(
raw, self.instrumental) for raw in self.raw.split('\n') if len(raw) != 0]
latex = '\n'.join([lin.table() for lin in lines])
return latex
class prechorus(verse):
def __init__(self, raw, part_id=''):
super().__init__(raw, part_id)
self.title = 'Pre-coro'
self.color = 'prechorus'
class bridge(verse):
def __init__(self, raw, part_id=''):
super().__init__(raw, part_id)
self.title = 'Puente'
self.color = 'bridge'
class outro_verse(verse):
def __init__(self, raw, part_id=''):
super().__init__(raw, part_id)
self.title = 'Outro'
self.color = 'outro'
class intro_verse(verse):
def __init__(self, raw, part_id=''):
super().__init__(raw, part_id)
self.title = 'Intro'
self.color = 'intro'
class intermedio(verse):
def __init__(self, raw, part_id=''):
self.raw = raw.replace(' ', '\\quad')
self.title = 'Intermedio'
self.klass = 'inst'
self.instrumental = True
self.id = (' ' + part_id) if part_id != '' else ''
self.color = 'intermedio'
self.repeat_counter = 1
class intro(intermedio):
def __init__(self, raw, part_id=''):
super().__init__(raw, part_id)
self.raw = raw.replace(' ', '\\quad')
self.instrumental = True
self.id = (' ' + part_id) if part_id != '' else ''
self.title = f'Intro{self.id}'
self.color = 'intro'
class outro(intermedio):
def __init__(self, raw, part_id=''):
super().__init__(raw, part_id)
self.title = 'Outro'
self.color = 'outro'
parts = {'Coro': Chorus, 'coro': chorus, 'verse': verse, 'verso': verse, 'prechorus': prechorus, 'intermedio': intermedio, 'intro': intro,
'intro_verse': intro_verse, 'outro': outro, 'bridge': bridge, 'gen': general, 'outro_verse': outro_verse}
class song:
def __init__(self, file_name, folder=''):
raw = open(file_name, 'r', encoding='utf-8').read()
meta = raw.split('{song}')[0].split('\n')[:-1]
self.meta = {}
# print(file_name)
for entry in meta:
self.meta[entry.split(' : ')[0]] = entry.split(' : ')[1]
for entry in ['capo','composer','liturgy']:
if entry not in self.meta.keys():
self.meta[entry] = ''
self.parts = raw.split('/')
groups = [(self.parts[2*i-1], self.parts[2*i])
for i in range(1, ceil(len(self.parts)/2))]
self.teile = [parts[group[0].split('-')[0]](group[1], '' if len(
group[0].split('-')) == 1 else group[0].split('-')[1]) for group in groups]
part_counter = {}
# repeated chorus
x = 0 # counter
while x < (len(self.teile)-1):
if isinstance(self.teile[x],chorus) and isinstance(self.teile[x+1],chorus):
self.teile.pop(x+1)
self.teile[x].repeat_counter += 1
else:
try:
part_counter[type(self.teile[x])] += 1
except:
part_counter[type(self.teile[x])] = 1
self.teile[x].part_counter = part_counter[type(self.teile[x])]
x += 1
for teil in self.teile:
try:
if part_counter[type(teil)] > 1:
teil.title += f' {teil.part_counter}'
except:
pass
metadata = '\\noindent'
def to_latex(self):
latex = '\n'.join([teil.to_latex() for teil in self.teile])
try:
metadata = f'{self.meta["composer"]}\hfill{self.meta["capo"]}'
except:
metadata = f'{self.meta["composer"]}'
preamble = f'%\\section*{{{self.meta["title"]}}}\n{metadata}\n\\begin{{multicols}}{{2}}\n'
footer = f'\n\\end{{multicols}}'
return preamble + latex + footer
def create_latex(file_path):
file_name = file_path.split('/')[-1][:-4] # FILE NAME
name_ext = '/'.join(file_path.split('/')[:-1]) # FOLDER NAME
newfile = open(f'CantoralLatex/{name_ext}/{file_name}.tex', 'w', encoding='utf-8')
newfile.write(song(file_path, folder=name_ext).to_latex())
newfile.close()
import os
import pathlib
path = pathlib.Path(__file__).parent.resolve() # Automated path retriever
dirs = sorted(os.listdir(path))
print(f'curent path: {path}')
create_latex('María/Contigo María.txt')
create_latex('María/Un Mundo Hizo Dios.txt')
create_latex('Hora Santa/5 - Eucarísticos/Jesús está vivo.txt')
create_latex('Hora Santa/5 - Eucarísticos/Vida en Abundancia.txt')
create_latex('Hora Santa/5 - Eucarísticos/Que bien se está aquí II.txt')
create_latex('Semana Santa/Mi Dios está vivo.txt')
create_latex('Originales/Popurrí Día de las Madres.txt')
create_latex('Alabanzas/Popurrí Pentecostés.txt')
create_latex('Alabanzas/Popurrí Pentecostés2.txt')
if __name__ == '__main__' and False:
import os
import pathlib
path = pathlib.Path(__file__).parent.resolve() # Automated path retriever
dirs = sorted(os.listdir(path))
print(f'curent path: {path}')
def rech_backsteps(path, sub_path, is_file=False):
if str(path) == str(sub_path):
return ''
else:
backsteps = len(str(sub_path).split('/'))-len(str(path).split('/'))
if is_file:
backsteps -= 1
pre = ''
for i in range(backsteps):
pre += '../'
return pre
folders = [str(path)+'/'+i for i in dirs if i.find('.') == -1]
abc_songs = []
for folder in folders:
dirs = sorted(os.listdir(folder))
songs = [i for i in dirs if i[-4:] == '.txt']
sub_dirs = sorted(os.listdir(folder))
sub_folders = [i for i in dirs if i.find('.') == -1]
if sub_folders == []:
for s in songs:
create_html(folder+'/'+s)
abc_songs += [(s, folder.split('/')[-1]+'/')]
create_index(folder)
else:
for sub_folder in sub_folders:
dirs = sorted(os.listdir(folder+'/'+sub_folder))
songs = [i for i in dirs if i[-4:] == '.txt']
for s in songs:
if sub_folder == '8 - Comunión':
comunion = True
else:
comunion = False
create_html(folder+'/'+sub_folder+'/'+s, comunion=comunion)
abc_songs += [(s, folder.split('/')
[-1]+'/'+sub_folder+'/')]
create_index(folder)