Merge lp:~googol-deactivatedaccount/openlp/bug-863376 into lp:openlp

Proposed by Andreas Preikschat
Status: Merged
Merged at revision: 1774
Proposed branch: lp:~googol-deactivatedaccount/openlp/bug-863376
Merge into: lp:openlp
Diff against target: 108 lines (+23/-22)
1 file modified
openlp/plugins/songs/lib/xml.py (+23/-22)
To merge this branch: bzr merge lp:~googol-deactivatedaccount/openlp/bug-863376
Reviewer Review Type Date Requested Status
Tim Bentley Approve
Review via email: mp+78778@code.launchpad.net

This proposal supersedes a proposal from 2011-10-08.

Commit message

- fixed bug 863376 ('<' in song lyrics causes a traceback)
- clean ups

Description of the change

Hello,

- fixed bug 863376 ("<" in song lyrics causes a traceback) (See line 32)
- clean ups

To post a comment you must log in.
Revision history for this message
Raoul Snyman (raoul-snyman) wrote : Posted in a previous version of this proposal

What happens if you add a song with "&" in it?

Revision history for this message
Tim Bentley (trb143) wrote : Posted in a previous version of this proposal

Comments should be on a separate line 17,18
Line 95 u'<lines>' + text + u'</lines>' should be u'<lines>%s</lines>' % text ??????????

review: Needs Fixing
Revision history for this message
Andreas Preikschat (googol-deactivatedaccount) wrote : Posted in a previous version of this proposal

> What happens if you add a song with "&" in it?

All critical characters are escaped:
>>> import cgi
>>> cgi.escape(u'aaa < bbb > ccc & ddd')
u'aaa &lt; bbb &gt; ccc &amp; ddd'

Revision history for this message
Tim Bentley (trb143) :
review: Approve
Revision history for this message
Andreas Preikschat (googol-deactivatedaccount) wrote :

I assume you forgot to set it to "Approved". However, I am merging it now.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'openlp/plugins/songs/lib/xml.py'
2--- openlp/plugins/songs/lib/xml.py 2011-09-23 00:12:55 +0000
3+++ openlp/plugins/songs/lib/xml.py 2011-10-09 19:56:25 +0000
4@@ -60,7 +60,7 @@
5 </lyrics>
6 </song>
7 """
8-
9+import cgi
10 import logging
11 import re
12
13@@ -257,11 +257,12 @@
14
15 """
16 IMPLEMENTED_VERSION = u'0.8'
17+ START_TAGS_REGEX = re.compile(r'\{(\w+)\}')
18+ END_TAGS_REGEX = re.compile(r'\{\/(\w+)\}')
19+ VERSE_NUMBER_REGEX = re.compile(u'[a-zA-Z]*')
20
21 def __init__(self, manager):
22 self.manager = manager
23- self.start_tags_regex = re.compile(r'\{(\w+)\}') # {abc} -> abc
24- self.end_tags_regex = re.compile(r'\{\/(\w+)\}') # {/abc} -> abc
25
26 def song_to_xml(self, song):
27 """
28@@ -334,7 +335,8 @@
29 if u'lang' in verse[0]:
30 verse_element.set(u'lang', verse[0][u'lang'])
31 # Create a list with all "virtual" verses.
32- virtual_verses = verse[1].split(u'[---]')
33+ virtual_verses = cgi.escape(verse[1])
34+ virtual_verses = virtual_verses.split(u'[---]')
35 for index, virtual_verse in enumerate(virtual_verses):
36 # Add formatting tags to text
37 lines_element = self._add_text_with_tags_to_lines(verse_element,
38@@ -402,32 +404,32 @@
39
40 def _add_tag_to_formatting(self, tag_name, tags_element):
41 """
42- Add new formatting tag to the element ``<format>``
43- if the tag is not present yet.
44+ Add new formatting tag to the element ``<format>`` if the tag is not
45+ present yet.
46 """
47 available_tags = FormattingTags.get_html_tags()
48 start_tag = '{%s}' % tag_name
49- for t in available_tags:
50- if t[u'start tag'] == start_tag:
51+ for tag in available_tags:
52+ if tag[u'start tag'] == start_tag:
53 # Create new formatting tag in openlyrics xml.
54- el = self._add_text_to_element(u'tag', tags_element)
55- el.set(u'name', tag_name)
56- el_open = self._add_text_to_element(u'open', el)
57- el_open.text = etree.CDATA(t[u'start html'])
58+ element = self._add_text_to_element(u'tag', tags_element)
59+ element.set(u'name', tag_name)
60+ element_open = self._add_text_to_element(u'open', element)
61+ element_open.text = etree.CDATA(tag[u'start html'])
62 # Check if formatting tag contains end tag. Some formatting
63 # tags e.g. {br} has only start tag. If no end tag is present
64 # <close> element has not to be in OpenLyrics xml.
65- if t['end tag']:
66- el_close = self._add_text_to_element(u'close', el)
67- el_close.text = etree.CDATA(t[u'end html'])
68+ if tag['end tag']:
69+ element_close = self._add_text_to_element(u'close', element)
70+ element_close.text = etree.CDATA(tag[u'end html'])
71
72 def _add_text_with_tags_to_lines(self, verse_element, text, tags_element):
73 """
74 Convert text with formatting tags from OpenLP format to OpenLyrics
75 format and append it to element ``<lines>``.
76 """
77- start_tags = self.start_tags_regex.findall(text)
78- end_tags = self.end_tags_regex.findall(text)
79+ start_tags = OpenLyrics.START_TAGS_REGEX.findall(text)
80+ end_tags = OpenLyrics.END_TAGS_REGEX.findall(text)
81 # Replace start tags with xml syntax.
82 for tag in start_tags:
83 # Tags already converted to xml structure.
84@@ -442,12 +444,11 @@
85 if tag not in xml_tags:
86 self._add_tag_to_formatting(tag, tags_element)
87 # Replace end tags.
88- for t in end_tags:
89- text = text.replace(u'{/%s}' % t, u'</tag>')
90+ for tag in end_tags:
91+ text = text.replace(u'{/%s}' % tag, u'</tag>')
92 # Replace \n with <br/>.
93 text = text.replace(u'\n', u'<br/>')
94- text = u'<lines>' + text + u'</lines>'
95- element = etree.XML(text)
96+ element = etree.XML(u'<lines>%s</lines>' % text)
97 verse_element.append(element)
98 return element
99
100@@ -692,7 +693,7 @@
101 verse_tag = verse_def[0]
102 else:
103 verse_tag = VerseType.Tags[VerseType.Other]
104- verse_number = re.compile(u'[a-zA-Z]*').sub(u'', verse_def)
105+ verse_number = OpenLyrics.VERSE_NUMBER_REGEX.sub(u'', verse_def)
106 # OpenLyrics allows e. g. "c", but we need "c1". However, this does
107 # not correct the verse order.
108 if not verse_number: