Mercurial > dottes
comparison abcfield.py @ 581:760d0ae5acea
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.
| author | Jim Hague <jim.hague@acm.org> |
|---|---|
| date | Sat, 29 Oct 2016 19:32:53 +0100 |
| parents | 27f29e8aafea |
| children | 696c461c8dc0 |
comparison
equal
deleted
inserted
replaced
| 580:575f1eae4137 | 581:760d0ae5acea |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Extact a text field (title, by default) from a .abc file, and print it out | 3 # Extact a text field (title, by default) from a .abc file, and print it out |
| 4 # formatted for use in LaTeX or HTML. | 4 # with any ABC accented characters converted to HTML (default) or Latex. |
| 5 # Recognise continuation fields and print those too. | |
| 5 # | 6 # |
| 6 | 7 |
| 7 import optparse | 8 import optparse |
| 8 import sys | 9 import sys |
| 9 | 10 |
| 84 "AE" : ("Æ", "\\AE"), | 85 "AE" : ("Æ", "\\AE"), |
| 85 "ae" : ("æ", "\\ae"), | 86 "ae" : ("æ", "\\ae"), |
| 86 "ss" : ("ß", "\\ss"), | 87 "ss" : ("ß", "\\ss"), |
| 87 } | 88 } |
| 88 | 89 |
| 89 def convertTitle(t, options): | 90 def convertField(t, options): |
| 90 res = "" | 91 res = "" |
| 91 while True: | 92 while True: |
| 92 p = t.partition('\\') | 93 p = t.partition('\\') |
| 93 res += p[0] | 94 res += p[0] |
| 94 if p[1] == "": | 95 if p[1] == "": |
| 95 break | 96 break |
| 96 abc = p[2][0:2] | 97 abc = p[2][0:2] |
| 97 t = p[2][2:] | 98 t = p[2][2:] |
| 98 if (options.html or options.latex) and abc in accentedletters: | 99 if abc in accentedletters: |
| 99 if options.html: | 100 if options.html: |
| 100 res += accentedletters[abc][0] | 101 res += accentedletters[abc][0] |
| 101 else: | 102 else: |
| 102 res += accentedletters[abc][1] | 103 res += accentedletters[abc][1] |
| 103 else: | 104 else: |
| 104 res += "\\" + abc | 105 res += "\\" + abc |
| 105 return res | 106 return res |
| 106 | 107 |
| 107 def process(inf, options): | 108 def process(inf, options): |
| 108 n = options.index | 109 n = options.index |
| 110 found = False | |
| 109 for line in inf: | 111 for line in inf: |
| 110 line = line.strip() | 112 line = line.strip() |
| 111 if len(line) > 2 and line[0] == options.field and line[1] == ':': | 113 if len(line) > 2 and line[1] == ':': |
| 112 if len(options.contains) > 0: | 114 if found: |
| 113 if line.find(options.contains) < 0: | 115 if line[0] != '+': |
| 116 break | |
| 117 line = line[2:].strip() | |
| 118 elif line[0] == options.field: | |
| 119 if n > 1: | |
| 120 n = n - 1 | |
| 114 continue | 121 continue |
| 115 if n > 1: | 122 else: |
| 116 n = n - 1 | 123 line = line[2:].strip() |
| 124 if len(options.starts) > 0: | |
| 125 if line.find(options.starts) == 0: | |
| 126 line = line[len(options.starts):].strip() | |
| 127 else: | |
| 128 continue | |
| 117 else: | 129 else: |
| 118 print(convertTitle(line[2:].strip(), options)) | 130 continue |
| 119 break | 131 found = True |
| 132 print(convertField(line, options)) | |
| 120 | 133 |
| 121 parser = optparse.OptionParser(usage="usage: %prog [options] [filename]\n\n" | 134 parser = optparse.OptionParser(usage="usage: %prog [options] [filename]\n\n" |
| 122 " Extract field data from ABC file.") | 135 " Extract field data from ABC file.") |
| 123 parser.add_option("-f", "--field", dest="field", default="T", | 136 parser.add_option("-f", "--field", dest="field", default="T", |
| 124 help="extract the field FIELD", metavar="FIELD") | 137 help="extract the field FIELD", metavar="FIELD") |
| 125 parser.add_option("-m", "--html", dest="html", | |
| 126 action="store_true", default=False, | |
| 127 help="format output for HTML") | |
| 128 parser.add_option("-l", "--latex", dest="latex", | 138 parser.add_option("-l", "--latex", dest="latex", |
| 129 action="store_true", default=False, | 139 action="store_true", default=False, |
| 130 help="format ouput for LaTeX") | 140 help="convert special characters for LaTeX") |
| 131 parser.add_option("-n", "--index", dest="index", | 141 parser.add_option("-n", "--index", dest="index", |
| 132 action="store", type="int", default=1, | 142 action="store", type="int", default=1, |
| 133 help="report INDEXth value [default: %default]", | 143 help="report INDEXth value [default: %default]", |
| 134 metavar="INDEX") | 144 metavar="INDEX") |
| 135 parser.add_option("-c", "--contains", dest="contains", | 145 parser.add_option("-s", "--starts", dest="starts", |
| 136 action="store", type="string", default="", | 146 action="store", type="string", default="", |
| 137 help="report only if line contains CONTENT", | 147 help="report only if line starts CONTENT and remove CONTENT", |
| 138 metavar="CONTENT") | 148 metavar="CONTENT") |
| 139 (options, args) = parser.parse_args() | 149 (options, args) = parser.parse_args() |
| 140 | |
| 141 if options.html and options.latex: | |
| 142 sys.exit("You must choose one of HTML or LaTeX output") | |
| 143 | 150 |
| 144 if len(args) > 0: | 151 if len(args) > 0: |
| 145 for arg in args: | 152 for arg in args: |
| 146 try: | 153 try: |
| 147 inf = open(arg, "r") | 154 inf = open(arg, "r") |
