5 ly-options: intertext, quote ?
7 check latex parameters, twocolumn
15 filter-lilypond-book --filter="tr '[a-z]' '[A-Z]'" BOOK
18 filter-lilypond-book --filter="convert-ly --no-version --from=1.6.11 -" BOOK
20 classic lilypond-book (WIP):
21 filter-lilypond-book --process="lilypond-bin" BOOK.tely
23 options in .tely/.lytex must be converted (or grokked by me :-)
24 (([0-9][0-9])*pt) -> staffsize=\2
25 @mbinclude foo.itely -> @include foo.itely
27 relative X -> relative=X
28 singleline -> raggedright
35 ################################################################
36 # Users of python modules should include this snippet
37 # and customize variables below.
39 # We'll suffer this path init stuff as long as we don't install our
40 # python packages in <prefix>/lib/pythonx.y (and don't kludge around
41 # it as we do with teTeX on Red Hat Linux: set some environment var
42 # (PYTHONPATH) in profile)
44 # If set, LILYPONDPREFIX must take prevalence
45 # if datadir is not set, we're doing a build and LILYPONDPREFIX
46 import getopt, os, sys
47 datadir = '@local_lilypond_datadir@'
48 if not os.path.isdir (datadir):
49 datadir = '@lilypond_datadir@'
50 if os.environ.has_key ('LILYPONDPREFIX') :
51 datadir = os.environ['LILYPONDPREFIX']
52 while datadir[-1] == os.sep:
55 sys.path.insert (0, os.path.join (datadir, 'python'))
58 #if __name__ == '__main__':
65 program_version = '@TOPLEVEL_VERSION@'
66 #program_name = 'new-book'
67 program_name = 'filter-lilypond-book'
70 original_dir = os.getcwd ()
73 # help_summary = _ ("Process LilyPond snippets in hybrid html, LaTeX or texinfo document")
74 help_summary = _ ("""Process ly snippets from lilypond-book source. Example usage:
76 filter-lilypond-book --filter="tr '[a-z]' '[A-Z]'" BOOK
77 filter-lilypond-book --filter="convert-ly --no-version --from=1.6.11 -" BOOK
80 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>>',
81 'Han-Wen Nienhuys <hanwen@cs.uu.nl>')
83 option_definitions = [
84 (_ ("EXT"), 'f', 'format', _ ("use output format EXT (texi [default], texi-html, latex, html)")),
85 (_ ("FILTER"), 'F', 'filter', _ ("pipe snippets through FILTER [convert-ly -n -]")),
86 ('', 'h', 'help', _ ("print this help")),
87 (_ ("DIR"), 'I', 'include', _ ("add DIR to include path")),
88 (_ ("COMMAND"), 'P', 'process', _ ("process ly_files using COMMAND FILE...")),
89 (_ ("FILE"), 'o', 'output', _ ("write output to FILE")),
90 ('', 'V', 'verbose', _ ("be verbose")),
91 ('', 'v', 'version', _ ("print version information")),
92 ('', 'w', 'warranty', _ ("show warranty and copyright")),
93 ('', 'M', 'dependencies', _ ("TODO")),
94 (_ ("DIR"), '', 'outdir', _ ("DEPRECATED, use --output")),
97 include_path = [ly.abspath (os.getcwd ())]
99 lilypond_binary = os.path.join ('@bindir@', 'lilypond-bin')
101 # only use installed binary when we're installed too.
102 if '@bindir@' == ('@' + 'bindir@') or not os.path.exists (lilypond_binary):
103 lilypond_binary = 'lilypond-bin'
109 latex_filter_cmd = 'latex "\\nonstopmode \input /dev/stdin"'
110 ##filter_cmd = 'convert-ly --no-version --from=2.0.0 -'
113 #process_cmd = 'convert-ly --no-version --from=2.0.0'
115 # process_cmd = 'lilypond-bin'
116 process_cmd = 'lilypond-bin -I ../../../input/test'
117 default_ly_options = { }
125 VERBATIM = 'verbatim'
126 LINEWIDTH = 'linewidth'
128 ## lilypond-book heritage. to be cleaned
130 ################################################################
131 # Recognize special sequences in the input
134 # Warning: This uses extended regular expressions. Tread with care.
138 # (?P<name>regex) -- assign result of REGEX to NAME
139 # *? -- match non-greedily.
140 # (?m) -- multiline regex: make ^ and $ match at each line
141 # (?s) -- make the dot match all characters including newline
148 'landscape': no_match,
149 'verbatim': r'''(?s)(?P<code><pre>\s.*?</pre>\s)''',
150 'verb': r'''(?P<code><pre>.*?</pre>)''',
151 'lilypond-file': r'(?m)(?P<match><lilypondfile(?P<options>[^>]+)?>\s*(?P<filename>[^<]+)\s*</lilypondfile>)',
152 'lilypond' : '(?m)(?P<match><lilypond((?P<options>[^:]*):)(?P<code>.*?)/>)',
153 'lilypond-block': r'''(?ms)(?P<match><lilypond(?P<options>[^>]+)?>(?P<code>.*?)</lilypond>)''',
154 'option-sep' : '\s*',
155 'intertext': r',?\s*intertext=\".*?\"',
156 'multiline-comment': r"(?sm)\s*(?!@c\s+)(?P<code><!--\s.*?!-->)\s",
157 'singleline-comment': no_match,
159 'multicols': no_match,
160 'ly2dvi': r'(?m)(?P<match><ly2dvifile(?P<options>[^>]+)?>\s*(?P<filename>[^<]+)\s*</ly2dvifile>)',
164 'include': r'(?m)^[^%\n]*?(?P<match>\\input{(?P<filename>[^}]+)})',
165 'option-sep' : ',\s*',
166 'header': r"\n*\\documentclass\s*(\[.*?\])?",
167 'verbatim': r"(?s)(?P<code>\\begin\s*{verbatim}.*?\\end{verbatim})",
168 'verb': r"(?P<code>\\verb(?P<del>.).*?(?P=del))",
169 'lilypond-file': r'(?m)^[^%\n]*?(?P<match>\\lilypondfile\s*(\[(?P<options>.*?)\])?\s*\{(?P<filename>.+)})',
170 'lilypond' : r'(?m)^[^%\n]*?(?P<match>\\lilypond\s*(\[(?P<options>.*?)\])?\s*{(?P<code>.*?)})',
171 'lilypond-block': r"(?sm)^[^%\n]*?(?P<match>\\begin\s*(\[(?P<options>.*?)\])?\s*{lilypond}(?P<code>.*?)\\end{lilypond})",
172 'def-post-re': r"\\def\\postLilyPondExample",
173 'def-pre-re': r"\\def\\preLilyPondExample",
174 'usepackage-graphics': r"\usepackage\s*{graphics}",
175 'intertext': r',?\s*intertext=\".*?\"',
176 'multiline-comment': no_match,
177 'singleline-comment': r"(?m)^.*?(?P<match>(?P<code>^%.*$\n+))",
178 'numcols': r"(?P<code>\\(?P<num>one|two)column)",
179 'multicols': r"(?P<code>\\(?P<be>begin|end)\s*{multicols}({(?P<num>\d+)?})?)",
184 # why do we have distinction between @mbinclude and @include?
187 'junkmeinclude': '(?m)^[^%\n]*?(?P<match>@mbinclude\s+(?P<filename>\S*))',
188 'include': '(?m)^[^%\n]*?(?P<match>@include\s+(?P<filename>\S*))',
191 'landscape': no_match,
192 'verbatim': r'''(?s)(?P<code>@example\s.*?@end example\s)''',
193 'verb': r'''(?P<code>@code{.*?})''',
194 'lilypond-file': '(?m)^(?P<match>@lilypondfile(\[(?P<options>[^]]*)\])?{(?P<filename>[^}]+)})',
195 'lilypond' : '(?m)^(?P<match>@lilypond(\[(?P<options>[^]]*)\])?{(?P<code>.*?)})',
196 'lilypond-block': r'''(?ms)^(?P<match>@lilypond(\[(?P<options>[^]]*)\])?\s(?P<code>.*?)@end lilypond)\s''',
197 'option-sep' : ',\s*',
198 'intertext': r',?\s*intertext=\".*?\"',
199 'multiline-comment': r"(?sm)^\s*(?!@c\s+)(?P<code>@ignore\s.*?@end ignore)\s",
200 'singleline-comment': r"(?m)^.*?(?P<match>(?P<code>@c.*$\n+))",
202 'multicols': no_match,
208 PREAMBLE = 'preamble'
213 #'xrelative': r'''\relative #(ly:make-pitch %(relative)s 0 0)''',
214 'relative': r'''\relative c%(relative_quotes)s''',
218 indent = %(indent)s''',
220 linewidth = %(linewidth)s''',
226 \remove Time_signature_engraver
230 raggedright = ##t''',
234 #(set-global-staff-size %(staffsize)s)''',
242 OUTPUT: r'''<img align="center" valign="center"
243 border="0" src="%(base)s.png" alt="[picture of music]">''',
251 OUTPUT: r'''{\parindent 0pt
253 \ifx\preLilyPondExample\preLilyPondExample\fi
256 \ifx\preLilyPondExample\postLilyPondExample\fi
258 VERBATIM: r'''\begin{verbatim}
259 %(verb)s\end{verbatim}
266 VERBATIM: r'''@example
267 %(verb)s@end example''',
273 PREAMBLE_LY = r'''%% Generated by %(program_name)s
274 %% Options: [%(option_string)s]
276 \paper {%(paper_string)s
280 FRAGMENT_LY = r'''\score{
281 \notes%(notes_string)s{
287 def compose_ly (code, option_string):
290 for i in default_ly_options.keys ():
292 vars ()[i] = default_ly_options[i]
295 options = options + split_options (option_string)
297 m = re.search (r'''\\score''', code)
298 if not m and (not options \
299 or not 'nofragment' in options \
300 or 'fragment' in options):
301 options.append ('raggedright')
312 preamble_options = []
314 if string.find (i, '=') > 0:
315 key, value = string.split (i, '=')
321 if key in ly_options[NOTES].keys ():
322 notes_options.append (ly_options[NOTES][key])
323 elif key in ly_options[PREAMBLE].keys ():
324 preamble_options.append (ly_options[PREAMBLE][key])
325 elif key in ly_options[PAPER].keys ():
326 paper_options.append (ly_options[PAPER][key])
327 elif key not in ('fragment', 'nofragment',
328 'relative', 'verbatim'):
329 ly.warning (_("ignoring unknown ly option: %s") % i)
331 relative_quotes = (",,,", ",,", ",", "", "'", "''", "'''")[relative-3]
332 program_name = __main__.program_name
333 notes_string = string.join (notes_options, '\n ') % vars ()
334 paper_string = string.join (paper_options, '\n ') % vars ()
335 preamble_string = string.join (preamble_options, '\n ') % vars ()
336 return (PREAMBLE_LY + body) % vars ()
340 # use lilypond-bin for latex (.lytex) books,
341 # and lilypond --preview for html, texinfo books?
343 cmd = r'latex "\nonstopmode \input %s"' % file
344 # Ugh. (La)TeX writes progress and error messages on stdout
346 cmd = '(( %s >&2 ) >&- )' % cmd
348 ly.system ('dvips -Ppdf -u+lilypond.map -E -o %s.eps %s' \
351 ## make source, index statics of Snippet?
354 def find_file (name):
355 for i in include_path:
356 full = os.path.join (i, name)
357 if os.path.exists (full):
359 ly.error (_ ('file not found: %s\n' % name))
363 def verbatim_html (s):
364 return re.sub ('>', '>',
366 re.sub ('&', '&', s)))
368 def verbatim_texinfo (s):
369 return re.sub ('{', '@{',
371 re.sub ('@', '@@', s)))
373 def split_options (option_string):
374 return re.split (re_dict[format]['option-sep'], option_string)
377 def __init__ (self, type, index, match):
384 return self.index + self.match.start (s)
387 return self.index + self.match.end (s)
389 def substring (self, source, s):
390 return source[self.start (s):self.end (s)]
392 def ly (self, source):
394 if self.type == 'lilypond-block' or self.type == 'lilypond':
395 s = self.substring (source, 'code')
396 elif self.type == 'lilypond-file':
397 name = self.substring (source, 'filename')
398 s = open (find_file (name)).read ()
401 def full_ly (self, source):
404 return compose_ly (s, self.match.group ('options'))
407 def get_hash (self, source):
409 self.hash = abs (hash (self.ly (source)))
411 print 'TYPE:: ' + self.type
412 print 'CODE:: ' + self.substring (source, 0)
416 def basename (self, source):
418 return 'lily-%d' % self.get_hash (source)
421 def write_ly (self, source):
422 if self.type == 'lilypond-block' or self.type == 'lilypond'\
423 or self.type == 'lilypond-file':
424 h = open (self.basename (source) + '.ly', 'w')
425 h.write (self.full_ly (source))
428 def output_html (self, source):
429 base = self.basename (source)
430 option_string = self.match.group ('options')
431 if option_string and VERBATIM in split_options (option_string)\
433 verb = verbatim_html (self.substring (source, 'code'))
434 h.write (output[HTML][VERBATIM] % vars ())
435 h.write (output[HTML][BEFORE])
436 h.write (output[HTML][OUTPUT] % vars ())
437 h.write (output[HTML][AFTER])
439 def output_latex (self, source):
440 option_string = self.match.group ('options')
441 if option_string and VERBATIM in split_options (option_string)\
443 verb = self.substring (source, 'code')
444 h.write (output[LATEX][VERBATIM] % vars ())
445 h.write (output[LATEX][BEFORE])
446 base = self.basename (source)
447 h.write (output[LATEX][OUTPUT] % vars ())
448 h.write (output[LATEX][AFTER])
450 def output_texinfo (self, source):
451 option_string = self.match.group ('options')
452 if option_string and VERBATIM in split_options (option_string):
453 verb = verbatim_texinfo (self.substring (source,
455 h.write (output[TEXINFO][VERBATIM] % vars ())
457 self.output_latex (source)
458 h.write ('\n@end tex\n')
460 h.write ('\n@html\n')
461 self.output_html (source)
462 h.write ('\n@end html\n')
464 def outdated_p (self, source):
465 if self.type != 'lilypond-block' and self.type != 'lilypond'\
466 and self.type != 'lilypond-file':
468 base = self.basename (source)
469 if os.path.exists (base + '.ly') \
470 and os.path.exists (base + '.tex') \
472 or self.ly (source) == open (base + '.ly').read ()):
473 # TODO: something smart with target formats
474 # (ps, png) and m/ctimes
478 def find_snippets (s, type):
479 re = ly.re.compile (re_dict[format][type])
482 m = re.search (s[i:])
484 snippets.append (Snippet (type, i, m))
486 m = re.search (s[i:])
489 def filter_pipe (input, cmd):
491 ly.progress (_ ("Opening filter `%s\'") % cmd)
493 stdin, stdout, stderr = os.popen3 (cmd)
495 status = stdin.close ()
499 output = stdout.read ()
500 status = stdout.close ()
501 error = stderr.read ()
505 signal = 0x0f & status
506 if status or (not output and error):
507 exit_status = status >> 8
508 ly.error (_ ("`%s\' failed (%d)") % (cmd, exit_status))
509 ly.error (_ ("The error log is as follows:"))
510 sys.stderr.write (error)
511 sys.stderr.write (stderr.read ())
520 return filter_pipe (s, filter_cmd)
522 def compare_index (a, b):
523 return a.start (0) - b.start (0)
525 # apply FUNC to every toplevel block in SNIPPETS, ie, enclosed
526 # snippets are skipped. return list with all non-empty return values
529 # Hmm, do we need enclosed snippets at all? Maybe use MAP_SNIPPETS
530 # once and use simple filter/map on that resulting toplevel list iso
531 # silly map_snippets/do_snippets.
532 def map_snippets (source, snippets, func):
537 if i.start (0) < index:
539 # lst.append (func (i, source))
546 # apply FUNC to every toplevel block in SNIPPETS, ie, enclosed
547 # snippets are skipped. return last snippet's index
548 def do_snippets (source, snippets, func):
552 if i.start (0) < index:
556 #index = i.end ('code')
559 def process_snippets (source, snippets, cmd):
560 names = map_snippets (source, snippets, Snippet.basename)
562 ly.system (string.join ([cmd] + names))
564 if format == HTML or format == TEXINFO:
567 ly.make_ps_images (i + '.eps', resolution=110)
569 LATEX_DOCUMENT = r'''
572 \typeout{textwidth=\the\textwidth}
573 \typeout{columnsep=\the\columnsep}
574 \makeatletter\if@twocolumn\typeout{columns=2}\fi\makeatother
577 #need anything else besides textwidth?
578 def get_latex_textwidth (source):
579 m = re.search (r'''(?P<preabmle>\\begin\s*{document})''', source)
580 preamble = source[:m.start (0)]
581 latex_document = LATEX_DOCUMENT % vars ()
582 parameter_string = filter_pipe (latex_document, latex_filter_cmd)
585 m = re.search ('columns=([0-9.]*)', parameter_string)
587 columns = string.atoi (m.group (1))
590 m = re.search ('columnsep=([0-9.]*)pt', parameter_string)
592 columnsep = string.atof (m.group (1))
595 m = re.search('textwidth=([0-9.]*)pt', parameter_string)
597 textwidth = string.atof (m.group (1))
599 textwidth = (textwidth - columnsep) / columns
611 '.texinfo' : TEXINFO,
617 #TEXINFO: '.texinfo',
622 def do_file (input_filename):
626 e = os.path.splitext (input_filename)[1]
627 if e in ext2format.keys ():
629 format = ext2format[e]
631 ly.error (_ ("cannot determine format for: %s" \
634 ly.progress (_ ("Reading %s...") % input_filename)
636 if not input_filename or input_filename == '-':
639 h = open (input_filename)
643 ly.progress (_ ("Dissecting..."))
644 snippet_types = ('verbatim', 'verb',
645 'multiline-comment', 'singleline-comment',
646 'lilypond', 'lilypond-block', 'lilypond-file',
649 for i in snippet_types:
650 snippets += find_snippets (source, i)
651 snippets.sort (compare_index)
654 if output_name == '-' or not output_name:
656 output_filename = '-'
658 if not os.path.isdir (output_name):
659 os.mkdir (output_name, 0777)
660 if input_filename == '-':
663 input_base = os.path.splitext (input_filename)[0]
664 output_filename = output_name + '/' + input_base \
666 h = open (output_filename, 'w')
667 os.chdir (output_name)
669 def filter_source (snippet, source):
671 # Hmm, why is verbatim's group called 'code'; rename to 'verb'?
672 #if snippet.match.group ('code'):
674 if snippet.type == 'lilypond' or snippet.type == 'lilypond-block':
675 h.write (source[index:snippet.start ('code')])
676 h.write (run_filter (snippet.substring (source, 'code')))
677 h.write (source[snippet.end ('code'):snippet.end (0)])
679 h.write (source[index:snippet.end (0)])
680 index = snippet.end (0)
682 snippet_output = eval ("Snippet.output_" + format)
683 def compile_output (snippet, source):
685 # Hmm, why is verbatim's group called 'code'; rename to 'verb'?
686 # if snippet.match.group ('code'):
688 if snippet.type == 'lilypond' \
689 or snippet.type == 'lilypond-block'\
690 or snippet.type == 'lilypond-file':
691 h.write (source[index:snippet.start (0)])
692 snippet_output (snippet, source)
693 elif snippet.type == 'include':
694 h.write (source[index:snippet.start ('filename')])
695 base = os.path.splitext (snippet.substring (source, 'filename'))[0]
696 h.write (base + format2ext[format])
697 h.write (source[snippet.end ('filename'):snippet.end (0)])
699 h.write (source[index:snippet.end (0)])
700 index = snippet.end (0)
702 global default_ly_options
704 if format == LATEX and LINEWIDTH not in default_ly_options.keys ():
705 textwidth = get_latex_textwidth (source)
706 default_ly_options[LINEWIDTH] = '''%.0f\pt''' % textwidth
710 index = do_snippets (source, snippets, filter_source)
711 h.write (source[index:])
713 outdated = map_snippets (source, snippets, Snippet.outdated_p)
714 ly.progress (_ ("Writing snippets..."))
715 do_snippets (source, snippets, Snippet.write_ly)
718 ly.progress (_ ("Processing..."))
719 process_snippets (source, outdated, process_cmd)
721 ly.progress (_ ("All snippets are up to date..."))
723 ly.progress (_ ("Compiling %s...") % output_filename)
724 do_snippets (source, snippets, compile_output)
725 h.write (source[index:])
728 includes = find_snippets (source, 'include')
730 os.chdir (original_dir)
733 options.append ('--linewidth=%f' % textwidth)
735 name = i.substring (source, 'filename')
736 ly.progress (_ ('Processing include: %s') % name)
738 cmd = 'python ' + string.join (sys.argv[:-1]) \
739 + string.join (options) + ' ' + name
743 global format, output_name
744 global filter_cmd, process_cmd, verbose_p
746 (sh, long) = ly.getopt_args (option_definitions)
748 (options, files) = getopt.getopt (sys.argv[1:], sh, long)
749 except getopt.error, s:
750 sys.stderr.write ('\n')
751 ly.error (_ ("getopt says: `%s\'" % s))
752 sys.stderr.write ('\n')
762 elif o == '--version' or o == '-v':
763 ly.identify (sys.stdout)
765 elif o == '--verbose' or o == '-V':
767 elif o == '--filter' or o == '-F':
770 elif o == '--format' or o == '-f':
772 if a == 'texi-html' or a == 'texi':
774 elif o == '--help' or o == '-h':
777 elif o == '--include' or o == '-I':
778 include_path.append (os.path.join (original_dir,
780 elif o == '--output' or o == '-o':
782 elif o == '--outdir':
784 elif o == '--process' or o == '-P':
787 elif o == '--warranty' or o == '-w':
794 files = do_options ()
795 ly.identify (sys.stderr)
796 ly.setup_environment ()
800 if __name__ == '__main__':