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.
This commit is contained in:
Jim Hague 2016-10-29 19:32:53 +01:00
parent 6c18791f5e
commit fcc48a7a58
3 changed files with 34 additions and 27 deletions

View File

@ -1,7 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
# #
# Extact a text field (title, by default) from a .abc file, and print it out # 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 import optparse
@ -86,7 +87,7 @@ accentedletters = {
"ss" : ("ß", "\\ss"), "ss" : ("ß", "\\ss"),
} }
def convertTitle(t, options): def convertField(t, options):
res = "" res = ""
while True: while True:
p = t.partition('\\') p = t.partition('\\')
@ -95,7 +96,7 @@ def convertTitle(t, options):
break break
abc = p[2][0:2] abc = p[2][0:2]
t = p[2][2:] t = p[2][2:]
if (options.html or options.latex) and abc in accentedletters: if abc in accentedletters:
if options.html: if options.html:
res += accentedletters[abc][0] res += accentedletters[abc][0]
else: else:
@ -106,41 +107,47 @@ def convertTitle(t, options):
def process(inf, options): def process(inf, options):
n = options.index n = options.index
found = False
for line in inf: for line in inf:
line = line.strip() line = line.strip()
if len(line) > 2 and line[0] == options.field and line[1] == ':': if len(line) > 2 and line[1] == ':':
if len(options.contains) > 0: if found:
if line.find(options.contains) < 0: if line[0] != '+':
continue break
line = line[2:].strip()
elif line[0] == options.field:
if n > 1: if n > 1:
n = n - 1 n = n - 1
continue
else: else:
print(convertTitle(line[2:].strip(), options)) line = line[2:].strip()
break if len(options.starts) > 0:
if line.find(options.starts) == 0:
line = line[len(options.starts):].strip()
else:
continue
else:
continue
found = True
print(convertField(line, options))
parser = optparse.OptionParser(usage="usage: %prog [options] [filename]\n\n" parser = optparse.OptionParser(usage="usage: %prog [options] [filename]\n\n"
" Extract field data from ABC file.") " Extract field data from ABC file.")
parser.add_option("-f", "--field", dest="field", default="T", parser.add_option("-f", "--field", dest="field", default="T",
help="extract the field FIELD", metavar="FIELD") 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", parser.add_option("-l", "--latex", dest="latex",
action="store_true", default=False, action="store_true", default=False,
help="format ouput for LaTeX") help="convert special characters for LaTeX")
parser.add_option("-n", "--index", dest="index", parser.add_option("-n", "--index", dest="index",
action="store", type="int", default=1, action="store", type="int", default=1,
help="report INDEXth value [default: %default]", help="report INDEXth value [default: %default]",
metavar="INDEX") metavar="INDEX")
parser.add_option("-c", "--contains", dest="contains", parser.add_option("-s", "--starts", dest="starts",
action="store", type="string", default="", action="store", type="string", default="",
help="report only if line contains CONTENT", help="report only if line starts CONTENT and remove CONTENT",
metavar="CONTENT") metavar="CONTENT")
(options, args) = parser.parse_args() (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: if len(args) > 0:
for arg in args: for arg in args:
try: try:

View File

@ -46,7 +46,7 @@ find $booke -name "*.abc" | sort |
subtitle=$retval subtitle=$retval
composer=`$dir/abcfield.py --field C --latex $filename` 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="" changename=""
changetitle="" changetitle=""
if [ -n "$changefile" ]; then if [ -n "$changefile" ]; then
@ -56,7 +56,7 @@ find $booke -name "*.abc" | sort |
changetitle=$retval changetitle=$retval
fi 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 "\showtune{$name}{$title}{$subtitle}{$composer}{$graphicsdir/$name}{$changename}{$changetitle}{$credit}" >> $tunesoutput
echo -E "\showfirstline{$name}{$title}{$graphicsdir/firstline-$name}" >> $indexoutput echo -E "\showfirstline{$name}{$title}{$graphicsdir/firstline-$name}" >> $indexoutput

View File

@ -105,24 +105,24 @@ find $bookedir -name "*.abc" | sort |
name=`basename $filename .abc` name=`basename $filename .abc`
# Extract items to substitute in the web page. # 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" fixtitle "$title"
title=$retval title=$retval
subtitle=`$dir/abcfield.py --index 2 --field T --html $filename` subtitle=`$dir/abcfield.py --index 2 --field T $filename`
fixtitle "$subtitle" fixtitle "$subtitle"
subtitle=$retval subtitle=$retval
composer=`$dir/abcfield.py --field C --html $filename` composer=`$dir/abcfield.py --field C $filename`
changefile=`$dir/abcfield.py --field N --contains "Change:" $filename | sed -e "s/Change: *//"` changefile=`$dir/abcfield.py --field N --starts "Change:" $filename`
changetitle="" changetitle=""
changevisibility="no" changevisibility="no"
if [ -n "$changefile" ]; then if [ -n "$changefile" ]; then
changetitle=`$dir/abcfield.py --field T --html $bookedir/$changefile` changetitle=`$dir/abcfield.py --field T $bookedir/$changefile`
changevisibility="yes" changevisibility="yes"
fixtitle "$changetitle" fixtitle "$changetitle"
changetitle=$retval changetitle=$retval
fi 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" creditvisibility="no"
if [ -n "$credit" ]; then if [ -n "$credit" ]; then
creditvisibility="yes" creditvisibility="yes"