forked from openlp/openlp
fixed bug 885874 (Song with mis matched formatting tags abends on render); fixed one extra new lines added before and after an optional break
bzr-revno: 1965 Fixes: https://launchpad.net/bugs/885874
This commit is contained in:
commit
265b703ba9
@ -235,8 +235,8 @@ class Renderer(object):
|
|||||||
# the first two slides (and neglect the last for now).
|
# the first two slides (and neglect the last for now).
|
||||||
if len(slides) == 3:
|
if len(slides) == 3:
|
||||||
html_text = expand_tags(u'\n'.join(slides[:2]))
|
html_text = expand_tags(u'\n'.join(slides[:2]))
|
||||||
# We check both slides to determine if the optional break is
|
# We check both slides to determine if the optional split is
|
||||||
# needed (there is only one optional break).
|
# needed (there is only one optional split).
|
||||||
else:
|
else:
|
||||||
html_text = expand_tags(u'\n'.join(slides))
|
html_text = expand_tags(u'\n'.join(slides))
|
||||||
html_text = html_text.replace(u'\n', u'<br>')
|
html_text = html_text.replace(u'\n', u'<br>')
|
||||||
@ -247,14 +247,18 @@ class Renderer(object):
|
|||||||
else:
|
else:
|
||||||
# The first optional slide fits, which means we have to
|
# The first optional slide fits, which means we have to
|
||||||
# render the first optional slide.
|
# render the first optional slide.
|
||||||
text_contains_break = u'[---]' in text
|
text_contains_split = u'[---]' in text
|
||||||
if text_contains_break:
|
if text_contains_split:
|
||||||
try:
|
try:
|
||||||
text_to_render, text = \
|
text_to_render, text = \
|
||||||
text.split(u'\n[---]\n', 1)
|
text.split(u'\n[---]\n', 1)
|
||||||
except:
|
except:
|
||||||
text_to_render = text.split(u'\n[---]\n')[0]
|
text_to_render = text.split(u'\n[---]\n')[0]
|
||||||
text = u''
|
text = u''
|
||||||
|
text_to_render, raw_tags, html_tags = \
|
||||||
|
self._get_start_tags(text_to_render)
|
||||||
|
if text:
|
||||||
|
text = raw_tags + text
|
||||||
else:
|
else:
|
||||||
text_to_render = text
|
text_to_render = text
|
||||||
text = u''
|
text = u''
|
||||||
@ -263,7 +267,7 @@ class Renderer(object):
|
|||||||
if len(slides) > 1 and text:
|
if len(slides) > 1 and text:
|
||||||
# Add all slides apart from the last one the list.
|
# Add all slides apart from the last one the list.
|
||||||
pages.extend(slides[:-1])
|
pages.extend(slides[:-1])
|
||||||
if text_contains_break:
|
if text_contains_split:
|
||||||
text = slides[-1] + u'\n[---]\n' + text
|
text = slides[-1] + u'\n[---]\n' + text
|
||||||
else:
|
else:
|
||||||
text = slides[-1] + u'\n'+ text
|
text = slides[-1] + u'\n'+ text
|
||||||
|
@ -33,7 +33,7 @@ The basic XML for storing the lyrics in the song database looks like this::
|
|||||||
<song version="1.0">
|
<song version="1.0">
|
||||||
<lyrics>
|
<lyrics>
|
||||||
<verse type="c" label="1" lang="en">
|
<verse type="c" label="1" lang="en">
|
||||||
<![CDATA[Chorus virtual slide 1[---]Chorus virtual slide 2]]>
|
<![CDATA[Chorus optional split 1[---]Chorus optional split 2]]>
|
||||||
</verse>
|
</verse>
|
||||||
</lyrics>
|
</lyrics>
|
||||||
</song>
|
</song>
|
||||||
@ -135,7 +135,7 @@ class SongXML(object):
|
|||||||
The returned list has the following format::
|
The returned list has the following format::
|
||||||
|
|
||||||
[[{'type': 'v', 'label': '1'},
|
[[{'type': 'v', 'label': '1'},
|
||||||
u"virtual slide 1[---]virtual slide 2"],
|
u"optional slide split 1[---]optional slide split 2"],
|
||||||
[{'lang': 'en', 'type': 'c', 'label': '1'}, u"English chorus"]]
|
[{'lang': 'en', 'type': 'c', 'label': '1'}, u"English chorus"]]
|
||||||
"""
|
"""
|
||||||
self.song_xml = None
|
self.song_xml = None
|
||||||
@ -334,18 +334,59 @@ class OpenLyrics(object):
|
|||||||
self._add_text_to_element(u'verse', lyrics, None, verse_def)
|
self._add_text_to_element(u'verse', lyrics, None, verse_def)
|
||||||
if u'lang' in verse[0]:
|
if u'lang' in verse[0]:
|
||||||
verse_element.set(u'lang', verse[0][u'lang'])
|
verse_element.set(u'lang', verse[0][u'lang'])
|
||||||
# Create a list with all "virtual" verses.
|
# Create a list with all "optional" verses.
|
||||||
virtual_verses = cgi.escape(verse[1])
|
optional_verses = cgi.escape(verse[1])
|
||||||
virtual_verses = virtual_verses.split(u'[---]')
|
optional_verses = optional_verses.split(u'\n[---]\n')
|
||||||
for index, virtual_verse in enumerate(virtual_verses):
|
start_tags = u''
|
||||||
|
end_tags = u''
|
||||||
|
for index, optional_verse in enumerate(optional_verses):
|
||||||
|
# Fix up missing end and start tags such as {r} or {/r}.
|
||||||
|
optional_verse = start_tags + optional_verse
|
||||||
|
start_tags, end_tags = self._get_missing_tags(optional_verse)
|
||||||
|
optional_verse += end_tags
|
||||||
# Add formatting tags to text
|
# Add formatting tags to text
|
||||||
lines_element = self._add_text_with_tags_to_lines(verse_element,
|
lines_element = self._add_text_with_tags_to_lines(verse_element,
|
||||||
virtual_verse, tags_element)
|
optional_verse, tags_element)
|
||||||
# Do not add the break attribute to the last lines element.
|
# Do not add the break attribute to the last lines element.
|
||||||
if index < len(virtual_verses) - 1:
|
if index < len(optional_verses) - 1:
|
||||||
lines_element.set(u'break', u'optional')
|
lines_element.set(u'break', u'optional')
|
||||||
return self._extract_xml(song_xml)
|
return self._extract_xml(song_xml)
|
||||||
|
|
||||||
|
def _get_missing_tags(self, text):
|
||||||
|
"""
|
||||||
|
Tests the given text for not closed formatting tags and returns a tuple
|
||||||
|
consisting of two unicode strings::
|
||||||
|
|
||||||
|
(u'{st}{r}', u'{/r}{/st}')
|
||||||
|
|
||||||
|
The first unicode string are the start tags (for the next slide). The
|
||||||
|
second unicode string are the end tags.
|
||||||
|
|
||||||
|
``text``
|
||||||
|
The text to test. The text must **not** contain html tags, only
|
||||||
|
OpenLP formatting tags are allowed::
|
||||||
|
|
||||||
|
{st}{r}Text text text
|
||||||
|
"""
|
||||||
|
tags = []
|
||||||
|
for tag in FormattingTags.get_html_tags():
|
||||||
|
if tag[u'start tag'] == u'{br}':
|
||||||
|
continue
|
||||||
|
if text.count(tag[u'start tag']) != text.count(tag[u'end tag']):
|
||||||
|
tags.append((text.find(tag[u'start tag']),
|
||||||
|
tag[u'start tag'], tag[u'end tag']))
|
||||||
|
# Sort the lists, so that the tags which were opened first on the first
|
||||||
|
# slide (the text we are checking) will be opened first on the next
|
||||||
|
# slide as well.
|
||||||
|
tags.sort(key=lambda tag: tag[0])
|
||||||
|
end_tags = []
|
||||||
|
start_tags = []
|
||||||
|
for tag in tags:
|
||||||
|
start_tags.append(tag[1])
|
||||||
|
end_tags.append(tag[2])
|
||||||
|
end_tags.reverse()
|
||||||
|
return u''.join(start_tags), u''.join(end_tags)
|
||||||
|
|
||||||
def xml_to_song(self, xml, parse_and_temporary_save=False):
|
def xml_to_song(self, xml, parse_and_temporary_save=False):
|
||||||
"""
|
"""
|
||||||
Create and save a song from OpenLyrics format xml to the database. Since
|
Create and save a song from OpenLyrics format xml to the database. Since
|
||||||
|
Loading…
Reference in New Issue
Block a user