new file.
[lilypond.git] / buildscripts / new-chords.py
blob12bedf629556f30e6a691b69606c24a2fd5fc1f5
2 # to nwe chord syntax.
3 import re
4 import string
5 import sys
6 import getopt
7 import os
10 def sub_chord (m):
11 str = m.group(1)
13 origstr = '<%s>' % str
14 if re.search (r'\\\\', str):
15 return origstr
17 if re.search (r'\\property', str):
18 return origstr
20 if re.match (r'^\s*\)?\s*\\[a-zA-Z]+', str):
21 return origstr
23 durs = []
24 def sub_durs (m):
25 durs.append(m.group(2))
26 return m.group (1)
28 str = re.sub ("([a-z]+[,'!? ]*)([0-9.]+)", sub_durs, str)
29 dur_str = ''
31 for d in durs:
32 if dur_str == '':
33 dur_str = d
34 if dur_str <> d:
35 return '<%s>' % m.group (1)
37 pslur_strs = ['']
38 dyns = ['']
39 slur_strs = ['']
41 last_str = ''
42 while last_str <> str:
43 last_str = str
44 def sub_dyn_end (m):
45 dyns.append (' -\!')
46 return ' ' + m.group(2)
48 str = re.sub (r'(\\!)\s*([a-z]+)', sub_dyn_end, str)
49 def sub_slurs(m):
50 if '-)' not in slur_strs:
51 slur_strs.append ( '-)')
52 return m.group(1)
53 def sub_p_slurs(m):
54 if '-\)' not in slur_strs:
55 slur_strs.append ( '-\)')
56 return m.group(1)
57 str = re.sub (r"\)[ ]*([a-z]+)", sub_slurs, str)
58 str = re.sub (r"\\\)[ ]*([a-z]+)", sub_p_slurs, str)
59 def sub_begin_slurs(m):
60 if '-(' not in slur_strs:
61 slur_strs.append ( '-(')
62 return m.group(1)
63 str = re.sub (r"([a-z]+[,'!?0-9 ]*)\(", sub_begin_slurs, str)
64 def sub_begin_p_slurs(m):
65 if '-\(' not in slur_strs:
66 slur_strs.append ( '-\(')
67 return m.group(1)
69 str = re.sub (r"([a-z]+[,'!?0-9 ]*)\\\(", sub_begin_p_slurs, str)
71 def sub_dyns (m):
72 s = m.group(0)
73 if s == '@STARTCRESC@':
74 slur_strs.append ("-\\<")
75 elif s == '@STARTDECRESC@':
76 slur_strs.append ("-\\>")
77 elif s == r'-?\\!':
78 slur_strs.append ('-\\!')
79 return ''
81 str = re.sub (r'@STARTCRESC@', sub_dyns, str)
82 str = re.sub (r'-?\\!', sub_dyns, str)
84 def sub_articulations (m):
85 a = m.group(1)
86 if a not in slur_strs:
87 slur_strs.append (a)
88 return ''
90 str = re.sub (r"([_^-]\@ACCENT\@)", sub_articulations, str)
91 str = re.sub (r"([_^-]\\[a-z]+)", sub_articulations, str)
92 str = re.sub (r"([_^-][>_.+|^-])", sub_articulations, str)
94 def sub_pslurs(m):
95 slur_strs.append ( ' -\\)')
96 return m.group(1)
97 str = re.sub (r"\\\)[ ]*([a-z]+)", sub_pslurs, str)
99 suffix = string.join (slur_strs, '') + string.join (pslur_strs, '') \
100 + string.join (dyns, '')
102 return '@STARTCHORD@%s@ENDCHORD@%s%s' % (str , dur_str, suffix)
107 simend = '}'
108 simstart = "\n\\simultaneous {"
109 chordstart = '<'
110 chordend = '>'
112 old_syntax = 1
114 if old_syntax:
115 simend = '>'
116 simstart = "<"
117 chordstart = '<<'
118 chordend = '>>'
121 marker_str = '%% new-chords-done %%'
123 def sub_chords (str):
124 if re.search (marker_str,str):
125 return str
127 str= re.sub (r'\\<', '@STARTCRESC@', str)
128 str= re.sub (r'\\>', '@STARTDECRESC@', str)
129 str= re.sub (r'([_^-])>', r'\1@ACCENT@', str)
130 str = re.sub ('<([^<>{}]+)>', sub_chord, str)
132 str = re.sub ('<([^?])', r'%s\1' % simstart, str)
133 str = re.sub ('>([^?])', r'%s\1' % simend, str)
134 str = re.sub ('@STARTCRESC@', r'\\<', str)
135 str = re.sub ('@STARTDECRESC@', r'\\>' ,str)
136 str = re.sub (r'\\context *Voice *@STARTCHORD@', '@STARTCHORD@', str)
137 str = re.sub ('@STARTCHORD@', chordstart, str)
138 str = re.sub ('@ENDCHORD@', chordend, str)
139 str = re.sub (r'@ACCENT@', '>', str)
140 return str
142 (opts, files)= getopt.getopt( sys.argv[1:], 'e',['edit'])
143 edit = 0
144 for (o,a) in opts:
145 if o == '-e' or o == '--edit':
146 edit = 1
148 for a in files:
149 str = open (a).read()
150 if re.search (marker_str, str):
151 continue
153 sys.stderr.write ("processing %s\n" %a)
155 str = sub_chords (str) + marker_str + '\n'
157 if edit:
158 open (a + '.NEW', 'w').write (str)
159 os.rename (a, a + '~')
160 os.rename (a + '.NEW', a)
161 else:
162 print str