From c8c8c7e6548710d33d597ee5f0a50801418ec117 Mon Sep 17 00:00:00 2001 From: Martin Zibricky Date: Thu, 8 Sep 2011 01:16:14 +0200 Subject: [PATCH] code to expand tag for openlyrics export --- openlp/plugins/songs/lib/xml.py | 26 ++++++++++++++++++++++++-- testing/test_openlyrics.py | 2 +- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/openlp/plugins/songs/lib/xml.py b/openlp/plugins/songs/lib/xml.py index a20951753..52f9b5292 100644 --- a/openlp/plugins/songs/lib/xml.py +++ b/openlp/plugins/songs/lib/xml.py @@ -310,6 +310,8 @@ class OpenLyrics(object): tags_element = None match = re.search(u'\{/?\w+\}', song.lyrics, re.UNICODE) if match: + # reset available tags + FormattingTags.reset_html_tags() # named 'formatting' - 'format' is built-in fuction in Python formatting = etree.SubElement(song_xml, u'format') tags_element = etree.SubElement(formatting, u'tags') @@ -398,12 +400,32 @@ class OpenLyrics(object): parent.append(element) return element + def _add_tag_to_formatting(self, tag_name, tags_element): + print '------' + available_tags = FormattingTags.get_html_tags() + start_tag = '{%s}' % tag_name + for t in available_tags: + if t[u'start tag'] == start_tag: + # create new formatting tag in openlyrics xml + el = self._add_text_to_element(u'tag', tags_element) + el.set(u'name', tag_name) + el_open = self._add_text_to_element(u'open', el) + el_close = self._add_text_to_element(u'close', el) + el_open.text = etree.CDATA(t[u'start html']) + el_close.text = etree.CDATA(t[u'end html']) + def _add_line_with_tags_to_lines(self, parent, text, tags_element): + # tags already converted to xml structure + xml_tags = tags_element.xpath(u'tag/attribute::name') start_tags = self.start_tags_regex.findall(text) end_tags = self.end_tags_regex.findall(text) # replace start tags with xml syntax - for t in start_tags: - text = text.replace(t, u'' % t[1:-1]) + for tag in start_tags: + name = tag[1:-1] + text = text.replace(tag, u'' % name) + # add tag to elment if tag not present + if name not in xml_tags: + self._add_tag_to_formatting(name, tags_element) # replace end tags for t in end_tags: text = text.replace(t, u'') diff --git a/testing/test_openlyrics.py b/testing/test_openlyrics.py index 2d4ce024e..32bde3fa0 100644 --- a/testing/test_openlyrics.py +++ b/testing/test_openlyrics.py @@ -46,7 +46,7 @@ def test_openlyrics_export(songs_db, openlyrics_validator, pth, tmpdir): tree.write(open(f.strpath, u'w'), encoding=u'utf-8', xml_declaration=True, pretty_print=True) # validate file - #assert openlyrics_validator.validate(f.strpath) == True + assert openlyrics_validator.validate(f.strpath) == True # string comparison with original file line by line f_orig = pth.songs.join('openlyrics_test_1.xml') for l, l_orig in zip(f.readlines(), f_orig.readlines()):