class HTMLSong(object):
''' A class to contain and create an HTML file from a provided text file path.
Returns 0 if successful
1 if the path cannot be found
2 if HTML file cannot be created or written'''
HTMLFileName = "Song_Lyric_"
extension = "html"
colours = {"chorus" : "red",
"foreground" : "black",
"background" : "white"}
titleHead = "h1"
lineHead = "h3"
def __init__(self, name, path, index, lastSong = False):
self.name = name
self.path = path
self.index = index
self.lastSong = lastSong
def HTMLify(self):
try:
fh = open(self.path,"r")
lines = fh.readlines()
fh.close()
except:
fh.close()
return 1
if self.name is None:
self.name = lines[0].strip()
# Add the HTML title to the list
HTMLList = ["\n
%s\n<%s>%s%s>" % (self.name,self.titleHead,self.name,self.titleHead)]
for line in lines[2:]:
if "[chorus]" in line:
HTMLList.append("" % (self.colours["chorus"]))
continue
if "[/chorus]" in line:
HTMLList.append("")
continue
if line.strip():
HTMLList.append("<%s>%s%s>
" % (self.lineHead,line.strip(),self.lineHead))
else:
HTMLList.append("
")
if not self.lastSong:
HTMLList.append('Next' % (self.HTMLFileName,self.index+1,self.extension))
try:
fh = open("%s%d.%s" % (self.HTMLFileName,self.index, self.extension), "w")
fh.write("\n".join(HTMLList))
fh.close()
return 0
except:
fh.close()
return 2
def makeHTMLFiles(pathsList):
''' Take a list of tuples of (name, path) and make the relevant linked HTML files.
Return 0 if successful,
1 if list lengths don't match,
2 if HTML creation fails'''
i = 0
# if len(namesList) != len(pathsList):
# return 1
for name, path in [item for item in pathsList]:
i = i + 1
if HTMLSong(name, path, i, i == len(pathsList)).HTMLify():
return 2
return 0