Reintroduced span-based chord html for main and stage view.

This commit is contained in:
Tomas Groth 2017-02-19 22:35:40 +01:00
parent 7da6d5c686
commit 32794d7e97
7 changed files with 180 additions and 32 deletions

View File

@ -302,19 +302,116 @@ def clean_tags(text, remove_chords=False):
return text
def expand_tags(text):
def expand_tags(text, for_printing=False):
"""
Expand tags HTML for display
:param text: The text to be expanded.
"""
text = expand_chords(text, '{br}')
if for_printing:
text = expand_chords_for_printing(text, '{br}')
else:
text = expand_chords(text)
for tag in FormattingTags.get_html_tags():
text = text.replace(tag['start tag'], tag['start html'])
text = text.replace(tag['end tag'], tag['end html'])
return text
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
"""
slimchars = 'fiíIÍjlĺľrtť.,;/ ()|"\'!:\\'
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)
# 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:
for c in range(math.ceil((chordlen - taillen) / 2) + 1):
whitespaces += '_'
else:
for c in range(chordlen - taillen + 2):
whitespaces += ' '
else:
if not remainder:
for c in range(math.floor((chordlen - taillen) / 2)):
whitespaces += '_'
else:
for c in range(chordlen - taillen + 1):
whitespaces += ' '
else:
if not tail and remainder and remainder[0] == ' ':
for c in range(chordlen):
whitespaces += ' '
if whitespaces:
whitespaces = '<span class="ws">' + whitespaces + '</span>'
return '<span class="chord"><span><strong>' + chord + '</strong></span></span>' + tail + whitespaces + remainder
def expand_chords(text):
"""
Expand ChordPro tags
:param text:
"""
text_lines = text.split('{br}')
expanded_text_lines = []
chords_on_prev_line = False
for line in text_lines:
# If a ChordPro is detected in the line, replace it with a html-span tag and wrap the line in a span tag.
if '[' in line and ']' in line:
if chords_on_prev_line:
new_line = '<span class="chordline">'
else:
new_line = '<span class="chordline firstchordline">'
chords_on_prev_line = True
# 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:
chords_on_prev_line = False
expanded_text_lines.append(line)
return '{br}'.join(expanded_text_lines)
def compare_chord_lyric(chord, lyric):
"""
@ -380,7 +477,7 @@ def find_formatting_tags(text, active_formatting_tags):
return active_formatting_tags
def expand_chords(text, line_split):
def expand_chords_for_printing(text, line_split):
"""
Expand ChordPro tags
@ -445,13 +542,16 @@ def expand_chords(text, line_split):
end_formatting_tags = '{/' + '}{/'.join(active_formatting_tags) + '}'
new_chord_line += '<td class="chord">%s</td>' % chords[i]
if i + 1 == len(lyrics):
new_lyric_line += '<td class="lyrics">{starttags}{lyrics}&nbsp;{endtags}</td>'.format(starttags=start_formatting_tags, lyrics=lyrics[i], endtags=end_formatting_tags)
new_lyric_line += '<td class="lyrics">{starttags}{lyrics}&nbsp;{endtags}</td>'.format(
starttags=start_formatting_tags, lyrics=lyrics[i], endtags=end_formatting_tags)
else:
spacing = ''
if spacer > 0:
space = '&nbsp;' * int(math.ceil(spacer / 2))
spacing = '<span class="chordspacing">%s-%s</span>' % (space, space)
new_lyric_line += '<td class="lyrics">{starttags}{lyrics}{spacing}{endtags}</td>'.format(starttags=start_formatting_tags, lyrics=lyrics[i], spacing=spacing, endtags=end_formatting_tags)
new_lyric_line += '<td class="lyrics">{starttags}{lyrics}{spacing}{endtags}</td>'.format(
starttags=start_formatting_tags, lyrics=lyrics[i], spacing=spacing,
endtags=end_formatting_tags)
new_line += new_chord_line + new_lyric_line + '</tr>'
else:
start_formatting_tags = ''
@ -461,7 +561,9 @@ def expand_chords(text, line_split):
end_formatting_tags = ''
if active_formatting_tags:
end_formatting_tags = '{/' + '}{/'.join(active_formatting_tags) + '}'
new_line += '<tr class="chordrow"><td class="chord">&nbsp;</td></tr><tr><td class="lyrics">{starttags}{lyrics}&nbsp;{endtags}</td></tr>'.format(starttags=start_formatting_tags, lyrics=word, endtags=end_formatting_tags)
new_line += '<tr class="chordrow"><td class="chord">&nbsp;</td></tr><tr><td class="lyrics">' \
'{starttags}{lyrics}&nbsp;{endtags}</td></tr>'.format(
starttags=start_formatting_tags, lyrics=word, endtags=end_formatting_tags)
new_line += '</table>'
else:
new_line += line

View File

@ -613,20 +613,24 @@ LYRICS_FORMAT_SRC = Template("""
""")
CHORDS_FORMAT = Template("""
table.line {}
table.segment {
float: left;
.chordline {
line-height: ${chord_line_height};
}
td.chord {
font-size: 80%;
display: ${chords_display};
.chordline span.chord span {
position: relative;
}
td.lyrics {
.chordline span.chord span strong {
position: absolute;
top: -0.8em;
left: 0;
font-size: 75%;
font-weight: normal;
line-height: normal;
display: ${chords_display};
}
.chordspacing {
display: ${chords_display};
}
""")
.firstchordline {
line-height: ${first_chord_line_height};
}""")
def build_html(item, screen, is_live, background, image=None, plugins=None):
@ -809,7 +813,12 @@ def build_footer_css(item, height):
def build_chords_css():
if Settings().value('songs/mainview chords'):
chords_display = 'table-cell'
chord_line_height = '2.0em'
chords_display = 'inline'
first_chord_line_height = '2.1em'
else:
chord_line_height = '1.0em'
chords_display = 'none'
return CHORDS_FORMAT.substitute(chords_display=chords_display)
first_chord_line_height = '1.0em'
return CHORDS_FORMAT.substitute(chord_line_height=chord_line_height, chords_display=chords_display,
first_chord_line_height=first_chord_line_height)

View File

@ -263,8 +263,10 @@ class ServiceItem(RegistryProperties):
new_frame = {
'title': clean_tags(page),
'text': clean_tags(page.rstrip(), True),
'chords_text': expand_chords(clean_tags(page.rstrip(), False), '\n'),
# 'chords_text': expand_chords(clean_tags(page.rstrip(), False), '\n'),
'chords_text': expand_chords(clean_tags(page.rstrip(), False)),
'html': html_data.replace('&amp;nbsp;', '&nbsp;'),
'printing_html': expand_tags(html.escape(page.rstrip()), True),
'verseTag': verse_tag,
}
self._display_frames.append(new_frame)

View File

@ -215,13 +215,13 @@ class PrintServiceForm(QtWidgets.QDialog, Ui_PrintServiceDialog, RegistryPropert
verse_def = None
verse_html = None
for slide in item.get_frames():
if not verse_def or verse_def != slide['verseTag'] or verse_html == slide['html']:
if not verse_def or verse_def != slide['verseTag'] or verse_html == slide['printing_html']:
text_div = self._add_element('div', parent=div, classId='itemText')
elif 'chordspacing' not in slide['html']:
elif 'chordspacing' not in slide['printing_html']:
self._add_element('br', parent=text_div)
self._add_element('span', slide['html'], text_div)
self._add_element('span', slide['printing_html'], text_div)
verse_def = slide['verseTag']
verse_html = slide['html']
verse_html = slide['printing_html']
# Break the page before the div element.
if index != 0 and self.page_break_after_text.isChecked():
div.set('class', 'item newPage')

View File

@ -33,6 +33,7 @@
<input type="hidden" id="next-text" value="${next}" />
<div id="right">
<div id="clock"></div>
<div id="chords">Toggle Chords</div>
<div id="notes"></div>
</div>
<div id="header">

View File

@ -17,6 +17,14 @@
* Temple Place, Suite 330, Boston, MA 02111-1307 USA *
******************************************************************************/
#chords {
font-size: 20pt;
color: gray;
background-color: gray;
color: white;
cursor: pointer;
}
#header {
padding-bottom: 1em;
@ -45,6 +53,14 @@
cursor: pointer;
}
#chords {
line-height: 1.2em;
color: gray;
background-color: gray;
color: white;
cursor: pointer;
}
/* Extending exiting definition in stage.css */
#verseorder {
line-height: 1.5;
@ -52,19 +68,32 @@
vertical-align: middle;
}
#nextslide td.chord {
color: gray;
.chordline {
line-height: 2.0;
}
table.line {}
table.segment {
float: left;
.chordline1 {
line-height: 1.0
}
td.chord {
.firstchordline {
line-height: 2.1em;
}
.chordline span.chord span {
position: relative;
}
.chordline span.chord span strong {
position: absolute;
top: -0.8em;
left: 0;
font-size: 30pt;
font-weight: normal;
line-height: normal;
color: yellow;
}
td.lyrics {}
#nextslide .chordline span.chord span strong {
color: gray;
}

View File

@ -265,6 +265,7 @@ window.OpenLP = {
text = "<p class=\"nextslide\">" + $("#next-text").val() + ": " + OpenLP.nextSong + "</p>";
$("#nextslide").html(text);
}
if(!OpenLP.showchords) $(".chordline").toggleClass('chordline1');
},
updateClock: function(data) {
var div = $("#clock");
@ -311,4 +312,8 @@ $(document).ready(function() {
storeTransposeValue(OpenLP.currentSlides[0].text.split("\n")[0], $('#transposevalue').text());
OpenLP.loadSlides();
});
$('#chords').click(function () {
OpenLP.showchords = OpenLP.showchords ? false : true;
OpenLP.updateSlide();
});
});