diff --git a/openlp/plugins/songs/lib/ewimport.py b/openlp/plugins/songs/lib/ewimport.py index 251068fb4..820cf595a 100644 --- a/openlp/plugins/songs/lib/ewimport.py +++ b/openlp/plugins/songs/lib/ewimport.py @@ -31,17 +31,33 @@ EasyWorship song databases into the current installation database. import os import struct +import re from openlp.core.lib import translate from openlp.plugins.songs.lib import VerseType from openlp.plugins.songs.lib import retrieve_windows_encoding from songimport import SongImport +RTF_STRIPPING_REGEX = re.compile(r'\{\\tx[^}]*\}') +# regex: at least two newlines, can have spaces between them +SLIDE_BREAK_REGEX = re.compile(r'\n *?\n[\n ]*') +NUMBER_REGEX = re.compile(r'[0-9]+') +NOTE_REGEX = re.compile(r'\(.*?\)') + def strip_rtf(blob, encoding): depth = 0 control = False clear_text = [] control_word = [] + + # workaround for \tx bug: remove one pair of curly braces + # if \tx is encountered + match = RTF_STRIPPING_REGEX.search(blob) + if match: + # start and end indices of match are curly braces - filter them out + blob = ''.join([blob[i] for i in xrange(len(blob)) + if i != match.start() and i !=match.end()]) + for c in blob: if control: # for delimiters, set control to False @@ -258,9 +274,45 @@ class EasyWorshipSongImport(SongImport): if words: # Format the lyrics words = strip_rtf(words, self.encoding) - for verse in words.split(u'\n\n'): + verse_type = VerseType.Tags[VerseType.Verse] + for verse in SLIDE_BREAK_REGEX.split(words): + verse = verse.strip() + if not verse: + continue + verse_split = verse.split(u'\n', 1) + first_line_is_tag = False + # EW tags: verse, chorus, pre-chorus, bridge, tag, + # intro, ending, slide + for type in VerseType.Names+[u'tag', u'slide']: + type = type.lower() + ew_tag = verse_split[0].strip().lower() + if ew_tag.startswith(type): + verse_type = type[0] + if type == u'tag' or type == u'slide': + verse_type = VerseType.Tags[VerseType.Other] + first_line_is_tag = True + number_found = False + # check if tag is followed by number and/or note + if len(ew_tag) > len(type): + match = NUMBER_REGEX.search(ew_tag) + if match: + number = match.group() + verse_type += number + number_found = True + match = NOTE_REGEX.search(ew_tag) + if match: + self.comments += ew_tag + u'\n' + if not number_found: + verse_type += u'1' + break self.add_verse( - verse.strip(), VerseType.Tags[VerseType.Verse]) + verse_split[-1].strip() if first_line_is_tag else verse, + verse_type) + if len(self.comments) > 5: + self.comments += unicode( + translate('SongsPlugin.EasyWorshipSongImport', + '\n[above are Song Tags with notes imported from \ + EasyWorship]')) if self.stop_import_flag: break if not self.finish():