Fixed an issue and added a test.

This commit is contained in:
Tomas Groth 2017-05-17 22:06:45 +02:00
parent 798093adfe
commit 5988f1c56a
4 changed files with 25 additions and 8 deletions

View File

@ -23,12 +23,11 @@
The :mod:`lib` module contains most of the components and libraries that make The :mod:`lib` module contains most of the components and libraries that make
OpenLP work. OpenLP work.
""" """
import html
import logging import logging
import os import os
import re import re
import math import math
from distutils.version import LooseVersion
from PyQt5 import QtCore, QtGui, Qt, QtWidgets from PyQt5 import QtCore, QtGui, Qt, QtWidgets
@ -36,6 +35,8 @@ from openlp.core.common import translate
log = logging.getLogger(__name__ + '.__init__') log = logging.getLogger(__name__ + '.__init__')
SLIMCHARS = 'fiíIÍjlĺľrtť.,;/ ()|"\'!:\\'
class ServiceItemContext(object): class ServiceItemContext(object):
""" """
@ -327,7 +328,6 @@ def expand_and_align_chords_in_line(match):
:param match: :param match:
:return: The line with expanded html-chords :return: The line with expanded html-chords
""" """
SLIMCHARS = 'fiíIÍjlĺľrtť.,;/ ()|"\'!:\\'
whitespaces = '' whitespaces = ''
chordlen = 0 chordlen = 0
taillen = 0 taillen = 0
@ -389,7 +389,8 @@ def expand_and_align_chords_in_line(match):
ws_right = ws_left = ' ' * wsl_mod ws_right = ws_left = ' ' * wsl_mod
whitespaces = ws_left + '–' + ws_right whitespaces = ws_left + '–' + ws_right
whitespaces = '<span class="ws">' + whitespaces + '</span>' whitespaces = '<span class="ws">' + whitespaces + '</span>'
return '<span class="chord"><span><strong>' + chord + '</strong></span></span>' + tail + whitespaces + remainder return '<span class="chord"><span><strong>' + html.escape(chord) + '</strong></span></span>' + html.escape(tail) + \
whitespaces + html.escape(remainder)
def expand_chords(text): def expand_chords(text):
@ -417,7 +418,7 @@ def expand_chords(text):
expanded_text_lines.append(new_line) expanded_text_lines.append(new_line)
else: else:
chords_on_prev_line = False chords_on_prev_line = False
expanded_text_lines.append(line) expanded_text_lines.append(html.escape(line))
return '{br}'.join(expanded_text_lines) return '{br}'.join(expanded_text_lines)
@ -429,7 +430,6 @@ def compare_chord_lyric(chord, lyric):
:param lyric: :param lyric:
:return: :return:
""" """
SLIMCHARS = 'fiíIÍjlĺľrtť.,;/ ()|"\'!:\\'
chordlen = 0 chordlen = 0
if chord == '&nbsp;': if chord == '&nbsp;':
return 0 return 0

View File

@ -261,7 +261,7 @@ class ServiceItem(RegistryProperties):
previous_pages[verse_tag] = (slide['raw_slide'], pages) previous_pages[verse_tag] = (slide['raw_slide'], pages)
for page in pages: for page in pages:
page = page.replace('<br>', '{br}') page = page.replace('<br>', '{br}')
html_data = expand_tags(html.escape(page.rstrip()), expand_chord_tags) html_data = expand_tags(page.rstrip(), expand_chord_tags)
new_frame = { new_frame = {
'title': clean_tags(page), 'title': clean_tags(page),
'text': clean_tags(page.rstrip(), expand_chord_tags), 'text': clean_tags(page.rstrip(), expand_chord_tags),

View File

@ -764,6 +764,23 @@ class TestLib(TestCase):
'&nbsp;&nbsp;</span> <span class="chord"><span><strong>G</strong></span></span></span>' '&nbsp;&nbsp;</span> <span class="chord"><span><strong>G</strong></span></span></span>'
self.assertEqual(expected_html, text_with_expanded_chords, 'The expanded chords should look as expected!') self.assertEqual(expected_html, text_with_expanded_chords, 'The expanded chords should look as expected!')
def test_expand_chords2(self):
"""
Test that the expanding of chords works as expected when special chars are involved.
"""
import html
# GIVEN: A lyrics-line with chords
text_with_chords = "I[D]'M NOT MOVED BY WHAT I SEE HALLE[F]LUJA[C]H"
# WHEN: Expanding the chords
text_with_expanded_chords = expand_tags(text_with_chords, True)
# THEN: We should get html that looks like below
expected_html = '<span class="chordline firstchordline">I<span class="chord"><span><strong>D</strong></span>' \
'</span>&#x27;M NOT MOVED BY WHAT I SEE HALLE<span class="chord"><span><strong>F</strong>' \
'</span></span>LUJA<span class="chord"><span><strong>C</strong></span></span>H</span>'
self.assertEqual(expected_html, text_with_expanded_chords, 'The expanded chords should look as expected!')
def test_compare_chord_lyric_short_chord(self): def test_compare_chord_lyric_short_chord(self):
""" """
Test that the chord/lyric comparing works. Test that the chord/lyric comparing works.

View File

@ -206,7 +206,7 @@ class TestLib(TestCase):
assert result[0][3] == 0, 'The start indices should be kept.' assert result[0][3] == 0, 'The start indices should be kept.'
assert result[0][4] == 21, 'The stop indices should be kept.' assert result[0][4] == 21, 'The stop indices should be kept.'
def test_remove_typos_beginning_negated(self): def test_remove_typos_middle_negated(self):
""" """
Test the _remove_typos function with a large difference in the middle. Test the _remove_typos function with a large difference in the middle.
""" """