14 filter-lilypond-book --filter="tr '[a-z]' '[A-Z]'" BOOK
17 filter-lilypond-book --filter="convert-ly --no-version --from=1.6.11 -" BOOK
19 minimal classic lilypond-book (WIP):
20 filter-lilypond-book --process="lilypond-bin" BOOK.tely
22 (([0-9][0-9])*pt) -> staffsize=\2
29 ################################################################
30 # Users of python modules should include this snippet
31 # and customize variables below.
33 # We'll suffer this path init stuff as long as we don't install our
34 # python packages in <prefix>/lib/pythonx.y (and don't kludge around
35 # it as we do with teTeX on Red Hat Linux: set some environment var
36 # (PYTHONPATH) in profile)
38 # If set, LILYPONDPREFIX must take prevalence
39 # if datadir is not set, we're doing a build and LILYPONDPREFIX
40 import getopt, os, sys
41 datadir = '@local_lilypond_datadir@'
42 if not os.path.isdir (datadir):
43 datadir = '@lilypond_datadir@'
44 if os.environ.has_key ('LILYPONDPREFIX') :
45 datadir = os.environ['LILYPONDPREFIX']
46 while datadir[-1] == os.sep:
49 sys.path.insert (0, os.path.join (datadir, 'python'))
52 #if __name__ == '__main__':
59 program_version = '@TOPLEVEL_VERSION@'
60 #program_name = 'new-book'
61 program_name = 'filter-lilypond-book'
64 original_dir = os.getcwd ()
67 # help_summary = _ ("Process LilyPond snippets in hybrid html, LaTeX or texinfo document")
68 help_summary = _ ("""Process ly snippets from lilypond-book source. Example usage:
70 filter-lilypond-book --filter="tr '[a-z]' '[A-Z]'" BOOK
71 filter-lilypond-book --filter="convert-ly --no-version --from=1.6.11 -" BOOK
74 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>>',
75 'Han-Wen Nienhuys <hanwen@cs.uu.nl>')
77 option_definitions = [
78 (_ ("EXT"), 'f', 'format', _ ("use output format EXT (texi [default], texi-html, latex, html)")),
79 (_ ("FILTER"), 'F', 'filter', _ ("pipe snippets through FILTER [convert-ly -n -]")),
80 ('', 'h', 'help', _ ("print this help")),
81 (_ ("COMMAND"), 'P', 'process', _ ("process ly_files using COMMAND FILE...")),
82 (_ ("FILE"), 'o', 'output', _ ("write output to FILE")),
83 ('', 'V', 'verbose', _ ("be verbose")),
84 ('', 'v', 'version', _ ("print version information")),
85 ('', 'w', 'warranty', _ ("show warranty and copyright")),
88 include_path = [os.getcwd ()]
90 lilypond_binary = os.path.join ('@bindir@', 'lilypond-bin')
92 # only use installed binary when we're installed too.
93 if '@bindir@' == ('@' + 'bindir@') or not os.path.exists (lilypond_binary):
94 lilypond_binary = 'lilypond-bin'
100 latex_filter_cmd = 'latex "\\nonstopmode \input /dev/stdin"'
101 filter_cmd = 'convert-ly --no-version --from=2.0.0 -'
103 #process_cmd = 'convert-ly --no-version --from=2.0.0'
105 default_ly_options = { }
113 VERBATIM = 'verbatim'
114 LINEWIDTH = 'linewidth'
116 ## lilypond-book heritage. to be cleaned
118 ################################################################
119 # Recognize special sequences in the input
122 # Warning: This uses extended regular expressions. Tread with care.
126 # (?P<name>regex) -- assign result of REGEX to NAME
127 # *? -- match non-greedily.
128 # (?m) -- multiline regex: make ^ and $ match at each line
129 # (?s) -- make the dot match all characters including newline
136 'landscape': no_match,
137 'verbatim': r'''(?s)(?P<code><pre>\s.*?</pre>\s)''',
138 'verb': r'''(?P<code><pre>.*?</pre>)''',
139 'lilypond-file': r'(?m)(?P<match><lilypondfile(?P<options>[^>]+)?>\s*(?P<filename>[^<]+)\s*</lilypondfile>)',
140 'lilypond' : '(?m)(?P<match><lilypond((?P<options>[^:]*):)(?P<code>.*?)/>)',
141 'lilypond-block': r'''(?ms)(?P<match><lilypond(?P<options>[^>]+)?>(?P<code>.*?)</lilypond>)''',
142 'option-sep' : '\s*',
143 'intertext': r',?\s*intertext=\".*?\"',
144 'multiline-comment': r"(?sm)\s*(?!@c\s+)(?P<code><!--\s.*?!-->)\s",
145 'singleline-comment': no_match,
147 'multicols': no_match,
148 'ly2dvi': r'(?m)(?P<match><ly2dvifile(?P<options>[^>]+)?>\s*(?P<filename>[^<]+)\s*</ly2dvifile>)',
152 'junkmeinput': r'(?m)^[^%\n]*?(?P<match>\\mbinput{?([^}\t \n}]*))',
153 'junkmeinclude': r'(?m)^[^%\n]*?(?P<match>\\mbinclude{(?P<filename>[^}]+)})',
154 'include': r'(?m)^[^%\n]*?(?P<match>\\input{(?P<filename>[^}]+)})',
155 'option-sep' : ',\s*',
156 'header': r"\n*\\documentclass\s*(\[.*?\])?",
157 'verbatim': r"(?s)(?P<code>\\begin\s*{verbatim}.*?\\end{verbatim})",
158 'verb': r"(?P<code>\\verb(?P<del>.).*?(?P=del))",
159 'lilypond-file': r'(?m)^[^%\n]*?(?P<match>\\lilypondfile\s*(\[(?P<options>.*?)\])?\s*\{(?P<filename>.+)})',
160 'lilypond' : r'(?m)^[^%\n]*?(?P<match>\\lilypond\s*(\[(?P<options>.*?)\])?\s*{(?P<code>.*?)})',
161 'lilypond-block': r"(?sm)^[^%\n]*?(?P<match>\\begin\s*(\[(?P<options>.*?)\])?\s*{lilypond}(?P<code>.*?)\\end{lilypond})",
162 'def-post-re': r"\\def\\postLilyPondExample",
163 'def-pre-re': r"\\def\\preLilyPondExample",
164 'usepackage-graphics': r"\usepackage\s*{graphics}",
165 'intertext': r',?\s*intertext=\".*?\"',
166 'multiline-comment': no_match,
167 'singleline-comment': r"(?m)^.*?(?P<match>(?P<code>^%.*$\n+))",
168 'numcols': r"(?P<code>\\(?P<num>one|two)column)",
169 'multicols': r"(?P<code>\\(?P<be>begin|end)\s*{multicols}({(?P<num>\d+)?})?)",
174 # why do we have distinction between @mbinclude and @include?
177 'junkmeinclude': '(?m)^[^%\n]*?(?P<match>@mbinclude\s+(?P<filename>\S*))',
178 'include': '(?m)^[^%\n]*?(?P<match>@include\s+(?P<filename>\S*))',
181 'landscape': no_match,
182 'verbatim': r'''(?s)(?P<code>@example\s.*?@end example\s)''',
183 'verb': r'''(?P<code>@code{.*?})''',
184 'lilypond-file': '(?m)^(?P<match>@lilypondfile(\[(?P<options>[^]]*)\])?{(?P<filename>[^}]+)})',
185 'lilypond' : '(?m)^(?P<match>@lilypond(\[(?P<options>[^]]*)\])?{(?P<code>.*?)})',
186 'lilypond-block': r'''(?ms)^(?P<match>@lilypond(\[(?P<options>[^]]*)\])?\s(?P<code>.*?)@end lilypond)\s''',
187 'option-sep' : ',\s*',
188 'intertext': r',?\s*intertext=\".*?\"',
189 'multiline-comment': r"(?sm)^\s*(?!@c\s+)(?P<code>@ignore\s.*?@end ignore)\s",
190 'singleline-comment': r"(?m)^.*?(?P<match>(?P<code>@c.*$\n+))",
192 'multicols': no_match,
198 PREAMBLE = 'preamble'
203 #'xrelative': r'''\relative #(ly:make-pitch %(relative)s 0 0)''',
204 'relative': r'''\relative c%(relative_quotes)s''',
208 indent = %(indent)s''',
210 linewidth = %(linewidth)s''',
216 \remove Time_signature_engraver
220 raggedright = ##t''',
224 #(set-global-staff-size %(staffsize)s)''',
233 OUTPUT: r'''<src image="%(base)s.png">''',
241 OUTPUT: r'''{\parindent 0pt
242 \ifx\preLilyPondExample\preLilyPondExample\fi
245 \ifx\preLilyPondExample\postLilyPondExample\fi}
247 VERBATIM: r'''\begin{verbatim}
248 %(code)s\end{verbatim}
259 PREAMBLE_LY = r'''%% Generated by %(program_name)s
260 %% Options: [%(option_string)s]
262 \paper {%(paper_string)s
266 FRAGMENT_LY = r'''\score{
267 \notes%(notes_string)s{
273 def compose_ly (code, option_string):
276 for i in default_ly_options.keys ():
278 vars ()[i] = default_ly_options[i]
280 options = options + string.split (option_string, ',')
282 m = re.search (r'''\\score''', code)
283 if not m and (not options \
284 or not 'nofragment' in options \
285 or 'fragment' in options):
296 preamble_options = []
298 if string.find (i, '=') > 0:
299 key, value = string.split (i, '=')
305 if key in ly_options[NOTES].keys ():
306 notes_options.append (ly_options[NOTES][key])
307 elif key in ly_options[PREAMBLE].keys ():
308 preamble_options.append (ly_options[PREAMBLE][key])
309 elif key in ly_options[PAPER].keys ():
310 paper_options.append (ly_options[PAPER][key])
312 relative_quotes = (",,,", ",,", ",", "", "'", "''", "'''")[relative-3]
313 program_name = __main__.program_name
314 notes_string = string.join (notes_options, '\n ') % vars ()
315 paper_string = string.join (paper_options, '\n ') % vars ()
316 preamble_string = string.join (preamble_options, '\n ') % vars ()
317 return (PREAMBLE_LY + body) % vars ()
321 # use lilypond-bin for latex (.lytex) books,
322 # and lilypond --preview for html, texinfo books?
324 cmd = r'latex "\nonstopmode \input %s"' % file
325 # Ugh. (La)TeX writes progress and error messages on stdout
327 cmd = '(( %s >&2 ) >&- )' % cmd
329 ly.system ('dvips -Ppdf -u+lilypond.map -E -o %s.eps %s' \
332 ## make source, index statics of Snippet?
336 def __init__ (self, type, index, match):
343 return self.index + self.match.start (s)
346 return self.index + self.match.end (s)
348 def substring (self, source, s):
349 return source[self.start (s):self.end (s)]
351 def ly (self, source):
352 if self.type == 'lilypond-block' or self.type == 'lilypond':
353 return compose_ly (self.substring (source, 'code'),
354 self.match.group ('options'))
357 def get_hash (self, source):
359 self.hash = abs (hash (self.substring (source,
363 def basename (self, source):
365 return 'lily-%d' % self.get_hash (source)
368 def write_ly (self, source):
369 h = open (self.basename (source) + '.ly', 'w')
370 h.write (self.ly (source))
373 def output_html (self, source):
374 base = self.basename (source)
375 if VERBATIM in self.match.group ('options'):
376 code = self.substring (source, 'code')
377 h.write (output[HTML][VERBATIM] % vars ())
378 h.write (output[HTML][BEFORE])
379 h.write (output[HTML][OUTPUT] % vars ())
380 h.write (output[HTML][AFTER])
382 def output_latex (self, source):
383 if VERBATIM in self.match.group ('options'):
384 code = self.substring (source, 'code')
385 h.write (output[LATEX][VERBATIM] % vars ())
386 h.write (output[LATEX][BEFORE])
387 base = self.basename (source)
388 # h.write (open (base + '.tex').read ())
389 # output = open (base + '.tex').read ()
390 h.write (output[LATEX][OUTPUT] % vars ())
391 h.write (output[LATEX][AFTER])
393 def output_texinfo (self, source):
395 self.output_latex (source)
396 h.write ('\n@end tex\n')
398 h.write ('\n@html\n')
399 self.output_html (source)
400 h.write ('\n@end html\n')
402 def outdated_p (self, source):
403 base = self.basename (source)
404 if os.path.exists (base + '.ly') \
405 and os.path.exists (base + '.tex') \
406 and (not use_hash_p \
407 or self.ly (source) == open (base + '.ly').read ()):
408 # TODO: something smart with target formats
409 # (ps, png) and m/ctimes
413 def find_snippets (s, type):
414 re = ly.re.compile (re_dict[format][type])
417 m = re.search (s[i:])
419 snippets.append (Snippet (type, i, m))
421 m = re.search (s[i:])
424 def filter_pipe (input, cmd):
426 ly.progress (_ ("Opening filter `%s\'") % cmd)
428 stdin, stdout, stderr = os.popen3 (cmd)
430 status = stdin.close ()
434 output = stdout.read ()
435 status = stdout.close ()
436 error = stderr.read ()
440 signal = 0x0f & status
441 if status or (not output and error):
442 exit_status = status >> 8
443 ly.error (_ ("`%s\' failed (%d)") % (cmd, exit_status))
444 ly.error (_ ("The error log is as follows:"))
445 sys.stderr.write (error)
446 sys.stderr.write (stderr.read ())
455 return filter_pipe (s, filter_cmd)
457 def compare_index (a, b):
458 return a.start (0) - b.start (0)
460 # apply FUNC to every toplevel block in SNIPPETS, ie, enclosed
461 # snippets are skipped. return list with all non-empty return values
464 # Hmm, do we need enclosed snippets at all? Maybe use MAP_SNIPPETS
465 # once and use simple filter/map on that resulting toplevel list iso
466 # silly map_snippets/do_snippets.
467 def map_snippets (source, snippets, func):
472 if i.start (0) < index:
474 # lst.append (func (i, source))
481 # apply FUNC to every toplevel block in SNIPPETS, ie, enclosed
482 # snippets are skipped. return last snippet's index
483 def do_snippets (source, snippets, func):
487 if i.start (0) < index:
491 #index = i.end ('code')
494 def process_snippets (source, snippets, cmd):
495 names = map_snippets (source, snippets, Snippet.basename)
497 ly.system (string.join ([cmd] + names))
499 if format == HTML or format == TEXINFO:
502 ly.make_ps_images (i + '.eps', resolution=110)
504 LATEX_DOCUMENT = r'''
507 \typeout{textwidth=\the\textwidth}
508 \typeout{columnsep=\the\columnsep}
509 \makeatletter\if@twocolumn\typeout{columns=2}\fi\makeatother
512 #need anything else besides textwidth?
513 def get_latex_textwidth (source):
514 m = re.search (r'''(?P<preabmle>\\begin\s*{document})''', source)
515 preamble = source[:m.start (0)]
516 latex_document = LATEX_DOCUMENT % vars ()
517 parameter_string = filter_pipe (latex_document, latex_filter_cmd)
520 m = re.search ('columns=([0-9.]*)', parameter_string)
522 columns = string.atoi (m.group (1))
525 m = re.search ('columnsep=([0-9.]*)pt', parameter_string)
527 columnsep = string.atof (m.group (1))
530 m = re.search('textwidth=([0-9.]*)pt', parameter_string)
532 textwidth = string.atof (m.group (1))
534 textwidth = (textwidth - columnsep) / columns
539 def do_file (input_filename):
553 e = os.path.splitext (input_filename)[1]
554 if e in ext2format.keys ():
555 format = ext2format[e]
557 ly.error (_ ("cannot determine format for: %s" \
563 if input_filename != '-':
564 h = open (input_filename)
567 snippet_types = ('verbatim', 'verb', 'multiline-comment',
568 'lilypond', 'lilypond-block')
570 for i in snippet_types:
571 snippets += find_snippets (source, i)
573 snippets.sort (compare_index)
576 if output_name and output_name != '-':
577 if not os.path.isdir (output_name):
578 os.mkdir (output_name, 0777)
579 if input_filename != '-':
580 h = open (output_name + '/' + input_filename + '.out',
582 os.chdir (output_name)
584 def filter_source (snippet, source):
586 # Hmm, why is verbatim's group called 'code'; rename to 'verb'?
587 #if snippet.match.group ('code'):
589 if snippet.type == 'lilypond' or snippet.type == 'lilypond-block':
590 h.write (source[index:snippet.start ('code')])
591 h.write (run_filter (snippet.substring (source, 'code')))
592 h.write (source[snippet.end ('code'):snippet.end (0)])
594 h.write (source[index:snippet.end (0)])
595 index = snippet.end (0)
599 snippet_output = eval ("Snippet.output_" + format)
600 def compile_output (snippet, source):
602 # Hmm, why is verbatim's group called 'code'; rename to 'verb'?
603 # if snippet.match.group ('code'):
605 if snippet.type == 'lilypond' \
606 or snippet.type == 'lilypond-block':
607 h.write (source[index:snippet.start (0)])
608 snippet_output (snippet, source)
609 index = snippet.end (0)
611 global default_ly_options
612 if format == LATEX and LINEWIDTH not in default_ly_options.keys ():
613 textwidth = get_latex_textwidth (source)
614 default_ly_options[LINEWIDTH] = '''%.0f\pt''' % textwidth
618 index = do_snippets (source, snippets, filter_source)
619 h.write (source[index:])
621 outdated = map_snippets (source, snippets, Snippet.outdated_p)
622 do_snippets (source, snippets, Snippet.write_ly)
623 process_snippets (source, outdated, process_cmd)
624 do_snippets (source, snippets, compile_output)
625 h.write (source[index:])
627 # misusing snippet bit silly?
628 includes = find_snippets (source, 'include')
630 os.chdir (original_dir)
631 sys.stderr.write ('DO: ' + sys.argv[0] + ' OPTIONS ' + i.substring (source, 'filename') + '\n')
634 global format, output_name
635 global filter_cmd, process_cmd, verbose_p
637 (sh, long) = ly.getopt_args (option_definitions)
639 (options, files) = getopt.getopt (sys.argv[1:], sh, long)
640 except getopt.error, s:
641 sys.stderr.write ('\n')
642 ly.error (_ ("getopt says: `%s\'" % s))
643 sys.stderr.write ('\n')
653 elif o == '--version' or o == '-v':
654 ly.identify (sys.stdout)
656 elif o == '--verbose' or o == '-V':
658 elif o == '--filter' or o == '-F':
661 elif o == '--format' or o == '-f':
665 elif o == '--help' or o == '-h':
668 elif o == '--output' or o == '-o':
670 elif o == '--process' or o == '-P':
673 elif o == '--warranty' or o == '-w':
680 files = do_options ()
681 ly.identify (sys.stderr)
682 ly.setup_environment ()
686 if __name__ == '__main__':