some cleanups, comments and a test.

This commit is contained in:
Tomas Groth 2016-07-26 21:02:35 +02:00
parent de54ec2715
commit b781fa6169
5 changed files with 93 additions and 12 deletions

View File

@ -319,7 +319,7 @@ def expand_and_align_chords_in_line(match):
"""
Expand the chords in the line and align them using whitespaces.
NOTE: There is equivalent javascript code in chords.js, in the updateSlide function. Make sure to update both!
:param match:
:return: The line with expanded html-chords
"""
@ -327,30 +327,38 @@ def expand_and_align_chords_in_line(match):
whitespaces = ''
chordlen = 0
taillen = 0
# The match could be "[G]sweet the " from a line like "A[D]mazing [D7]grace! How [G]sweet the [D]sound!"
# The actual chord, would be "G" in match "[G]sweet the "
chord = match.group(1)
# The tailing word of the chord, would be "sweet" in match "[G]sweet the "
tail = match.group(2)
# The remainder of the line, until line end or next chord. Would be " the " in match "[G]sweet the "
remainder = match.group(3)
# Line end if found, else None
end = match.group(4)
print('chord: %s, tail: %s, remainder: %s, end: %s' % (chord, tail, remainder, end))
# Based on char width calculate width of chord
for chord_char in chord:
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:
taillen += 2
else:
taillen += 1
# Based on char width calculate width of remainder
for remainder_char in remainder:
if remainder_char not in slimchars:
taillen += 2
else:
taillen += 1
# If the chord is wider than the tail+remainder and the line goes on, some padding is needed
if chordlen >= taillen and end is None:
# Decide if the padding should be "_" for drawing out words or spaces
if tail:
if not remainder:
print()
for c in range(math.ceil((chordlen - taillen) / 2) + 1):
whitespaces += '_'
else:
@ -389,9 +397,10 @@ def expand_chords(text):
else:
new_line = '<span class="chordline firstchordline">'
chords_on_prev_line = True
new_line += re.sub(r'\[(.+?)\]([\u0080-\uFFFF,\w]*)([\u0080-\uFFFF,\w,\s,\.,\,,\!,\?,\;,\:,\|,\",\',\-,\_]*)(\Z)?', expand_and_align_chords_in_line, line)
#new_line += re.sub(r'(.*?)\[(.+?)\](.*?)',
# r'\1<span class="chord"><span><strong>\2</strong></span></span>\3', line)
# Matches a chord, a tail, a remainder and a line end. See expand_and_align_chords_in_line() for more info.
new_line += re.sub(r'\[(\w.*?)\]([\u0080-\uFFFF,\w]*)'
'([\u0080-\uFFFF,\w,\s,\.,\,,\!,\?,\;,\:,\|,\",\',\-,\_]*)(\Z)?',
expand_and_align_chords_in_line, line)
new_line += '</span>'
expanded_text_lines.append(new_line)
else:

View File

@ -124,6 +124,25 @@ is the function which has to be called from outside. The generated and returned
position: relative;
top: -0.3em;
}
/* Chords css */
.chordline {
line-height: 1.0em;
}
.chordline span.chord span {
position: relative;
}
.chordline span.chord span strong {
position: absolute;
top: -0.8em;
left: 0;
font-size: 75%;
font-weight: normal;
line-height: normal;
display: none;
}
.firstchordline {
line-height: 1.0em;
}
</style>
<script>
var timer = null;
@ -445,7 +464,6 @@ HTML_SRC = Template("""
top: -0.3em;
}
/* Chords css */${chords_css}
</style>
<script>
var timer = null;

View File

@ -181,7 +181,6 @@ window.OpenLP = {
var regchord=/<span class="chord"><span><strong>([\(\w#b♭\+\*\d/\)-]+)<\/strong><\/span><\/span>([\u0080-\uFFFF,\w]*)(<span class="ws">.+?<\/span>)?([\u0080-\uFFFF,\w,\s,\.,\,,\!,\?,\;,\:,\|,\",\',\-,\_]*)(<br>)?/g;
// NOTE: There is equivalent python code in openlp/core/lib/__init__.py, in the expand_and_align_chords_in_line function. Make sure to update both!
var replaceChords=function(mstr,$chord,$tail,$skips,$remainder,$end) {
var v='';
var w='';
var $chordlen = 0;
var $taillen = 0;
@ -190,7 +189,6 @@ window.OpenLP = {
$chord = transposeChord($chord, transposeValue, OpenLP.chordNotation);
// Replace any padding '_' added to tail
$tail = $tail.replace(/_+$/, '')
console.log('chord: ' +$chord +', tail: ' + $tail + ', remainder: ' + $remainder +', end: ' + $end +', match: ' + mstr)
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;}

View File

@ -258,8 +258,8 @@ class OpenSongImport(SongImport):
if chords and not Settings().value('songs/disable chords import'):
offset = 0
for (column, chord) in chords:
this_line = '{pre}[{chord}]{post}'.format(pre=this_line[:offset+column], chord=chord,
post=this_line[offset+column:])
this_line = '{pre}[{chord}]{post}'.format(pre=this_line[:offset + column], chord=chord,
post=this_line[offset + column:])
offset += len(chord) + 2
# Tidy text and remove the ____s from extended words
this_line = self.tidy_text(this_line)

View File

@ -8,7 +8,7 @@ from PyQt5 import QtCore, QtWebKit
from openlp.core.common import Settings
from openlp.core.lib.htmlbuilder import build_html, build_background_css, build_lyrics_css, build_lyrics_outline_css, \
build_lyrics_format_css, build_footer_css, webkit_version
build_lyrics_format_css, build_footer_css, webkit_version, build_chords_css
from openlp.core.lib.theme import HorizontalType, VerticalType
from tests.functional import MagicMock, patch
from tests.helpers.testmixin import TestMixin
@ -60,6 +60,25 @@ HTML = """
position: relative;
top: -0.3em;
}
/* Chords css */
.chordline {
line-height: 1.0em;
}
.chordline span.chord span {
position: relative;
}
.chordline span.chord span strong {
position: absolute;
top: -0.8em;
left: 0;
font-size: 75%;
font-weight: normal;
line-height: normal;
display: none;
}
.firstchordline {
line-height: 1.0em;
}
</style>
<script>
var timer = null;
@ -211,6 +230,29 @@ FOOTER_CSS_BASE = """
FOOTER_CSS = FOOTER_CSS_BASE % ('nowrap')
FOOTER_CSS_WRAP = FOOTER_CSS_BASE % ('normal')
FOOTER_CSS_INVALID = ''
CHORD_CSS_ENABLED = """
.chordline {
line-height: 2.0em;
}
.chordline span.chord span {
position: relative;
}
.chordline span.chord span strong {
position: absolute;
top: -0.8em;
left: 0;
font-size: 75%;
font-weight: normal;
line-height: normal;
display: inline;
}
.firstchordline {
line-height: 2.1em;
}"""
__default_settings__ = {
'songs/mainview chords': False,
}
class Htmbuilder(TestCase, TestMixin):
@ -222,6 +264,7 @@ class Htmbuilder(TestCase, TestMixin):
Create the UI
"""
self.build_settings()
Settings().extend_default_settings(__default_settings__)
def tearDown(self):
"""
@ -403,3 +446,16 @@ class Htmbuilder(TestCase, TestMixin):
# WHEN: Retrieving the webkit version
# THEN: Webkit versions should match
self.assertEquals(webkit_version(), webkit_ver, "The returned webkit version doesn't match the installed one")
def test_build_chords_css(self):
"""
Test the build_chords_css() function
"""
# GIVEN: A setting that activates chords on the mainview
Settings().setValue('songs/mainview chords', True)
# WHEN: Building the chord CSS
chord_css = build_chords_css()
# THEN: The build css should look as expected
self.assertEqual(CHORD_CSS_ENABLED, chord_css, 'The chord CSS should look as expected')