2 # vim: set noexpandtab:
4 # * junk --outdir for--output
5 # * Figure out clean set of options.
7 # * texinfo: add support for @pagesize
9 # todo: dimension handling (all the x2y) is clumsy. (tca: Thats
10 # because the values are taken directly from texinfo.tex,
11 # geometry.sty and article.cls. Give me a hint, and I'll
15 # TODO: magnification support should also work for texinfo -> html: eg. add as option to dvips.
20 # This is a slightly hairy program. The general approach is as follows
21 # The input string is chopped up in chunks, i.e. , a list of tuples
23 # with the format (TAG_STR, MAIN_STR, OPTIONS, TODO, BASE)
25 # This list is build step by step: first ignore and verbatim commands are handled,
26 # delivering a list of chunks.
28 # then all chunks containing lilypnod commands are chopped up
30 # when all chunks have their final form, all bodies from lilypond blocks are
31 # extracted, and if applicable, written do disk and run through lilypond.
35 # This is was the idea for handling of comments:
38 # Multiline comments, @ignore .. @end ignore is scanned for
39 # in read_doc_file, and the chunks are marked as 'ignore', so
40 # lilypond-book will not touch them any more. The content of the
41 # chunks are written to the output file. Also 'include' and 'input'
42 # regex has to check if they are commented out.
45 # Then it is scanned for 'lilypond', 'lilypond-file' and 'lilypond-block'.
46 # These three regex's has to check if they are on a commented line,
47 # % for latex, @c for texinfo.
49 # Then lines that are commented out with % (latex) and @c (Texinfo)
50 # are put into chunks marked 'ignore'. This cannot be done before
51 # searching for the lilypond-blocks because % is also the comment character
54 # The the rest of the rexeces are searched for. They don't have to test
55 # if they are on a commented out line.
62 ################################################################
63 # Users of python modules should include this snippet
64 # and customize variables below.
66 # We'll suffer this path init stuff as long as we don't install our
67 # python packages in <prefix>/lib/pythonx.y (and don't kludge around
68 # it as we do with teTeX on Red Hat Linux: set some environment var
69 # (PYTHONPATH) in profile)
71 # If set, LILYPONDPREFIX must take prevalence
72 # if datadir is not set, we're doing a build and LILYPONDPREFIX
73 import getopt, os, sys
74 datadir = '@local_lilypond_datadir@'
75 if not os.path.isdir (datadir):
76 datadir = '@lilypond_datadir@'
77 if os.environ.has_key ('LILYPONDPREFIX') :
78 datadir = os.environ['LILYPONDPREFIX']
79 while datadir[-1] == os.sep:
82 sys.path.insert (0, os.path.join (datadir, 'python'))
85 #if __name__ == '__main__':
92 program_name = 'lilypond-book'
95 original_dir = os.getcwd ()
96 #temp_dir = os.path.join (original_dir, '%s.dir' % program_name)
100 preview_resolution = 90
103 ## ly2dvi: silly name?
104 ## do -P or -p by default?
105 ##help_summary = _ ("Run LilyPond using LaTeX for titling")
106 help_summary = _ ("Process LilyPond snippets in hybrid html, LaTeX or texinfo document")
107 copyright = ('Tom Cato Amundsen <tca@gnu.org>',
108 'Han-Wen Nienhuys <hanwen@cs.uu.nl>')
110 option_definitions = [
111 (_ ("EXT"), 'f', 'format', _ ("use output format EXT (texi [default], texi-html, latex, html)")),
112 (_ ("DIM"), '', 'default-music-fontsize', _ ("default fontsize for music. DIM is assumed to be in points")),
113 (_ ("DIM"), '', 'default-lilypond-fontsize', _ ("deprecated, use --default-music-fontsize")),
114 (_ ("OPT"), '', 'extra-options', _ ("pass OPT quoted to the lilypond command line")),
115 (_ ("DIM"), '', 'force-music-fontsize', _ ("force fontsize for all inline lilypond. DIM is assumed be to in points")),
116 (_ ("DIM"), '', 'force-lilypond-fontsize', _ ("deprecated, use --force-music-fontsize")),
117 ('', 'h', 'help', _ ("this help")),
118 (_ ("DIR"), 'I', 'include', _ ("include path")),
119 ('', 'M', 'dependencies', _ ("write dependencies")),
120 (_ ("PREF"), '', 'dep-prefix', _ ("prepend PREF before each -M dependency")),
121 ('', 'n', 'no-lily', _ ("don't run lilypond")),
122 ('', '', 'no-pictures', _ ("don't generate pictures")),
123 ('', '', 'no-music', _ ("strip all lilypond blocks from output")),
124 ('', '', 'read-lys', _ ("don't write ly files.")),
125 (_ ("FILE"), 'o', 'outname', _ ("filename main output file")),
126 (_ ("FILE"), '', 'outdir', _ ("where to place generated files")),
127 (_ ('RES'), '', 'preview-resolution',
128 _ ("set the resolution of the preview to RES")),
129 ('', 'V', 'verbose', _ ("verbose")),
130 ('', 'v', 'version', _ ("print version information")),
131 ('', 'w', 'warranty', _ ("show warranty and copyright")),
134 # format specific strings, ie. regex-es for input, and % strings for output
138 include_path = [os.getcwd ()]
141 lilypond_cmd = 'lilypond'
142 #lilypond_cmd = 'valgrind --suppressions=/home/hanwen/usr/src/guile-1.6.supp --num-callers=10 /home/hanwen/usr/src/lilypond/lily/out/lilypond'
146 g_here_dir = os.getcwd ()
149 g_force_music_fontsize = 0
159 default_music_fontsize = 16
160 default_text_fontsize = 12
163 ################################################################
164 # Dimension handling for LaTeX.
168 self.m_document_preamble = []
172 def find_latex_dims (self):
174 fname = os.path.join (g_outdir, "lily-tmp.tex")
176 fname = "lily-tmp.tex"
178 f = open (fname, "w")
180 error ("Error creating temporary file '%s'" % fname)
182 for s in self.m_document_preamble:
187 \typeout{\columnsep \the\columnsep}
188 \typeout{\textwidth \the\textwidth}
193 re_dim = re.compile (r"\\(\w+)\s+(\d+\.\d+)")
195 cmd = "latex '\\nonstopmode \input %s'" % fname
196 # Ugh. (La)TeX writes progress and error messages on stdout
198 cmd += ' 1>/dev/stderr'
199 status = ly.system (cmd, ignore_error = 1)
200 signal = 0xf & status
201 exit_status = status >> 8
204 ly.error (_ ("LaTeX failed."))
205 ly.error (_ ("The error log is as follows:"))
209 lns = open ('lily-tmp.log').readlines ()
214 sys.stderr.write (ln)
215 if re.match ('^!', ln):
222 countdown = countdown -1
224 sys.stderr.write (" ... (further messages elided)...\n")
227 lns = open ('lily-tmp.log').readlines ()
229 ln = string.strip (ln)
230 m = re_dim.match (ln)
232 if m.groups ()[0] in ('textwidth', 'columnsep'):
233 self.__dict__['m_%s' % m.groups ()[0]] = float (m.groups ()[1])
237 os.remove (os.path.splitext (fname)[0]+".aux")
238 os.remove (os.path.splitext (fname)[0]+".log")
242 if not self.__dict__.has_key ('m_textwidth'):
245 def get_linewidth (self):
246 if self.m_num_cols == 1:
249 w = (self.m_textwidth - self.m_columnsep)/2
250 if self.m_multicols > 1:
251 return (w - self.m_columnsep* (self.m_multicols-1)) \
258 self.m_papersize = 'letterpaper'
260 def get_linewidth (self):
261 return html_linewidths[self.m_papersize][self.m_fontsize]
265 self.m_papersize = 'letterpaper'
267 def get_linewidth (self):
268 return texi_linewidths[self.m_papersize][self.m_fontsize]
274 def em2pt (x, fontsize = 10):
275 return {10: 10.00002, 11: 10.8448, 12: 11.74988}[fontsize] * x
276 def ex2pt (x, fontsize = 10):
277 return {10: 4.30554, 11: 4.7146, 12: 5.16667}[fontsize] * x
282 dimension_conversion_dict ={
284 'cm': lambda x: mm2pt (10*x),
291 # Convert numeric values, with or without specific dimension, to floats.
293 def conv_dimen_to_float (value):
294 if type (value) == type (""):
295 m = re.match ("([0-9.]+)(cm|in|pt|mm|em|ex)",value)
298 num = string.atof (m.group (1))
299 conv = dimension_conversion_dict[m.group (2)]
303 elif re.match ("^[0-9.]+$",value):
304 value = float (value)
309 'afourpaper': {12: mm2pt (160)},
310 'afourwide': {12: in2pt (6.5)},
311 'afourlatex': {12: mm2pt (150)},
312 'smallbook': {12: in2pt (5)},
313 'letterpaper': {12: in2pt (6)}}
316 'afourpaper': {12: mm2pt (160)},
317 'afourwide': {12: in2pt (6.5)},
318 'afourlatex': {12: mm2pt (150)},
319 'smallbook': {12: in2pt (5)},
320 'letterpaper': {12: in2pt (6)}}
323 ################################################################
324 # How to output various structures.
330 'output-lilypond': '''<lilypond%s>
333 'output-filename' : r'''
336 <pre>%s</pre></a>:''',
337 'output-lilypond-fragment': '''<lilypond%s>
338 \context Staff\context Voice{ %s }
340 'output-noinline': r'''
341 <!-- generated: %(fn)s.png !-->
345 # Verbatim text is always finished with \n. FIXME: For HTML,
346 # this newline should be removed.
347 'output-verbatim': r'''<pre>
349 # Verbatim text is always finished with \n. FIXME: For HTML,
350 # this newline should be removed.
351 'output-small-verbatim': r'''<font size=-1><pre>
353 ## Ugh we need to differentiate on origin:
354 ## lilypond-block origin wants an extra <p>, but
355 ## inline music doesn't.
356 ## possibly other center options?
358 <a href="%(fn)s.png">
359 <img align="center" valign="center" border="0" src="%(fn)s.png" alt="[picture of music]"></a>
366 'output-lilypond-fragment' : r'''\begin[eps,singleline,%s]{lilypond}
373 'output-filename' : r'''\verb+%s+:\\
377 'output-lilypond': r'''\begin[%s]{lilypond}
381 # verbatim text is always finished with \n
382 'output-verbatim': r'''\begin{verbatim}
385 # verbatim text is always finished with \n
386 'output-small-verbatim': r'''{\small\begin{verbatim}
389 'output-default-post': "\\def\postLilypondExample{}\n",
390 'output-default-pre': "\\def\preLilypondExample{}\n",
391 'usepackage-graphics': '\\usepackage{graphics}\n',
392 'output-eps': '\\noindent\\parbox{\\lilypondepswidth{%(fn)s.eps}}{\includegraphics{%(fn)s}}',
393 'output-noinline': r'''
394 %% generated: %(fn)s.eps
396 'output-latex-quoted': r'''{\preLilypondExample
398 \postLilypondExample}''',
399 'output-latex-noquote': r'''{\parindent 0pt
402 \postLilypondExample}''',
403 'pagebreak': r'\pagebreak',
409 'output-lilypond': '''@lilypond[%s]
413 'output-filename' : r'''@ifnothtml
420 'output-lilypond-fragment': '''@lilypond[%s]
421 \context Staff\context Voice{ %s }
423 'output-noinline': r'''
424 @c generated: %(fn)s.png
427 # verbatim text is always finished with \n
428 'output-small-verbatim': r'''@smallexample
431 # verbatim text is always finished with \n
432 'output-verbatim': r'''@example
435 # do some tweaking: @ is needed in some ps stuff.
437 # ugh, the <p> below breaks inline images...
438 'output-texi-noquote': r'''@tex
446 <p><a href="%(fn)s.png">
447 <img border=0 src="%(fn)s.png" alt="[picture of music]">
451 'output-texi-quoted': r'''@quotation
459 <a href="%(fn)s.png">
460 <img border=0 src="%(fn)s.png" alt="[picture of music]">
469 def output_verbatim (body, small):
472 body = re.sub ('&', '&', body)
473 body = re.sub ('>', '>', body)
474 body = re.sub ('<', '<', body)
475 elif format == 'texi':
476 # clumsy workaround for python 2.2 pre bug.
477 body = re.sub ('@', '@@', body)
478 body = re.sub ('{', '@{', body)
479 body = re.sub ('}', '@}', body)
482 key = 'output-small-verbatim'
484 key = 'output-verbatim'
485 return get_output (key) % body
488 ################################################################
489 # Recognize special sequences in the input
492 # Warning: This uses extended regular expressions. Tread with care.
496 # (?P<name>regex) -- assign result of REGEX to NAME
497 # *? -- match non-greedily.
498 # (?m) -- multiline regex: make ^ and $ match at each line
499 # (?s) -- make the dot match all characters including newline
505 'preamble-end': no_match,
506 'landscape': no_match,
507 'verbatim': r'''(?s)(?P<code><pre>\s.*?</pre>\s)''',
508 'verb': r'''(?P<code><pre>.*?</pre>)''',
509 'lilypond-file': r'(?m)(?P<match><lilypondfile(?P<options>[^>]+)?>\s*(?P<filename>[^<]+)\s*</lilypondfile>)',
510 'lilypond' : '(?m)(?P<match><lilypond((?P<options>[^:]*):)(?P<code>.*?)/>)',
511 'lilypond-block': r'''(?ms)(?P<match><lilypond(?P<options>[^>]+)?>(?P<code>.*?)</lilypond>)''',
512 'option-sep' : '\s*',
513 'intertext': r',?\s*intertext=\".*?\"',
514 'multiline-comment': r"(?sm)\s*(?!@c\s+)(?P<code><!--\s.*?!-->)\s",
515 'singleline-comment': no_match,
517 'multicols': no_match,
518 'ly2dvi': r'(?m)(?P<match><ly2dvifile(?P<options>[^>]+)?>\s*(?P<filename>[^<]+)\s*</ly2dvifile>)',
522 'input': r'(?m)^[^%\n]*?(?P<match>\\mbinput{?([^}\t \n}]*))',
523 'include': r'(?m)^[^%\n]*?(?P<match>\\mbinclude{(?P<filename>[^}]+)})',
524 'option-sep' : ',\s*',
525 'header': r"\n*\\documentclass\s*(\[.*?\])?",
526 'preamble-end': r'(?P<code>\\begin\s*{document})',
527 'verbatim': r"(?s)(?P<code>\\begin\s*{verbatim}.*?\\end{verbatim})",
528 'verb': r"(?P<code>\\verb(?P<del>.).*?(?P=del))",
529 'lilypond-file': r'(?m)^[^%\n]*?(?P<match>\\lilypondfile\s*(\[(?P<options>.*?)\])?\s*\{(?P<filename>.+)})',
530 'lilypond' : r'(?m)^[^%\n]*?(?P<match>\\lilypond\s*(\[(?P<options>.*?)\])?\s*{(?P<code>.*?)})',
531 'lilypond-block': r"(?sm)^[^%\n]*?(?P<match>\\begin\s*(\[(?P<options>.*?)\])?\s*{lilypond}(?P<code>.*?)\\end{lilypond})",
532 'def-post-re': r"\\def\\postLilypondExample",
533 'def-pre-re': r"\\def\\preLilypondExample",
534 'usepackage-graphics': r"\usepackage\s*{graphics}",
535 'intertext': r',?\s*intertext=\".*?\"',
536 'multiline-comment': no_match,
537 'singleline-comment': r"(?m)^.*?(?P<match>(?P<code>^%.*$\n+))",
538 'numcols': r"(?P<code>\\(?P<num>one|two)column)",
539 'multicols': r"(?P<code>\\(?P<be>begin|end)\s*{multicols}({(?P<num>\d+)?})?)",
544 # why do we have distinction between @mbinclude and @include?
547 'include': '(?m)^[^%\n]*?(?P<match>@mbinclude[ \n\t]+(?P<filename>[^\t \n]*))',
550 'preamble-end': no_match,
551 'landscape': no_match,
552 'verbatim': r'''(?s)(?P<code>@example\s.*?@end example\s)''',
553 'verb': r'''(?P<code>@code{.*?})''',
554 'lilypond-file': '(?m)^(?P<match>@lilypondfile(\[(?P<options>[^]]*)\])?{(?P<filename>[^}]+)})',
555 'lilypond' : '(?m)^(?P<match>@lilypond(\[(?P<options>[^]]*)\])?{(?P<code>.*?)})',
556 'lilypond-block': r'''(?ms)^(?P<match>@lilypond(\[(?P<options>[^]]*)\])?\s(?P<code>.*?)@end +lilypond)\s''',
557 'option-sep' : ',\s*',
558 'intertext': r',?\s*intertext=\".*?\"',
559 'multiline-comment': r"(?sm)^\s*(?!@c\s+)(?P<code>@ignore\s.*?@end ignore)\s",
560 'singleline-comment': r"(?m)^.*?(?P<match>(?P<code>@c.*$\n+))",
562 'multicols': no_match,
568 for r in re_dict.keys ():
571 for k in olddict.keys ():
573 newdict[k] = re.compile (olddict[k])
575 print 'invalid regexp: %s' % olddict[k]
577 ## we'd like to catch and reraise a more
578 ## detailed error, but alas, the exceptions
579 ## changed across the 1.5/2.1 boundary.
595 def get_output (name):
596 return output_dict[format][name]
599 return re_dict[format][name]
601 def bounding_box_dimensions (fname):
603 fname = os.path.join (g_outdir, fname)
607 error ("Error opening `%s'" % fname)
609 s = re.search ('%%BoundingBox: ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)', str)
612 gs = map (lambda x: string.atoi (x), s.groups ())
613 return (int (gs[2] - gs[0] + 0.5),
614 int (gs[3] - gs[1] + 0.5))
619 sys.stderr.write ("\n\n" + str + "\nExiting ... \n\n")
623 def compose_full_body (body, opts):
624 '''Construct the lilypond code to send to Lilypond.
625 Add stuff to BODY using OPTS as options.'''
626 music_size = default_music_fontsize
627 if g_force_music_fontsize:
628 music_size = g_force_music_fontsize
633 if not g_force_music_fontsize:
634 m = re.match ('([0-9]+)pt', o)
636 music_size = string.atoi (m.group (1))
638 m = re.match ('indent=([-.0-9]+)(cm|in|mm|pt)', o)
640 f = float (m.group (1))
641 indent = 'indent = %f\\%s' % (f, m.group (2))
643 m = re.match ('linewidth=([-.0-9]+)(cm|in|mm|pt)', o)
645 f = float (m.group (1))
646 linewidth = 'linewidth = %f\\%s' % (f, m.group (2))
648 if re.search ('\\\\score', body):
652 if 'fragment' in opts:
654 if 'nofragment' in opts:
657 if is_fragment and not 'multiline' in opts:
658 opts.append ('singleline')
660 if 'singleline' in opts:
662 linewidth = 'linewidth = -1.0'
664 indent = 'indent = 0.0\mm'
667 l = paperguru.get_linewidth ()
668 linewidth = 'linewidth = %f\pt' % l
670 if 'noindent' in opts:
671 indent = 'indent = 0.0\mm'
677 \remove Time_signature_engraver
682 m= re.search ('relative(.*)', o)
686 v = string.atoi (m.group (1))
693 pitch = pitch + '\,' * v
695 pitch = pitch + '\'' * v
697 body = '\\relative %s { %s }' % (pitch, body)
709 optstring = string.join (opts, ' ')
710 optstring = re.sub ('\n', ' ', optstring)
712 %% Generated automatically by: lilypond-book.py
714 \include "paper%d.ly"
720 ''' % (optstring, music_size, linewidth, indent, notime) + body
722 # ughUGH not original options
725 def scan_html_preamble (chunks):
728 def scan_latex_preamble (chunks):
729 # First we want to scan the \documentclass line
730 # it should be the first non-comment line.
731 # The only thing we really need to know about the \documentclass line
732 # is if there are one or two columns to begin with.
735 if chunks[idx][0] == 'ignore':
738 m = get_re ('header').match (chunks[idx][1])
740 error ("Latex documents must start with a \documentclass command")
742 options = re.split (',[\n \t]*', m.group (1)[1:-1])
745 if 'twocolumn' in options:
746 paperguru.m_num_cols = 2
750 # Then we add everything before \begin{document} to
751 # paperguru.m_document_preamble so that we can later write this header
752 # to a temporary file in find_latex_dims() to find textwidth.
753 while idx < len (chunks) and chunks[idx][0] != 'preamble-end':
754 if chunks[idx] == 'ignore':
757 paperguru.m_document_preamble.append (chunks[idx][1])
760 if len (chunks) == idx:
761 error ("Didn't find end of preamble (\\begin{document})")
763 paperguru.find_latex_dims ()
765 def scan_texi_preamble (chunks):
766 # this is not bulletproof..., it checks the first 10 chunks
767 for c in chunks[:10]:
769 for s in ('afourpaper', 'afourwide', 'letterpaper',
770 'afourlatex', 'smallbook'):
771 if string.find (c[1], "@%s" % s) != -1:
772 paperguru.m_papersize = s
775 def scan_preamble (chunks):
778 scan_html_preamble (chunks)
779 elif format == 'latex':
780 scan_latex_preamble (chunks)
781 elif format == 'texi':
782 scan_texi_preamble (chunks)
785 def completize_preamble (chunks):
787 if format != 'latex':
789 pre_b = post_b = graphics_b = None
791 if chunk[0] == 'preamble-end':
793 if chunk[0] == 'input':
794 m = get_re ('def-pre-re').search (chunk[1])
797 if chunk[0] == 'input':
798 m = get_re ('def-post-re').search (chunk[1])
802 if chunk[0] == 'input':
803 m = get_re ('usepackage-graphics').search (chunk[1])
807 while x < len (chunks) and chunks[x][0] != 'preamble-end':
810 if x == len (chunks):
814 chunks.insert (x, ('input', get_output ('output-default-pre')))
816 chunks.insert (x, ('input', get_output ('output-default-post')))
818 chunks.insert (x, ('input', get_output ('usepackage-graphics')))
824 def find_file (name):
826 Search the include path for NAME. If found, return the (CONTENTS, PATH) of the file.
830 return (sys.stdin.read (), '<stdin>')
833 for a in include_path:
835 nm = os.path.join (a, name)
838 read_files.append (nm)
843 sys.stderr.write ("Reading `%s'\n" % nm)
844 return (f.read (), nm)
846 error ("File not found `%s'\n" % name)
849 def do_ignore (match_object):
850 return [('ignore', match_object.group ('code'))]
851 def do_preamble_end (match_object):
852 return [('preamble-end', match_object.group ('code'))]
854 def make_verbatim (match_object):
855 return [('verbatim', match_object.group ('code'))]
857 def make_verb (match_object):
858 return [('verb', match_object.group ('code'))]
860 def do_include_file (m):
862 return [('input', get_output ('pagebreak'))] \
863 + read_doc_file (m.group ('filename')) \
864 + [('input', get_output ('pagebreak'))]
866 def do_input_file (m):
867 return read_doc_file (m.group ('filename'))
869 def make_lilypond (m):
870 if m.group ('options'):
871 options = m.group ('options')
874 return [('input', get_output ('output-lilypond-fragment') %
875 (options, m.group ('code')))]
877 def make_lilypond_file (m):
880 Find @lilypondfile{bla.ly} occurences and substitute bla.ly
881 into a @lilypond .. @end lilypond block.
885 if m.group ('options'):
886 options = m.group ('options')
889 (content, nm) = find_file (m.group ('filename'))
890 options = "filename=%s," % nm + options
892 return [('input', get_output ('output-lilypond') %
895 def make_ly2dvi_block (m):
898 Find <ly2dvifile .. >
901 return [('ly2dvi', m.group ('filename'), m.group ('options'))]
904 def make_lilypond_block (m):
908 if m.group ('options'):
909 options = get_re ('option-sep').split (m.group ('options'))
912 options = filter (lambda s: s != '', options)
913 return [('lilypond', m.group ('code'), options)]
918 if format != 'latex':
920 if m.group ('num') == 'one':
921 return [('numcols', m.group ('code'), 1)]
922 if m.group ('num') == 'two':
923 return [('numcols', m.group ('code'), 2)]
925 def do_multicols (m):
927 if format != 'latex':
929 if m.group ('be') == 'begin':
930 return [('multicols', m.group ('code'), int (m.group ('num')))]
932 return [('multicols', m.group ('code'), 1)]
935 def chop_chunks (chunks, re_name, func, use_match=0):
941 m = get_re (re_name).search (str)
943 newchunks.append (('input', str))
947 newchunks.append (('input', str[:m.start ('match')]))
949 newchunks.append (('input', str[:m.start (0)]))
950 #newchunks.extend (func (m))
951 # python 1.5 compatible:
952 newchunks = newchunks + func (m)
953 str = str [m.end (0):]
958 def determine_format (str):
961 html = re.search ('(?i)<[dh]tml', str[:200])
962 latex = re.search (r'''\\document''', str[:200])
963 texi = re.search ('@node|@setfilename', str[:200])
968 if html and not latex and not texi:
970 elif latex and not html and not texi:
972 elif texi and not html and not latex:
975 error ("can't determine format, please specify")
979 if paperguru == None:
982 elif format == 'latex':
984 elif format == 'texi':
990 def read_doc_file (filename):
991 '''Read the input file, find verbatim chunks and do \input and \include
993 (str, path) = find_file (filename)
994 determine_format (str)
996 chunks = [('input', str)]
998 # we have to check for verbatim before doing include,
999 # because we don't want to include files that are mentioned
1000 # inside a verbatim environment
1001 chunks = chop_chunks (chunks, 'verbatim', make_verbatim)
1003 chunks = chop_chunks (chunks, 'verb', make_verb)
1004 chunks = chop_chunks (chunks, 'multiline-comment', do_ignore)
1006 chunks = chop_chunks (chunks, 'include', do_include_file, 1)
1007 chunks = chop_chunks (chunks, 'input', do_input_file, 1)
1011 taken_file_names = {}
1013 def unique_file_name (body):
1014 return 'lily-' + `abs (hash (body))`
1016 def schedule_lilypond_block (chunk):
1017 '''Take the body and options from CHUNK, figure out how the
1018 real .ly should look, and what should be left MAIN_STR (meant
1019 for the main file). The .ly is written, and scheduled in
1022 Return: a chunk (TYPE_STR, MAIN_STR, OPTIONS, TODO, BASE)
1024 TODO has format [basename, extension, extension, ... ]
1026 (type, body, opts) = chunk
1027 assert type == 'lilypond'
1028 file_body = compose_full_body (body, opts)
1029 ## Hmm, we should hash only lilypond source, and skip the
1032 basename = unique_file_name (file_body)
1034 m = re.search ('filename="(.*?)"', o)
1036 basename = m.group (1)
1037 if not taken_file_names.has_key (basename):
1038 taken_file_names[basename] = 0
1040 taken_file_names[basename] = taken_file_names[basename] + 1
1041 basename = basename + "-%i" % taken_file_names[basename]
1043 update_file (file_body, os.path.join (g_outdir, basename) + '.ly')
1044 needed_filetypes = ['tex']
1046 if format == 'html' or g_make_html:
1047 needed_filetypes.append ('eps')
1048 needed_filetypes.append ('png')
1049 if 'eps' in opts and not ('eps' in needed_filetypes):
1050 needed_filetypes.append ('eps')
1052 pathbase = os.path.join (g_outdir, basename)
1053 def f (base, ext1, ext2):
1054 a = os.path.isfile (base + ext2)
1055 if (os.path.isfile (base + ext1) and
1056 os.path.isfile (base + ext2) and
1057 os.stat (base+ext1)[stat.ST_MTIME] >
1058 os.stat (base+ext2)[stat.ST_MTIME]) or \
1059 not os.path.isfile (base + ext2):
1062 if 'tex' in needed_filetypes and f (pathbase, '.ly', '.tex'):
1064 if 'eps' in needed_filetypes and f (pathbase, '.tex', '.eps'):
1066 if 'png' in needed_filetypes and f (pathbase, '.eps', '.png'):
1070 if 'printfilename' in opts:
1072 m= re.match ("filename=(.*)", o)
1074 newbody = newbody + get_output ("output-filename") % (m.group (1), basename + '.ly', m.group (1))
1078 if 'smallverbatim' in opts:
1079 newbody = newbody + output_verbatim (body, 1)
1080 elif 'verbatim' in opts:
1081 newbody = newbody + output_verbatim (body, 0)
1084 m = re.search ('intertext="(.*?)"', o)
1086 newbody = newbody + "\n"
1087 if format == 'texi':
1088 newbody = newbody + "@noindent\n"
1089 elif format == 'latex':
1090 newbody = newbody + "\\noindent\n"
1091 newbody = newbody + m.group (1) + "\n"
1093 if 'noinline' in opts:
1094 s = 'output-noinline'
1095 elif format == 'latex':
1099 if 'noquote' in opts:
1100 s = 'output-latex-noquote'
1102 s = 'output-latex-quoted'
1103 elif format == 'texi':
1104 if 'noquote' in opts:
1105 s = 'output-texi-noquote'
1107 s = 'output-texi-quoted'
1108 else: # format == 'html'
1110 newbody = newbody + get_output (s) % {'fn': basename }
1111 return ('lilypond', newbody, opts, todo, basename)
1116 def process_lilypond_blocks (chunks):#ugh rename
1118 # Count sections/chapters.
1120 if c[0] == 'lilypond':
1121 c = schedule_lilypond_block (c)
1122 elif c[0] == 'numcols':
1123 paperguru.m_num_cols = c[2]
1124 elif c[0] == 'multicols':
1125 paperguru.m_multicols = c[2]
1126 newchunks.append (c)
1129 def process_ly2dvi_blocks (chunks):
1131 def process_ly2dvi_block (chunk):
1134 Run ly2dvi script on filename specified in CHUNK.
1135 This is only supported for HTML output.
1137 In HTML output it will leave a download menu with ps/pdf/midi etc. in
1138 a separate HTML file, and a title + preview in the main html file,
1139 linking to the menu.
1142 (tag, name, opts) = chunk
1143 assert format == 'html'
1144 (content, original_name) = find_file (name)
1146 original_name = os.path.basename (original_name)
1148 base = unique_file_name (content)
1149 outname = base + '.ly'
1150 changed = update_file (content, outname)
1152 preview = base + ".png"
1153 if changed or not os.path.isfile (preview):
1154 ly.system ('ly2dvi --preview --postscript --verbose %s ' % base)
1156 ly.make_page_images (base)
1157 ly.system ('gzip -9 - < %s.ps > %s.ps.gz' % (base, base))
1160 b = os.stat(fn)[stat.ST_SIZE]
1162 return '%d bytes' % b
1164 return '%d kb' % (b >> 10)
1166 return '%d mb' % (b >> 20)
1169 'pdf' : "Print (PDF, %s)",
1170 'ps.gz' : "Print (gzipped PostScript, %s)",
1171 'png' : "View (PNG, %s)",
1172 'midi' : "Listen (MIDI, %s)",
1173 'ly' : "View source code (%s)",
1177 page_files = ly.read_pipe ('ls --color=no -1 %s-page*.png' % base)
1179 for p in string.split (page_files, '\n'):
1181 if os.path.isfile (p):
1183 page = re.sub ('.*page([0-9])+.*', 'View page \\1 (PNG picture, %s)\n', p)
1185 menu += '<li><a href="%s">%s</a>' % (p, page)
1187 ext_order = ['ly', 'pdf', 'ps.gz', 'midi']
1190 print 'checking,' , fn
1191 if not os.path.isfile (fn):
1194 entry = exts[e] % size_str (fn)
1196 ## TODO: do something like
1197 ## this for texinfo/latex as well ?
1199 menu += '<li><a href="%s">%s</a>\n\n' % (fn, entry)
1202 explanatory_para = """The pictures are 90dpi
1203 anti-aliased snapshots of the printed output, in PNG format. Both PDF and PS
1204 use scalable fonts and should look OK at any resolution."""
1207 <title>LilyPond example %s</title>
1213 <ul>%s</ul>''' % (original_name,original_name, preview, explanatory_para, menu)
1215 open (base + '.html','w'). write (separate_menu)
1217 inline_menu = '<p/><a href="%s.html"><img src="%s"><p/></a>' % (base, original_name, preview)
1219 return ('ly2dvi', inline_menu)
1223 if c[0] == 'ly2dvi':
1224 c = process_ly2dvi_block (c)
1225 newchunks.append (c)
1229 def compile_all_files (chunks):
1236 if c[0] != 'lilypond':
1245 if base + '.ly' not in tex:
1246 tex.append (base + '.ly')
1247 elif e == 'png' and g_do_pictures:
1253 # fixme: be sys-independent.
1255 if g_outdir and x[0] != '/' :
1256 x = os.path.join (g_here_dir, x)
1259 incs = map (incl_opt, include_path)
1260 lilyopts = string.join (incs)
1262 lilyopts += ' --dependencies'
1264 lilyopts += ' --dep-prefix=' + g_outdir + '/'
1265 lilyopts += ' --header=texidoc'
1266 texfiles = string.join (tex)
1267 cmd = string.join ((lilypond_cmd, lilyopts, g_extra_opts,
1269 ly.system (cmd, ignore_error = 0, progress_p = 1)
1272 # Ugh, fixing up dependencies for .tex generation
1275 depfiles=map (lambda x: re.sub ('(.*)\.ly', '\\1.dep',
1282 text=re.sub ('\n([^:\n]*):',
1283 '\n' + foutn + ':', text)
1289 cmd = r"latex '\nonstopmode \input %s'" % file
1290 # Ugh. (La)TeX writes progress and error messages on stdout
1291 # Redirect to stderr
1292 cmd += ' 1>/dev/stderr'
1294 ly.system ("dvips -E -o %s.eps %s" % (file, file))
1297 map (ly.make_preview, png)
1301 def update_file (body, name):
1303 write the body if it has changed. Return whether BODY has changed.
1314 f = open (name , 'w')
1321 def write_deps (fn, target, chunks):
1323 sys.stderr.write ('Writing `%s\'\n' % os.path.join (g_outdir, fn))
1324 f = open (os.path.join (g_outdir, fn), 'w')
1325 f.write ('%s%s: ' % (g_dep_prefix, target))
1326 for d in read_files:
1330 if c[0] == 'lilypond':
1331 (type, body, opts, todo, basename) = c;
1332 basenames.append (basename)
1335 d=g_outdir + '/' + d
1337 #if not os.isfile (d): # thinko?
1338 if not re.search ('/', d):
1339 d = g_dep_prefix + d
1340 f.write ('%s.tex ' % d)
1342 #if len (basenames):
1343 # for d in basenames:
1344 # f.write ('%s.ly ' % d)
1345 # f.write (' : %s' % target)
1350 def check_texidoc (chunks):
1353 if c[0] == 'lilypond':
1354 (type, body, opts, todo, basename) = c;
1355 pathbase = os.path.join (g_outdir, basename)
1356 if os.path.isfile (pathbase + '.texidoc') \
1357 and 'notexidoc' not in opts:
1358 body = '\n@include %s.texidoc\n' % basename + body
1359 c = (type, body, opts, todo, basename)
1364 ## what's this? Docme --hwn
1366 def fix_epswidth (chunks):
1369 if c[0] != 'lilypond' or 'eps' not in c[2]:
1370 newchunks.append (c)
1375 m = re.match ('magnification=([0-9.]+)', o)
1377 mag = string.atof (m.group (1))
1379 def replace_eps_dim (match, lmag = mag):
1380 filename = match.group (1)
1381 dims = bounding_box_dimensions (filename)
1383 return '%fpt' % (dims[0] *lmag)
1385 body = re.sub (r'''\\lilypondepswidth{(.*?)}''', replace_eps_dim, c[1])
1386 newchunks.append (('lilypond', body, c[2], c[3], c[4]))
1391 ##docme: why global?
1394 def do_file (input_filename):
1395 chunks = read_doc_file (input_filename)
1396 chunks = chop_chunks (chunks, 'ly2dvi', make_ly2dvi_block, 1)
1397 chunks = chop_chunks (chunks, 'lilypond', make_lilypond, 1)
1398 chunks = chop_chunks (chunks, 'lilypond-file', make_lilypond_file, 1)
1399 chunks = chop_chunks (chunks, 'lilypond-block', make_lilypond_block, 1)
1400 chunks = chop_chunks (chunks, 'singleline-comment', do_ignore, 1)
1401 chunks = chop_chunks (chunks, 'preamble-end', do_preamble_end)
1402 chunks = chop_chunks (chunks, 'numcols', do_columns)
1403 chunks = chop_chunks (chunks, 'multicols', do_multicols)
1405 #for c in chunks: print "c:", c;
1407 scan_preamble (chunks)
1408 chunks = process_lilypond_blocks (chunks)
1409 chunks = process_ly2dvi_blocks (chunks)
1412 global g_run_lilypond
1414 compile_all_files (chunks)
1415 chunks = fix_epswidth (chunks)
1418 if format == 'texi':
1419 chunks = check_texidoc (chunks)
1422 chunks = completize_preamble (chunks)
1427 my_outname = outname
1428 elif input_filename == '-' or input_filename == "/dev/stdin":
1431 my_outname = os.path.basename (os.path.splitext (input_filename)[0]) + '.' + format
1432 my_depname = my_outname + '.dep'
1434 if my_outname == '-' or my_outname == '/dev/stdout':
1440 foutn = os.path.join (g_outdir, my_outname)
1441 sys.stderr.write ("Writing `%s'\n" % foutn)
1442 fout = open (foutn, 'w')
1449 write_deps (my_depname, foutn, chunks)
1453 (sh, long) = ly.getopt_args (option_definitions)
1454 (options, files) = getopt.getopt (sys.argv[1:], sh, long)
1456 except getopt.error, msg:
1457 sys.stderr.write ('\n')
1458 ly.error (_ ("getopt says: `%s\'" % s))
1459 sys.stderr.write ('\n')
1468 if o == '--include' or o == '-I':
1469 include_path.append (a)
1470 elif o == '--version' or o == '-v':
1471 ly.identify (sys.stdout)
1473 elif o == '--verbose' or o == '-V':
1475 elif o == '--format' or o == '-f':
1477 if a == 'texi-html':
1480 elif o == '--outname' or o == '-o':
1483 sys.stderr.write ("Lilypond-book is confused by --outname on multiple files")
1486 elif o == '--help' or o == '-h':
1489 elif o == '--no-lily' or o == '-n':
1491 elif o == '--preview-resolution':
1492 preview_resolution = string.atoi (a)
1493 elif o == '--dependencies' or o == '-M':
1495 elif o == '--default-music-fontsize':
1496 default_music_fontsize = string.atoi (a)
1497 elif o == '--default-lilypond-fontsize':
1498 print "--default-lilypond-fontsize is deprecated, use --default-music-fontsize"
1499 default_music_fontsize = string.atoi (a)
1500 elif o == '--extra-options':
1502 elif o == '--force-music-fontsize':
1503 g_force_music_fontsize = string.atoi (a)
1504 elif o == '--force-lilypond-fontsize':
1505 print "--force-lilypond-fontsize is deprecated, use --default-lilypond-fontsize"
1506 g_force_music_fontsize = string.atoi (a)
1507 elif o == '--dep-prefix':
1509 elif o == '--no-pictures':
1511 elif o == '--no-music':
1513 elif o == '--read-lys':
1515 elif o == '--outdir':
1517 elif o == '--warranty' or o == '-w':
1518 #status = os.system ('lilypond -w')
1523 ly.identify (sys.stderr)
1526 if os.path.isfile (g_outdir):
1527 error ("outdir is a file: %s" % g_outdir)
1528 if not os.path.exists (g_outdir):
1533 ly.error (_ ("no files specified on command line"))
1536 ly.setup_environment ()
1538 for input_filename in files:
1539 do_file (input_filename)
1543 # Petr, ik zou willen dat ik iets zinvoller deed,
1544 # maar wat ik kan ik doen, het verandert toch niets?