From 111826dd69f081c1f3eb5057a8b494ffc08fddbd Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Thu, 2 Feb 2017 21:32:35 +0100 Subject: [PATCH] Added better support for rendering formatting tags when using chords. --- openlp/core/lib/__init__.py | 98 +++++++++++++++++----- openlp/core/lib/htmlbuilder.py | 45 +++------- openlp/core/lib/serviceitem.py | 2 +- openlp/plugins/remotes/html/css/chords.css | 12 +-- 4 files changed, 94 insertions(+), 63 deletions(-) diff --git a/openlp/core/lib/__init__.py b/openlp/core/lib/__init__.py index 8e29ef539..85b59091a 100644 --- a/openlp/core/lib/__init__.py +++ b/openlp/core/lib/__init__.py @@ -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 = '
' - 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 += '' - 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 = ' ' @@ -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 = '' for i in range(len(lyrics)): spacer = compare_chord_lyric(chords[i], lyrics[i]) - new_chord_line += '' % 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 += '' % chords[i] if i + 1 == len(lyrics): - new_lyric_line += '' % lyrics[i] + new_lyric_line += ''.format(starttags=start_formatting_tags, lyrics=lyrics[i], endtags=end_formatting_tags) else: + spacing = '' if spacer > 0: space = ' ' * int(math.ceil(spacer / 2)) - new_lyric_line += '' % (lyrics[i], space, space) - else: - new_lyric_line += '' % lyrics[i] + spacing = '%s-%s' % (space, space) + new_lyric_line += ''.format(starttags=start_formatting_tags, lyrics=lyrics[i], spacing=spacing, endtags=end_formatting_tags) new_line += new_chord_line + new_lyric_line + '' else: - new_line += '' % 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 += ''.format(starttags=start_formatting_tags, lyrics=word, endtags=end_formatting_tags) new_line += '
%s%s%s {starttags}{lyrics} {endtags}%s%s-%s%s{starttags}{lyrics}{spacing}{endtags}
 
%s 
 
{starttags}{lyrics} {endtags}
' + #print(new_line) else: new_line += line new_line += '
' 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) diff --git a/openlp/core/lib/htmlbuilder.py b/openlp/core/lib/htmlbuilder.py index ea8b9bcd7..d18591091 100644 --- a/openlp/core/lib/htmlbuilder.py +++ b/openlp/core/lib/htmlbuilder.py @@ -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) diff --git a/openlp/core/lib/serviceitem.py b/openlp/core/lib/serviceitem.py index 6a3f2c289..60ec3009e 100644 --- a/openlp/core/lib/serviceitem.py +++ b/openlp/core/lib/serviceitem.py @@ -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(' ', ' '), 'verseTag': verse_tag, } diff --git a/openlp/plugins/remotes/html/css/chords.css b/openlp/plugins/remotes/html/css/chords.css index 0454ae014..9de99b186 100644 --- a/openlp/plugins/remotes/html/css/chords.css +++ b/openlp/plugins/remotes/html/css/chords.css @@ -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; }