-
Notifications
You must be signed in to change notification settings - Fork 1
/
chordpy.py
102 lines (86 loc) · 2.47 KB
/
chordpy.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
import sys, re
scale = ["A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"]
section_titles = ["intro", "verse", "chorus", "bridge", "tag", "instrumental"]
def transpose(note, step):
noteNum = scale.index(note)
noteNum = (noteNum+step) % 12
return scale[noteNum]
filename = sys.argv[1]
name_parts = re.split('-', filename[:-4])
title = name_parts[0].strip()
if len(name_parts) < 2:
name_parts.append("")
artist = name_parts[1].strip()
# Get transposition amt
while True:
transText = input("Transpose (+# or -#): ")
if transText == "" or transText == None:
transText = 0
break
else:
try:
transValue = int(transText)
break
except ValueError:
print("Invalid transposition.")
f = open(filename, "r")
outFilename = title + ("-"+artist, "")[artist == ""] + "_trans-"+str(transValue)+".html"
out = open(outFilename, "w+")
out.write('''<html>
<head>
<title>''' + title + " - " + artist + '''</title>
<style> code {white-space: pre-wrap;} </style>
</head>
<body>
<h1>''' + title + "</h1>"
+ ("<h2>" + artist + "</h2>", "")[artist == ""] + '''
<code>''')
skip_next_newline = False
for line in f:
outputChords = ""
outputLyrics = ""
inBracket = False
for i in range(len(line)):
if line[i] == '[':
inBracket = True
outputChords = outputChords + " "*(max(0, len(outputLyrics)-len(outputChords)))
elif inBracket and line[i] == ']':
inBracket = False
elif inBracket and line[i] =='#':
pass
elif inBracket and str.isupper(line[i]):
if line[i+1] == '#':
outputChords += transpose(line[i]+"#", transValue)
else:
outputChords += transpose(line[i], transValue)
elif inBracket:
#DONT TRANSPOSE AND PUT IN OUTPUTCHORDS
outputChords = outputChords + line[i]
else:
outputLyrics = outputLyrics + line[i]
outputChords = outputChords.strip()
outputLyrics = outputLyrics.strip()
isTitle = False;
for t in section_titles:
if t in line.lower():
isTitle = True;
break
if isTitle:
outputLyrics = ("<h3>" + outputLyrics.strip() + "</h3>")
out.write(outputLyrics)
skip_next_newline = True
else:
if not outputChords == "":
if not skip_next_newline:
outputChords = "\n" + outputChords
out.write(outputChords)
skip_next_newline = False
if not outputLyrics == "":
if not skip_next_newline:
outputLyrics = "\n" + outputLyrics.strip()
out.write(outputLyrics)
skip_next_newline = False
out.write("</code></body></html>")
out.close()
f.close()
print("Transposed " + str(transValue) + " steps.")