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,
'tryToShrinkRatio' : 0.25,
'lowestwhitespaceonwidthratioallowed': 0.90,
'hightestwhitespaceonwidthratioallowed': 0.40
'hightestwhitespaceonwidthratioallowed': 0.40,
'keepEmptyLines': 1
}
# (if CMD arguments: load CMD arguments to override specific settings)
with open('config.ini', 'w') as configfile:

View File

@ -24,11 +24,11 @@ A5 = {'width': 210, 'height': 148}
@param inputString raw txt input
@return string of parsed input
"""
def stripEmptyLines(inputString):
def stripEmptyLines(inputString, keepEmptyLines):
nonEmptyLines = ""
lines = inputString.split("\n")
for line in lines:
if line.strip() != "":
if keepEmptyLines or line.strip() != "":
nonEmptyLines += line + "\r\n"
return nonEmptyLines
@ -125,9 +125,12 @@ class Section:
isFirstLine = True
# 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
lines = self.rawData.split('\r\n')
lines = self.rawData.splitlines(True)
for line in lines:
if not len(line):
# Empty line whitespace
if not line:
self.lyrics.append("")
self.tablatures.append("")
continue
# Determine lyric or tablature line
currentIsTablature = isTablatureData(line)
@ -230,7 +233,8 @@ class Song:
# Some sections are very small, so the highest whitespace can be very large.
# It is advised to keep this value relatively small
self.hightestWhitespaceOnWidthRatioAllowed = float(configObj['hightestWhitespaceOnWidthRatioAllowed'])
# Strip empty lines from input or keep em
self.keepEmptyLines = configObj['keepEmptyLines'] == '1'
"""!@brief Calculates dimensions of metadata
@ -422,7 +426,7 @@ class Song:
# Get raw data
self.rawData = readSourceFile(self.inputFile)
parseData = self.rawData
# While not EOF: build sections untill new section found.
# While not EOF: build sections until new section found.
delimiterIndex = parseData.find("[")
if delimiterIndex == -1:
print("Cannot parse input file, since it is not delimited by '[<sectionName>]' entries")
@ -482,9 +486,9 @@ class Song:
# Get raw data
self.rawData = readSourceFile(self.inputFile)
# Clean up input
parseData = stripEmptyLines(self.rawData)
parseData = stripEmptyLines(self.rawData, self.keepEmptyLines)
#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("[")
if delimiterIndex == -1:
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'):
# remove any unwanted characters from metadata
line = line.rstrip()
if not line:
if not line and not songObj.keepEmptyLines:
continue
#print("meta line '{}'".format(line))
metadataTextWidth, metadataTextHeight = songObj.fontMetadata.getsize(line)
@ -83,7 +83,8 @@ def outputToImage(folderLocation, songObj):
currentHeight += lyricTextHeight
lineIterator += 1
#print("currentheight={}".format(currentHeight))
# Margin between each section
# If we stripped al whitespace, we need to add whitespace between sections
if not songObj.keepEmptyLines:
currentHeight += songObj.topMargin
# Got all sections in the page, so write it
outputLocation = folderLocation + "/" + songObj.title + '-' + str(imageNumber) + ".png"

View File

@ -67,6 +67,10 @@ def outputToTxt(folderLocation, printRaw, songObj):
output += tabline + '\r\n'
if printRaw or len(lyricline):
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
# If exporting raw, do not include the whitespace between sections
if not printRaw: