annotate abcfield.py @ 584:696c461c8dc0

Set exit status on abcfield.py to indicate if anything found.
author Jim Hague <jim.hague@acm.org>
date Sat, 29 Oct 2016 23:18:45 +0100
parents 760d0ae5acea
children daa3b76bd11f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1 #!/usr/bin/env python
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2 #
122
295ba8275ab4 Make output larger where possible.
Jim Hague <jim.hague@laicatc.com>
parents: 110
diff changeset
3 # Extact a text field (title, by default) from a .abc file, and print it out
581
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
4 # with any ABC accented characters converted to HTML (default) or Latex.
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
5 # Recognise continuation fields and print those too.
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
6 #
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
7
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
8 import optparse
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
9 import sys
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
10
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
11 accentedletters = {
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
12 # Acute accents
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
13 "'A" : ("&Aacute;", "\\'{A}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
14 "'E" : ("&Eacute;", "\\'{E}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
15 "'I" : ("&Iacute;", "\\'{I}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
16 "'O" : ("&Oacute;", "\\'{O}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
17 "'U" : ("&Uacute;", "\\'{U}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
18 "'Y" : ("&Yacute;", "\\'{Y}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
19 "'a" : ("&aacute;", "\\'{a}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
20 "'e" : ("&eacute;", "\\'{e}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
21 "'i" : ("&iacute;", "\\'{i}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
22 "'o" : ("&oacute;", "\\'{o}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
23 "'u" : ("&uacute;", "\\'{u}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
24 "'y" : ("&yacute;", "\\'{y}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
25
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
26 # Grave accents
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
27 "`A" : ("&Agrave;", "\\`{A}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
28 "`E" : ("&Egrave;", "\\`{E}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
29 "`I" : ("&Igrave;", "\\`{I}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
30 "`O" : ("&Ograve;", "\\`{O}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
31 "`U" : ("&Ugrave;", "\\`{U}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
32 "`a" : ("&agrave;", "\\`{a}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
33 "`e" : ("&egrave;", "\\`{e}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
34 "`i" : ("&igrave;", "\\`{i}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
35 "`o" : ("&ograve;", "\\`{o}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
36 "`u" : ("&ugrave;", "\\`{u}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
37
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
38 # Umlauts
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
39 "\"A" : ("&Auml;", "\\\"{A}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
40 "\"E" : ("&Euml;", "\\\"{E}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
41 "\"I" : ("&Iuml;", "\\\"{I}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
42 "\"O" : ("&Ouml;", "\\\"{O}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
43 "\"U" : ("&Uuml;", "\\\"{U}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
44 "\"Y" : ("&Yuml;", "\\\"{Y}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
45 "\"a" : ("&auml;", "\\\"{a}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
46 "\"e" : ("&euml;", "\\\"{e}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
47 "\"i" : ("&iuml;", "\\\"{\i}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
48 "\"o" : ("&ouml;", "\\\"{o}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
49 "\"u" : ("&uuml;", "\\\"{u}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
50 "\"y" : ("&yuml;", "\\\"{y}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
51
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
52 # Circumflexes
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
53 "^A" : ("&Acirc;", "\\^{A}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
54 "^E" : ("&Ecirc;", "\\^{E}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
55 "^I" : ("&Icirc;", "\\^{I}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
56 "^O" : ("&Ocirc;", "\\^{O}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
57 "^U" : ("&Ucirc;", "\\^{U}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
58 "^a" : ("&acirc;", "\\^{a}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
59 "^e" : ("&ecirc;", "\\^{e}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
60 "^i" : ("&icirc;", "\\^{\i}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
61 "^o" : ("&ocirc;", "\\^{o}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
62 "^u" : ("&ucirc;", "\\^{u}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
63
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
64 # Tilde
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
65 "~A" : ("&Atilde;", "\\~{A}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
66 "~N" : ("&Ntilde;", "\\~{N}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
67 "~O" : ("&Otilde;", "\\~{O}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
68 "~a" : ("&atilde;", "\\~{a}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
69 "~n" : ("&ntilde;", "\\~{n}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
70 "~o" : ("&otilde;", "\\~{o}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
71
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
72 # Cedilla
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
73 ",C" : ("&Ccedil;", "\\c{C}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
74 ",c" : ("&ccedil;", "\\c{c}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
75
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
76 # Slash
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
77 "/O" : ("&Oslash;", "\\O"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
78 "/o" : ("&oslash;", "\\o"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
79
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
80 # Ring
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
81 "AA" : ("&Aring;", "\\r{A}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
82 "aa" : ("&aring;", "\\r{a}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
83
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
84 # Ligatures
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
85 "AE" : ("&AElig;", "\\AE"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
86 "ae" : ("&aelig;", "\\ae"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
87 "ss" : ("&szlig;", "\\ss"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
88 }
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
89
581
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
90 def convertField(t, options):
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
91 res = ""
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
92 while True:
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
93 p = t.partition('\\')
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
94 res += p[0]
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
95 if p[1] == "":
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
96 break
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
97 abc = p[2][0:2]
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
98 t = p[2][2:]
581
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
99 if abc in accentedletters:
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
100 if options.html:
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
101 res += accentedletters[abc][0]
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
102 else:
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
103 res += accentedletters[abc][1]
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
104 else:
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
105 res += "\\" + abc
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
106 return res
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
107
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
108 def process(inf, options):
316
eedf65564226 Add --index parameter to allow selection of nth occurence of field.
Jim Hague <jim.hague@acm.org>
parents: 122
diff changeset
109 n = options.index
581
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
110 found = False
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
111 for line in inf:
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
112 line = line.strip()
581
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
113 if len(line) > 2 and line[1] == ':':
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
114 if found:
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
115 if line[0] != '+':
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
116 break
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
117 line = line[2:].strip()
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
118 elif line[0] == options.field:
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
119 if n > 1:
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
120 n = n - 1
326
27f29e8aafea Add --contains flag to abcfield.py.
Jim Hague <jim.hague@acm.org>
parents: 316
diff changeset
121 continue
581
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
122 else:
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
123 line = line[2:].strip()
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
124 if len(options.starts) > 0:
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
125 if line.find(options.starts) == 0:
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
126 line = line[len(options.starts):].strip()
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
127 else:
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
128 continue
316
eedf65564226 Add --index parameter to allow selection of nth occurence of field.
Jim Hague <jim.hague@acm.org>
parents: 122
diff changeset
129 else:
581
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
130 continue
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
131 found = True
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
132 print(convertField(line, options))
584
696c461c8dc0 Set exit status on abcfield.py to indicate if anything found.
Jim Hague <jim.hague@acm.org>
parents: 581
diff changeset
133 return found
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
134
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
135 parser = optparse.OptionParser(usage="usage: %prog [options] [filename]\n\n"
122
295ba8275ab4 Make output larger where possible.
Jim Hague <jim.hague@laicatc.com>
parents: 110
diff changeset
136 " Extract field data from ABC file.")
295ba8275ab4 Make output larger where possible.
Jim Hague <jim.hague@laicatc.com>
parents: 110
diff changeset
137 parser.add_option("-f", "--field", dest="field", default="T",
295ba8275ab4 Make output larger where possible.
Jim Hague <jim.hague@laicatc.com>
parents: 110
diff changeset
138 help="extract the field FIELD", metavar="FIELD")
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
139 parser.add_option("-l", "--latex", dest="latex",
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
140 action="store_true", default=False,
581
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
141 help="convert special characters for LaTeX")
316
eedf65564226 Add --index parameter to allow selection of nth occurence of field.
Jim Hague <jim.hague@acm.org>
parents: 122
diff changeset
142 parser.add_option("-n", "--index", dest="index",
eedf65564226 Add --index parameter to allow selection of nth occurence of field.
Jim Hague <jim.hague@acm.org>
parents: 122
diff changeset
143 action="store", type="int", default=1,
eedf65564226 Add --index parameter to allow selection of nth occurence of field.
Jim Hague <jim.hague@acm.org>
parents: 122
diff changeset
144 help="report INDEXth value [default: %default]",
eedf65564226 Add --index parameter to allow selection of nth occurence of field.
Jim Hague <jim.hague@acm.org>
parents: 122
diff changeset
145 metavar="INDEX")
581
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
146 parser.add_option("-s", "--starts", dest="starts",
326
27f29e8aafea Add --contains flag to abcfield.py.
Jim Hague <jim.hague@acm.org>
parents: 316
diff changeset
147 action="store", type="string", default="",
581
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
148 help="report only if line starts CONTENT and remove CONTENT",
326
27f29e8aafea Add --contains flag to abcfield.py.
Jim Hague <jim.hague@acm.org>
parents: 316
diff changeset
149 metavar="CONTENT")
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
150 (options, args) = parser.parse_args()
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
151
584
696c461c8dc0 Set exit status on abcfield.py to indicate if anything found.
Jim Hague <jim.hague@acm.org>
parents: 581
diff changeset
152 res = False
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
153 if len(args) > 0:
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
154 for arg in args:
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
155 try:
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
156 inf = open(arg, "r")
584
696c461c8dc0 Set exit status on abcfield.py to indicate if anything found.
Jim Hague <jim.hague@acm.org>
parents: 581
diff changeset
157 res = res or process(inf, options)
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
158 finally:
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
159 inf.close()
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
160 else:
584
696c461c8dc0 Set exit status on abcfield.py to indicate if anything found.
Jim Hague <jim.hague@acm.org>
parents: 581
diff changeset
161 res = process(sys.stdin, options)
696c461c8dc0 Set exit status on abcfield.py to indicate if anything found.
Jim Hague <jim.hague@acm.org>
parents: 581
diff changeset
162 sys.exit(int(not res))