Set a bunch of sane defaults

This commit is contained in:
Raoul Snyman 2021-08-03 10:39:00 -07:00
parent ba0e53ee6a
commit 454c29152d

View File

@ -4,18 +4,18 @@ CHORD = '<td class="chord">{}</td>'
SYLLB = '<td class="syllable">{}</td>' SYLLB = '<td class="syllable">{}</td>'
LINE = '<table class="line" border="0" cellpadding="0" cellspacing="0"><tr class="chords-line">{}</tr><tr ' \ LINE = '<table class="line" border="0" cellpadding="0" cellspacing="0"><tr class="chords-line">{}</tr><tr ' \
'class="lyrics-line">{}</tr></table>' 'class="lyrics-line">{}</tr></table>'
VERSE = '''<section class="stanza {verse_type}"> STANZA = '''<section class="stanza-section {verse_type}-section">
<h4 class="stanza-heading {verse_type}-heading">{verse_name}</h4> <h4 class="stanza-heading {verse_type}-heading">{verse_name}</h4>
<div class="stanza {verse_type}"> <div class="stanza {verse_type}">
{verse_body} {verse_body}
</div> </div>
</section>''' </section>'''
TITLE = '<section class="metadata"><h1 class="title">{title}</h1><h3 class="artist">{artist}</h3></section>' TITLE = '<section class="metadata"><h1 class="title">{title}</h1><h3 class="composer">{composer}</h3></section>'
HTML = '''<html> HTML = '''<html>
<head> <head>
<title>{title}</title> <title>{title}</title>
<style> <style>
h1.title, h4.stanza-heading {{ font-weight: normal; }} h1.title, h3.composer, h4.stanza-heading {{ font-weight: normal; }}
{styles} {styles}
</style> </style>
</head> </head>
@ -41,7 +41,7 @@ HTML_OPTIONS = {
'default_size': { 'default_size': {
'description': 'The default size to use.', 'description': 'The default size to use.',
'type': int, 'type': int,
'default': None, 'default': 12,
'group': 'default' 'group': 'default'
}, },
'title_font': { 'title_font': {
@ -51,9 +51,9 @@ HTML_OPTIONS = {
'group': 'title' 'group': 'title'
}, },
'title_size': { 'title_size': {
'description': 'The size of the title in rem', 'description': 'The size of the title in pt',
'type': int, 'type': int,
'default': None, 'default': 14,
'group': 'title' 'group': 'title'
}, },
'title_is_bold': { 'title_is_bold': {
@ -65,13 +65,13 @@ HTML_OPTIONS = {
'title_is_centered': { 'title_is_centered': {
'description': 'Center the title on the page', 'description': 'Center the title on the page',
'type': bool, 'type': bool,
'default': None, 'default': False,
'group': 'title' 'group': 'title'
}, },
'title_is_upper': { 'title_is_upper': {
'description': 'Force the title to be uppercase', 'description': 'Force the title to be uppercase',
'type': bool, 'type': bool,
'default': None, 'default': False,
'group': 'title' 'group': 'title'
}, },
'composer_font': { 'composer_font': {
@ -81,27 +81,27 @@ HTML_OPTIONS = {
'group': 'composer' 'group': 'composer'
}, },
'composer_size': { 'composer_size': {
'description': 'The size of the composer in rem', 'description': 'The size of the composer in pt',
'type': int, 'type': int,
'default': None, 'default': 12,
'group': 'composer' 'group': 'composer'
}, },
'composer_is_bold': { 'composer_is_bold': {
'description': 'Set to True to make the composer bold', 'description': 'Set to True to make the composer bold',
'type': bool, 'type': bool,
'default': None, 'default': False,
'group': 'composer' 'group': 'composer'
}, },
'composer_is_centered': { 'composer_is_centered': {
'description': 'Center the composer on the page', 'description': 'Center the composer on the page',
'type': bool, 'type': bool,
'default': None, 'default': False,
'group': 'composer' 'group': 'composer'
}, },
'composer_is_upper': { 'composer_is_upper': {
'description': 'Force the composer to be uppercase', 'description': 'Force the composer to be uppercase',
'type': bool, 'type': bool,
'default': None, 'default': False,
'group': 'composer' 'group': 'composer'
}, },
'copyright_font': { 'copyright_font': {
@ -111,9 +111,9 @@ HTML_OPTIONS = {
'group': 'copyright' 'group': 'copyright'
}, },
'copyright_size': { 'copyright_size': {
'description': 'The size of the copyright in rem', 'description': 'The size of the copyright in pt',
'type': int, 'type': int,
'default': None, 'default': 12,
'group': 'copyright' 'group': 'copyright'
}, },
'copyright_is_bold': { 'copyright_is_bold': {
@ -125,7 +125,7 @@ HTML_OPTIONS = {
'copyright_is_upper': { 'copyright_is_upper': {
'description': 'Force the copyright to be uppercase', 'description': 'Force the copyright to be uppercase',
'type': bool, 'type': bool,
'default': None, 'default': False,
'group': 'copyright' 'group': 'copyright'
}, },
'stanza_font': { 'stanza_font': {
@ -135,9 +135,9 @@ HTML_OPTIONS = {
'group': 'stanza' 'group': 'stanza'
}, },
'stanza_size': { 'stanza_size': {
'description': 'The size of the stanza in rem', 'description': 'The size of the stanza in pt',
'type': int, 'type': int,
'default': None, 'default': 12,
'group': 'stanza' 'group': 'stanza'
}, },
'stanza_is_bold': { 'stanza_is_bold': {
@ -147,7 +147,7 @@ HTML_OPTIONS = {
'group': 'stanza' 'group': 'stanza'
}, },
'stanza_indent': { 'stanza_indent': {
'description': 'How much to indent the stanza by, in rem', 'description': 'How much to indent the stanza',
'type': int, 'type': int,
'default': 4, 'default': 4,
'group': 'stanza' 'group': 'stanza'
@ -159,9 +159,9 @@ HTML_OPTIONS = {
'group': 'stanza_heading' 'group': 'stanza_heading'
}, },
'stanza_heading_size': { 'stanza_heading_size': {
'description': 'The size of the stanza heading in rem', 'description': 'The size of the stanza heading in pt',
'type': int, 'type': int,
'default': None, 'default': 12,
'group': 'stanza_heading' 'group': 'stanza_heading'
}, },
'stanza_heading_is_bold': { 'stanza_heading_is_bold': {
@ -183,21 +183,21 @@ HTML_OPTIONS = {
'group': 'verse' 'group': 'verse'
}, },
'verse_size': { 'verse_size': {
'description': 'The size of the verse in rem', 'description': 'The size of the verse in pt',
'type': int, 'type': int,
'default': None, 'default': 12,
'group': 'verse' 'group': 'verse'
}, },
'verse_is_bold': { 'verse_is_bold': {
'description': 'Set to True to make the verse bold', 'description': 'Set to True to make the verse bold',
'type': bool, 'type': bool,
'default': None, 'default': False,
'group': 'verse' 'group': 'verse'
}, },
'verse_indent': { 'verse_indent': {
'description': 'How much to indent the verse by, in rem', 'description': 'How much to indent the verse',
'type': int, 'type': int,
'default': None, 'default': 4,
'group': 'verse' 'group': 'verse'
}, },
'verse_heading_font': { 'verse_heading_font': {
@ -207,15 +207,15 @@ HTML_OPTIONS = {
'group': 'verse_heading' 'group': 'verse_heading'
}, },
'verse_heading_size': { 'verse_heading_size': {
'description': 'The size of the verse heading in rem', 'description': 'The size of the verse heading in pt',
'type': int, 'type': int,
'default': None, 'default': 12,
'group': 'verse_heading' 'group': 'verse_heading'
}, },
'verse_heading_is_bold': { 'verse_heading_is_bold': {
'description': 'Set to True to make the verse heading bold', 'description': 'Set to True to make the verse heading bold',
'type': bool, 'type': bool,
'default': None, 'default': True,
'group': 'verse_heading' 'group': 'verse_heading'
}, },
'verse_heading_is_upper': { 'verse_heading_is_upper': {
@ -231,21 +231,21 @@ HTML_OPTIONS = {
'group': 'chorus' 'group': 'chorus'
}, },
'chorus_size': { 'chorus_size': {
'description': 'The size of the chorus in rem', 'description': 'The size of the chorus in pt',
'type': int, 'type': int,
'default': None, 'default': 12,
'group': 'chorus' 'group': 'chorus'
}, },
'chorus_is_bold': { 'chorus_is_bold': {
'description': 'Set to True to make the chorus bold', 'description': 'Set to True to make the chorus bold',
'type': bool, 'type': bool,
'default': None, 'default': False,
'group': 'chorus' 'group': 'chorus'
}, },
'chorus_indent': { 'chorus_indent': {
'description': 'How much to indent the chorus by, in rem', 'description': 'How much to indent the chorus',
'type': int, 'type': int,
'default': None, 'default': 4,
'group': 'chorus' 'group': 'chorus'
}, },
'chorus_heading_font': { 'chorus_heading_font': {
@ -255,15 +255,15 @@ HTML_OPTIONS = {
'group': 'chorus_heading' 'group': 'chorus_heading'
}, },
'chorus_heading_size': { 'chorus_heading_size': {
'description': 'The size of the chorus heading in rem', 'description': 'The size of the chorus heading in pt',
'type': int, 'type': int,
'default': None, 'default': 12,
'group': 'chorus_heading' 'group': 'chorus_heading'
}, },
'chorus_heading_is_bold': { 'chorus_heading_is_bold': {
'description': 'Set to True to make the chorus heading bold', 'description': 'Set to True to make the chorus heading bold',
'type': bool, 'type': bool,
'default': None, 'default': True,
'group': 'chorus_heading' 'group': 'chorus_heading'
}, },
'chorus_heading_is_upper': { 'chorus_heading_is_upper': {
@ -279,21 +279,21 @@ HTML_OPTIONS = {
'group': 'bridge' 'group': 'bridge'
}, },
'bridge_size': { 'bridge_size': {
'description': 'The size of the bridge in rem', 'description': 'The size of the bridge in pt',
'type': int, 'type': int,
'default': None, 'default': 12,
'group': 'bridge' 'group': 'bridge'
}, },
'bridge_is_bold': { 'bridge_is_bold': {
'description': 'Set to True to make the bridge bold', 'description': 'Set to True to make the bridge bold',
'type': bool, 'type': bool,
'default': None, 'default': False,
'group': 'bridge' 'group': 'bridge'
}, },
'bridge_indent': { 'bridge_indent': {
'description': 'How much to indent the bridge by, in rem', 'description': 'How much to indent the bridge',
'type': int, 'type': int,
'default': None, 'default': 4,
'group': 'bridge' 'group': 'bridge'
}, },
'bridge_heading_font': { 'bridge_heading_font': {
@ -303,15 +303,15 @@ HTML_OPTIONS = {
'group': 'bridge_heading' 'group': 'bridge_heading'
}, },
'bridge_heading_size': { 'bridge_heading_size': {
'description': 'The size of the bridge heading in rem', 'description': 'The size of the bridge heading in pt',
'type': int, 'type': int,
'default': None, 'default': 12,
'group': 'bridge_heading' 'group': 'bridge_heading'
}, },
'bridge_heading_is_bold': { 'bridge_heading_is_bold': {
'description': 'Set to True to make the bridge heading bold', 'description': 'Set to True to make the bridge heading bold',
'type': bool, 'type': bool,
'default': None, 'default': True,
'group': 'bridge_heading' 'group': 'bridge_heading'
}, },
'bridge_heading_is_upper': { 'bridge_heading_is_upper': {
@ -327,9 +327,9 @@ HTML_OPTIONS = {
'group': 'chord' 'group': 'chord'
}, },
'chord_size': { 'chord_size': {
'description': 'The size of the chord in rem', 'description': 'The size of the chord in pt',
'type': int, 'type': int,
'default': None, 'default': 12,
'group': 'chord' 'group': 'chord'
}, },
'chord_is_bold': { 'chord_is_bold': {
@ -367,14 +367,10 @@ def get_option_groups():
def make_style(name, font=None, size=None, is_bold=None, is_centered=None, is_upper=None, indent=None): def make_style(name, font=None, size=None, is_bold=None, is_centered=None, is_upper=None, indent=None):
"""Build a CSS style""" """Build a CSS style"""
styles = [] styles = []
if name == 'default':
styles.append('html, body, * {')
else:
styles.append('.{name} {{'.format(name=name))
if font: if font:
styles.append(' font-family: \'{font}\';'.format(font=font)) styles.append(' font-family: \'{font}\';'.format(font=font))
if size: if size:
styles.append(' font-size: {size}rem;'.format(size=size)) styles.append(' font-size: {size}pt;'.format(size=size))
if is_bold: if is_bold:
styles.append(' font-weight: bold !important;') styles.append(' font-weight: bold !important;')
if is_centered: if is_centered:
@ -383,7 +379,12 @@ def make_style(name, font=None, size=None, is_bold=None, is_centered=None, is_up
styles.append(' text-transform: uppercase;') styles.append(' text-transform: uppercase;')
if indent: if indent:
styles.append(' margin-left: {indent}rem;'.format(indent=indent)) styles.append(' margin-left: {indent}rem;'.format(indent=indent))
styles.append('}') if styles:
if name == 'default':
styles.insert(0, 'html, body, * {')
else:
styles.insert(0, '.{name} {{'.format(name=name))
styles.append('}')
return os.linesep.join(styles) return os.linesep.join(styles)
@ -395,7 +396,7 @@ def generate_option_styles(options):
name = '{group}_{key}'.format(group=group, key=key) name = '{group}_{key}'.format(group=group, key=key)
if name not in HTML_OPTIONS: if name not in HTML_OPTIONS:
continue continue
option = options.get(name, default=HTML_OPTIONS[name]['default']) option = options.get(name, HTML_OPTIONS[name]['default'])
if option: if option:
kwargs[key] = option kwargs[key] = option
styles.append(make_style(group.replace('_', '-'), **kwargs)) styles.append(make_style(group.replace('_', '-'), **kwargs))
@ -419,9 +420,9 @@ def render(song, options=None):
rendered_syllables.append(SYLLB.format( rendered_syllables.append(SYLLB.format(
syllable.syllable + ('&nbsp;' if is_last_syllable and not is_last_word else ''))) syllable.syllable + ('&nbsp;' if is_last_syllable and not is_last_word else '')))
rendered_lines.append(LINE.format(''.join(rendered_chords), ''.join(rendered_syllables))) rendered_lines.append(LINE.format(''.join(rendered_chords), ''.join(rendered_syllables)))
rendered_verses.append(VERSE.format(verse_type=verse.type_ or '', verse_name=verse.title, rendered_verses.append(STANZA.format(verse_type=verse.type_ or '', verse_name=verse.title,
verse_body='\n'.join(rendered_lines))) verse_body='\n'.join(rendered_lines)))
title = song.metadata.get('title') or 'Song' title = song.metadata.get('title') or 'Song'
metadata = TITLE.format(title=title, artist=song.metadata.get('artist') or song.metadata.get('composer') or '') metadata = TITLE.format(title=title, composer=song.metadata.get('artist') or song.metadata.get('composer') or '')
body = metadata + '\n' + '\n'.join(rendered_verses) body = metadata + '\n' + '\n'.join(rendered_verses)
return HTML.format(title=title, body=body, styles=os.linesep.join(styles)) return HTML.format(title=title, body=body, styles=os.linesep.join(styles))