7 lilypond-book --filter="tr '[a-z]' '[A-Z]'" BOOK
10 lilypond-book --filter="convert-ly --no-version --from=1.6.11 -" BOOK
12 classic lilypond-book:
13 lilypond-book --process="lilypond" BOOK.tely
17 * this script is too complex. Modularize.
19 * ly-options: intertext?
21 * eps in latex / eps by lilypond -b ps?
22 * check latex parameters, twocolumn, multicolumn?
23 * use --png --ps --pdf for making images?
25 * Converting from lilypond-book source, substitute:
26 @mbinclude foo.itely -> @include foo.itely
42 # Users of python modules should include this snippet
43 # and customize variables below.
45 # We'll suffer this path initialization stuff as long as we don't install
46 # our python packages in <prefix>/lib/pythonX.Y
48 # If set, LILYPONDPREFIX must take prevalence.
49 # if datadir is not set, we're doing a build and LILYPONDPREFIX.
55 datadir = '@local_lilypond_datadir@'
56 if not os.path.isdir (datadir):
57 datadir = '@lilypond_datadir@'
59 sys.path.insert (0, os.path.join (datadir, 'python'))
61 if os.environ.has_key ('LILYPONDPREFIX'):
62 datadir = os.environ['LILYPONDPREFIX']
63 while datadir[-1] == os.sep:
66 datadir = os.path.join (datadir, "share/lilypond/current/")
67 sys.path.insert (0, os.path.join (datadir, 'python'))
69 # dynamic relocation, for GUB binaries.
70 bindir = os.path.split (sys.argv[0])[0]
71 for p in ['share', 'lib']:
72 datadir = os.path.abspath (bindir + '/../%s/lilypond/current/python/' % p)
73 sys.path.insert (0, os.path.join (datadir))
82 program_version = '@TOPLEVEL_VERSION@'
83 program_name = os.path.basename (sys.argv[0])
85 original_dir = os.getcwd ()
89 '''Process LilyPond snippets in hybrid HTML, LaTeX, or texinfo document.
93 lilypond-book --filter="tr '[a-z]' '[A-Z]'" BOOK
94 lilypond-book --filter="convert-ly --no-version --from=2.0.0 -" BOOK
95 lilypond-book --process='lilypond -I include' BOOK
98 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>',
99 'Han-Wen Nienhuys <hanwen@cs.uu.nl>')
101 def get_option_parser ():
102 p = ly.get_option_parser (usage='lilypond-book [OPTIONS] FILE',
103 version="@TOPLEVEL_VERSION@",
104 description=help_summary)
106 p.add_option ('-F', '--filter', metavar=_ ("FILTER"),
109 help=_ ("pipe snippets through FILTER [convert-ly -n -]"),
111 p.add_option ('-f', '--format', help=_('''use output format FORMAT (texi [default], texi-html, latex, html)'''),
113 p.add_option ("-I", '--include', help=_('add DIR to include path'),
115 action='append', dest='include_path',
116 default=[os.path.abspath (os.getcwd ())])
118 p.add_option ("-o", '--output', help=_('write output to DIR'),
120 action='store', dest='output_name', default=None)
121 p.add_option ('-P', '--process', metavar=_("COMMAND"),
122 help = _ ("process ly_files using COMMAND FILE..."),
124 dest='process_cmd', default='lilypond -b eps')
126 p.add_option ('', '--psfonts', action="store_true", dest="psfonts",
127 help=_ ('''extract all PostScript fonts into INPUT.psfonts for LaTeX'''
128 '''must use this with dvips -h INPUT.psfonts'''),
130 p.add_option ('-V', '--verbose', help=_("be verbose"), action="store_true",
133 p.add_option ('-w', '--warranty', help=_("show warranty and copyright"),
134 action='store_true'),
137 p.add_option_group ('bugs',
138 description='''Report bugs via http://post.gmane.org/post.php'''
139 '''?group=gmane.comp.gnu.lilypond.bugs\n''')
143 lilypond_binary = os.path.join ('@bindir@', 'lilypond')
145 # Only use installed binary when we are installed too.
146 if '@bindir@' == ('@' + 'bindir@') or not os.path.exists (lilypond_binary):
147 lilypond_binary = 'lilypond'
149 global_options = None
152 default_ly_options = { 'alt': "[image of music]" }
155 # Is this pythonic? Personally, I find this rather #define-nesque. --hwn
159 EXAMPLEINDENT = 'exampleindent'
161 FRAGMENT = 'fragment'
166 LINE_WIDTH = 'line-width'
167 NOFRAGMENT = 'nofragment'
168 NOINDENT = 'noindent'
173 OUTPUTIMAGE = 'outputimage'
176 PREAMBLE = 'preamble'
177 PRINTFILENAME = 'printfilename'
179 RAGGED_RIGHT = 'ragged-right'
180 RELATIVE = 'relative'
181 STAFFSIZE = 'staffsize'
184 VERBATIM = 'verbatim'
185 FONTLOAD = 'fontload'
186 FILENAME = 'filename'
190 # NOTIME has no opposite so it isn't part of this dictionary.
191 # NOQUOTE is used internally only.
193 NOFRAGMENT: FRAGMENT,
198 # Recognize special sequences in the input.
200 # (?P<name>regex) -- Assign result of REGEX to NAME.
201 # *? -- Match non-greedily.
202 # (?m) -- Multiline regex: Make ^ and $ match at each line.
203 # (?s) -- Make the dot match all characters including newline.
204 # (?x) -- Ignore whitespace in patterns.
216 (\s*(?P<options>.*?)\s*:)?\s*
224 \s*(?P<options>.*?)\s*
233 \s*(?P<options>.*?)\s*
235 \s*(?P<filename>.*?)\s*
242 (?P<code><!--\s.*?!-->)
245 'singleline_comment':
251 (?P<code><pre>.*?</pre>))''',
257 (?P<code><pre>\s.*?</pre>\s))''',
276 \s*(?P<options>.*?)\s*
287 \s*(?P<options>.*?)\s*
291 \\end\s*{lilypond})''',
299 \s*(?P<options>.*?)\s*
307 'singleline_comment':
330 \\end\s*{verbatim}))''',
339 (?P<filename>\S+))''',
343 ^[^\n]*?(?!@c\s+)[^\n]*?
347 \s*(?P<options>.*?)\s*
357 \s*(?P<options>.*?)\s*
360 ^@end\s+lilypond)\s''',
367 \s*(?P<options>.*?)\s*
378 @end\s+ignore))\s''',
380 'singleline_comment':
385 @c([ \t][^\n]*|)\n))''',
387 # Don't do this: It interferes with @code{@{}.
388 # 'verb': r'''(?P<code>@code{.*?})''',
396 @end\s+example\s))''',
405 'intertext': r',?\s*intertext=\".*?\"',
410 'intertext': r',?\s*intertext=\".*?\"',
411 'option_sep': '\s*,\s*',
415 'intertext': r',?\s*intertext=\".*?\"',
416 'option_sep': '\s*,\s*',
420 # Options without a pattern in ly_options.
437 RELATIVE: r'''\relative c%(relative_quotes)s''',
442 INDENT: r'''indent = %(indent)s''',
444 LINE_WIDTH: r'''line-width = %(line-width)s''',
446 QUOTE: r'''line-width = %(line-width)s - 2.0 * %(exampleindent)s''',
448 RAGGED_RIGHT: r'''ragged-right = ##t''',
450 PACKED: r'''packed = ##t''',
462 \remove Time_signature_engraver
468 STAFFSIZE: r'''#(set-global-staff-size %(staffsize)s)''',
475 FILTER: r'''<lilypond %(options)s>
485 <a href="%(base)s.ly">''',
488 <img align="center" valign="center"
489 border="0" src="%(image)s" alt="%(alt)s">''',
491 PRINTFILENAME: '<p><tt><a href="%(base)s.ly">%(filename)s</a></tt></p>',
493 QUOTE: r'''<blockquote>
507 \ifx\preLilyPondExample \undefined%%
510 \preLilyPondExample%%
512 \def\lilypondbook{}%%
513 \input %(base)s-systems.tex%%
514 \ifx\postLilyPondExample \undefined%%
517 \postLilyPondExample%%
521 PRINTFILENAME: '''\\texttt{%(filename)s}
524 QUOTE: r'''\begin{quotation}%(str)s
527 VERBATIM: r'''\noindent
528 \begin{verbatim}%(verb)s\end{verbatim}''',
530 FILTER: r'''\begin{lilypond}[%(options)s]
537 FILTER: r'''@lilypond[%(options)s]
543 @include %(base)s-systems.texi
547 OUTPUTIMAGE: r'''@noindent
549 @image{%(base)s,,,%(alt)s,%(ext)s}
553 <a href="%(base)s.ly">
554 <img align="center" valign="center"
555 border="0" src="%(image)s" alt="%(alt)s">
561 PRINTFILENAME: '''@file{%(filename)s}
564 QUOTE: r'''@quotation
565 %(str)s@end quotation
572 VERBATIM: r'''@exampleindent 0
580 # Maintain line numbers.
585 for f in [HTML, LATEX]:
586 for s in (QUOTE, VERBATIM):
587 output[f][s] = output[f][s].replace("\n"," ")
590 PREAMBLE_LY = '''%%%% Generated by %(program_name)s
591 %%%% Options: [%(option_string)s]
593 #(set! toplevel-score-handler print-score-with-defaults)
594 #(set! toplevel-music-handler
596 (if (not (eq? (ly:music-property m \'void) #t))
597 (print-score-with-defaults
598 p (scorify-music m p)))))
600 #(ly:set-option (quote no-point-and-click))
601 #(define inside-lilypond-book #t)
602 #(define version-seen? #t)
610 %% ****************************************************************
611 %% Start cut-&-pastable-section
612 %% ****************************************************************
615 #(define dump-extents #t)
616 %(font_dump_setting)s
630 %% ****************************************************************
631 %% ly snippet contents follows:
632 %% ****************************************************************
636 %% ****************************************************************
638 %% ****************************************************************
645 %% ****************************************************************
647 %% ****************************************************************
651 %% ****************************************************************
653 %% ****************************************************************
656 texinfo_line_widths = {
657 '@afourpaper': '160\\mm',
658 '@afourwide': '6.5\\in',
659 '@afourlatex': '150\\mm',
660 '@smallbook': '5\\in',
661 '@letterpaper': '6\\in',
664 def classic_lilypond_book_compatibility (key, value):
665 if key == 'singleline' and value == None:
666 return (RAGGED_RIGHT, None)
668 m = re.search ('relative\s*([-0-9])', key)
670 return ('relative', m.group (1))
672 m = re.match ('([0-9]+)pt', key)
674 return ('staffsize', m.group (1))
676 if key == 'indent' or key == 'line-width':
677 m = re.match ('([-.0-9]+)(cm|in|mm|pt|staffspace)', value)
679 f = float (m.group (1))
680 return (key, '%f\\%s' % (f, m.group (2)))
684 def find_file (name):
685 for i in global_options.include_path:
686 full = os.path.join (i, name)
687 if os.path.exists (full):
689 ly.error (_ ("file not found: %s") % name + '\n')
693 def verbatim_html (s):
694 return re.sub ('>', '>',
696 re.sub ('&', '&', s)))
698 def verbatim_texinfo (s):
699 return re.sub ('{', '@{',
701 re.sub ('@', '@@', s)))
703 def split_options (option_string):
705 if global_options.format == HTML:
706 options = re.findall('[\w\.-:]+(?:\s*=\s*(?:"[^"]*"|\'[^\']*\'|\S+))?',option_string)
707 for i in range(len(options)):
708 options[i] = re.sub('^([^=]+=\s*)(?P<q>["\'])(.*)(?P=q)','\g<1>\g<3>',options[i])
711 return re.split (format_res[global_options.format]['option_sep'],
715 def set_default_options (source):
716 global default_ly_options
717 if not default_ly_options.has_key (LINE_WIDTH):
718 if global_options.format == LATEX:
719 textwidth = get_latex_textwidth (source)
720 default_ly_options[LINE_WIDTH] = \
721 '''%.0f\\pt''' % textwidth
722 elif global_options.format == TEXINFO:
723 for (k, v) in texinfo_line_widths.items ():
724 # FIXME: @layout is usually not in
727 # \input texinfo @c -*-texinfo-*-
729 # Bluntly search first K items of
731 # s = chunks[0].replacement_text ()
732 if re.search (k, source[:1024]):
733 default_ly_options[LINE_WIDTH] = v
737 def replacement_text (self):
740 def filter_text (self):
741 return self.replacement_text ()
743 def ly_is_outdated (self):
746 def png_is_outdated (self):
752 class Substring (Chunk):
753 def __init__ (self, source, start, end, line_number):
757 self.line_number = line_number
758 self.override_text = None
763 def replacement_text (self):
764 if self.override_text:
765 return self.override_text
767 return self.source[self.start:self.end]
769 class Snippet (Chunk):
770 def __init__ (self, type, match, format, line_number):
774 self.option_dict = {}
776 self.line_number = line_number
778 def replacement_text (self):
779 return self.match.group ('match')
781 def substring (self, s):
782 return self.match.group (s)
785 return `self.__class__` + ' type = ' + self.type
787 class Include_snippet (Snippet):
788 def processed_filename (self):
789 f = self.substring ('filename')
790 return os.path.splitext (f)[0] + format2ext[global_options.format]
792 def replacement_text (self):
793 s = self.match.group ('match')
794 f = self.substring ('filename')
796 return re.sub (f, self.processed_filename (), s)
798 class Lilypond_snippet (Snippet):
799 def __init__ (self, type, match, format, line_number):
800 Snippet.__init__ (self, type, match, format, line_number)
801 os = match.group ('options')
802 self.do_options (os, self.type)
805 return self.substring ('code')
810 return self.compose_ly (s)
813 def do_options (self, option_string, type):
814 self.option_dict = {}
816 options = split_options (option_string)
819 if string.find (i, '=') > 0:
820 (key, value) = re.split ('\s*=\s*', i)
821 self.option_dict[key] = value
823 if i in no_options.keys ():
824 if no_options[i] in self.option_dict.keys ():
825 del self.option_dict[no_options[i]]
827 self.option_dict[i] = None
829 has_line_width = self.option_dict.has_key (LINE_WIDTH)
830 no_line_width_value = 0
832 # If LINE_WIDTH is used without parameter, set it to default.
833 if has_line_width and self.option_dict[LINE_WIDTH] == None:
834 no_line_width_value = 1
835 del self.option_dict[LINE_WIDTH]
837 for i in default_ly_options.keys ():
838 if i not in self.option_dict.keys ():
839 self.option_dict[i] = default_ly_options[i]
841 if not has_line_width:
842 if type == 'lilypond' or FRAGMENT in self.option_dict.keys ():
843 self.option_dict[RAGGED_RIGHT] = None
845 if type == 'lilypond':
846 if LINE_WIDTH in self.option_dict.keys ():
847 del self.option_dict[LINE_WIDTH]
849 if RAGGED_RIGHT in self.option_dict.keys ():
850 if LINE_WIDTH in self.option_dict.keys ():
851 del self.option_dict[LINE_WIDTH]
853 if QUOTE in self.option_dict.keys () or type == 'lilypond':
854 if LINE_WIDTH in self.option_dict.keys ():
855 del self.option_dict[LINE_WIDTH]
857 if not INDENT in self.option_dict.keys ():
858 self.option_dict[INDENT] = '0\\mm'
860 # The QUOTE pattern from ly_options only emits the `line-width'
862 if has_line_width and QUOTE in self.option_dict.keys ():
863 if no_line_width_value:
864 del self.option_dict[LINE_WIDTH]
866 del self.option_dict[QUOTE]
868 def compose_ly (self, code):
869 if FRAGMENT in self.option_dict.keys ():
877 # The original concept of the `exampleindent' option is broken.
878 # It is not possible to get a sane value for @exampleindent at all
879 # without processing the document itself. Saying
887 # causes ugly results with the DVI backend of texinfo since the
888 # default value for @exampleindent isn't 5em but 0.4in (or a smaller
889 # value). Executing the above code changes the environment
890 # indentation to an unknown value because we don't know the amount
891 # of 1em in advance since it is font-dependent. Modifying
892 # @exampleindent in the middle of a document is simply not
893 # supported within texinfo.
895 # As a consequence, the only function of @exampleindent is now to
896 # specify the amount of indentation for the `quote' option.
898 # To set @exampleindent locally to zero, we use the @format
899 # environment for non-quoted snippets.
900 override[EXAMPLEINDENT] = r'0.4\in'
901 override[LINE_WIDTH] = texinfo_line_widths['@smallbook']
902 override.update (default_ly_options)
905 for (key, value) in self.option_dict.items ():
907 option_list.append (key)
909 option_list.append (key + '=' + value)
910 option_string = string.join (option_list, ',')
913 compose_types = [NOTES, PREAMBLE, LAYOUT, PAPER]
914 for a in compose_types:
917 for (key, value) in self.option_dict.items ():
919 classic_lilypond_book_compatibility (key, value)
923 (_ ("deprecated ly-option used: %s=%s" \
926 (_ ("compatibility mode translation: %s=%s" \
930 (_ ("deprecated ly-option used: %s" \
933 (_ ("compatibility mode translation: %s" \
936 (key, value) = (c_key, c_value)
939 override[key] = value
941 if not override.has_key (key):
945 for type in compose_types:
946 if ly_options[type].has_key (key):
947 compose_dict[type].append (ly_options[type][key])
951 if not found and key not in simple_options:
952 ly.warning (_ ("ignoring unknown ly option: %s") % key)
955 if RELATIVE in override.keys () and override[RELATIVE]:
956 relative = string.atoi (override[RELATIVE])
962 relative_quotes += ',' * (- relative)
964 relative_quotes += "'" * relative
966 program_name = __main__.program_name
968 paper_string = string.join (compose_dict[PAPER],
970 layout_string = string.join (compose_dict[LAYOUT],
972 notes_string = string.join (compose_dict[NOTES],
974 preamble_string = string.join (compose_dict[PREAMBLE],
977 font_dump_setting = ''
978 if FONTLOAD in self.option_dict:
979 font_dump_setting = '#(define-public force-eps-font-include #t)\n'
981 return (PREAMBLE_LY + body) % vars ()
986 self.hash = abs (hash (self.full_ly ()))
990 if FILENAME in self.option_dict:
991 return self.option_dict[FILENAME]
992 if global_options.use_hash:
993 return 'lily-%d' % self.get_hash ()
997 outf = open (self.basename () + '.ly', 'w')
998 outf.write (self.full_ly ())
1000 open (self.basename () + '.txt', 'w').write ('image of music')
1002 def ly_is_outdated (self):
1003 base = self.basename ()
1005 tex_file = '%s.tex' % base
1006 eps_file = '%s.eps' % base
1007 system_file = '%s-systems.tex' % base
1008 ly_file = '%s.ly' % base
1009 ok = os.path.exists (ly_file) \
1010 and os.path.exists (system_file)\
1011 and os.stat (system_file)[stat.ST_SIZE] \
1012 and re.match ('% eof', open (system_file).readlines ()[-1])
1013 if ok and (not global_options.use_hash or FILENAME in self.option_dict):
1014 ok = (self.full_ly () == open (ly_file).read ())
1016 # TODO: Do something smart with target formats
1017 # (ps, png) and m/ctimes.
1021 def png_is_outdated (self):
1022 base = self.basename ()
1023 ok = self.ly_is_outdated ()
1024 if global_options.format in (HTML, TEXINFO):
1025 ok = ok and os.path.exists (base + '.eps')
1029 page_count = ly.ps_page_count (base + '.eps')
1032 ok = ok and os.path.exists (base + '.png')
1033 elif page_count > 1:
1034 for a in range (1, page_count + 1):
1035 ok = ok and os.path.exists (base + '-page%d.png' % a)
1039 def texstr_is_outdated (self):
1043 base = self.basename ()
1044 ok = self.ly_is_outdated ()
1045 ok = ok and (os.path.exists (base + '.texstr'))
1048 def filter_text (self):
1049 code = self.substring ('code')
1050 s = run_filter (code)
1053 'options': self.match.group ('options')
1056 return output[self.format][FILTER] % d
1058 def replacement_text (self):
1059 func = Lilypond_snippet.__dict__['output_' + self.format]
1062 def get_images (self):
1063 base = self.basename ()
1065 single = '%(base)s.png' % vars ()
1066 multiple = '%(base)s-page1.png' % vars ()
1068 if os.path.exists (multiple) \
1069 and (not os.path.exists (single) \
1070 or (os.stat (multiple)[stat.ST_MTIME] \
1071 > os.stat (single)[stat.ST_MTIME])):
1072 count = ly.ps_page_count ('%(base)s.eps' % vars ())
1073 images = ['%s-page%d.png' % (base, a) for a in range (1, count+1)]
1074 images = tuple (images)
1077 def output_html (self):
1079 base = self.basename ()
1080 if global_options.format == HTML:
1081 str += self.output_print_filename (HTML)
1082 if VERBATIM in self.option_dict:
1083 verb = verbatim_html (self.substring ('code'))
1084 str += write (output[HTML][VERBATIM] % vars ())
1085 if QUOTE in self.option_dict:
1086 str = output[HTML][QUOTE] % vars ()
1088 str += output[HTML][BEFORE] % vars ()
1089 for image in self.get_images ():
1090 (base, ext) = os.path.splitext (image)
1091 alt = self.option_dict[ALT]
1092 str += output[HTML][OUTPUT] % vars ()
1093 str += output[HTML][AFTER] % vars ()
1096 def output_info (self):
1098 for image in self.get_images ():
1099 (base, ext) = os.path.splitext (image)
1101 # URG, makeinfo implicitly prepends dot to extension.
1102 # Specifying no extension is most robust.
1104 alt = self.option_dict[ALT]
1105 str += output[TEXINFO][OUTPUTIMAGE] % vars ()
1107 base = self.basename ()
1108 str += output[global_options.format][OUTPUT] % vars ()
1111 def output_latex (self):
1113 base = self.basename ()
1114 if global_options.format == LATEX:
1115 str += self.output_print_filename (LATEX)
1116 if VERBATIM in self.option_dict:
1117 verb = self.substring ('code')
1118 str += (output[LATEX][VERBATIM] % vars ())
1120 str += (output[LATEX][OUTPUT] % vars ())
1122 ## todo: maintain breaks
1124 breaks = self.ly ().count ("\n")
1125 str += "".ljust (breaks, "\n").replace ("\n","%\n")
1127 if QUOTE in self.option_dict:
1128 str = output[LATEX][QUOTE] % vars ()
1131 def output_print_filename (self, format):
1133 if PRINTFILENAME in self.option_dict:
1134 base = self.basename ()
1135 filename = self.substring ('filename')
1136 str = output[global_options.format][PRINTFILENAME] % vars ()
1140 def output_texinfo (self):
1142 if self.output_print_filename (TEXINFO):
1144 + self.output_print_filename (HTML)
1147 + self.output_print_filename (LATEX)
1149 base = self.basename ()
1150 if TEXIDOC in self.option_dict:
1151 texidoc = base + '.texidoc'
1152 if os.path.exists (texidoc):
1153 str += '@include %(texidoc)s\n\n' % vars ()
1155 if VERBATIM in self.option_dict:
1156 verb = verbatim_texinfo (self.substring ('code'))
1157 str += (output[TEXINFO][VERBATIM] % vars ())
1158 if not QUOTE in self.option_dict:
1159 str = output[TEXINFO][NOQUOTE] % vars ()
1161 str += self.output_info ()
1163 # str += ('@ifinfo\n' + self.output_info () + '\n@end ifinfo\n')
1164 # str += ('@tex\n' + self.output_latex () + '\n@end tex\n')
1165 # str += ('@html\n' + self.output_html () + '\n@end html\n')
1167 if QUOTE in self.option_dict:
1168 str = output[TEXINFO][QUOTE] % vars ()
1170 # need par after image
1175 class Lilypond_file_snippet (Lilypond_snippet):
1177 name = self.substring ('filename')
1178 return '\\sourcefilename \"%s\"\n%s' \
1179 % (name, open (find_file (name)).read ())
1181 snippet_type_to_class = {
1182 'lilypond_file': Lilypond_file_snippet,
1183 'lilypond_block': Lilypond_snippet,
1184 'lilypond': Lilypond_snippet,
1185 'include': Include_snippet,
1188 def find_linestarts (s):
1193 i = s.find ('\n', start)
1201 nls.append (len (s))
1204 def find_toplevel_snippets (s, types):
1207 res[i] = ly.re.compile (snippet_res[global_options.format][i])
1211 ## found = dict (map (lambda x: (x, None),
1215 map (lambda x, f = found: f.setdefault (x, None),
1218 line_starts = find_linestarts (s)
1220 # We want to search for multiple regexes, without searching
1221 # the string multiple times for one regex.
1222 # Hence, we use earlier results to limit the string portion
1224 # Since every part of the string is traversed at most once for
1225 # every type of snippet, this is linear.
1231 if not found[type] or found[type][0] < index:
1234 m = res[type].search (s[index:endex])
1239 if snippet_type_to_class.has_key (type):
1240 cl = snippet_type_to_class[type]
1243 start = index + m.start ('match')
1244 line_number = line_start_idx
1245 while (line_starts[line_number] < start):
1249 snip = cl (type, m, global_options.format, line_number)
1251 found[type] = (start, snip)
1255 or found[type][0] < found[first][0]):
1260 # Limiting the search space is a cute
1261 # idea, but this *requires* to search
1262 # for possible containing blocks
1263 # first, at least as long as we do not
1264 # search for the start of blocks, but
1265 # always/directly for the entire
1266 # @block ... @end block.
1268 endex = found[first][0]
1271 snippets.append (Substring (s, index, len (s), line_start_idx))
1274 while (start > line_starts[line_start_idx+1]):
1277 (start, snip) = found[first]
1278 snippets.append (Substring (s, index, start, line_start_idx + 1))
1279 snippets.append (snip)
1281 index = start + len (snip.match.group ('match'))
1285 def filter_pipe (input, cmd):
1286 if global_options.verbose:
1287 ly.progress (_ ("Opening filter `%s'") % cmd)
1289 (stdin, stdout, stderr) = os.popen3 (cmd)
1291 status = stdin.close ()
1295 output = stdout.read ()
1296 status = stdout.close ()
1297 error = stderr.read ()
1301 signal = 0x0f & status
1302 if status or (not output and error):
1303 exit_status = status >> 8
1304 ly.error (_ ("`%s' failed (%d)") % (cmd, exit_status))
1305 ly.error (_ ("The error log is as follows:"))
1306 sys.stderr.write (error)
1307 sys.stderr.write (stderr.read ())
1310 if global_options.verbose:
1316 return filter_pipe (s, global_options.filter_cmd)
1318 def is_derived_class (cl, baseclass):
1321 for b in cl.__bases__:
1322 if is_derived_class (b, baseclass):
1326 def process_snippets (cmd, ly_snippets, texstr_snippets, png_snippets):
1327 ly_names = filter (lambda x: x,
1328 map (Lilypond_snippet.basename, ly_snippets))
1329 texstr_names = filter (lambda x: x,
1330 map (Lilypond_snippet.basename, texstr_snippets))
1331 png_names = filter (lambda x: x,
1332 map (Lilypond_snippet.basename, png_snippets))
1335 def my_system (cmd):
1336 status = ly.system (cmd,
1337 ignore_error = 1, progress_p = 1)
1340 ly.error ('Process %s exited unsuccessfully.' % cmd)
1344 # the --process=CMD switch is a bad idea
1345 # it is too generic for lilypond-book.
1347 my_system (string.join ([cmd, '--backend texstr',
1348 'snippet-map.ly'] + texstr_names))
1349 for l in texstr_names:
1350 my_system ('latex %s.texstr' % l)
1353 my_system (string.join ([cmd, 'snippet-map.ly'] + ly_names))
1355 LATEX_INSPECTION_DOCUMENT = r'''
1359 \typeout{textwidth=\the\textwidth}
1360 \typeout{columnsep=\the\columnsep}
1361 \makeatletter\if@twocolumn\typeout{columns=2}\fi\makeatother
1365 # Do we need anything else besides `textwidth'?
1366 def get_latex_textwidth (source):
1367 m = re.search (r'''(?P<preamble>\\begin\s*{document})''', source)
1368 preamble = source[:m.start (0)]
1369 latex_document = LATEX_INSPECTION_DOCUMENT % vars ()
1370 # Workaround problems with unusable $TMP on Cygwin:
1371 tempfile.tempdir = ''
1372 tmpfile = tempfile.mktemp('.tex')
1373 logfile = os.path.splitext (tmpfile)[0] + '.log'
1374 open (tmpfile,'w').write (latex_document)
1375 ly.system ('latex %s' % tmpfile)
1376 parameter_string = open (logfile).read()
1381 m = re.search ('columns=([0-9.]*)', parameter_string)
1383 columns = string.atoi (m.group (1))
1386 m = re.search ('columnsep=([0-9.]*)pt', parameter_string)
1388 columnsep = string.atof (m.group (1))
1391 m = re.search ('textwidth=([0-9.]*)pt', parameter_string)
1393 textwidth = string.atof (m.group (1))
1395 textwidth = (textwidth - columnsep) / columns
1399 def modify_preamble (chunk):
1400 str = chunk.replacement_text ()
1401 if (re.search (r"\\begin *{document}", str)
1402 and not re.search ("{graphic[sx]", str)):
1403 str = re.sub (r"\\begin{document}",
1404 r"\\usepackage{graphics}" + '\n'
1405 + r"\\begin{document}",
1407 chunk.override_text = str
1419 '.texinfo': TEXINFO,
1425 # TEXINFO: '.texinfo',
1430 class Compile_error:
1433 def write_file_map (lys, name):
1434 snippet_map = open ('snippet-map.ly', 'w')
1435 snippet_map.write ("""
1436 #(define version-seen? #t)
1437 #(ly:add-file-name-alist '(
1440 snippet_map.write ('("%s.ly" . "%s:%d (%s.ly)")\n'
1446 snippet_map.write ('))\n')
1448 def do_process_cmd (chunks, input_name):
1449 all_lys = filter (lambda x: is_derived_class (x.__class__,
1453 write_file_map (all_lys, input_name)
1455 filter (lambda x: is_derived_class (x.__class__,
1457 and x.ly_is_outdated (),
1460 filter (lambda x: is_derived_class (x.__class__,
1462 and x.texstr_is_outdated (),
1465 filter (lambda x: is_derived_class (x.__class__,
1467 and x.png_is_outdated (),
1470 ly.progress (_ ("Writing snippets..."))
1471 map (Lilypond_snippet.write_ly, ly_outdated)
1475 ly.progress (_ ("Processing..."))
1477 process_snippets (global_options.process_cmd, ly_outdated, texstr_outdated, png_outdated)
1479 ly.progress (_ ("All snippets are up to date..."))
1482 def guess_format (input_filename):
1484 e = os.path.splitext (input_filename)[1]
1485 if e in ext2format.keys ():
1487 format = ext2format[e]
1489 ly.error (_ ("can't determine format for: %s" \
1494 def write_if_updated (file_name, lines):
1496 f = open (file_name)
1498 new_str = string.join (lines, '')
1499 if oldstr == new_str:
1500 ly.progress (_ ("%s is up to date.") % file_name)
1506 ly.progress (_ ("Writing `%s'...") % file_name)
1507 open (file_name, 'w').writelines (lines)
1510 def note_input_file (name, inputs=[]):
1511 inputs.append (name)
1514 def do_file (input_filename):
1516 if not input_filename or input_filename == '-':
1517 in_handle = sys.stdin
1518 input_fullname = '<stdin>'
1520 if os.path.exists (input_filename):
1521 input_fullname = input_filename
1522 elif global_options.format == LATEX and ly.search_exe_path ('kpsewhich'):
1523 # urg python interface to libkpathsea?
1524 input_fullname = ly.read_pipe ('kpsewhich '
1525 + input_filename)[:-1]
1527 input_fullname = find_file (input_filename)
1529 note_input_file (input_fullname)
1530 in_handle = open (input_fullname)
1532 if input_filename == '-':
1533 input_base = 'stdin'
1535 input_base = os.path.basename \
1536 (os.path.splitext (input_filename)[0])
1538 # Only default to stdout when filtering.
1539 if global_options.output_name == '-' or (not global_options.output_name and global_options.filter_cmd):
1540 output_filename = '-'
1541 output_file = sys.stdout
1543 # don't complain when global_options.output_name is existing
1544 output_filename = input_base + format2ext[global_options.format]
1545 if global_options.output_name:
1546 if not os.path.isdir (global_options.output_name):
1547 os.mkdir (global_options.output_name, 0777)
1548 os.chdir (global_options.output_name)
1550 if os.path.exists (input_filename) \
1551 and os.path.exists (output_filename) \
1552 and os.path.samefile (output_filename, input_fullname):
1554 _ ("Output would overwrite input file; use --output."))
1558 ly.progress (_ ("Reading %s...") % input_fullname)
1559 source = in_handle.read ()
1562 set_default_options (source)
1565 # FIXME: Containing blocks must be first, see
1566 # find_toplevel_snippets.
1568 'multiline_comment',
1572 'singleline_comment',
1577 ly.progress (_ ("Dissecting..."))
1578 chunks = find_toplevel_snippets (source, snippet_types)
1580 if global_options.format == LATEX:
1582 if (c.is_plain () and
1583 re.search (r"\\begin *{document}", c.replacement_text())):
1588 if global_options.filter_cmd:
1589 write_if_updated (output_filename,
1590 [c.filter_text () for c in chunks])
1591 elif global_options.process_cmd:
1592 do_process_cmd (chunks, input_fullname)
1593 ly.progress (_ ("Compiling %s...") % output_filename)
1595 write_if_updated (output_filename,
1596 [s.replacement_text ()
1599 def process_include (snippet):
1600 os.chdir (original_dir)
1601 name = snippet.substring ('filename')
1602 ly.progress (_ ("Processing include: %s") % name)
1604 return do_file (name)
1606 include_chunks = map (process_include,
1607 filter (lambda x: is_derived_class (x.__class__,
1612 return chunks + reduce (lambda x,y: x + y, include_chunks, [])
1614 except Compile_error:
1615 os.chdir (original_dir)
1616 ly.progress (_ ("Removing `%s'") % output_filename)
1621 opt_parser = get_option_parser()
1622 (options, args) = opt_parser.parse_args ()
1624 if options.format in ('texi-html', 'texi'):
1625 options.format = TEXINFO
1626 options.use_hash = True
1627 options.pseudo_filter = False
1629 global global_options
1630 global_options = options
1634 files = do_options ()
1635 if not files or len (files) > 1:
1641 basename = os.path.splitext (file)[0]
1642 basename = os.path.split (basename)[1]
1644 if not global_options.format:
1645 global_options.format = guess_format (files[0])
1648 if global_options.format in (TEXINFO, HTML):
1650 if global_options.process_cmd == '':
1651 global_options.process_cmd = lilypond_binary \
1652 + ' --formats=%s --backend eps ' % formats
1654 if global_options.process_cmd:
1655 global_options.process_cmd += string.join ([(' -I %s' % commands.mkarg (p))
1656 for p in global_options.include_path])
1658 ly.identify (sys.stderr)
1661 chunks = do_file (file)
1662 if global_options.psfonts:
1663 fontextract.verbose = global_options.verbose
1664 snippet_chunks = filter (lambda x: is_derived_class (x.__class__,
1668 psfonts_file = basename + '.psfonts'
1669 if not global_options.verbose:
1670 ly.progress (_ ("Writing fonts to %s...") % psfonts_file)
1671 fontextract.extract_fonts (psfonts_file,
1672 [x.basename() + '.eps'
1673 for x in snippet_chunks])
1674 if not global_options.verbose:
1677 except Compile_error:
1680 if global_options.format in (TEXINFO, LATEX):
1681 if not global_options.psfonts:
1682 ly.warning (_ ("option --psfonts not used"))
1683 ly.warning (_ ("processing with dvips will have no fonts"))
1685 psfonts_file = os.path.join (global_options.output_name, basename + '.psfonts')
1686 output = os.path.join (global_options.output_name, basename + '.dvi' )
1689 ly.progress (_ ("DVIPS usage:"))
1691 ly.progress (" dvips -h %(psfonts_file)s %(output)s" % vars ())
1694 inputs = note_input_file ('')
1697 base_file_name = os.path.splitext (os.path.basename (file))[0]
1698 dep_file = os.path.join (global_options.output_name, base_file_name + '.dep')
1699 final_output_file = os.path.join (global_options.output_name,
1701 + '.%s' % global_options.format)
1703 os.chdir (original_dir)
1704 open (dep_file, 'w').write ('%s: %s' % (final_output_file, ' '.join (inputs)))
1706 if __name__ == '__main__':