The first line only ends at the first line that does not continue.

This commit is contained in:
Jim Hague 2023-03-03 11:12:00 +00:00
parent 0dcd0e9e46
commit 9a8bc8c55d
1 changed files with 15 additions and 14 deletions

View File

@ -4,6 +4,8 @@
# to print the first line of the music only. # to print the first line of the music only.
# #
import argparse
import pathlib
import sys import sys
def process(inf): def process(inf):
@ -21,21 +23,20 @@ def process(inf):
if len(start) > 1 and start[1] == ":" and (start[0].isalpha() or start[0] == '+'): if len(start) > 1 and start[1] == ":" and (start[0].isalpha() or start[0] == '+'):
if start[0] in ["M", "K", "L"]: if start[0] in ["M", "K", "L"]:
print(line) print(line)
# Output line. If it is a continuation, output at most one # Output line. If the line ends with a continuation, carry
# continuation. # on outputting lines, otherwise stop.
else: else:
print(line) print(line)
if continued or line[-1] != "\\": if line[-1] != "\\":
break break
else:
continued = True
if len(sys.argv) > 1: if __name__ == "__main__":
for arg in sys.argv[1:]: parser = argparse.ArgumentParser(description="Write minimal version of ABC input file with just the first line of music.")
try: parser.add_argument('input', type=argparse.FileType('r'),
inf = open(arg, "r") help='input ABC file')
process(inf) args = parser.parse_args()
finally:
inf.close() path = pathlib.Path(args.input.name)
else: with path.open() as f:
process(sys.stdin) process(f)
sys.exit(0)