From 9ae1af72c708c2eca1c4dd5ce4991c6669f14815 Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Fri, 17 Mar 2017 22:12:29 +0100 Subject: [PATCH] Minor adjustsments + a test. --- openlp/core/lib/__init__.py | 12 ++++++------ openlp/plugins/remotes/html/css/chords.css | 1 - openlp/plugins/remotes/html/js/chords.js | 13 ++++++------- openlp/plugins/songs/lib/__init__.py | 6 +++--- tests/functional/openlp_core_lib/test_lib.py | 18 +++++++++++++++++- 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/openlp/core/lib/__init__.py b/openlp/core/lib/__init__.py index da8f9187d..6c3ac8664 100644 --- a/openlp/core/lib/__init__.py +++ b/openlp/core/lib/__init__.py @@ -326,7 +326,7 @@ def expand_and_align_chords_in_line(match): :param match: :return: The line with expanded html-chords """ - slimchars = 'fiíIÍjlĺľrtť.,;/ ()|"\'!:\\' + SLIMCHARS = 'fiíIÍjlĺľrtť.,;/ ()|"\'!:\\' whitespaces = '' chordlen = 0 taillen = 0 @@ -341,19 +341,19 @@ def expand_and_align_chords_in_line(match): end = match.group(4) # Based on char width calculate width of chord for chord_char in chord: - if chord_char not in slimchars: + if chord_char not in SLIMCHARS: chordlen += 2 else: chordlen += 1 # Based on char width calculate width of tail for tail_char in tail: - if tail_char not in slimchars: + if tail_char not in SLIMCHARS: taillen += 2 else: taillen += 1 # Based on char width calculate width of remainder for remainder_char in remainder: - if remainder_char not in slimchars: + if remainder_char not in SLIMCHARS: taillen += 2 else: taillen += 1 @@ -362,10 +362,10 @@ def expand_and_align_chords_in_line(match): # Decide if the padding should be "_" for drawing out words or spaces if tail: if not remainder: - for c in range(math.ceil((chordlen - taillen) / 2) + 1): + for c in range(math.ceil((chordlen - taillen) / 2) + 2): whitespaces += '_' else: - for c in range(chordlen - taillen + 2): + for c in range(chordlen - taillen + 1): whitespaces += ' ' else: if not remainder: diff --git a/openlp/plugins/remotes/html/css/chords.css b/openlp/plugins/remotes/html/css/chords.css index b77006770..2bb357886 100644 --- a/openlp/plugins/remotes/html/css/chords.css +++ b/openlp/plugins/remotes/html/css/chords.css @@ -89,7 +89,6 @@ .ws { white-space: pre-wrap; - font-family: monospace; } #nextslide .chordline span.chord span strong { diff --git a/openlp/plugins/remotes/html/js/chords.js b/openlp/plugins/remotes/html/js/chords.js index 39b92d2e4..07d0548d4 100644 --- a/openlp/plugins/remotes/html/js/chords.js +++ b/openlp/plugins/remotes/html/js/chords.js @@ -40,18 +40,17 @@ function storeTransposeValue(songId,transposeValueToSet) { // NOTE: This function has a python equivalent in openlp/plugins/songs/lib/__init__.py - make sure to update both! function transposeChord(chord, transposeValue, notation) { - var chordSplit = chord.replace('♭', 'b').split(/[\/\(\)]/); + var chordSplit = chord.replace('♭', 'b').split(/[\/]/); var transposedChord = '', note, notenumber, rest, currentChord; var notesSharp = notesSharpNotation[notation]; var notesFlat = notesFlatNotation[notation]; var notesPreferred = ['b','#','#','#','#','#','#','#','#','#','#','#']; - var chordNotes = Array(); for (i = 0; i <= chordSplit.length - 1; i++) { if (i > 0) { transposedChord += '/'; } currentchord = chordSplit[i]; - if (currentchord.charAt(0) === '(') { + if (currentchord.length > 0 && currentchord.charAt(0) === '(') { transposedChord += '('; if (currentchord.length > 1) { currentchord = currentchord.substr(1); @@ -186,9 +185,9 @@ window.OpenLP = { var $taillen = 0; var slimchars='fiíIÍjlĺľrtť.,;/ ()|"\'!:\\'; // Transpose chord as dictated by the transpose value in local storage - $chord = transposeChord($chord, transposeValue, OpenLP.chordNotation); - // Replace any padding '_' added to tail - $tail = $tail.replace(/_+$/, '') + if (transposeValue != 0) { + $chord = transposeChord($chord, transposeValue, OpenLP.chordNotation); + } for (var i = 0; i < $chord.length; i++) if (slimchars.indexOf($chord.charAt(i)) === -1) {$chordlen += 2;} else {$chordlen += 1;} for (var i = 0; i < $tail.length; i++) if (slimchars.indexOf($tail.charAt(i)) === -1) {$taillen += 2;} else {$taillen += 1;} for (var i = 0; i < $remainder.length; i++) if (slimchars.indexOf($tail.charAt(i)) === -1) {$taillen += 2;} else {$taillen += 1;} @@ -216,7 +215,7 @@ window.OpenLP = { w = '–'; } else { wsl_mod = Math.floor(ws_length / 2); - ws_right = ws_left = new Array(wsl_mod +1).join(' '); + ws_right = ws_left = new Array(wsl_mod + 1).join(' '); w = ws_left + '–' + ws_right; } } diff --git a/openlp/plugins/songs/lib/__init__.py b/openlp/plugins/songs/lib/__init__.py index c0a9d917b..7563b9f1a 100644 --- a/openlp/plugins/songs/lib/__init__.py +++ b/openlp/plugins/songs/lib/__init__.py @@ -617,7 +617,7 @@ def transpose_chord(chord, transpose_value, notation): notes_flat_notation['english'] = ['C', 'Db', 'D', 'Eb', 'Fb', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B'] notes_sharp_notation['neo-latin'] = ['Do', 'Do#', 'Re', 'Re#', 'Mi', 'Fa', 'Fa#', 'Sol', 'Sol#', 'La', 'La#', 'Si'] notes_flat_notation['neo-latin'] = ['Do', 'Reb', 'Re', 'Mib', 'Fab', 'Fa', 'Solb', 'Sol', 'Lab', 'La', 'Sib', 'Si'] - chord_split = chord.replace('♭', 'b').split('/[\/\(\)]/') + chord_split = chord.replace('♭', 'b').split('/') transposed_chord = '' last_chord = '' notes_sharp = notes_sharp_notation[notation] @@ -627,12 +627,12 @@ def transpose_chord(chord, transpose_value, notation): if i > 0: transposed_chord += '/' currentchord = chord_split[i] - if currentchord[0] == '(': + if currentchord and currentchord[0] == '(': transposed_chord += '(' if len(currentchord) > 1: currentchord = currentchord[1:] else: - currentchord = "" + currentchord = '' if len(currentchord) > 0: if len(currentchord) > 1: if '#b'.find(currentchord[1]) == -1: diff --git a/tests/functional/openlp_core_lib/test_lib.py b/tests/functional/openlp_core_lib/test_lib.py index c08262506..8eabd252a 100644 --- a/tests/functional/openlp_core_lib/test_lib.py +++ b/tests/functional/openlp_core_lib/test_lib.py @@ -30,7 +30,7 @@ from datetime import datetime, timedelta from PyQt5 import QtCore, QtGui from openlp.core.lib import build_icon, check_item_selected, clean_tags, create_thumb, create_separated_list, \ - expand_tags, get_text_file_string, image_to_byte, resize_image, str_to_bool, validate_thumb + expand_tags, get_text_file_string, image_to_byte, resize_image, str_to_bool, validate_thumb, expand_chords from tests.functional import MagicMock, patch TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'resources')) @@ -746,3 +746,19 @@ class TestLib(TestCase): # THEN: We should have "Author 1, Author 2 and Author 3" self.assertEqual(string_result, 'Author 1, Author 2 and Author 3', 'The string should be "Author 1, ' 'Author 2, and Author 3".') + + def test_expand_chords(self): + """ + Test that the expanding of chords works as expected. + """ + # GIVEN: A lyrics-line with chords + text_with_chords = 'H[C]alleluya.[F] [G]' + + # WHEN: Expanding the chords + text_with_expanded_chords = expand_chords(text_with_chords) + + # THEN: We should get html that looks like below + expected_html = 'HC' \ + 'alleluya.F' \ + '   G' + self.assertEqual(expected_html, text_with_expanded_chords, 'The expanded chords should look as expected!')