-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzpevnik.py
83 lines (59 loc) · 1.82 KB
/
zpevnik.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
#!/usr/bin/env python2
import os
import locale
import re
locale.setlocale(locale.LC_ALL, "cs_CZ.UTF-8")
LYRICS_DIR = './pisne/'
DATA_DIR = './data/'
class Output:
out = ''
songs = []
def __init__(self, songs):
self.songs = songs
out = open(DATA_DIR + 'template.tex').read()
out = out.replace("%CONTENT%", self.getContent())
self.out = out
def getContent(self):
ret = ''
template = open(DATA_DIR + '/song.tex').read()
for song in self.songs:
cret = template.replace("%NAME%", song.name)
cret = cret.replace("%AUTHOR%", song.author)
cret = cret.replace("%LYRICS%", song.lyrics)
ret += cret
return ret
def get(self):
return self.out
class Lyrics:
name = ''
lyrics = ''
author = ''
RE_NAME = re.compile(r'(.*)\[(.*)\]$')
def __init__(self, content):
name = self.RE_NAME.split(content.readline().strip())
if len(name) == 1:
self.name = name[0]
else:
self.name, self.author = name[1:-1]
content.readline()
l = content.read()
l = l.replace("#", "\#")
l = l.replace("[", "\ch{")
l = l.replace("]", "}")
l = l.replace("\n", "\\\\\n")
self.lyrics = l
def get(self):
return self.name
if __name__ == "__main__":
songs = []
for file in os.listdir(LYRICS_DIR):
filePath = LYRICS_DIR + '/' + file
print(file)
if os.path.isfile(filePath) and file.split('.')[1] == "txt":
content = open(filePath)
songs.append(Lyrics(content))
songs.sort(key=lambda song: song.name, cmp=locale.strcoll)
for song in songs:
print(song.get())
save = open('./output/zpevnik.tex', 'w')
save.write(Output(songs).get())