2021-07-24 06:31:21 +00:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
|
|
|
from chordpro.base import Song
|
2021-07-28 18:41:15 +00:00
|
|
|
from chordpro.renderers.html import render as render_html
|
|
|
|
from chordpro.renderers.text import render as render_text
|
2021-07-24 06:31:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = ArgumentParser()
|
|
|
|
parser.add_argument('input', metavar='FILENAME', help='Input ChordPro file')
|
|
|
|
parser.add_argument('-o', '--output', metavar='FILENAME', help='Output to a file')
|
2021-07-28 18:41:15 +00:00
|
|
|
parser.add_argument('-f', '--format', metavar='FORMAT', choices=['html', 'text'], default='html',
|
|
|
|
help='Output format')
|
|
|
|
parser.add_argument('-p', '--param', metavar='KEY=VALUE', action='append', help='Parameter to send to renderer')
|
2021-07-24 06:31:21 +00:00
|
|
|
args = parser.parse_args()
|
2021-07-28 18:41:15 +00:00
|
|
|
render_params = {kv.split('=')[0]: kv.split('=')[-1] for kv in args.param} if args.param else {}
|
|
|
|
# Prepare the params
|
|
|
|
for key, value in render_params.items():
|
|
|
|
if value.lower() == "true":
|
|
|
|
render_params[key] = True
|
|
|
|
elif value.lower() == "false":
|
|
|
|
render_params[key] = False
|
|
|
|
elif value.isdigit():
|
|
|
|
render_params[key] = int(value)
|
2021-07-24 06:31:21 +00:00
|
|
|
|
|
|
|
song = Song(args.input)
|
2021-07-28 18:41:15 +00:00
|
|
|
if args.format == 'text':
|
|
|
|
output = render_text(song, **render_params)
|
|
|
|
else:
|
|
|
|
output = render_html(song, **render_params)
|
2021-07-24 06:31:21 +00:00
|
|
|
if args.output:
|
|
|
|
with open(args.output, 'w') as html_file:
|
|
|
|
html_file.write(output)
|
|
|
|
else:
|
|
|
|
print(output)
|