From fcc48a7a58469e653396367e2bcd6d2c4fad6143 Mon Sep 17 00:00:00 2001 From: Jim Hague Date: Sat, 29 Oct 2016 19:32:53 +0100 Subject: [PATCH] Revise abcfield.py to recognise continuation fields. Also default to HTML entity output, and replace --contains with --starts, which does the same thing but checks only the start of the line and removes the matched item. --- abcfield.py | 45 +++++++++++++++++++++++++------------------ makeBookeTunePages.sh | 4 ++-- makeWeb.sh | 12 ++++++------ 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/abcfield.py b/abcfield.py index 3a89a31..15d8ca2 100755 --- a/abcfield.py +++ b/abcfield.py @@ -1,7 +1,8 @@ #!/usr/bin/env python # # Extact a text field (title, by default) from a .abc file, and print it out -# formatted for use in LaTeX or HTML. +# with any ABC accented characters converted to HTML (default) or Latex. +# Recognise continuation fields and print those too. # import optparse @@ -86,7 +87,7 @@ accentedletters = { "ss" : ("ß", "\\ss"), } -def convertTitle(t, options): +def convertField(t, options): res = "" while True: p = t.partition('\\') @@ -95,7 +96,7 @@ def convertTitle(t, options): break abc = p[2][0:2] t = p[2][2:] - if (options.html or options.latex) and abc in accentedletters: + if abc in accentedletters: if options.html: res += accentedletters[abc][0] else: @@ -106,41 +107,47 @@ def convertTitle(t, options): def process(inf, options): n = options.index + found = False for line in inf: line = line.strip() - if len(line) > 2 and line[0] == options.field and line[1] == ':': - if len(options.contains) > 0: - if line.find(options.contains) < 0: + if len(line) > 2 and line[1] == ':': + if found: + if line[0] != '+': + break + line = line[2:].strip() + elif line[0] == options.field: + if n > 1: + n = n - 1 continue - if n > 1: - n = n - 1 + else: + line = line[2:].strip() + if len(options.starts) > 0: + if line.find(options.starts) == 0: + line = line[len(options.starts):].strip() + else: + continue else: - print(convertTitle(line[2:].strip(), options)) - break + continue + found = True + print(convertField(line, options)) parser = optparse.OptionParser(usage="usage: %prog [options] [filename]\n\n" " Extract field data from ABC file.") parser.add_option("-f", "--field", dest="field", default="T", help="extract the field FIELD", metavar="FIELD") -parser.add_option("-m", "--html", dest="html", - action="store_true", default=False, - help="format output for HTML") parser.add_option("-l", "--latex", dest="latex", action="store_true", default=False, - help="format ouput for LaTeX") + help="convert special characters for LaTeX") parser.add_option("-n", "--index", dest="index", action="store", type="int", default=1, help="report INDEXth value [default: %default]", metavar="INDEX") -parser.add_option("-c", "--contains", dest="contains", +parser.add_option("-s", "--starts", dest="starts", action="store", type="string", default="", - help="report only if line contains CONTENT", + help="report only if line starts CONTENT and remove CONTENT", metavar="CONTENT") (options, args) = parser.parse_args() -if options.html and options.latex: - sys.exit("You must choose one of HTML or LaTeX output") - if len(args) > 0: for arg in args: try: diff --git a/makeBookeTunePages.sh b/makeBookeTunePages.sh index feb63a7..9cc1830 100755 --- a/makeBookeTunePages.sh +++ b/makeBookeTunePages.sh @@ -46,7 +46,7 @@ find $booke -name "*.abc" | sort | subtitle=$retval composer=`$dir/abcfield.py --field C --latex $filename` - changefile=`$dir/abcfield.py --field N --contains "Change:" $filename | sed -e "s/Change: *//"` + changefile=`$dir/abcfield.py --field N --starts "Change:" $filename` changename="" changetitle="" if [ -n "$changefile" ]; then @@ -56,7 +56,7 @@ find $booke -name "*.abc" | sort | changetitle=$retval fi - credit=`$dir/abcfield.py --field N --contains "Credit:" $filename | sed -e "s/Credit: *//"` + credit=`$dir/abcfield.py --field N --starts "Credit:" $filename` echo -E "\showtune{$name}{$title}{$subtitle}{$composer}{$graphicsdir/$name}{$changename}{$changetitle}{$credit}" >> $tunesoutput echo -E "\showfirstline{$name}{$title}{$graphicsdir/firstline-$name}" >> $indexoutput diff --git a/makeWeb.sh b/makeWeb.sh index 40d5a76..46f4ae9 100755 --- a/makeWeb.sh +++ b/makeWeb.sh @@ -105,24 +105,24 @@ find $bookedir -name "*.abc" | sort | name=`basename $filename .abc` # Extract items to substitute in the web page. - title=`$dir/abcfield.py --field T --html $filename` + title=`$dir/abcfield.py --field T $filename` fixtitle "$title" title=$retval - subtitle=`$dir/abcfield.py --index 2 --field T --html $filename` + subtitle=`$dir/abcfield.py --index 2 --field T $filename` fixtitle "$subtitle" subtitle=$retval - composer=`$dir/abcfield.py --field C --html $filename` - changefile=`$dir/abcfield.py --field N --contains "Change:" $filename | sed -e "s/Change: *//"` + composer=`$dir/abcfield.py --field C $filename` + changefile=`$dir/abcfield.py --field N --starts "Change:" $filename` changetitle="" changevisibility="no" if [ -n "$changefile" ]; then - changetitle=`$dir/abcfield.py --field T --html $bookedir/$changefile` + changetitle=`$dir/abcfield.py --field T $bookedir/$changefile` changevisibility="yes" fixtitle "$changetitle" changetitle=$retval fi - credit=`$dir/abcfield.py --field N --contains "Credit:" $filename | sed -e "s/Credit: *//"` + credit=`$dir/abcfield.py --field N --starts "Credit:" $filename` creditvisibility="no" if [ -n "$credit" ]; then creditvisibility="yes"