5 ly-options: intertext, quote ?
8 check latex parameters, twocolumn
16 lilypond-book --filter="tr '[a-z]' '[A-Z]'" BOOK
19 lilypond-book --filter="convert-ly --no-version --from=1.6.11 -" BOOK
21 classic lilypond-book:
22 lilypond-book --process="lilypond-bin" BOOK.tely
25 @mbinclude foo.itely -> @include foo.itely
33 ################################################################
34 # Users of python modules should include this snippet
35 # and customize variables below.
37 # We'll suffer this path init stuff as long as we don't install our
38 # python packages in <prefix>/lib/pythonx.y (and don't kludge around
39 # it as we do with teTeX on Red Hat Linux: set some environment var
40 # (PYTHONPATH) in profile)
42 # If set, LILYPONDPREFIX must take prevalence
43 # if datadir is not set, we're doing a build and LILYPONDPREFIX
44 import getopt, os, sys
45 datadir = '@local_lilypond_datadir@'
46 if not os.path.isdir (datadir):
47 datadir = '@lilypond_datadir@'
48 if os.environ.has_key ('LILYPONDPREFIX') :
49 datadir = os.environ['LILYPONDPREFIX']
50 while datadir[-1] == os.sep:
53 sys.path.insert (0, os.path.join (datadir, 'python'))
56 #if __name__ == '__main__':
64 program_version = '@TOPLEVEL_VERSION@'
65 program_name = 'lilypond-book'
68 original_dir = os.getcwd ()
71 help_summary = _ ("""Process LilyPond snippets in hybrid HTML, LaTeX or texinfo document. Example usage:
73 lilypond-book --filter="tr '[a-z]' '[A-Z]'" BOOK
74 lilypond-book --filter="convert-ly --no-version --from=2.0.0 -" BOOK
75 lilypond-book --process='lilypond-bin -I include' BOOK
79 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>>',
80 'Han-Wen Nienhuys <hanwen@cs.uu.nl>')
82 option_definitions = [
83 (_ ("EXT"), 'f', 'format', _ ("use output format EXT (texi [default], texi-html, latex, html)")),
84 (_ ("FILTER"), 'F', 'filter', _ ("pipe snippets through FILTER [convert-ly -n -]")),
85 ('', 'h', 'help', _ ("print this help")),
86 (_ ("DIR"), 'I', 'include', _ ("add DIR to include path")),
87 (_ ("COMMAND"), 'P', 'process', _ ("process ly_files using COMMAND FILE...")),
88 (_ ("DIR"), 'o', 'output', _ ("write output to DIR")),
89 ('', 'V', 'verbose', _ ("be verbose")),
90 ('', 'v', 'version', _ ("print version information")),
91 ('', 'w', 'warranty', _ ("show warranty and copyright")),
94 include_path = [ly.abspath (os.getcwd ())]
95 lilypond_binary = os.path.join ('@bindir@', 'lilypond-bin')
97 # only use installed binary when we're installed too.
98 if '@bindir@' == ('@' + 'bindir@') or not os.path.exists (lilypond_binary):
99 lilypond_binary = 'lilypond-bin'
105 latex_filter_cmd = 'latex "\\nonstopmode \input /dev/stdin"'
107 process_cmd = lilypond_binary
108 default_ly_options = { }
114 LINEWIDTH = 'linewidth'
118 PREAMBLE = 'preamble'
120 VERBATIM = 'verbatim'
121 PRINTFILENAME = 'printfilename'
123 # Recognize special sequences in the input
125 # (?P<name>regex) -- assign result of REGEX to NAME
126 # *? -- match non-greedily.
127 # (?m) -- multiline regex: make ^ and $ match at each line
128 # (?s) -- make the dot match all characters including newline
133 'lilypond' : '(?m)(?P<match><lilypond((?P<options>[^:]*):)(?P<code>.*?)/>)',
134 'lilypond_block': r'''(?ms)(?P<match><lilypond(?P<options>[^>]+)?>(?P<code>.*?)</lilypond>)''',
135 'lilypond_file': r'(?m)(?P<match><lilypondfile(?P<options>[^>]+)?>\s*(?P<filename>[^<]+)\s*</lilypondfile>)',
136 'multiline_comment': r"(?sm)\s*(?!@c\s+)(?P<code><!--\s.*?!-->)\s",
137 'singleline_comment': no_match,
138 'verb': r'''(?P<code><pre>.*?</pre>)''',
139 'verbatim': r'''(?s)(?P<code><pre>\s.*?</pre>\s)''',
143 'include': r'(?m)^[^%\n]*?(?P<match>\\input{(?P<filename>[^}]+)})',
144 'lilypond' : r'(?m)^[^%\n]*?(?P<match>\\lilypond\s*(\[(?P<options>.*?)\])?\s*{(?P<code>.*?)})',
145 'lilypond_block': r"(?sm)^[^%\n]*?(?P<match>\\begin\s*(\[(?P<options>.*?)\])?\s*{lilypond}(?P<code>.*?)\\end{lilypond})",
146 'lilypond_file': r'(?m)^[^%\n]*?(?P<match>\\lilypondfile\s*(\[(?P<options>.*?)\])?\s*\{(?P<filename>.+)})',
147 'multiline_comment': no_match,
148 'singleline_comment': r"(?m)^.*?(?P<match>(?P<code>^%.*$\n+))",
149 'verb': r"(?P<code>\\verb(?P<del>.).*?(?P=del))",
150 'verbatim': r"(?s)(?P<code>\\begin\s*{verbatim}.*?\\end{verbatim})",
154 'include': '(?m)^[^%\n]*?(?P<match>@include\s+(?P<filename>\S*))',
155 'lilypond' : '(?m)^(?P<match>@lilypond(\[(?P<options>[^]]*)\])?{(?P<code>.*?)})',
156 'lilypond_block': r'''(?ms)^(?P<match>@lilypond(\[(?P<options>[^]]*)\])?\s(?P<code>.*?)@end lilypond)\s''',
157 'lilypond_file': '(?m)^(?P<match>@lilypondfile(\[(?P<options>[^]]*)\])?{(?P<filename>[^}]+)})',
158 'multiline_comment': r"(?sm)^\s*(?!@c\s+)(?P<code>@ignore\s.*?@end ignore)\s",
159 'singleline_comment': r"(?m)^.*(?P<match>(?P<code>@c([ \t][^\n]*|)\n))",
161 # don't do this: fucks up with @code{@{}
162 # 'verb': r'''(?P<code>@code{.*?})''',
163 'verbatim': r'''(?s)(?P<code>@example\s.*?@end\s+example\s)''',
169 'option-sep' : '\s*',
170 'intertext': r',?\s*intertext=\".*?\"',
173 'intertext': r',?\s*intertext=\".*?\"',
174 'option-sep' : ',\s*',
177 'intertext': r',?\s*intertext=\".*?\"',
178 'option-sep' : ',\s*',
184 'relative': r'''\relative c%(relative_quotes)s''',
188 indent = %(indent)s''',
190 linewidth = %(linewidth)s''',
196 \remove Time_signature_engraver
200 raggedright = ##t''',
204 #(set-global-staff-size %(staffsize)s)''',
211 PRINTFILENAME:'<p><tt><a href="%(base)s.ly">%(filename)s</a></tt></p>',
214 <img align="center" valign="center"
215 border="0" src="%(base)s.png" alt="[picture of music]">''',
222 PRINTFILENAME: '''\\texttt{%(filename)s}
226 OUTPUT: r'''{\parindent 0pt
228 \ifx\preLilyPondExample\preLilyPondExample\fi
231 \ifx\preLilyPondExample\postLilyPondExample\fi
233 VERBATIM: r'''\begin{verbatim}
234 %(verb)s\end{verbatim}
239 PRINTFILENAME: '''@file{%(filename)s}
244 VERBATIM: r'''@example
252 PREAMBLE_LY = r'''%% Generated by %(program_name)s
253 %% Options: [%(option_string)s]
255 \paper {%(paper_string)s
259 FRAGMENT_LY = r'''\score{
260 \notes%(notes_string)s{
265 texi_linewidths = { 'afourpaper': '160 \\mm',
266 'afourwide': '6.5\\in',
267 'afourlatex': '150 \\mm',
268 'smallbook': '5 \\in' ,
269 'letterpaper': '6\\in'}
271 def classic_lilypond_book_compatibility (o):
272 if o == 'singleline':
274 m = re.search ('relative\s*([-0-9])', o)
276 return 'relative=%s' % m.group (1)
277 m = re.match ('([0-9]+)pt', o)
279 return 'staffsize=%s' % m.group (1)
280 m = re.match ('indent=([-.0-9]+)(cm|in|mm|pt)', o)
282 f = float (m.group (1))
283 return 'indent=%f\\%s' % (f, m.group (2))
284 m = re.match ('linewidth=([-.0-9]+)(cm|in|mm|pt)', o)
286 f = float (m.group (1))
287 return 'linewidth=%f\\%s' % (f, m.group (2))
290 def compose_ly (code, options):
292 options += default_ly_options.keys ()
293 vars ().update (default_ly_options)
295 m = re.search (r'''\\score''', code)
296 if not m and (not options \
297 or not 'nofragment' in options \
298 or 'fragment' in options):
299 options.append ('raggedright')
307 option_string = string.join (options, ',')
310 preamble_options = []
312 c = classic_lilypond_book_compatibility (i)
314 ly.warning (_ ("deprecated ly-option used: %s" % i))
315 ly.warning (_ ("compatibility mode translation: %s" \
319 if string.find (i, '=') > 0:
320 key, value = string.split (i, '=')
326 if key in ly_options[NOTES].keys ():
327 notes_options.append (ly_options[NOTES][key])
328 elif key in ly_options[PREAMBLE].keys ():
329 preamble_options.append (ly_options[PREAMBLE][key])
330 elif key in ly_options[PAPER].keys ():
331 paper_options.append (ly_options[PAPER][key])
332 elif key not in ('fragment', 'nofragment', 'printfilename',
333 'relative', 'verbatim', 'texidoc'):
334 ly.warning (_("ignoring unknown ly option: %s") % i)
336 relative_quotes = (",,,", ",,", ",", "", "'", "''", "'''")[relative-3]
337 program_name = __main__.program_name
338 notes_string = string.join (notes_options, '\n ') % vars ()
339 paper_string = string.join (paper_options, '\n ') % vars ()
340 preamble_string = string.join (preamble_options, '\n ') % vars ()
341 return (PREAMBLE_LY + body) % vars ()
345 # use lilypond-bin for latex (.lytex) books,
346 # and lilypond --preview for html, texinfo books?
348 cmd = r'latex "\nonstopmode \input %s"' % file
349 # Ugh. (La)TeX writes progress and error messages on stdout
351 cmd = '(( %s >&2 ) >&- )' % cmd
353 ly.system ('dvips -Ppdf -u+lilypond.map -E -o %s.eps %s' \
356 def find_file (name):
357 for i in include_path:
358 full = os.path.join (i, name)
359 if os.path.exists (full):
361 ly.error (_ ('file not found: %s\n' % name))
365 def verbatim_html (s):
366 return re.sub ('>', '>',
368 re.sub ('&', '&', s)))
370 def verbatim_texinfo (s):
371 return re.sub ('{', '@{',
373 re.sub ('@', '@@', s)))
375 def split_options (option_string):
376 return re.split (format_res[format]['option-sep'], option_string)
380 def replacement_text (self):
382 def is_outdated (self):
385 class Substring (Chunk):
386 def __init__ (self, source, start, end):
390 def replacement_text (self):
391 return self.source [self.start:self.end]
393 class Snippet (Chunk):
394 def __init__ (self, type, match, format):
400 def replacement_text (self):
401 return self.match.group (0)
403 def substring (self, s):
404 return self.match.group (s)
405 def filter_code (self):
408 return `self.__class__` + " type = " + self.type
410 class Include_snippet (Snippet):
411 def processed_filename (self):
412 f = self.substring ('filename')
413 return os.path.splitext (f)[0] + format2ext[format]
415 def replacement_text (self):
416 s = self.match.group (0)
417 f = self.substring ('filename')
419 return re.sub (f, self.processed_filename (), s)
421 class Lilypond_snippet (Snippet):
422 def __init__ (self, type, match, format):
423 Snippet.__init__ (self, type, match, format)
424 os = match.group ('options')
426 self.options = split_options (os)
430 if self.type == 'lilypond_file':
431 name = self.substring ('filename')
432 return open (find_file (name)).read ()
434 return self.substring ('code')
439 return compose_ly (s, self.options)
444 self.hash = abs (hash (self.full_ly ()))
449 return 'lily-%d' % self.get_hash ()
453 outf = open (self.basename () + '.ly', 'w')
454 outf.write (self.full_ly ())
456 def is_outdated (self):
457 base = self.basename ()
458 if os.path.exists (base + '.ly') \
459 and os.path.exists (base + '.tex') \
461 or self.ly () == open (base + '.ly').read ()):
462 # TODO: something smart with target formats
463 # (ps, png) and m/ctimes
468 def replacement_text (self):
469 func = Lilypond_snippet.__dict__ ['output_' + self.format]
472 def output_html (self):
473 base = self.basename ()
474 str = self.output_print_filename (HTML)
475 if VERBATIM in self.options and format == HTML:
476 verb = verbatim_html (self.substring ('code'))
477 str += write (output[HTML][VERBATIM] % vars ())
478 str += (output[HTML][BEFORE]
479 + (output[HTML][OUTPUT] % vars ())
480 + output[HTML][AFTER])
484 def output_latex (self):
485 str = self.output_print_filename (LATEX)
487 base = self.basename ()
488 if VERBATIM in self.options\
490 verb = self.substring ('code')
491 str += (output[LATEX][VERBATIM] % vars ())
493 str += (output[LATEX][BEFORE]
494 + (output[LATEX][OUTPUT] % vars ())
495 + output[LATEX][AFTER])
500 def output_print_filename (self,format):
502 if PRINTFILENAME in self.options:
503 base = self.basename ()
504 filename = self.substring ('filename')
505 str += output[format][PRINTFILENAME] % vars ()
509 def output_texinfo (self):
512 ## Ugh, this breaks texidoc.
513 str = self.output_print_filename (TEXINFO)
515 base = self.basename ()
518 if 'texidoc' in self.options :
519 texidoc = base + '.texidoc'
520 if os.path.exists (texidoc):
521 str += '@include %s\n' % texidoc
524 str += (output[LATEX][BEFORE]
525 + (output[LATEX][OUTPUT] % vars ())
526 + output[LATEX][AFTER])
527 str += ('\n@end tex\n')
529 str += ('\n\n@html\n')
530 str += (output[HTML][BEFORE]
531 + (output[HTML][OUTPUT] % vars ())
532 + output[HTML][AFTER])
533 str += ('\n@end html\n\n') # need par after picture.
535 if VERBATIM in self.options:
536 verb = verbatim_texinfo (self.substring ('code'))
537 str += (output[TEXINFO][VERBATIM] % vars ())
542 snippet_type_to_class = {
543 'lilypond_file' : Lilypond_snippet,
544 'lilypond_block' : Lilypond_snippet,
545 'lilypond' : Lilypond_snippet,
546 'include' : Include_snippet
550 def find_toplevel_snippets (infile, types):
554 res[i] = ly.re.compile (snippet_res[format][i])
558 found = dict ([(t, None) for t in types] )
561 # We want to search for multiple regexes,
562 # without searching the string multiple times for one regex.
564 # Hence, we use earlier results to limit the string portion
566 # Since every part of the string is traversed at most once for
567 # every type of snippet, this is linear.
573 if not found[type] or found[type][0] < index:
575 m = res[type].search (s[index:endex])
580 if snippet_type_to_class.has_key (type):
581 cl = snippet_type_to_class[type]
582 snip = cl (type, m, format)
583 start = index + m.start (0)
584 found[type] = (start, snip)
586 if found[type] and (first == None
587 or found[type][0] < found[first][0]):
590 endex = found[first][0]
593 snippets.append (Substring (s, index, len (s)))
596 (start , snip) = found[first]
597 snippets.append (Substring (s, index, start))
598 snippets.append (snip)
599 index = start + len (snip.match.group (0))
604 def filter_pipe (input, cmd):
606 ly.progress (_ ("Opening filter `%s\'") % cmd)
608 stdin, stdout, stderr = os.popen3 (cmd)
610 status = stdin.close ()
614 output = stdout.read ()
615 status = stdout.close ()
616 error = stderr.read ()
620 signal = 0x0f & status
621 if status or (not output and error):
622 exit_status = status >> 8
623 ly.error (_ ("`%s\' failed (%d)") % (cmd, exit_status))
624 ly.error (_ ("The error log is as follows:"))
625 sys.stderr.write (error)
626 sys.stderr.write (stderr.read ())
635 return filter_pipe (s, filter_cmd)
637 def process_snippets (cmd, snippets):
638 names = filter (lambda x:x, [y.basename () for y in snippets])
640 ly.system (string.join ([cmd] + names))
642 if format == HTML or format == TEXINFO:
644 if os.path.exists (i + '.tex'):
646 ly.make_ps_images (i + '.eps', resolution=110)
648 LATEX_DOCUMENT = r'''
651 \typeout{textwidth=\the\textwidth}
652 \typeout{columnsep=\the\columnsep}
653 \makeatletter\if@twocolumn\typeout{columns=2}\fi\makeatother
656 #need anything else besides textwidth?
657 def get_latex_textwidth (source):
658 m = re.search (r'''(?P<preamble>\\begin\s*{document})''', source)
659 preamble = source[:m.start (0)]
660 latex_document = LATEX_DOCUMENT % vars ()
661 parameter_string = filter_pipe (latex_document, latex_filter_cmd)
664 m = re.search ('columns=([0-9.]*)', parameter_string)
666 columns = string.atoi (m.group (1))
669 m = re.search ('columnsep=([0-9.]*)pt', parameter_string)
671 columnsep = string.atof (m.group (1))
674 m = re.search('textwidth=([0-9.]*)pt', parameter_string)
676 textwidth = string.atof (m.group (1))
678 textwidth = (textwidth - columnsep) / columns
690 '.texinfo' : TEXINFO,
696 #TEXINFO: '.texinfo',
702 def do_file (input_filename):
706 e = os.path.splitext (input_filename)[1]
707 if e in ext2format.keys ():
709 format = ext2format[e]
711 ly.error (_ ("cannot determine format for: %s" \
714 ly.progress (_ ("Reading %s...") % input_filename)
715 if not input_filename or input_filename == '-':
718 ih = open (input_filename)
722 ly.progress (_ ("Dissecting..."))
727 'singleline_comment',
734 if output_name == '-' or not output_name:
735 output_file = sys.stdout
736 output_filename = '-'
738 if not os.path.isdir (output_name):
739 os.mkdir (output_name, 0777)
740 if input_filename == '-':
743 input_base = os.path.splitext (input_filename)[0]
744 input_base = os.path.basename (input_base)
746 output_filename = output_name + '/' + input_base \
748 output_file = open (output_filename, 'w')
749 os.chdir (output_name)
752 chunks = find_toplevel_snippets (ih, snippet_types)
755 global default_ly_options
757 if LINEWIDTH not in default_ly_options.keys ():
759 textwidth = get_latex_textwidth (source)
760 default_ly_options[LINEWIDTH] = '''%.0f\\pt''' % textwidth
761 elif format == TEXINFO:
762 for (k,v) in texi_linewidths.items ():
763 s = chunks[0].replacement_text()
765 default_ly_options[LINEWIDTH] = v
771 outdated = filter (lambda x: x.__class__ == Lilypond_snippet and x.is_outdated (),
773 ly.progress (_ ("Writing snippets..."))
774 map (Lilypond_snippet.write_ly, outdated)
778 ly.progress (_ ("Processing..."))
779 process_snippets (process_cmd, outdated)
781 ly.progress (_ ("All snippets are up to date..."))
784 ly.progress (_ ("Compiling %s...") % output_filename)
787 def process_include (snippet):
788 os.chdir (original_dir)
789 name = snippet.substring ('filename')
790 ly.progress (_ ('Processing include: %s') % name)
796 output_file.writelines ([s.replacement_text () for s in chunks])
799 included_files = [input_filename]
800 def notice_include (target, snip):
801 included_files.append (snip.match.group ('filename'))
802 included_files.append (os.path.join (output_name, snip.processed_filename ()))
804 [notice_include (output_filename, x) for x in
806 ## UGH. how do you do dynamic_cast/typecheck in Python?
807 filter (lambda x: x.__class__ == Include_snippet, chunks)]
809 target = re.sub (r'^\./','', output_filename)
810 open (os.path.split (output_filename)[1] + '.dep', 'w').write ('%s: %s\n' % (target,
811 string.join (included_files)))
813 map (process_include, filter (lambda x: x.__class__ == Include_snippet, chunks))
817 global format, output_name
818 global filter_cmd, process_cmd, verbose_p
820 (sh, long) = ly.getopt_args (option_definitions)
822 (options, files) = getopt.getopt (sys.argv[1:], sh, long)
823 except getopt.error, s:
824 sys.stderr.write ('\n')
825 ly.error (_ ("getopt says: `%s\'" % s))
826 sys.stderr.write ('\n')
836 elif o == '--filter' or o == '-F':
839 elif o == '--format' or o == '-f':
841 if a == 'texi-html' or a == 'texi':
843 elif o == '--help' or o == '-h':
846 elif o == '--include' or o == '-I':
847 include_path.append (os.path.join (original_dir,
849 elif o == '--output' or o == '-o':
851 elif o == '--outdir':
853 elif o == '--process' or o == '-P':
856 elif o == '--version' or o == '-v':
857 ly.identify (sys.stdout)
859 elif o == '--verbose' or o == '-V':
861 elif o == '--warranty' or o == '-w':
869 files = do_options ()
872 process_cmd += string.join ([(' -I %s' % p)
873 for p in include_path])
875 ly.identify (sys.stderr)
876 ly.setup_environment ()
879 if __name__ == '__main__':