diff --git a/openlp/core/utils/__init__.py b/openlp/core/utils/__init__.py index 67d624b6b..297388072 100644 --- a/openlp/core/utils/__init__.py +++ b/openlp/core/utils/__init__.py @@ -53,6 +53,8 @@ APPLICATION_VERSION = {} IMAGES_FILTER = None UNO_CONNECTION_TYPE = u'pipe' #UNO_CONNECTION_TYPE = u'socket' +CONTROL_CHARS = re.compile(u'[\x00-\x1F\x7F-\x9F]', re.UNICODE) +INVALID_FILE_CHARS = re.compile(u'[\\\/:\*\?"<>\|]', re.UNICODE) VERSION_SPLITTER = re.compile(r'([0-9]+).([0-9]+).([0-9]+)(?:-bzr([0-9]+))?') class VersionThread(QtCore.QThread): @@ -400,7 +402,7 @@ def clean_filename(filename): """ if not isinstance(filename, unicode): filename = unicode(filename, u'utf-8') - return re.sub(r'[/\\?*|<>\[\]":<>+%\n]+', u'_', filename).strip(u'_') + return INVALID_FILE_CHARS.sub(u'_', CONTROL_CHARS.sub(u'', filename)) def delete_file(file_path_name): """ diff --git a/openlp/plugins/songs/lib/__init__.py b/openlp/plugins/songs/lib/__init__.py index 1c3fb76df..b17d3b1a8 100644 --- a/openlp/plugins/songs/lib/__init__.py +++ b/openlp/plugins/songs/lib/__init__.py @@ -256,6 +256,13 @@ def clean_string(string): Strips punctuation from the passed string to assist searching """ return WHITESPACE.sub(u' ', APOSTROPHE.sub(u'', string)).lower() + +def clean_title(title): + """ + Cleans the song title by removing Unicode control chars groups C0 & C1, + as well as any trailing spaces + """ + return re.sub(u'[\x00-\x1F\x7F-\x9F]| +$', u'', title, re.UNICODE) def clean_song(manager, song): """ @@ -275,10 +282,14 @@ def clean_song(manager, song): song.alternate_title = unicode(song.alternate_title) if isinstance(song.lyrics, buffer): song.lyrics = unicode(song.lyrics) - song.title = song.title.rstrip() if song.title else u'' - if song.alternate_title is None: + if song.title: + song.title = clean_title(song.title) + else: + song.title = u'' + if song.alternate_title: + song.alternate_title = clean_title(song.alternate_title) + else: song.alternate_title = u'' - song.alternate_title = song.alternate_title.strip() song.search_title = clean_string(song.title) + u'@' + \ clean_string(song.alternate_title) # Only do this, if we the song is a 1.9.4 song (or older).