From b6d3be22acd627d9bd4e4313b269234e02ef53cf Mon Sep 17 00:00:00 2001 From: STEPHANVS Date: Sun, 4 Jul 2021 18:02:22 +0200 Subject: [PATCH] Support for more complex chords; have chordless songs in
--- openlp/core/display/render.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/openlp/core/display/render.py b/openlp/core/display/render.py index 5fb487093..75a7233f5 100644 --- a/openlp/core/display/render.py +++ b/openlp/core/display/render.py @@ -44,13 +44,15 @@ from openlp.core.lib.formattingtags import FormattingTags log = logging.getLogger(__name__) -ENGLISH_NOTES = '[CDEFGAB]' -GERMAN_NOTES = '[CDEFGAH]' -NEOLATIN_NOTES = '(Do|Re|Mi|Fa|Sol|La|Si)' -CHORD_SUFFIXES = '(b|bb)?(#)?(m|maj7|maj|min7|min|sus)?(1|2|3|4|5|6|7|8|9)?' +ENGLISH_NOTES = '[CDEFGAB]?' +GERMAN_NOTES = '[CDEFGABH]?' +NEOLATIN_NOTES = '(Do|Re|Mi|Fa|Sol|La|Si)?' +CHORD_PREFIXES = '(=|\(|\|)*?' +CHORD_SUFFIXES = '(b|bb|#|##|x|-|m|maj|min|sus|dim|0|1|2|3|4|5|6|7|8|9|\))*?' SLIM_CHARS = 'fiíIÍjlĺľrtť.,;/ ()|"\'!:\\' CHORD_TEMPLATE = '{chord}' -FIRST_CHORD_TEMPLATE = '{chord}' +FIRST_CHORD_TEMPLATE = '{chord}' +NO_CHORD_TEMPLATE = '{chord}' CHORD_LINE_TEMPLATE = '{chord}{tail}{whitespace}{remainder}' WHITESPACE_TEMPLATE = '{whitespaces}' VERSE = 'The Lord said to {r}Noah{/r}: \n' \ @@ -78,8 +80,8 @@ def _construct_chord_regex(notes): :param notes: The regular expression for a set of valid notes :return: An expanded regular expression for valid chords """ - chord = notes + CHORD_SUFFIXES - return '(' + chord + '(/' + chord + ')?)' + #chord = CHORD_PREFIXES + notes + CHORD_SUFFIXES + return '(' + CHORD_PREFIXES + notes + CHORD_SUFFIXES + '(/' + notes + CHORD_SUFFIXES + ')?)' def _construct_chord_match(notes): @@ -186,11 +188,11 @@ def render_chords_in_line(match): # 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(11) + tail = match.group(6) # The remainder of the line, until line end or next chord. Would be " the " in match "[G]sweet the " - remainder = match.group(12) + remainder = match.group(7) # Line end if found, else None - end = match.group(13) + end = match.group(8) # Based on char width calculate width of chord for chord_char in chord: if chord_char not in SLIM_CHARS: @@ -268,7 +270,10 @@ def render_chords(text): rendered_lines.append(new_line) else: chords_on_prev_line = False - rendered_lines.append(html.escape(line)) + #rendered_lines.append(html.escape(line)) + chord_template = NO_CHORD_TEMPLATE + new_line = chord_template.format(chord=line) + rendered_lines.append(new_line) return '{br}'.join(rendered_lines)