From 25b56cff3e4d1b639669500435035daea16080da Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Sat, 24 Aug 2013 09:23:52 +0200 Subject: [PATCH 1/4] Added validation of tags in the song lyrics Fixes partly bug #1173749 --- openlp/plugins/songs/forms/editsongform.py | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/openlp/plugins/songs/forms/editsongform.py b/openlp/plugins/songs/forms/editsongform.py index 3c5ddb010..a4ec7eb2d 100644 --- a/openlp/plugins/songs/forms/editsongform.py +++ b/openlp/plugins/songs/forms/editsongform.py @@ -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() From 85813a1f99054b1b125287b9d4159618e90a774d Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Sat, 24 Aug 2013 17:58:23 +0200 Subject: [PATCH 2/4] removed debug log --- openlp/plugins/songs/forms/editsongform.py | 1 - 1 file changed, 1 deletion(-) diff --git a/openlp/plugins/songs/forms/editsongform.py b/openlp/plugins/songs/forms/editsongform.py index a4ec7eb2d..bc7cf3008 100644 --- a/openlp/plugins/songs/forms/editsongform.py +++ b/openlp/plugins/songs/forms/editsongform.py @@ -723,7 +723,6 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): 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()) From 217ed36ee1bc48750f9b55e480ef270728946dc9 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Mon, 26 Aug 2013 19:58:45 +0200 Subject: [PATCH 3/4] remove empty lines --- openlp/plugins/songs/forms/editsongform.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/openlp/plugins/songs/forms/editsongform.py b/openlp/plugins/songs/forms/editsongform.py index bc7cf3008..c9f28aec9 100644 --- a/openlp/plugins/songs/forms/editsongform.py +++ b/openlp/plugins/songs/forms/editsongform.py @@ -748,12 +748,10 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): 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 - + if len(_tags) % 2 != 0: + return False for i in range(len(_tags)-1): if _tags[i+1] == "{/" + _tags[i][1:]: del _tags[i:i+2] From 684538214ced3f92c87acccae2183e7831860026 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Thu, 10 Oct 2013 18:20:59 +0200 Subject: [PATCH 4/4] validate all verses before showing a message box and list all verses with misplaced tags at once --- openlp/plugins/songs/forms/editsongform.py | 24 +++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/openlp/plugins/songs/forms/editsongform.py b/openlp/plugins/songs/forms/editsongform.py index c9f28aec9..c63a15c7f 100644 --- a/openlp/plugins/songs/forms/editsongform.py +++ b/openlp/plugins/songs/forms/editsongform.py @@ -721,18 +721,28 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): self.manager.save_object(book) else: return False + cnt_errors = 0 + error_list = '' + verse_tag = [] + verse_num = [] for i in range(self.verseListWidget.rowCount()): item = self.verseListWidget.item(i, 0) 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 + verse_tag.append(VerseType.translated_name(field[0])) + verse_num.append(field[1:]) + cnt_errors += 1; + if cnt_errors > 0: + for i in range(cnt_errors): + error_list += '%s %s' % (verse_tag[i], verse_num[i]) + if i < cnt_errors-1: + error_list += ', ' + critical_error_message_box( + message=translate('SongsPlugin.EditSongForm', + 'There are misplaced formatting tags in the following verses:\n\n%s\n\n' + 'Please correct these tags before continuing.' % error_list)) + return False return True def _validate_tags(self, _tags):