fenrir/src/fenrir-package/utils/mark_utils.py

54 lines
1.7 KiB
Python
Raw Normal View History

2016-08-23 11:12:51 -04:00
#!/bin/python
2016-08-23 11:38:13 -04:00
def getTextBetweenMarks(firstMark, secondMark, inText):
2016-08-24 10:58:59 -04:00
if inText == None:
return ''
if not isinstance(inText, list):
2016-08-24 10:58:59 -04:00
inText = inText.split('\n')
if len(inText) < 1:
return ''
2016-08-23 11:14:15 -04:00
if firstMark == None:
return ''
if secondMark == None:
return ''
2016-08-23 11:12:51 -04:00
if (firstMark['y'] + 1) * (firstMark['x'] + 1) <= (secondMark['y'] + 1) * (secondMark['x'] + 1):
startMark = firstMark.copy()
endMark = secondMark.copy()
else:
endMark = firstMark.copy()
startMark = secondMark.copy()
textPart = ''
if startMark['y'] == endMark['y']:
textPart += inText[startMark['y']][startMark['x']:endMark['x'] + 1]
else:
currY = startMark['y']
while currY <= endMark['y']:
2016-08-24 10:58:59 -04:00
if currY < endMark['y']:
if currY == startMark['y']:
textPart += inText[currY][startMark['x']:]
else:
textPart += inText[currY]
if len(inText[currY].strip()) != 0:
if len(textPart) - len(textPart.rstrip()) > 0:
textPart = textPart[:len(textPart.rstrip())] + "\n"
else:
textPart += '\n'
2016-08-23 18:41:16 -04:00
else:
textPart += inText[currY][:endMark['x'] + 1]
currY += 1
2016-08-23 11:12:51 -04:00
return textPart
2016-08-24 10:58:59 -04:00
def getTextBeforeMark(mark, inText):
if inText == None:
return ''
if mark == None:
return ''
return getTextBetweenMarks({'x':0,'y':0}, mark, inText)
def getTextAfterMark(mark, inText):
if inText == None:
return ''
if mark == None:
return ''
return getTextBetweenMarks(mark, {'x':len(inText[0])-1,'y':len(inText)-1}, inText)