5 ly-options: intertext, quote ?
8 check latex parameters, twocolumn
16 filter-lilypond-book --filter="tr '[a-z]' '[A-Z]'" BOOK
19 filter-lilypond-book --filter="convert-ly --no-version --from=1.6.11 -" BOOK
21 classic lilypond-book:
22 filter-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__':
63 program_version = '@TOPLEVEL_VERSION@'
64 #program_name = 'new-book'
65 program_name = 'filter-lilypond-book'
68 original_dir = os.getcwd ()
71 # help_summary = _ ("Process LilyPond snippets in hybrid html, LaTeX or texinfo document")
72 help_summary = _ ("""Process ly snippets from lilypond-book source. Example usage:
74 filter-lilypond-book --filter="tr '[a-z]' '[A-Z]'" BOOK
75 filter-lilypond-book --filter="convert-ly --no-version --from=1.6.11 -" BOOK
78 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>>',
79 'Han-Wen Nienhuys <hanwen@cs.uu.nl>')
81 option_definitions = [
82 (_ ("EXT"), 'f', 'format', _ ("use output format EXT (texi [default], texi-html, latex, html)")),
83 (_ ("FILTER"), 'F', 'filter', _ ("pipe snippets through FILTER [convert-ly -n -]")),
84 ('', 'h', 'help', _ ("print this help")),
85 (_ ("DIR"), 'I', 'include', _ ("add DIR to include path")),
86 (_ ("COMMAND"), 'P', 'process', _ ("process ly_files using COMMAND FILE...")),
87 (_ ("FILE"), 'o', 'output', _ ("write output to FILE")),
88 ('', 'V', 'verbose', _ ("be verbose")),
89 ('', 'v', 'version', _ ("print version information")),
90 ('', 'w', 'warranty', _ ("show warranty and copyright")),
91 ('', 'M', 'dependencies', _ ("TODO")),
92 (_ ("DIR"), '', 'outdir', _ ("DEPRECATED, use --output")),
95 include_path = [ly.abspath (os.getcwd ())]
97 lilypond_binary = os.path.join ('@bindir@', 'lilypond-bin')
99 # only use installed binary when we're installed too.
100 if '@bindir@' == ('@' + 'bindir@') or not os.path.exists (lilypond_binary):
101 lilypond_binary = 'lilypond-bin'
107 latex_filter_cmd = 'latex "\\nonstopmode \input /dev/stdin"'
108 ##filter_cmd = 'convert-ly --no-version --from=2.0.0 -'
111 #process_cmd = 'convert-ly --no-version --from=2.0.0'
113 # process_cmd = 'lilypond-bin'
114 process_cmd = 'lilypond-bin -I ../../../input/test'
115 default_ly_options = { }
123 VERBATIM = 'verbatim'
124 LINEWIDTH = 'linewidth'
126 ## lilypond-book heritage. to be cleaned
128 ################################################################
129 # Recognize special sequences in the input
132 # Warning: This uses extended regular expressions. Tread with care.
136 # (?P<name>regex) -- assign result of REGEX to NAME
137 # *? -- match non-greedily.
138 # (?m) -- multiline regex: make ^ and $ match at each line
139 # (?s) -- make the dot match all characters including newline
145 #'landscape': no_match,
146 #'ly2dvi': r'(?m)(?P<match><ly2dvifile(?P<options>[^>]+)?>\s*(?P<filename>[^<]+)\s*</ly2dvifile>)',
147 #'multicols': no_match,
148 #'numcols': no_match,
150 'lilypond' : '(?m)(?P<match><lilypond((?P<options>[^:]*):)(?P<code>.*?)/>)',
151 'lilypond-block': r'''(?ms)(?P<match><lilypond(?P<options>[^>]+)?>(?P<code>.*?)</lilypond>)''',
152 'lilypond-file': r'(?m)(?P<match><lilypondfile(?P<options>[^>]+)?>\s*(?P<filename>[^<]+)\s*</lilypondfile>)',
153 'multiline-comment': r"(?sm)\s*(?!@c\s+)(?P<code><!--\s.*?!-->)\s",
154 'singleline-comment': no_match,
155 'verb': r'''(?P<code><pre>.*?</pre>)''',
156 'verbatim': r'''(?s)(?P<code><pre>\s.*?</pre>\s)''',
160 #'def-post-re': r"\\def\\postLilyPondExample",
161 #'def-pre-re': r"\\def\\preLilyPondExample",
162 #'header': r"\n*\\documentclass\s*(\[.*?\])?",
164 #'multicols': r"(?P<code>\\(?P<be>begin|end)\s*{multicols}({(?P<num>\d+)?})?)",
165 #'numcols': r"(?P<code>\\(?P<num>one|two)column)",
166 #'usepackage-graphics': r"\usepackage\s*{graphics}",
167 'include': r'(?m)^[^%\n]*?(?P<match>\\input{(?P<filename>[^}]+)})',
168 'lilypond' : r'(?m)^[^%\n]*?(?P<match>\\lilypond\s*(\[(?P<options>.*?)\])?\s*{(?P<code>.*?)})',
169 'lilypond-block': r"(?sm)^[^%\n]*?(?P<match>\\begin\s*(\[(?P<options>.*?)\])?\s*{lilypond}(?P<code>.*?)\\end{lilypond})",
170 'lilypond-file': r'(?m)^[^%\n]*?(?P<match>\\lilypondfile\s*(\[(?P<options>.*?)\])?\s*\{(?P<filename>.+)})',
171 'multiline-comment': no_match,
172 'singleline-comment': r"(?m)^.*?(?P<match>(?P<code>^%.*$\n+))",
173 'verb': r"(?P<code>\\verb(?P<del>.).*?(?P=del))",
174 'verbatim': r"(?s)(?P<code>\\begin\s*{verbatim}.*?\\end{verbatim})",
179 #'landscape': no_match,
180 #'multicols': no_match,
181 #'numcols': no_match,
182 'include': '(?m)^[^%\n]*?(?P<match>@include\s+(?P<filename>\S*))',
183 'lilypond' : '(?m)^(?P<match>@lilypond(\[(?P<options>[^]]*)\])?{(?P<code>.*?)})',
184 'lilypond-block': r'''(?ms)^(?P<match>@lilypond(\[(?P<options>[^]]*)\])?\s(?P<code>.*?)@end lilypond)\s''',
185 'lilypond-file': '(?m)^(?P<match>@lilypondfile(\[(?P<options>[^]]*)\])?{(?P<filename>[^}]+)})',
186 'multiline-comment': r"(?sm)^\s*(?!@c\s+)(?P<code>@ignore\s.*?@end ignore)\s",
187 'singleline-comment': r"(?m)^.*?(?P<match>(?P<code>@c([ \t][^\n]*|)\n))",
188 'verb': r'''(?P<code>@code{.*?})''',
189 'verbatim': r'''(?s)(?P<code>@example\s.*?@end example\s)''',
195 'option-sep' : '\s*',
196 'intertext': r',?\s*intertext=\".*?\"',
199 'intertext': r',?\s*intertext=\".*?\"',
200 'option-sep' : ',\s*',
203 'intertext': r',?\s*intertext=\".*?\"',
204 'option-sep' : ',\s*',
209 PREAMBLE = 'preamble'
214 #'xrelative': r'''\relative #(ly:make-pitch %(relative)s 0 0)''',
215 'relative': r'''\relative c%(relative_quotes)s''',
219 indent = %(indent)s''',
221 linewidth = %(linewidth)s''',
227 \remove Time_signature_engraver
231 raggedright = ##t''',
235 #(set-global-staff-size %(staffsize)s)''',
243 OUTPUT: r'''<img align="center" valign="center"
244 border="0" src="%(base)s.png" alt="[picture of music]">''',
252 OUTPUT: r'''{\parindent 0pt
254 \ifx\preLilyPondExample\preLilyPondExample\fi
257 \ifx\preLilyPondExample\postLilyPondExample\fi
259 VERBATIM: r'''\begin{verbatim}
260 %(verb)s\end{verbatim}
267 VERBATIM: r'''@example
268 %(verb)s@end example''',
274 PREAMBLE_LY = r'''%% Generated by %(program_name)s
275 %% Options: [%(option_string)s]
277 \paper {%(paper_string)s
281 FRAGMENT_LY = r'''\score{
282 \notes%(notes_string)s{
287 def classic_lilypond_book_compatibility (o):
288 if o == 'singleline':
290 m = re.search ('relative\s*([-0-9])', o)
292 return 'relative=%s' % m.group (1)
293 m = re.match ('([0-9]+)pt', o)
295 return 'staffsize=%s' % m.group (1)
296 m = re.match ('indent=([-.0-9]+)(cm|in|mm|pt)', o)
298 f = float (m.group (1))
299 return 'indent=%f\\%s' % (f, m.group (2))
300 m = re.match ('linewidth=([-.0-9]+)(cm|in|mm|pt)', o)
302 f = float (m.group (1))
303 return 'linewidth=%f\\%s' % (f, m.group (2))
306 def compose_ly (code, option_string):
309 for i in default_ly_options.keys ():
311 vars ()[i] = default_ly_options[i]
314 options = options + split_options (option_string)
316 m = re.search (r'''\\score''', code)
317 if not m and (not options \
318 or not 'nofragment' in options \
319 or 'fragment' in options):
320 options.append ('raggedright')
331 preamble_options = []
333 c = classic_lilypond_book_compatibility (i)
335 ly.warning (_ ("deprecated ly-option used: %s" % i))
336 ly.warning (_ ("compatibility mode translation: %s" \
340 if string.find (i, '=') > 0:
341 key, value = string.split (i, '=')
347 if key in ly_options[NOTES].keys ():
348 notes_options.append (ly_options[NOTES][key])
349 elif key in ly_options[PREAMBLE].keys ():
350 preamble_options.append (ly_options[PREAMBLE][key])
351 elif key in ly_options[PAPER].keys ():
352 paper_options.append (ly_options[PAPER][key])
353 elif key not in ('fragment', 'nofragment',
354 'relative', 'verbatim'):
355 ly.warning (_("ignoring unknown ly option: %s") % i)
357 relative_quotes = (",,,", ",,", ",", "", "'", "''", "'''")[relative-3]
358 program_name = __main__.program_name
359 notes_string = string.join (notes_options, '\n ') % vars ()
360 paper_string = string.join (paper_options, '\n ') % vars ()
361 preamble_string = string.join (preamble_options, '\n ') % vars ()
362 return (PREAMBLE_LY + body) % vars ()
366 # use lilypond-bin for latex (.lytex) books,
367 # and lilypond --preview for html, texinfo books?
369 cmd = r'latex "\nonstopmode \input %s"' % file
370 # Ugh. (La)TeX writes progress and error messages on stdout
372 cmd = '(( %s >&2 ) >&- )' % cmd
374 ly.system ('dvips -Ppdf -u+lilypond.map -E -o %s.eps %s' \
377 def find_file (name):
378 for i in include_path:
379 full = os.path.join (i, name)
380 if os.path.exists (full):
382 ly.error (_ ('file not found: %s\n' % name))
386 def verbatim_html (s):
387 return re.sub ('>', '>',
389 re.sub ('&', '&', s)))
391 def verbatim_texinfo (s):
392 return re.sub ('{', '@{',
394 re.sub ('@', '@@', s)))
396 def split_options (option_string):
397 return re.split (format_res[format]['option-sep'], option_string)
400 ## make index static of Snippet?
404 def __init__ (self, type, source, index, match):
412 return self.index + self.match.start (s)
415 return self.index + self.match.end (s)
417 def substring (self, s):
418 return self.source[self.start (s):self.end (s)]
422 if self.type == 'lilypond-block' or self.type == 'lilypond':
423 s = self.substring ('code')
424 elif self.type == 'lilypond-file':
425 name = self.substring ('filename')
426 s = open (find_file (name)).read ()
432 return compose_ly (s, self.match.group ('options'))
437 self.hash = abs (hash (self.ly ()))
439 print 'TYPE:: ' + self.type
440 print 'CODE:: ' + self.substring (0)
446 return 'lily-%d' % self.get_hash ()
450 if self.type == 'lilypond-block' or self.type == 'lilypond'\
451 or self.type == 'lilypond-file':
452 h = open (self.basename () + '.ly', 'w')
453 h.write (self.full_ly ())
455 def output_html (self):
456 base = self.basename ()
457 option_string = self.match.group ('options')
458 if option_string and VERBATIM in split_options (option_string)\
460 verb = verbatim_html (self.substring ('code'))
461 h.write (output[HTML][VERBATIM] % vars ())
462 h.write (output[HTML][BEFORE])
463 h.write (output[HTML][OUTPUT] % vars ())
464 h.write (output[HTML][AFTER])
466 def output_latex (self):
467 option_string = self.match.group ('options')
468 if option_string and VERBATIM in split_options (option_string)\
470 verb = self.substring ('code')
471 h.write (output[LATEX][VERBATIM] % vars ())
472 h.write (output[LATEX][BEFORE])
473 base = self.basename ()
474 h.write (output[LATEX][OUTPUT] % vars ())
475 h.write (output[LATEX][AFTER])
477 def output_texinfo (self):
478 option_string = self.match.group ('options')
479 if option_string and VERBATIM in split_options (option_string):
480 verb = verbatim_texinfo (self.substring ('code'))
481 h.write (output[TEXINFO][VERBATIM] % vars ())
484 h.write ('\n@end tex\n')
486 h.write ('\n@html\n')
488 h.write ('\n@end html\n')
490 def outdated_p (self):
491 if self.type != 'lilypond-block' and self.type != 'lilypond'\
492 and self.type != 'lilypond-file':
494 base = self.basename ()
495 if os.path.exists (base + '.ly') \
496 and os.path.exists (base + '.tex') \
498 or self.ly () == open (base + '.ly').read ()):
499 # TODO: something smart with target formats
500 # (ps, png) and m/ctimes
504 def filter_code (self):
506 # Hmm, why is verbatim's group called 'code'; rename to 'verb'?
507 #if snippet.match.group ('code'):
509 if self.type == 'lilypond' or self.type == 'lilypond-block':
510 h.write (self.source[index:self.start ('code')])
511 h.write (run_filter (self.substring ('code')))
512 h.write (self.source[self.end ('code'):self.end (0)])
514 h.write (self.source[index:self.end (0)])
517 def compile_output (self):
519 # Hmm, why is verbatim's group called 'code'; rename to 'verb'?
520 # if snippet.match.group ('code'):
522 if self.type == 'lilypond' \
523 or self.type == 'lilypond-block'\
524 or self.type == 'lilypond-file':
525 h.write (self.source[index:self.start (0)])
526 snippet_output = eval ("Snippet.output_" + format)
527 snippet_output (self)
528 elif self.type == 'include':
529 h.write (self.source[index:self.start ('filename')])
530 base = os.path.splitext (self.substring ('filename'))[0]
531 h.write (base + format2ext[format])
532 h.write (self.source[self.end ('filename'):self.end (0)])
534 h.write (self.source[index:self.end (0)])
537 def find_toplevel_snippets (s, types):
540 res[i] = ly.re.compile (snippet_res[format][i])
544 found = {}.fromkeys (types)
549 if not found[i] or found[i].start (0) < index:
551 m = res[i].search (s[index:endex])
553 found[i] = Snippet (i, s, index, m)
556 or found[i].start (0) < found[first].start (0)):
558 endex = found[first].start (0)
561 snippets.append (found[first])
562 index = found[first].end (0)
566 def filter_pipe (input, cmd):
568 ly.progress (_ ("Opening filter `%s\'") % cmd)
570 stdin, stdout, stderr = os.popen3 (cmd)
572 status = stdin.close ()
576 output = stdout.read ()
577 status = stdout.close ()
578 error = stderr.read ()
582 signal = 0x0f & status
583 if status or (not output and error):
584 exit_status = status >> 8
585 ly.error (_ ("`%s\' failed (%d)") % (cmd, exit_status))
586 ly.error (_ ("The error log is as follows:"))
587 sys.stderr.write (error)
588 sys.stderr.write (stderr.read ())
597 return filter_pipe (s, filter_cmd)
599 def process_snippets (cmd, snippets):
600 names = filter (lambda x:x, map (Snippet.basename, snippets))
602 ly.system (string.join ([cmd] + names))
604 if format == HTML or format == TEXINFO:
607 ly.make_ps_images (i + '.eps', resolution=110)
609 LATEX_DOCUMENT = r'''
612 \typeout{textwidth=\the\textwidth}
613 \typeout{columnsep=\the\columnsep}
614 \makeatletter\if@twocolumn\typeout{columns=2}\fi\makeatother
617 #need anything else besides textwidth?
618 def get_latex_textwidth (source):
619 m = re.search (r'''(?P<preabmle>\\begin\s*{document})''', source)
620 preamble = source[:m.start (0)]
621 latex_document = LATEX_DOCUMENT % vars ()
622 parameter_string = filter_pipe (latex_document, latex_filter_cmd)
625 m = re.search ('columns=([0-9.]*)', parameter_string)
627 columns = string.atoi (m.group (1))
630 m = re.search ('columnsep=([0-9.]*)pt', parameter_string)
632 columnsep = string.atof (m.group (1))
635 m = re.search('textwidth=([0-9.]*)pt', parameter_string)
637 textwidth = string.atof (m.group (1))
639 textwidth = (textwidth - columnsep) / columns
651 '.texinfo' : TEXINFO,
657 #TEXINFO: '.texinfo',
662 def do_file (input_filename):
666 e = os.path.splitext (input_filename)[1]
667 if e in ext2format.keys ():
669 format = ext2format[e]
671 ly.error (_ ("cannot determine format for: %s" \
674 ly.progress (_ ("Reading %s...") % input_filename)
675 if not input_filename or input_filename == '-':
678 ih = open (input_filename)
682 ly.progress (_ ("Dissecting..."))
683 #snippets = find_toplevel_snippets (source, snippet_res[format].keys ())
688 'singleline-comment',
694 snippets = find_toplevel_snippets (source, snippet_types)
698 if output_name == '-' or not output_name:
700 output_filename = '-'
702 if not os.path.isdir (output_name):
703 os.mkdir (output_name, 0777)
704 if input_filename == '-':
707 input_base = os.path.splitext (input_filename)[0]
708 output_filename = output_name + '/' + input_base \
710 h = open (output_filename, 'w')
711 os.chdir (output_name)
713 global default_ly_options
715 if format == LATEX and LINEWIDTH not in default_ly_options.keys ():
716 textwidth = get_latex_textwidth (source)
717 default_ly_options[LINEWIDTH] = '''%.0f\pt''' % textwidth
722 map (Snippet.filter_code, snippets)
723 h.write (source[index:])
725 outdated = filter (lambda x:x,
726 map (Snippet.outdated_p, snippets))
727 ly.progress (_ ("Writing snippets..."))
728 map (Snippet.write_ly, snippets)
732 ly.progress (_ ("Processing..."))
733 process_snippets (process_cmd, outdated)
735 ly.progress (_ ("All snippets are up to date..."))
738 ly.progress (_ ("Compiling %s...") % output_filename)
740 map (Snippet.compile_output, snippets)
741 h.write (source[index:])
747 def process_include (snippet):
748 os.chdir (original_dir)
749 name = snippet.substring ('filename')
750 ly.progress (_ ('Processing include: %s') % name)
754 map (process_include, filter (lambda x: x.type == 'include', snippets))
757 global format, output_name
758 global filter_cmd, process_cmd, verbose_p
760 (sh, long) = ly.getopt_args (option_definitions)
762 (options, files) = getopt.getopt (sys.argv[1:], sh, long)
763 except getopt.error, s:
764 sys.stderr.write ('\n')
765 ly.error (_ ("getopt says: `%s\'" % s))
766 sys.stderr.write ('\n')
776 elif o == '--version' or o == '-v':
777 ly.identify (sys.stdout)
779 elif o == '--verbose' or o == '-V':
781 elif o == '--filter' or o == '-F':
784 elif o == '--format' or o == '-f':
786 if a == 'texi-html' or a == 'texi':
788 elif o == '--help' or o == '-h':
791 elif o == '--include' or o == '-I':
792 include_path.append (os.path.join (original_dir,
794 elif o == '--output' or o == '-o':
796 elif o == '--outdir':
798 elif o == '--process' or o == '-P':
801 elif o == '--warranty' or o == '-w':
808 files = do_options ()
809 ly.identify (sys.stderr)
810 ly.setup_environment ()
814 if __name__ == '__main__':