3 # The bugs you find are made by Tom Cato Amundsen <tomcato@xoommail.com>
4 # Send patches/questions/bugreports to a mailinglist:
5 # gnu-music-discuss@gnu.org
6 # bug-gnu-music@gnu.org
7 # help-gnu-music@gnu.org
9 # All non-english comments are NOT in swedish, they are norwegian!
11 # * output various stuff either via sys.stderr or sys.stdout, not using print.
12 # * center option (??)
13 # * make mudela-book understand usepackage{geometry}
14 # * check that linewidth set in \paper is not wider than actual linewidth?
15 # * the following fails because mudelabook doesn't care that the
16 # last } after \end{mudela} finishes the marginpar:
21 # * force-verbatim is maybe not that useful since latex fails with footnotes,
22 # marginpars and others
27 # much rewritten by new author. I think the work has been split more
28 # logical between different classes.
31 # - speedup, do all mudela parsing at the same time to avoid multiple
32 # guile startup that takes some seconds on my computer
34 # - fixed default latex fontsize, it should be 10pt not 11pt
35 # - verbatim option no longer visible
36 # - mudela-book produces .dvi output
37 # - change to use castingalgorithm = \Gourlay instead of \Wordwrap. It gives
38 # better result on small linewidths.
39 # - mudela-book-doc.doc rewritten
41 # - junked floating and fragment options, added eps option
42 # - mudela-book will scan the mudela code to find out if it has to add
43 # paper-definition and \score{\notes{...}}
44 # - possible to define commands that look like this: \mudela{ c d e }
45 # - don't produce .dvi output, it was a silly idea...
47 # - removed init/mudela-book-defs.py, \mudela is defined inside mudela-book
48 # - fragment and nonfragment options will override autodetection of type of
49 # in mudela-block (voice contents or complete code)
51 # - fixed verbatim option behaviour: don't show \begin{mudela} \end{mudela}
52 # and show both mudela code and music
53 # - veryverbatim option, same as verbatim but show \begin{mudela}
54 # and \end{mudela}. (saves keystrokes in mudela-book-doc.doc)
55 # - intertext="bla bla bla" option
56 # - mudela-book now understand latex \begin{verbatim}
58 # - bf: \mudela{ \times 2/3{...} }
59 # * \t in \times is not tab character and
60 # * dont treat the first '}' as command ending
62 # - .fly and .sly files in \mudelafile{} are treated as standalone Lilypond.
63 # - Fragments, .fly and .sly files are in \relative mode.
73 program_version = '0.5.3'
78 fontsize_i2a = {11:'eleven', 13:'thirteen', 16:'sixteen',
79 20:'twenty', 26:'twentysix'}
80 fontsize_pt2i = {'11pt':11, '13pt':13, '16pt':16, '20pt':20, '26pt':26}
82 begin_mudela_re = re.compile ('^ *\\\\begin{mudela}')
83 begin_verbatim_re = re.compile ('^ *\\\\begin{verbatim}')
84 end_verbatim_re = re.compile ('^ *\\\\end{verbatim}')
85 extract_papersize_re = re.compile('\\\\documentclass[\[, ]*(\w*)paper[\w ,]*\]\{\w*\}')
86 extract_fontsize_re = re.compile('[ ,\[]*([0-9]*)pt')
87 begin_mudela_opts_re = re.compile('\[[^\]]*\]')
88 end_mudela_re = re.compile ('^ *\\\\end{mudela}')
89 section_re = re.compile ('\\\\section')
90 chapter_re = re.compile ('\\\\chapter')
91 input_re = re.compile ('^\\\\input{([^}]*)')
92 include_re = re.compile ('^\\\\include{([^}]*)')
93 begin_document_re = re.compile ('^ *\\\\begin{document}')
94 documentclass_re = re.compile('\\\\documentclass')
95 twocolumn_re = re.compile('\\\\twocolumn')
96 onecolumn_re = re.compile('\\\\onecolumn')
97 mudela_file_re = re.compile('\\\\mudelafile{([^}]+)}')
98 file_ext_re = re.compile('.+\\.([^.}]+$)')
99 preMudelaExample_re = re.compile('\\\\def\\\\preMudelaExample')
100 postMudelaExample_re = re.compile('\\\\def\\\\postMudelaExample')
101 boundingBox_re = re.compile('%%BoundingBox: ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)')
102 intertext_re = re.compile("intertext=\"([^\"]*)\"")
104 def file_exist_b(name):
112 def ps_dimention(fname):
114 lines = fd.readlines()
116 s = boundingBox_re.search(line)
119 return (int(s.groups()[2])-int(s.groups()[0]),
120 int(s.groups()[3])-int(s.groups()[1]))
123 def find_file (name):
124 for a in include_path:
126 nm = os.path.join (a, name)
136 class SomethingIsSeriouslyBroken:
139 def file_mtime (name):
140 return os.stat (name)[8] #mod time
142 def need_recompile_b(infile, outfile):
143 indate = file_mtime (infile)
145 outdate = file_mtime (outfile)
146 return indate > outdate
151 # executes os.system(command) if infile is newer than
152 # outfile or outfile don't exist
154 def compile (command, workingdir, infile, outfile):
155 "Test if infile is newer than outfile. If so, cd to workingdir"
156 "and execute command"
157 indate = file_mtime (workingdir+infile)
159 outdate = file_mtime (workingdir+outfile)
160 recompile = indate > outdate
166 sys.stderr.write ('invoking `%s\'\n' % command)
168 status = os.system (command)
170 status = os.system ('cd %s; %s' %(workingdir, command))
180 1: {'a4':{10: 345, 11: 360, 12: 390},
181 'a5':{10: 276, 11: 276, 12: 276},
182 'b5':{10: 345, 11: 356, 12: 356},
183 'letter':{10: 345, 11: 360, 12: 390},
184 'legal': {10: 345, 11: 360, 12: 390},
185 'executive':{10: 345, 11: 360, 12: 379}},
186 2: {'a4':{10: 167, 11: 175, 12: 190},
187 'a5':{10: 133, 11: 133, 12: 133},
188 'b5':{10: 167, 11: 173, 12: 173},
189 'letter':{10: 167, 11: 175, 12: 190},
190 'legal':{10: 167, 11: 175, 12: 190},
191 'executive':{10: 167, 11: 175, 12: 184}}}
192 # >0 --> force all mudela to this pt size
193 self.force_mudela_fontsize = 0
194 self.force_verbatim_b = 0
196 'mudela-fontsize' : {'init': 16},
197 'papersize' : {'init': 'a4'},
198 'num-column' : {'init': 1},
199 'tex-fontsize' : {'init': 10}
201 def clear_for_new_file(self):
202 for var in self.__data.keys():
203 self.__data[var] = {'init': self.__data[var]['init']}
204 def clear_for_new_block(self):
205 for var in self.__data.keys():
206 if self.__data[var].has_key('block'):
207 del self.__data[var]['block']
208 def __get_variable(self, var):
209 if self.__data[var].has_key('block'):
210 return self.__data[var]['block']
211 elif self.__data[var].has_key('file'):
212 return self.__data[var]['file']
214 return self.__data[var]['init']
215 def setPapersize(self, size, requester):
216 self.__data['papersize'][requester] = size
217 def getPapersize(self):
218 return self.__get_variable('papersize')
219 def setMudelaFontsize(self, size, requester):
220 self.__data['mudela-fontsize'][requester] = size
221 def getMudelaFontsize(self):
222 if self.force_mudela_fontsize:
223 return self.force_mudela_fontsize
224 return self.__get_variable('mudela-fontsize')
225 def setTexFontsize(self, size, requester):
226 self.__data['tex-fontsize'][requester] = size
227 def getTexFontsize(self):
228 return self.__get_variable('tex-fontsize')
229 def setNumColumn(self, num, requester):
230 self.__data['num-column'][requester] = num
231 def getNumColumn(self):
232 return self.__get_variable('num-column')
233 def getLineWidth(self):
234 return self.__linewidth[self.getNumColumn()][self.getPapersize()][self.getTexFontsize()]
238 def __init__ (self, basename):
239 self.basename = basename
240 self.temp_filename = "%s/%s" %(outdir, 'mudela-temp.ly')
241 self.file = open (self.temp_filename, 'w')
244 self.graphic_type = 'tex'
245 self.code_type = 'unknown'
246 self.code_type_override = None
247 def write (self, line):
248 # match only if there is nothing but whitespace before \begin HACK
249 if re.search('^\s*\\\\begin{mudela}', line):
250 self.scan_begin_statement(line)
252 if self.code_type == 'unknown':
253 if re.search('^\s*\\\\score', line) or \
254 re.search('^\s*\\\\paper', line) or \
255 re.search('^\s*\\\\header', line) or \
256 re.search('^\s*[A-Za-z]*\s*=', line):
257 self.code_type = 'ly'
258 self.__lines.append(line)
259 def scan_begin_statement(self, line):
260 r = begin_mudela_opts_re.search(line)
263 optlist = re.compile('[\s,]*').split(o)
266 if 'fragment' in optlist:
267 self.code_type_override = 'sly'
268 if 'nonfragment' in optlist:
269 self.code_type_override = 'ly'
271 self.graphic_type = 'eps'
272 for pt in fontsize_pt2i.keys():
274 Props.setMudelaFontsize(fontsize_pt2i[pt], 'block')
275 def write_red_tape(self):
276 self.file.write ('\\include \"paper%d.ly\"\n' \
277 % Props.getMudelaFontsize())
279 s = fontsize_i2a[Props.getMudelaFontsize()]
280 if self.code_type == 'sly':
281 linewidth_str = 'linewidth = -1.\cm;'
282 self.code_type = 'fly'
284 linewidth_str = 'linewidth = %i.\\pt;' % Props.getLineWidth()
285 self.file.write("\\paper {"
288 + "castingalgorithm = \Gourlay; \n}")
289 #+ "castingalgorithm = \Wordwrap; indent = 2.\cm; \n}")
290 if self.code_type == 'fly':
291 self.file.write('\\score{\n\\notes\\relative c{')
293 if self.code_type == 'unknown':
294 self.code_type = 'fly'
295 if self.code_type_override:
296 self.code_type = self.code_type_override
297 self.write_red_tape()
298 for l in self.__lines:
300 if self.code_type == 'fly':
301 self.file.write('}}')
305 inf = outdir + self.basename + '.ly'
306 outf = outdir + self.basename + '.tex'
307 if not os.path.isfile(inf):
310 status = os.system ('diff -q %s %s' % (self.temp_filename, inf))
312 os.rename (self.temp_filename, inf)
314 recompile_b = need_recompile_b(inf, outf)
316 out_files.append((self.graphic_type, inf))
319 def insert_me_string(self):
320 "Returns a string that can be used directly in latex."
321 if self.graphic_type == 'tex':
322 return ['tex', self.basename]
323 elif self.graphic_type == 'eps':
324 return ['eps', self.basename]
326 raise SomethingIsSeriouslyBroken
329 def __init__ (self, name):
330 self.output_fn = '%s/%s' % (outdir, name)
332 def open_verbatim (self, line, level):
333 self.__lines.append('\\begin{verbatim}\n')
335 s = re.sub('veryverbatim[\s,]*', '', line)
336 s = re.sub('intertext=\"([^\"]*)\"[\s,]*', '', s)
337 s = re.sub(',\s*\]', ']', s)
338 s = re.sub('\[\]', '', s)
339 self.__lines.append(s);
340 def close_verbatim (self):
341 self.__lines.append('\\end{verbatim}\n')
343 self.__lines.append(s)
344 def create_graphics(self):
347 for line in self.__lines:
348 if type(line)==type([]):
351 if need_recompile_b(outdir+g[1]+'.ly', outdir+g[1]+'.tex'):
352 s = s + ' ' + g[1]+'.ly'
354 e = os.system('cd %s; lilypond %s' %(outdir, s))
356 print "error: lilypond exited with value", e
360 compile('tex %s' % g[1]+'.tex', outdir, g[1]+'.tex', g[1]+'.dvi')
361 compile('dvips -E -o %s %s' %(g[1]+'.eps', g[1]+'.dvi'), outdir,
362 g[1]+'.dvi', g[1]+'.eps')
363 def write_outfile(self):
364 file = open(self.output_fn+'.latex', 'w')
365 file.write('% Created by mudela-book\n')
366 for line in self.__lines:
367 if type(line)==type([]):
369 file.write('\\preMudelaExample\\input %s\n\postMudelaExample '\
370 # TeX applies the prefix of the main source automatically.
372 # % (outdir+line[1]+'.tex'))
374 ps_dim = ps_dimention(outdir+line[1]+'.eps')
375 file.write('\\parbox{%ipt}{\includegraphics{%s}}\n' \
376 % (ps_dim[0], line[1]+'.eps'))
377 # % (ps_dim[0], outdir+line[1]+'.eps'))
382 # given parameter s="\mudela[some options]{CODE} some text and commands"
383 # it returns a tuple:
385 # where the last number is the index of the ending '}'
386 def extract_command(s):
390 for idx in range(len(s)):
392 if not start_found_b:
398 if (start_found_b == 1) and (count == 0):
400 return s[start+1:idx], idx
403 def __init__ (self, filename):
404 for fn in [filename, filename+'.tex', filename+'.doc']:
406 self.infile = open (fn)
413 def get_lines (self):
414 lines = self.infile.readlines ()
415 (retlines, retdeps) = ([],[self.filename])
417 r_inp = input_re.search (line)
418 r_inc = include_re.search (line)
420 # Filename rules for \input :
422 # 1. will search for file with exact that name (tex-input.my will be found)
423 # 2. will search for file with .tex ext, (tex-input.my
424 # will find tex-input.my.tex)
425 # input: with .tex ext
426 # 1. will find exact match
428 # Filename rules for \include :
429 # 1. will only find files with name given to \include + .tex ext
432 t = Tex_input (r_inp.groups()[0])
434 retlines = retlines + ls[0]
435 retdeps = retdeps + ls[1]
437 print "warning: can't find %s, let's hope latex will" \
439 retlines.append (line)
442 t = Tex_input (r_inc.groups()[0]+'.tex')
444 ls[0].insert(0, '\\newpage\n')
445 ls[0].append('\\newpage\n')
446 retlines = retlines + ls[0]
447 retdeps = retdeps + ls[1]
449 print "warning: can't find %s, let's hope latex will" \
451 retlines.append (line)
453 # This code should be rewritten, it looks terrible
454 r_mud = defined_mudela_cmd_re.search(line)
457 ss = "\\\\verb(?P<xx>[^a-zA-Z])\s*\\\\%s\s*(?P=xx)" \
458 % re.escape(r_mud.group()[1:])
459 # just append the line if the command is inside \verb|..|
460 if re.search(ss, line):
461 retlines.append(line)
464 opts = r_mud.groups()[2]
465 cmd_start_idx = r_mud.span()[0]
466 if cmd_start_idx > 0:
467 retlines.append(line[:cmd_start_idx])
469 cmd_data, rest_idx = extract_command(line[cmd_start_idx:])
470 rest_idx = rest_idx + cmd_start_idx + 1
476 v = string.split(defined_mudela_cmd[r_mud.groups()[0]], '\n')
478 l = string.replace(l, '\\fontoptions', opts)
479 l = string.replace(l, '\\maininput', cmd_data)
481 r_mud = defined_mudela_cmd_re.search(line[rest_idx:])
487 retlines.append(line[rest_idx:])
489 line = line[rest_idx:]
491 retlines.append (line)
492 return (retlines, retdeps)
495 class Main_tex_input(Tex_input):
496 def __init__ (self, name, outname):
498 Tex_input.__init__ (self, name) # ugh
499 self.outname = outname
503 self.mudtex = Tex_output (self.outname)
507 # set to 'mudela' when we are processing mudela code,
508 # both verbatim and graphic-to-be
510 def set_sections (self, l):
511 if section_re.search (l):
512 self.section = self.section + 1
513 if chapter_re.search (l):
515 self.chapter = self.chapter + 1
517 def gen_basename (self):
518 return '%s-%d.%d.%d' % (self.outname, self.chapter,
519 self.section, self.fine_count)
520 def extract_papersize_from_documentclass(self, line):
521 pre = extract_papersize_re.search(line)
524 return pre.groups()[0]
525 def extract_fontsize_from_documentclass(self, line):
526 r = extract_fontsize_re.search(line)
528 return int(r.groups()[0])
530 preMudelaDef = postMudelaDef = 0
531 (lines, self.deps) = self.get_lines ()
535 if documentclass_re.search (line):
536 p = self.extract_papersize_from_documentclass (line)
538 Props.setPapersize(p, 'file')
539 f = self.extract_fontsize_from_documentclass (line)
541 Props.setTexFontsize (f, 'file')
542 elif twocolumn_re.search (line):
543 Props.setNumColumn (2, 'file')
544 elif onecolumn_re.search (line):
545 Props.setNumColumn (1, 'file')
546 elif preMudelaExample_re.search (line):
548 elif postMudelaExample_re.search (line):
550 elif begin_verbatim_re.search (line):
552 elif end_verbatim_re.search (line):
554 elif begin_document_re.search (line):
556 self.mudtex.write ('\\def\\preMudelaExample{}\n')
557 if not postMudelaDef:
558 self.mudtex.write ('\\def\\postMudelaExample{}\n')
560 elif mudela_file_re.search(line):
561 r = mudela_file_re.search(line)
563 self.mudela = Mudela_output(self.gen_basename())
565 full_path = find_file (fn)
567 print 'error: can\'t find file `%s\'.' % fn
571 r = file_ext_re.search(fn)
573 self.code_type = r.group(1)
575 f = open (full_path, 'r')
576 lines =f.readlines ()
578 self.mudela.write (x)
579 stat =self.mudela.close ()
581 print "(File %s needs recompiling)\n" % full_path
582 self.mudtex.write (self.mudela.insert_me_string())
583 self.deps.append (full_path)
586 self.fine_count = self.fine_count + 1
588 elif begin_mudela_re.search (line) and not latex_verbatim:
589 Props.clear_for_new_block()
591 if self.mode == 'mudela':
594 r = begin_mudela_opts_re.search (line)
596 o = r.group()[1:][:-1]
597 optlist = re.compile('[ ,]*').split(o)
598 m = intertext_re.search(r.group())
600 self.intertext = m.groups()[0]
602 self.intertext = None
605 if ('veryverbatim' in optlist):
607 elif ('verbatim' in optlist) or (Props.force_verbatim_b):
612 self.mudtex.open_verbatim (line, self.verbatim)
613 self.mudela = Mudela_output (self.gen_basename ())
614 self.mudela.write (line)
616 elif end_mudela_re.search (line) and not latex_verbatim:
618 if self.mode != 'mudela':
622 if self.verbatim == 2:
623 self.mudtex.write (line)
624 self.mudtex.close_verbatim ()
626 if self.verbatim and self.intertext:
627 self.mudtex.write(self.intertext)
628 self.mudtex.write (self.mudela.insert_me_string())
631 self.fine_count = self.fine_count + 1
636 if self.mode == 'mudela':
637 self.mudela.write (line)
639 self.mudtex.write (line)
641 self.mudtex.write (line)
642 self.set_sections(line)
643 self.mudtex.create_graphics()
644 self.mudtex.write_outfile()
649 sys.stdout.write("""Usage: mudela-book [options] FILE\n
650 Generate hybrid LaTeX input from Latex + mudela
652 -h, --help print this help
653 -d, --outdir=DIR directory to put generated files
654 -o, --outname=FILE prefix for filenames
655 --default-mudela-fontsize=??pt default fontsize for music
656 --force-mudela-fontsize=??pt force fontsize for all inline mudela
657 --force-verbatim make all mudela verbatim\n
658 --dependencies write dependencies
659 --include include path
660 --init mudela-book initfile
666 def write_deps (fn, out, deps):
667 out_fn = os.path.join (outdir, fn)
669 print 'writing `%s\'\n' % out_fn
671 f = open (out_fn, 'w')
672 target = re.sub (os.sep + os.sep, os.sep, os.path.join (outdir, out + '.latex'))
673 f.write ('%s: %s\n'% (target,
674 reduce (lambda x,y: x + ' '+ y, deps)))
678 sys.stderr.write ('This is %s version %s\n' % ('mudela-book', program_version))
681 global outdir, initfile, defined_mudela_cmd, defined_mudela_cmd_re
684 (options, files) = getopt.getopt(
685 sys.argv[1:], 'hd:o:I:', ['outdir=', 'outname=',
686 'default-mudela-fontsize=',
687 'force-mudela-fontsize=',
688 'help', 'dependencies', 'include=',
689 'force-verbatim', 'init='])
690 except getopt.error, msg:
698 if o == '--include' or o == '-I':
699 include_path.append (a)
700 elif o == '--outname' or o == '-o':
703 print "Mudela-book is confused by --outname on multiple files"
706 elif o == '--outdir' or o == '-d':
708 elif o == '--help' or o == '-h':
710 elif o == '--dependencies':
712 elif o == '--default-mudela-fontsize':
713 if not fontsize_pt2i.has_key(a):
714 print "Error: illegal fontsize:", a
715 print " accepted fontsizes are: 11pt, 13pt, 16pt, 20pt, 26pt"
717 Props.setMudelaFontsize(fontsize_pt2i[a], 'init')
718 elif o == '--force-mudela-fontsize':
719 if not fontsize_pt2i.has_key(a):
720 print "Error: illegal fontsize:", a
721 print " accepted fontsizes are: 11pt, 13pt, 16pt, 20pt, 26pt"
723 Props.force_mudela_fontsize = fontsize_pt2i[a]
724 elif o == '--force-verbatim':
725 Props.force_verbatim_b = 1
728 if outdir[-1:] != '/':
729 outdir = outdir + '/'
731 # r""" ... """ means: leave escape seqs alone.
732 defined_mudela_cmd = {'mudela': r"""
733 \begin{mudela}[eps \fontoptions]
747 defined_mudela_cmd[i] = d[i]
750 c = string.join (defined_mudela_cmd.keys(), '|')
752 defined_mudela_cmd_re = re.compile("\\\\(%s)(\[(\d*pt)\])*{([^}]*)}" %c)
754 if not os.path.isdir(outdir):
755 os.system('mkdir %s' % outdir)
757 for input_filename in files:
758 Props.clear_for_new_file()
762 my_outname = os.path.basename(os.path.splitext(input_filename)[0])
763 my_depname = my_outname + '.dep'
764 inp = Main_tex_input (input_filename, my_outname)
767 write_deps (my_depname, my_outname, inp.deps)