From 15400a037fa27ba22d040cd86fcff2bd3cd90391 Mon Sep 17 00:00:00 2001 From: Marco van Dijk Date: Fri, 9 Jul 2021 23:06:39 +0200 Subject: [PATCH] Added config option for stripping or keeping whitelines --- lib/config.py | 3 ++- lib/dataStructures.py | 22 +++++++++++++--------- output2img.py | 7 ++++--- output2txt.py | 4 ++++ 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/lib/config.py b/lib/config.py index 1aa3398..fbfff3a 100644 --- a/lib/config.py +++ b/lib/config.py @@ -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: diff --git a/lib/dataStructures.py b/lib/dataStructures.py index 907bc9f..f3f8e8e 100644 --- a/lib/dataStructures.py +++ b/lib/dataStructures.py @@ -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,10 +125,13 @@ 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): - continue + # Empty line whitespace + if not line: + self.lyrics.append("") + self.tablatures.append("") + continue # Determine lyric or tablature line currentIsTablature = isTablatureData(line) #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. # 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 '[]' 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 '[]' entries") diff --git a/output2img.py b/output2img.py index ff77fbd..cd6e068 100644 --- a/output2img.py +++ b/output2img.py @@ -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,8 +83,9 @@ def outputToImage(folderLocation, songObj): currentHeight += lyricTextHeight lineIterator += 1 #print("currentheight={}".format(currentHeight)) - # Margin between each section - currentHeight += songObj.topMargin + # 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" a4image.save(outputLocation) diff --git a/output2txt.py b/output2txt.py index cf617fe..590b2ab 100644 --- a/output2txt.py +++ b/output2txt.py @@ -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: