Added config option for stripping or keeping whitelines

This commit is contained in:
Marco van Dijk 2021-07-09 23:06:39 +02:00
parent b87b6cc9c6
commit 15400a037f
4 changed files with 23 additions and 13 deletions

View File

@ -51,7 +51,8 @@ def initConfig():
'rightMargin': 50, 'rightMargin': 50,
'tryToShrinkRatio' : 0.25, 'tryToShrinkRatio' : 0.25,
'lowestwhitespaceonwidthratioallowed': 0.90, 'lowestwhitespaceonwidthratioallowed': 0.90,
'hightestwhitespaceonwidthratioallowed': 0.40 'hightestwhitespaceonwidthratioallowed': 0.40,
'keepEmptyLines': 1
} }
# (if CMD arguments: load CMD arguments to override specific settings) # (if CMD arguments: load CMD arguments to override specific settings)
with open('config.ini', 'w') as configfile: with open('config.ini', 'w') as configfile:

View File

@ -24,11 +24,11 @@ A5 = {'width': 210, 'height': 148}
@param inputString raw txt input @param inputString raw txt input
@return string of parsed input @return string of parsed input
""" """
def stripEmptyLines(inputString): def stripEmptyLines(inputString, keepEmptyLines):
nonEmptyLines = "" nonEmptyLines = ""
lines = inputString.split("\n") lines = inputString.split("\n")
for line in lines: for line in lines:
if line.strip() != "": if keepEmptyLines or line.strip() != "":
nonEmptyLines += line + "\r\n" nonEmptyLines += line + "\r\n"
return nonEmptyLines return nonEmptyLines
@ -125,10 +125,13 @@ class Section:
isFirstLine = True isFirstLine = True
# Input sections may have tablature-only or lyric-only sections # Input sections may have tablature-only or lyric-only sections
# So we have to insert empty lines if we have subsequent tablature or lyric lines # So we have to insert empty lines if we have subsequent tablature or lyric lines
lines = self.rawData.split('\r\n') lines = self.rawData.splitlines(True)
for line in lines: for line in lines:
if not len(line): # Empty line whitespace
continue if not line:
self.lyrics.append("")
self.tablatures.append("")
continue
# Determine lyric or tablature line # Determine lyric or tablature line
currentIsTablature = isTablatureData(line) currentIsTablature = isTablatureData(line)
#print("Have line {} isTab={}, isLyric={}".format(line, currentIsTablature, not currentIsTablature)) #print("Have line {} isTab={}, isLyric={}".format(line, currentIsTablature, not currentIsTablature))
@ -230,7 +233,8 @@ class Song:
# Some sections are very small, so the highest whitespace can be very large. # Some sections are very small, so the highest whitespace can be very large.
# It is advised to keep this value relatively small # It is advised to keep this value relatively small
self.hightestWhitespaceOnWidthRatioAllowed = float(configObj['hightestWhitespaceOnWidthRatioAllowed']) self.hightestWhitespaceOnWidthRatioAllowed = float(configObj['hightestWhitespaceOnWidthRatioAllowed'])
# Strip empty lines from input or keep em
self.keepEmptyLines = configObj['keepEmptyLines'] == '1'
"""!@brief Calculates dimensions of metadata """!@brief Calculates dimensions of metadata
@ -422,7 +426,7 @@ class Song:
# Get raw data # Get raw data
self.rawData = readSourceFile(self.inputFile) self.rawData = readSourceFile(self.inputFile)
parseData = self.rawData parseData = self.rawData
# While not EOF: build sections untill new section found. # While not EOF: build sections until new section found.
delimiterIndex = parseData.find("[") delimiterIndex = parseData.find("[")
if delimiterIndex == -1: if delimiterIndex == -1:
print("Cannot parse input file, since it is not delimited by '[<sectionName>]' entries") print("Cannot parse input file, since it is not delimited by '[<sectionName>]' entries")
@ -482,9 +486,9 @@ class Song:
# Get raw data # Get raw data
self.rawData = readSourceFile(self.inputFile) self.rawData = readSourceFile(self.inputFile)
# Clean up input # Clean up input
parseData = stripEmptyLines(self.rawData) parseData = stripEmptyLines(self.rawData, self.keepEmptyLines)
#print("Clean data='{}'\n".format(parseData)) #print("Clean data='{}'\n".format(parseData))
# While not EOF: build sections untill new section found. # While not EOF: build sections until new section found.
delimiterIndex = parseData.find("[") delimiterIndex = parseData.find("[")
if delimiterIndex == -1: if delimiterIndex == -1:
print("Cannot parse input file, since it is not delimited by '[<sectionName>]' entries") print("Cannot parse input file, since it is not delimited by '[<sectionName>]' entries")

View File

@ -46,7 +46,7 @@ def outputToImage(folderLocation, songObj):
for line in songObj.metadata.split('\n'): for line in songObj.metadata.split('\n'):
# remove any unwanted characters from metadata # remove any unwanted characters from metadata
line = line.rstrip() line = line.rstrip()
if not line: if not line and not songObj.keepEmptyLines:
continue continue
#print("meta line '{}'".format(line)) #print("meta line '{}'".format(line))
metadataTextWidth, metadataTextHeight = songObj.fontMetadata.getsize(line) metadataTextWidth, metadataTextHeight = songObj.fontMetadata.getsize(line)
@ -83,8 +83,9 @@ def outputToImage(folderLocation, songObj):
currentHeight += lyricTextHeight currentHeight += lyricTextHeight
lineIterator += 1 lineIterator += 1
#print("currentheight={}".format(currentHeight)) #print("currentheight={}".format(currentHeight))
# Margin between each section # If we stripped al whitespace, we need to add whitespace between sections
currentHeight += songObj.topMargin if not songObj.keepEmptyLines:
currentHeight += songObj.topMargin
# Got all sections in the page, so write it # Got all sections in the page, so write it
outputLocation = folderLocation + "/" + songObj.title + '-' + str(imageNumber) + ".png" outputLocation = folderLocation + "/" + songObj.title + '-' + str(imageNumber) + ".png"
a4image.save(outputLocation) a4image.save(outputLocation)

View File

@ -67,6 +67,10 @@ def outputToTxt(folderLocation, printRaw, songObj):
output += tabline + '\r\n' output += tabline + '\r\n'
if printRaw or len(lyricline): if printRaw or len(lyricline):
output += lyricline + '\r\n' output += lyricline + '\r\n'
#If both lines are empty, it is an empty line
# So if printRaw == false, insert one empty line
if not printRaw and not len(tabline) and not len(lyricline):
output += '\r\n'
lineIterator += 1 lineIterator += 1
# If exporting raw, do not include the whitespace between sections # If exporting raw, do not include the whitespace between sections
if not printRaw: if not printRaw: