From 41caeed7471a3630ba9e459fa13047cd4815cbcd Mon Sep 17 00:00:00 2001 From: chrys87 Date: Wed, 24 Aug 2016 16:58:59 +0200 Subject: [PATCH] Update mark_utils.py --- src/fenrir-package/utils/mark_utils.py | 48 +++++++++++++++++++++----- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/src/fenrir-package/utils/mark_utils.py b/src/fenrir-package/utils/mark_utils.py index 6716df7b..4235da1c 100644 --- a/src/fenrir-package/utils/mark_utils.py +++ b/src/fenrir-package/utils/mark_utils.py @@ -1,6 +1,14 @@ #!/bin/python def getTextBetweenMarks(firstMark, secondMark, inText): + if inText == None: + return '' + if isinstance(inText, list): + inText = inText.split('\n') + if len(inText) < 1: + return '' + if inText == '': + return '' if firstMark == None: return '' if secondMark == None: @@ -11,19 +19,41 @@ def getTextBetweenMarks(firstMark, secondMark, inText): else: endMark = firstMark.copy() startMark = secondMark.copy() - startX = startMark['x'] - startY = startMark['y'] + currX = startMark['x'] + currY = startMark['y'] textPart = '' - while startY <= endMark['y']: + while currY <= endMark['y'] and currY <= len(inText): if startMark['y'] == endMark['y']: - textPart += inText[startY][startX:endMark['x'] + 1] + textPart += inText[currY][currX:endMark['x'] + 1] else: - if startY < endMark['y']: - textPart += inText[startY][startX:] + if currY < endMark['y']: + textPart += inText[currY][currX:] if len(textPart) - len(textPart[::-1].strip()) > 0: textPart = textPart[:len(textPart[::-1].strip())] + "\n" else: - textPart += inText[startY][:startX + 1] - startX = 0 - startY += 1 + textPart += inText[currY][:currX + 1] + currX = 0 + currY += 1 return textPart + +def getTextBeforeMark(mark, inText): + if inText == None: + return '' + if isinstance(inText, list): + inText = inText.split('\n') + if len(inText) < 1: + return '' + if mark == None: + return '' + return getTextBetweenMarks({'x':0,'y':0}, mark, inText) + +def getTextAfterMark(mark, inText): + if inText == None: + return '' + if isinstance(inText, list): + inText = inText.split('\n') + if len(inText) < 1: + return '' + if mark == None: + return '' + return getTextBetweenMarks(mark, {'x':len(inText[0])-1,'y':len(inText)-1}, inText)