- added the <u>Underline</u> tag

- clean ups

SongBeamerImport:
- clean ups and tweaks
- fixed a bug which would drop the first line of a verse, when it does not have any "verse type"
- convert SongBeamer's html tags to OpenLP specific style

bzr-revno: 1128
This commit is contained in:
andreas 2010-12-03 20:42:56 +00:00 committed by Tim Bentley
commit 7c65303400
4 changed files with 173 additions and 119 deletions

View File

@ -38,48 +38,51 @@ log = logging.getLogger(__name__)
# TODO make external and configurable in alpha 4 via a settings dialog # TODO make external and configurable in alpha 4 via a settings dialog
html_expands = [] html_expands = []
html_expands.append({u'desc':u'Red', u'start tag':u'{r}', html_expands.append({u'desc': u'Red', u'start tag': u'{r}',
u'start html':u'<span style="-webkit-text-fill-color:red">', u'start html': u'<span style="-webkit-text-fill-color:red">',
u'end tag':u'{/r}', u'end html':u'</span>', u'protected':False}) u'end tag': u'{/r}', u'end html': u'</span>', u'protected': False})
html_expands.append({u'desc':u'Black', u'start tag':u'{b}', html_expands.append({u'desc': u'Black', u'start tag': u'{b}',
u'start html':u'<span style="-webkit-text-fill-color:black">', u'start html': u'<span style="-webkit-text-fill-color:black">',
u'end tag':u'{/b}', u'end html':u'</span>', u'protected':False}) u'end tag': u'{/b}', u'end html': u'</span>', u'protected': False})
html_expands.append({u'desc':u'Blue', u'start tag':u'{bl}', html_expands.append({u'desc': u'Blue', u'start tag': u'{bl}',
u'start html':u'<span style="-webkit-text-fill-color:blue">', u'start html': u'<span style="-webkit-text-fill-color:blue">',
u'end tag':u'{/bl}', u'end html':u'</span>', u'protected':False}) u'end tag': u'{/bl}', u'end html': u'</span>', u'protected': False})
html_expands.append({u'desc':u'Yellow', u'start tag':u'{y}', html_expands.append({u'desc': u'Yellow', u'start tag': u'{y}',
u'start html':u'<span style="-webkit-text-fill-color:yellow">', u'start html': u'<span style="-webkit-text-fill-color:yellow">',
u'end tag':u'{/y}', u'end html':u'</span>', u'protected':False}) u'end tag': u'{/y}', u'end html': u'</span>', u'protected': False})
html_expands.append({u'desc':u'Green', u'start tag':u'{g}', html_expands.append({u'desc': u'Green', u'start tag': u'{g}',
u'start html':u'<span style="-webkit-text-fill-color:green">', u'start html': u'<span style="-webkit-text-fill-color:green">',
u'end tag':u'{/g}', u'end html':u'</span>', u'protected':False}) u'end tag': u'{/g}', u'end html': u'</span>', u'protected': False})
html_expands.append({u'desc':u'Pink', u'start tag':u'{pk}', html_expands.append({u'desc': u'Pink', u'start tag': u'{pk}',
u'start html':u'<span style="-webkit-text-fill-color:#CC33CC">', u'start html': u'<span style="-webkit-text-fill-color:#CC33CC">',
u'end tag':u'{/pk}', u'end html':u'</span>', u'protected':False}) u'end tag': u'{/pk}', u'end html': u'</span>', u'protected': False})
html_expands.append({u'desc':u'Orange', u'start tag':u'{o}', html_expands.append({u'desc': u'Orange', u'start tag': u'{o}',
u'start html':u'<span style="-webkit-text-fill-color:#CC0033">', u'start html': u'<span style="-webkit-text-fill-color:#CC0033">',
u'end tag':u'{/o}', u'end html':u'</span>', u'protected':False}) u'end tag': u'{/o}', u'end html': u'</span>', u'protected': False})
html_expands.append({u'desc':u'Purple', u'start tag':u'{pp}', html_expands.append({u'desc': u'Purple', u'start tag': u'{pp}',
u'start html':u'<span style="-webkit-text-fill-color:#9900FF">', u'start html': u'<span style="-webkit-text-fill-color:#9900FF">',
u'end tag':u'{/pp}', u'end html':u'</span>', u'protected':False}) u'end tag': u'{/pp}', u'end html': u'</span>', u'protected': False})
html_expands.append({u'desc':u'White', u'start tag':u'{w}', html_expands.append({u'desc': u'White', u'start tag': u'{w}',
u'start html':u'<span style="-webkit-text-fill-color:white">', u'start html': u'<span style="-webkit-text-fill-color:white">',
u'end tag':u'{/w}', u'end html':u'</span>', u'protected':False}) u'end tag': u'{/w}', u'end html': u'</span>', u'protected': False})
html_expands.append({u'desc':u'Superscript', u'start tag':u'{su}', html_expands.append({u'desc': u'Superscript', u'start tag': u'{su}',
u'start html':u'<sup>', u'end tag':u'{/su}', u'end html':u'</sup>', u'start html': u'<sup>', u'end tag': u'{/su}', u'end html': u'</sup>',
u'protected':True}) u'protected': True})
html_expands.append({u'desc':u'Subscript', u'start tag':u'{sb}', html_expands.append({u'desc': u'Subscript', u'start tag': u'{sb}',
u'start html':u'<sub>', u'end tag':u'{/sb}', u'end html':u'</sub>', u'start html': u'<sub>', u'end tag': u'{/sb}', u'end html': u'</sub>',
u'protected':True}) u'protected': True})
html_expands.append({u'desc':u'Paragraph', u'start tag':u'{p}', html_expands.append({u'desc': u'Paragraph', u'start tag': u'{p}',
u'start html':u'<p>', u'end tag':u'{/p}', u'end html':u'</p>', u'start html': u'<p>', u'end tag': u'{/p}', u'end html': u'</p>',
u'protected':True}) u'protected': True})
html_expands.append({u'desc':u'Bold', u'start tag':u'{st}', html_expands.append({u'desc': u'Bold', u'start tag': u'{st}',
u'start html':u'<strong>', u'end tag':u'{/st}', u'end html':u'</strong>', u'start html': u'<strong>', u'end tag': u'{/st}', u'end html': u'</strong>',
u'protected':True}) u'protected': True})
html_expands.append({u'desc':u'Italics', u'start tag':u'{it}', html_expands.append({u'desc': u'Italics', u'start tag': u'{it}',
u'start html':u'<em>', u'end tag':u'{/it}', u'end html':u'</em>', u'start html': u'<em>', u'end tag': u'{/it}', u'end html': u'</em>',
u'protected':True}) u'protected': True})
html_expands.append({u'desc': u'Underline', u'start tag': u'{u}',
u'start html': u'<span style="text-decoration: underline;">',
u'end tag': u'{/u}', u'end html': u'</span>', u'protected': True})
def translate(context, text, comment=None): def translate(context, text, comment=None):
""" """

View File

@ -507,8 +507,7 @@ class SongImportForm(QtGui.QWizard, Ui_SongImportWizard):
filenames=self.getListOfFiles( filenames=self.getListOfFiles(
self.songBeamerFileListWidget) self.songBeamerFileListWidget)
) )
success = importer.do_import() if importer.do_import():
if success:
# reload songs # reload songs
self.importProgressLabel.setText( self.importProgressLabel.setText(
translate('SongsPlugin.SongImportForm', 'Finished import.')) translate('SongsPlugin.SongImportForm', 'Finished import.'))

View File

@ -57,15 +57,17 @@ class SongBeamerTypes(object):
u'Unknown': u'O' u'Unknown': u'O'
} }
class SongBeamerImport(SongImport): class SongBeamerImport(SongImport):
""" """
Import Song Beamer files(s) Import Song Beamer files(s)
Song Beamer file format is text based Song Beamer file format is text based
in the beginning are one or more control tags written in the beginning are one or more control tags written
""" """
def __init__(self, master_manager, **kwargs): def __init__(self, master_manager, **kwargs):
""" """
Initialise the import. Initialise the import.
``master_manager`` ``master_manager``
The song manager for the running OpenLP installation. The song manager for the running OpenLP installation.
""" """
@ -88,6 +90,7 @@ class SongBeamerImport(SongImport):
# TODO: check that it is a valid SongBeamer file # TODO: check that it is a valid SongBeamer file
self.current_verse = u'' self.current_verse = u''
self.current_verse_type = u'V' self.current_verse_type = u'V'
read_verses = False
self.file_name = os.path.split(file)[1] self.file_name = os.path.split(file)[1]
self.import_wizard.incrementProgressBar( self.import_wizard.incrementProgressBar(
"Importing %s" % (self.file_name), 0) "Importing %s" % (self.file_name), 0)
@ -100,134 +103,182 @@ class SongBeamerImport(SongImport):
else: else:
return False return False
for line in self.songData: for line in self.songData:
line = line.strip() # Just make sure that the line is of the type 'Unicode'.
if line.startswith('#'): line = unicode(line).strip()
log.debug(u'find tag: %s' % line) if line.startswith(u'#') and not read_verses:
if not self.parse_tags(line): self.parse_tags(line)
return False elif line.startswith(u'---'):
elif line.startswith('---'): if self.current_verse:
log.debug(u'find ---') self.replace_html_tags()
if len(self.current_verse) > 0: self.add_verse(self.current_verse,
self.add_verse(self.current_verse,
self.current_verse_type) self.current_verse_type)
self.current_verse = u'' self.current_verse = u''
self.current_verse_type = u'V' self.current_verse_type = u'V'
self.read_verse = True read_verses = True
self.verse_start = True verse_start = True
elif self.read_verse: elif read_verses:
if self.verse_start: if verse_start:
self.check_verse_marks(line) verse_start = False
self.verse_start = False if not self.check_verse_marks(line):
self.current_verse = u'%s\n' % line
else: else:
self.current_verse += u'%s\n' % line self.current_verse += u'%s\n' % line
if len(self.current_verse) > 0: if self.current_verse:
self.replace_html_tags()
self.add_verse(self.current_verse, self.current_verse_type) self.add_verse(self.current_verse, self.current_verse_type)
self.finish() self.finish()
self.import_wizard.incrementProgressBar( self.import_wizard.incrementProgressBar(
"Importing %s" % (self.file_name)) "Importing %s" % (self.file_name))
return True return True
def replace_html_tags(self):
"""
This can be called to replace SongBeamer's specific (html) tags with
OpenLP's specific (html) tags.
"""
tag_pairs = [
(u'<b>', u'{st}'),
(u'</b>', u'{/st}'),
(u'<i>', u'{it}'),
(u'</i>', u'{/it}'),
(u'<u>', u'{u}'),
(u'</u>', u'{/u}'),
(u'<br>', u'{st}'),
(u'</br>', u'{st}'),
(u'</ br>', u'{st}'),
(u'<p>', u'{p}'),
(u'</p>', u'{/p}'),
(u'<super>', u'{su}'),
(u'</super>', u'{/su}'),
(u'<sub>', u'{sb}'),
(u'</sub>', u'{/sb}'),
(u'<wordwrap>', u''),
(u'</wordwrap>', u''),
(u'<strike>', u''),
(u'</strike>', u'')
]
for pair in tag_pairs:
self.current_verse = self.current_verse.replace(pair[0], pair[1])
# TODO: check for unsupported tags (see wiki) and remove them as well.
def parse_tags(self, line): def parse_tags(self, line):
tag_val = line.split('=') """
if len(tag_val[0]) == 0 or len(tag_val[1]) == 0: Parses a meta data line.
return True
if tag_val[0] == '#(c)': ``line``
The line in the file. It should consist of a tag and a value
for this tag (unicode)::
u'#Title=Nearer my God to Thee'
"""
tag_val = line.split(u'=', 1)
if len(tag_val) == 1:
return
if not tag_val[0] or not tag_val[1]:
return
if tag_val[0] == u'#(c)':
self.add_copyright(tag_val[1]) self.add_copyright(tag_val[1])
elif tag_val[0] == '#AddCopyrightInfo': elif tag_val[0] == u'#AddCopyrightInfo':
pass pass
elif tag_val[0] == '#Author': elif tag_val[0] == u'#Author':
#TODO split Authors self.parse_author(tag_val[1])
self.add_author(tag_val[1]) elif tag_val[0] == u'#BackgroundImage':
elif tag_val[0] == '#BackgroundImage':
pass pass
elif tag_val[0] == '#Bible': elif tag_val[0] == u'#Bible':
pass pass
elif tag_val[0] == '#Categories': elif tag_val[0] == u'#Categories':
self.topics = line.split(',') self.topics = line.split(',')
elif tag_val[0] == '#CCLI': elif tag_val[0] == u'#CCLI':
self.ccli_number = tag_val[1] self.ccli_number = tag_val[1]
elif tag_val[0] == '#Chords': elif tag_val[0] == u'#Chords':
pass pass
elif tag_val[0] == '#ChurchSongID': elif tag_val[0] == u'#ChurchSongID':
pass pass
elif tag_val[0] == '#ColorChords': elif tag_val[0] == u'#ColorChords':
pass pass
elif tag_val[0] == '#Comments': elif tag_val[0] == u'#Comments':
self.comments = tag_val[1] self.comments = tag_val[1]
elif tag_val[0] == '#Editor': elif tag_val[0] == u'#Editor':
pass pass
elif tag_val[0] == '#Font': elif tag_val[0] == u'#Font':
pass pass
elif tag_val[0] == '#FontLang2': elif tag_val[0] == u'#FontLang2':
pass pass
elif tag_val[0] == '#FontSize': elif tag_val[0] == u'#FontSize':
pass pass
elif tag_val[0] == '#Format': elif tag_val[0] == u'#Format':
pass pass
elif tag_val[0] == '#Format_PreLine': elif tag_val[0] == u'#Format_PreLine':
pass pass
elif tag_val[0] == '#Format_PrePage': elif tag_val[0] == u'#Format_PrePage':
pass pass
elif tag_val[0] == '#ID': elif tag_val[0] == u'#ID':
pass pass
elif tag_val[0] == '#Key': elif tag_val[0] == u'#Key':
pass pass
elif tag_val[0] == '#Keywords': elif tag_val[0] == u'#Keywords':
pass pass
elif tag_val[0] == '#LangCount': elif tag_val[0] == u'#LangCount':
pass pass
elif tag_val[0] == '#Melody': elif tag_val[0] == u'#Melody':
#TODO split Authors self.parse_author(tag_val[1])
self.add_author(tag_val[1]) elif tag_val[0] == u'#NatCopyright':
elif tag_val[0] == '#NatCopyright':
pass pass
elif tag_val[0] == '#OTitle': elif tag_val[0] == u'#OTitle':
pass pass
elif tag_val[0] == '#OutlineColor': elif tag_val[0] == u'#OutlineColor':
pass pass
elif tag_val[0] == '#OutlinedFont': elif tag_val[0] == u'#OutlinedFont':
pass pass
elif tag_val[0] == '#QuickFind': elif tag_val[0] == u'#QuickFind':
pass pass
elif tag_val[0] == '#Rights': elif tag_val[0] == u'#Rights':
song_book_pub = tag_val[1] song_book_pub = tag_val[1]
elif tag_val[0] == '#Songbook': elif tag_val[0] == u'#Songbook':
book_num = tag_val[1].split(' / ') book_num = tag_val[1].split(' / ')
self.song_book_name = book_num[0] self.song_book_name = book_num[0]
if len(book_num) == book_num[1]: if len(book_num) == book_num[1]:
self.song_number = u'' self.song_number = u''
elif tag_val[0] == '#Speed': elif tag_val[0] == u'#Speed':
pass pass
elif tag_val[0] == '#TextAlign': elif tag_val[0] == u'#TextAlign':
pass pass
elif tag_val[0] == '#Title': elif tag_val[0] == u'#Title':
self.title = u'%s' % tag_val[1] self.title = u'%s' % tag_val[1]
elif tag_val[0] == '#TitleAlign': elif tag_val[0] == u'#TitleAlign':
pass pass
elif tag_val[0] == '#TitleFontSize': elif tag_val[0] == u'#TitleFontSize':
pass pass
elif tag_val[0] == '#TitleLang2': elif tag_val[0] == u'#TitleLang2':
pass pass
elif tag_val[0] == '#TitleLang3': elif tag_val[0] == u'#TitleLang3':
pass pass
elif tag_val[0] == '#TitleLang4': elif tag_val[0] == u'#TitleLang4':
pass pass
elif tag_val[0] == '#Translation': elif tag_val[0] == u'#Translation':
pass pass
elif tag_val[0] == '#Transpose': elif tag_val[0] == u'#Transpose':
pass pass
elif tag_val[0] == '#TransposeAccidental': elif tag_val[0] == u'#TransposeAccidental':
pass pass
elif tag_val[0] == '#Version': elif tag_val[0] == u'#Version':
pass pass
else:
pass
return True
def check_verse_marks(self, line): def check_verse_marks(self, line):
marks = line.split(' ') """
Check and add the verse's MarkType. Returns ``True`` if the given line
contains a correct verse mark otherwise ``False``.
``line``
The line to check for marks (unicode).
"""
marks = line.split(u' ')
if len(marks) <= 2 and marks[0] in SongBeamerTypes.MarkTypes: if len(marks) <= 2 and marks[0] in SongBeamerTypes.MarkTypes:
self.current_verse_type = SongBeamerTypes.MarkTypes[marks[0]] self.current_verse_type = SongBeamerTypes.MarkTypes[marks[0]]
if len(marks) == 2: if len(marks) == 2:
#TODO: may check, because of only digits are allowed # If we have a digit, we append it to current_verse_type.
self.current_verse_type += marks[1] if marks[1].isdigit():
self.current_verse_type += marks[1]
return True
else:
return False

View File

@ -254,7 +254,8 @@ class SongImport(QtCore.QObject):
All fields have been set to this song. Write it away All fields have been set to this song. Write it away
""" """
if not self.authors: if not self.authors:
self.authors.append(u'Author unknown') self.authors.append(unicode(translate('SongsPlugin.SongImport',
'Author unknown')))
self.commit_song() self.commit_song()
def commit_song(self): def commit_song(self):