Added better support for rendering formatting tags when using chords.

This commit is contained in:
Tomas Groth 2017-02-02 21:32:35 +01:00
parent dda37552d1
commit 111826dd69
4 changed files with 94 additions and 63 deletions

View File

@ -308,7 +308,7 @@ def expand_tags(text):
:param text: The text to be expanded.
"""
text = expand_chords(text)
text = expand_chords(text, '{br}')
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'])
@ -326,6 +326,8 @@ def compare_chord_lyric(chord, lyric):
chordlen = 0
if chord == ' ':
return 0
chord = re.sub(r'\{.*?\}', r'', chord)
lyric = re.sub(r'\{.*?\}', r'', lyric)
for chord_char in chord:
if chord_char not in SLIMCHARS:
chordlen += 2
@ -343,32 +345,71 @@ def compare_chord_lyric(chord, lyric):
return 0
def expand_chords(text):
def find_formatting_tags(text, active_formatting_tags):
"""
Look for formatting tags in lyrics and adds/removes them to/from the given list. Returns the update list.
:param text:
:param active_formatting_tags:
:return:
"""
if not re.search(r'\{.*?\}', text):
return active_formatting_tags
word_it = iter(text)
# Loop through lyrics to find any formatting tags
for char in word_it:
if char == '{':
tag = ''
char = next(word_it)
start_tag = True
if char == '/':
start_tag = False
while char != '}':
tag += char
char = next(word_it)
# See if the found tag has an end tag
for formatting_tag in FormattingTags.get_html_tags():
if formatting_tag['start tag'] == '{' + tag + '}':
if formatting_tag['end tag']:
if start_tag:
# prepend the new tag to the list of active formatting tags
active_formatting_tags[:0] = tag
else:
# remove the tag from the list
active_formatting_tags.remove(tag)
return active_formatting_tags
def expand_chords(text, line_split):
"""
Expand ChordPro tags
:param text:
:param line_split:
"""
if '[' not in text and ']' not in text:
if not re.search(r'\[\w+.*?\]', text):
return text
text_lines = text.split('{br}')
text_lines = text.split(line_split)
expanded_text_lines = []
for line in text_lines:
# If a ChordPro is detected in the line, build html tables.
new_line = '<table class="line" width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td>'
if '[' in text and ']' in line:
active_formatting_tags = []
if re.search(r'\[\w+.*?\]', line):
words = line.split(' ')
in_note = False
in_chord = False
for word in words:
chords = []
lyrics = []
new_line += '<table class="segment" cellpadding="0" cellspacing="0" border="0" align="left">'
chord = ''
lyric = ''
if '[' in word and ']' in word:
# If the word contains a chord, we need to handle it.
if re.search(r'\[\w+.*?\]', word):
chord = ''
lyric = ''
# Loop over each character of the word
for char in word:
if char == '[':
in_note = True
in_chord = True
if lyric != '':
if chord == '':
chord = '&nbsp;'
@ -376,9 +417,9 @@ def expand_chords(text):
lyrics.append(lyric)
chord = ''
lyric = ''
elif char == ']' and in_note:
in_note = False
elif in_note:
elif char == ']' and in_chord:
in_chord = False
elif in_chord:
chord += char
else:
lyric += char
@ -393,24 +434,41 @@ def expand_chords(text):
new_lyric_line = '</tr><tr>'
for i in range(len(lyrics)):
spacer = compare_chord_lyric(chords[i], lyrics[i])
new_chord_line += '<td class="note">%s</td>' % chords[i]
# Handle formatting tags
start_formatting_tags = ''
if active_formatting_tags:
start_formatting_tags = '{' + '}{'.join(active_formatting_tags) + '}'
# Update list of active formatting tags
active_formatting_tags = find_formatting_tags(lyrics[i], active_formatting_tags)
end_formatting_tags = ''
if active_formatting_tags:
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">%s&nbsp;</td>' % lyrics[i]
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))
new_lyric_line += '<td class="lyrics">%s%s-%s</td>' % (lyrics[i], space, space)
else:
new_lyric_line += '<td class="lyrics">%s</td>' % lyrics[i]
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_line += new_chord_line + new_lyric_line + '</tr>'
else:
new_line += '<tr><td class="note">&nbsp;</td></tr><tr><td class="lyrics">%s&nbsp;</td></tr>' % word
start_formatting_tags = ''
if active_formatting_tags:
start_formatting_tags = '{' + '}{'.join(active_formatting_tags) + '}'
active_formatting_tags = find_formatting_tags(word, active_formatting_tags)
end_formatting_tags = ''
if active_formatting_tags:
end_formatting_tags = '{/' + '}{/'.join(active_formatting_tags) + '}'
new_line += '<tr><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>'
#print(new_line)
else:
new_line += line
new_line += '</td></tr></table>'
expanded_text_lines.append(new_line)
# the {br} tag used to split lines is not inserted again since the line-tables style make them redundant.
# the {br} tag used to split lines is not inserted again since the style of the line-tables makes them redundant.
return ''.join(expanded_text_lines)

View File

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

View File

@ -263,7 +263,7 @@ class ServiceItem(RegistryProperties):
new_frame = {
'title': clean_tags(page),
'text': clean_tags(page.rstrip(), True),
'chords_text': expand_chords(clean_tags(page.rstrip(), False)),
'chords_text': expand_chords(clean_tags(page.rstrip(), False), '\n'),
'html': html_data.replace('&amp;nbsp;', '&nbsp;'),
'verseTag': verse_tag,
}

View File

@ -17,14 +17,6 @@
* 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;
@ -60,7 +52,7 @@
vertical-align: middle;
}
#nextslide td.note {
#nextslide td.chord {
color: gray;
}
@ -70,7 +62,7 @@ table.segment {
float: left;
}
td.note {
td.chord {
font-size: 30pt;
color: yellow;
}