From f36830a272a42ede03ba9c588cb0abdb7e95c7a7 Mon Sep 17 00:00:00 2001 From: Mateus Meyer Jiacomelli Date: Sun, 18 Dec 2022 07:50:31 +0000 Subject: [PATCH] OpenLyrics importer: Fix whitespaces being 'eaten' when importing a song with chord tags --- .../plugins/songs/lib/importers/openlyrics.py | 12 ++++++---- .../songs/test_openlyricsimport.py | 22 +++++++++++++++++++ .../openlyrics/Amazing_Grace_3_chords.xml | 19 ++++++++++++++++ .../Amazing_Grace_3_chords_result.xml | 1 + 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 tests/resources/songs/openlyrics/Amazing_Grace_3_chords.xml create mode 100644 tests/resources/songs/openlyrics/Amazing_Grace_3_chords_result.xml diff --git a/openlp/plugins/songs/lib/importers/openlyrics.py b/openlp/plugins/songs/lib/importers/openlyrics.py index 2c4bc386a..9175f1a17 100644 --- a/openlp/plugins/songs/lib/importers/openlyrics.py +++ b/openlp/plugins/songs/lib/importers/openlyrics.py @@ -67,8 +67,8 @@ class OpenLyricsImport(SongImport): for elem in root.iter('{*}lines'): self._strip_whitespace(elem) for subelem in elem.iter('{*}br'): - self._strip_whitespace(subelem) - + next_subelem = subelem.getnext() + self._strip_whitespace(subelem, next_subelem) xml = etree.tostring(root).decode() self.open_lyrics.xml_to_song(xml) except etree.XMLSyntaxError: @@ -80,11 +80,15 @@ class OpenLyricsImport(SongImport): text=exception.log_message)) self.log_error(file_path, exception.display_message) - def _strip_whitespace(self, elem): + def _strip_whitespace(self, elem, next_subelem=None): """ Remove leading and trailing whitespace from the 'text' and 'tail' attributes of an etree._Element object """ + is_chord_after_tail = False + if next_subelem is not None: + if next_subelem.tag.endswith('chord'): + is_chord_after_tail = True if elem.text is not None: elem.text = elem.text.strip() - if elem.tail is not None: + if elem.tail is not None and not is_chord_after_tail: elem.tail = elem.tail.strip() diff --git a/tests/openlp_plugins/songs/test_openlyricsimport.py b/tests/openlp_plugins/songs/test_openlyricsimport.py index b5ac2f0dd..35ac42441 100644 --- a/tests/openlp_plugins/songs/test_openlyricsimport.py +++ b/tests/openlp_plugins/songs/test_openlyricsimport.py @@ -179,3 +179,25 @@ class TestOpenLyricsImport(TestCase, TestMixin): # THEN: The last call of the xml_to_song() method should have got the same XML content as its first call importer.open_lyrics.xml_to_song.assert_called_with(no_whitespaces_xml) + + def test_chord_leading_space_is_not_removed(self): + """ + Test if chords' leading space aren't removed when importing music. + """ + # GIVEN: One OpenLyrics XML with the tag (Amazing_Grace_3_chords.xml) + mocked_manager = MagicMock() + mocked_import_wizard = MagicMock() + importer = OpenLyricsImport(mocked_manager, file_paths=[]) + importer.import_wizard = mocked_import_wizard + expected_content_file = TEST_PATH / 'Amazing_Grace_3_chords_result.xml' + expected_content = expected_content_file.read_text() + + # WHEN: Importing the file not having those whitespaces... + importer.import_source = [TEST_PATH / 'Amazing_Grace_3_chords.xml'] + importer.open_lyrics = MagicMock() + importer.open_lyrics.xml_to_song = MagicMock() + importer.do_import() + + # THEN: The song should preserve spaces before chords + import_content = importer.open_lyrics.xml_to_song.call_args[0][0] + assert import_content == expected_content diff --git a/tests/resources/songs/openlyrics/Amazing_Grace_3_chords.xml b/tests/resources/songs/openlyrics/Amazing_Grace_3_chords.xml new file mode 100644 index 000000000..006cf18c6 --- /dev/null +++ b/tests/resources/songs/openlyrics/Amazing_Grace_3_chords.xml @@ -0,0 +1,19 @@ + + + + + Amazing Grace + + + + + + Amazing grace, how sweet the sound
That saved a wretch like me!
+
+
+
+
diff --git a/tests/resources/songs/openlyrics/Amazing_Grace_3_chords_result.xml b/tests/resources/songs/openlyrics/Amazing_Grace_3_chords_result.xml new file mode 100644 index 000000000..33c68a4ce --- /dev/null +++ b/tests/resources/songs/openlyrics/Amazing_Grace_3_chords_result.xml @@ -0,0 +1 @@ +Amazing GraceAmazing grace, how sweet the sound
That saved a wretch like me!
\ No newline at end of file