Added validation of tags in the song lyrics

Fixes partly bug #1173749
This commit is contained in:
Oliver Wieland 2013-08-24 09:23:52 +02:00
parent 81cd369bfe
commit 25b56cff3e
1 changed files with 39 additions and 0 deletions

View File

@ -132,6 +132,7 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog):
self.audioListWidget.setAlternatingRowColors(True)
self.findVerseSplit = re.compile(u'---\[\]---\n', re.UNICODE)
self.whitespace = re.compile(r'\W+', re.UNICODE)
self.find_tags = re.compile(u'\{/?\w+\}', re.UNICODE)
def keyPressEvent(self, event):
"""
@ -720,8 +721,46 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog):
self.manager.save_object(book)
else:
return False
for i in range(self.verseListWidget.rowCount()):
item = self.verseListWidget.item(i, 0)
log.debug(item.text())
tags = self.find_tags.findall(item.text())
if self._validate_tags(tags) == False:
field = unicode(item.data(QtCore.Qt.UserRole).toString())
verse_tag = VerseType.translated_name(field[0])
verse_num = field[1:]
critical_error_message_box(
message=translate('SongsPlugin.EditSongForm',
'There are misplaced tags in %s %s. '
'You need to fix this problem first.' % (verse_tag, verse_num)))
return False
return True
def _validate_tags(self, _tags):
"""
Validates a list of tags
Deletes the first affiliated tag pair which is located side by side in the list
and call itself recursively with the shortened tag list.
If there is any misplaced tag in the list, either the lenght of the tag list is not even,
or the function won't find any tag pairs side by side.
If there is no misplaced tag, the length of the list will be zero on any recursive run.
Return:
True if the function can't find any mismatched tags
False if there are mismatched tags.
"""
if len(_tags) % 2 != 0:
return False
if len(_tags) == 0:
return True
for i in range(len(_tags)-1):
if _tags[i+1] == "{/" + _tags[i][1:]:
del _tags[i:i+2]
return self._validate_tags(_tags)
return False
def onCopyrightInsertButtonTriggered(self):
text = self.copyrightEdit.text()
pos = self.copyrightEdit.cursorPosition()