From 938f59416d2b89d20e41730d3ea8f1b6f14f913b Mon Sep 17 00:00:00 2001 From: Samuel Mehrbrodt Date: Sat, 3 May 2014 15:46:48 +0200 Subject: [PATCH] Fix bug 1199639 Fixes: https://launchpad.net/bugs/1199639 --- openlp/plugins/songs/forms/editsongform.py | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/openlp/plugins/songs/forms/editsongform.py b/openlp/plugins/songs/forms/editsongform.py index 1814655ea..81a66e3f7 100644 --- a/openlp/plugins/songs/forms/editsongform.py +++ b/openlp/plugins/songs/forms/editsongform.py @@ -107,6 +107,7 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog, RegistryProperties): self.audio_list_widget.setAlternatingRowColors(True) self.find_verse_split = re.compile('---\[\]---\n', re.UNICODE) self.whitespace = re.compile(r'\W+', re.UNICODE) + self.find_tags = re.compile(u'\{/?\w+\}', re.UNICODE) def _load_objects(self, cls, combo, cache): """ @@ -234,8 +235,43 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog, RegistryProperties): self.manager.save_object(book) else: return False + # Validate tags (lp#1199639) + misplaced_tags = [] + for i in range(self.verse_list_widget.rowCount()): + item = self.verse_list_widget.item(i, 0) + tags = self.find_tags.findall(item.text()) + if not self._validate_tags(tags): + field = item.data(QtCore.Qt.UserRole) + misplaced_tags.append('%s %s' % (VerseType.translated_name(field[0]), field[1:])) + if misplaced_tags: + 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.' % ', '.join(misplaced_tags))) + 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 length 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. Else 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] + return self._validate_tags(_tags) + return False + def _process_lyrics(self): """ Process the lyric data entered by the user into the OpenLP XML format.