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