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
45 for d in ['@lilypond_datadir@',
47 sys.path.insert (0, os.path.join (d, 'python'))
49 # dynamic relocation, for GUB binaries.
50 bindir = os.path.abspath (os.path.split (sys.argv[0])[0])
52 os.environ['PATH'] = bindir + os.pathsep + os.environ['PATH']
53 for p in ['share', 'lib']:
54 datadir = os.path.abspath (bindir + '/../%s/lilypond/current/python/' % p)
55 sys.path.insert (0, datadir)
64 program_version = '@TOPLEVEL_VERSION@'
65 program_name = os.path.basename (sys.argv[0])
67 original_dir = os.getcwd ()
71 _ ("Process LilyPond snippets in hybrid HTML, LaTeX, texinfo or DocBook document.")
75 lilypond-book --filter="tr '[a-z]' '[A-Z]'" %(BOOK)s
76 lilypond-book --filter="convert-ly --no-version --from=2.0.0 -" %(BOOK)s
77 lilypond-book --process='lilypond -I include' %(BOOK)s
78 ''' % {'BOOK': _ ("BOOK")})
80 authors = ('Jan Nieuwenhuizen <janneke@gnu.org>',
81 'Han-Wen Nienhuys <hanwen@xs4all.nl>')
84 ################################################################
86 if global_options.verbose:
87 raise _ ('Exiting (%d)...') % i
92 sys.stdout.write ('%s (GNU LilyPond) %s\n' % (program_name, program_version))
98 sys.stderr.write (program_name + ": " + _ ("warning: %s") % s + '\n')
101 sys.stderr.write (program_name + ": " + _ ("error: %s") % s + '\n')
103 def ps_page_count (ps_name):
104 header = open (ps_name).read (1024)
105 m = re.search ('\n%%Pages: ([0-9]+)', header)
107 return string.atoi (m.group (1))
112 sys.stdout.write ('''
119 ''' % ( _ ('Copyright (c) %s by') % '2001--2006',
121 _ ("Distributed under terms of the GNU General Public License."),
122 _ ("It comes with NO WARRANTY.")))
124 def get_option_parser ():
125 p = ly.get_option_parser (usage=_ ("%s [OPTION]... FILE") % 'lilypond-book',
126 version="@TOPLEVEL_VERSION@",
127 description=help_summary)
129 p.add_option ('-F', '--filter', metavar=_ ("FILTER"),
132 help=_ ("pipe snippets through FILTER [convert-ly -n -]"),
134 p.add_option ('-f', '--format',
135 help=_ ("use output format FORMAT (texi [default], texi-html, latex, html, docbook)"),
138 p.add_option ("-I", '--include', help=_ ("add DIR to include path"),
140 action='append', dest='include_path',
141 default=[os.path.abspath (os.getcwd ())])
143 p.add_option ("-o", '--output', help=_ ("write output to DIR"),
145 action='store', dest='output_name',
147 p.add_option ('-P', '--process', metavar=_ ("COMMAND"),
148 help = _ ("process ly_files using COMMAND FILE..."),
150 dest='process_cmd', default='lilypond -b eps')
151 p.add_option ('--pdf',
154 help=_ ("Create PDF files for use with PDFTeX"),
156 p.add_option ('', '--psfonts', action="store_true", dest="psfonts",
157 help=_ ('''extract all PostScript fonts into INPUT.psfonts for LaTeX
158 must use this with dvips -h INPUT.psfonts'''),
160 p.add_option ('-V', '--verbose', help=_ ("be verbose"),
164 p.add_option ('-w', '--warranty',
165 help=_ ("show warranty and copyright"),
167 p.add_option_group ('bugs',
168 description=(_ ("Report bugs via")
169 + ''' http://post.gmane.org/post.php'''
170 '''?group=gmane.comp.gnu.lilypond.bugs\n'''))
173 lilypond_binary = os.path.join ('@bindir@', 'lilypond')
175 # Only use installed binary when we are installed too.
176 if '@bindir@' == ('@' + 'bindir@') or not os.path.exists (lilypond_binary):
177 lilypond_binary = 'lilypond'
179 global_options = None
182 default_ly_options = { 'alt': "[image of music]" }
185 # Is this pythonic? Personally, I find this rather #define-nesque. --hwn
190 EXAMPLEINDENT = 'exampleindent'
192 FRAGMENT = 'fragment'
197 LINE_WIDTH = 'line-width'
198 NOFRAGMENT = 'nofragment'
199 NOINDENT = 'noindent'
204 OUTPUTIMAGE = 'outputimage'
207 PREAMBLE = 'preamble'
208 PRINTFILENAME = 'printfilename'
210 RAGGED_RIGHT = 'ragged-right'
211 RELATIVE = 'relative'
212 STAFFSIZE = 'staffsize'
215 VERBATIM = 'verbatim'
216 FONTLOAD = 'fontload'
217 FILENAME = 'filename'
221 # NOTIME has no opposite so it isn't part of this dictionary.
222 # NOQUOTE is used internally only.
224 NOFRAGMENT: FRAGMENT,
229 # Recognize special sequences in the input.
231 # (?P<name>regex) -- Assign result of REGEX to NAME.
232 # *? -- Match non-greedily.
233 # (?m) -- Multiline regex: Make ^ and $ match at each line.
234 # (?s) -- Make the dot match all characters including newline.
235 # (?x) -- Ignore whitespace in patterns.
246 <(?P<inline>(inline)?)mediaobject>\s*<textobject.*?>\s*<programlisting\s+language="lilypond".*?(role="(?P<options>.*?)")?>(?P<code>.*?)</programlisting\s*>\s*</textobject\s*>\s*</(inline)?mediaobject>)''',
251 <(?P<inline>(inline)?)mediaobject>\s*<textobject.*?>\s*<programlisting\s+language="lilypond".*?(role="(?P<options>.*?)")?>(?P<code>.*?)</programlisting\s*>\s*</textobject\s*>\s*</(inline)?mediaobject>)''',
256 <(?P<inline>(inline)?)mediaobject>\s*<imageobject.*?>\s*<imagedata\s+fileref="(?P<filename>.*?\.ly)"\s*(role="(?P<options>.*?)")?\s*(/>|>\s*</imagedata>)\s*</imageobject>\s*</(inline)?mediaobject>)''',
262 (?P<code><!--\s.*?!-->)
265 'singleline_comment':
284 (\s*(?P<options>.*?)\s*:)?\s*
292 \s*(?P<options>.*?)\s*
301 \s*(?P<options>.*?)\s*
303 \s*(?P<filename>.*?)\s*
310 (?P<code><!--\s.*?!-->)
313 'singleline_comment':
319 (?P<code><pre>.*?</pre>))''',
325 (?P<code><pre>\s.*?</pre>\s))''',
344 \s*(?P<options>.*?)\s*
355 \s*(?P<options>.*?)\s*
359 \\end\s*{lilypond})''',
367 \s*(?P<options>.*?)\s*
375 'singleline_comment':
398 \\end\s*{verbatim}))''',
407 (?P<filename>\S+))''',
411 ^[^\n]*?(?!@c\s+)[^\n]*?
415 \s*(?P<options>.*?)\s*
425 \s*(?P<options>.*?)\s*
428 ^@end\s+lilypond)\s''',
435 \s*(?P<options>.*?)\s*
446 @end\s+ignore))\s''',
448 'singleline_comment':
453 @c([ \t][^\n]*|)\n))''',
455 # Don't do this: It interferes with @code{@{}.
456 # 'verb': r'''(?P<code>@code{.*?})''',
464 @end\s+example\s))''',
473 'intertext': r',?\s*intertext=\".*?\"',
477 'intertext': r',?\s*intertext=\".*?\"',
482 'intertext': r',?\s*intertext=\".*?\"',
483 'option_sep': '\s*,\s*',
487 'intertext': r',?\s*intertext=\".*?\"',
488 'option_sep': '\s*,\s*',
492 # Options without a pattern in ly_options.
509 RELATIVE: r'''\relative c%(relative_quotes)s''',
514 INDENT: r'''indent = %(indent)s''',
516 LINE_WIDTH: r'''line-width = %(line-width)s''',
518 QUOTE: r'''line-width = %(line-width)s - 2.0 * %(exampleindent)s''',
520 RAGGED_RIGHT: r'''ragged-right = ##t''',
522 PACKED: r'''packed = ##t''',
534 \remove Time_signature_engraver
540 STAFFSIZE: r'''#(set-global-staff-size %(staffsize)s)''',
547 FILTER: r'''<mediaobject><textobject><programlisting language="lilypond" role="%(options)s">%(code)s</programlisting></textobject></mediaobject>''',
550 <imageobject role="latex">
551 <imagedata fileref="%(base)s.pdf" format="PDF"/>
553 <imageobject role="html">
554 <imagedata fileref="%(base)s.png" format="PNG"/></imageobject>''',
556 VERBATIM: r'''<programlisting>%(verb)s</programlisting>''',
558 PRINTFILENAME: '<textobject><simpara><ulink url="%(base)s.ly"><filename>%(filename)s</filename></ulink></simpara></textobject>'
562 FILTER: r'''<lilypond %(options)s>
572 <a href="%(base)s.ly">''',
575 <img align="center" valign="center"
576 border="0" src="%(image)s" alt="%(alt)s">''',
578 PRINTFILENAME: '<p><tt><a href="%(base)s.ly">%(filename)s</a></tt></p>',
580 QUOTE: r'''<blockquote>
593 \ifx\preLilyPondExample \undefined%%
596 \preLilyPondExample%%
598 \def\lilypondbook{}%%
599 \input %(base)s-systems.tex%%
600 \ifx\postLilyPondExample \undefined%%
603 \postLilyPondExample%%
607 PRINTFILENAME: '''\\texttt{%(filename)s}
610 QUOTE: r'''\begin{quotation}%(str)s
613 VERBATIM: r'''\noindent
614 \begin{verbatim}%(verb)s\end{verbatim}''',
616 FILTER: r'''\begin{lilypond}[%(options)s]
623 FILTER: r'''@lilypond[%(options)s]
629 @include %(base)s-systems.texi
633 OUTPUTIMAGE: r'''@noindent
635 @image{%(base)s,,,%(alt)s,%(ext)s}
639 <a href="%(base)s.ly">
640 <img align="center" valign="center"
641 border="0" src="%(image)s" alt="%(alt)s">
649 <a href="%(base)s.ly">
657 QUOTE: r'''@quotation
658 %(str)s@end quotation
665 VERBATIM: r'''@exampleindent 0
667 %(verb)s@end verbatim
673 # Maintain line numbers.
678 for f in [HTML, LATEX]:
679 for s in (QUOTE, VERBATIM):
680 output[f][s] = output[f][s].replace("\n"," ")
683 PREAMBLE_LY = '''%%%% Generated by %(program_name)s
684 %%%% Options: [%(option_string)s]
685 \\include "lilypond-book-preamble.ly"
693 %% ****************************************************************
694 %% Start cut-&-pastable-section
695 %% ****************************************************************
698 #(define dump-extents #t)
699 %(font_dump_setting)s
713 %% ****************************************************************
714 %% ly snippet contents follows:
715 %% ****************************************************************
719 %% ****************************************************************
721 %% ****************************************************************
728 %% ****************************************************************
730 %% ****************************************************************
734 %% ****************************************************************
736 %% ****************************************************************
739 texinfo_line_widths = {
740 '@afourpaper': '160\\mm',
741 '@afourwide': '6.5\\in',
742 '@afourlatex': '150\\mm',
743 '@smallbook': '5\\in',
744 '@letterpaper': '6\\in',
747 def classic_lilypond_book_compatibility (key, value):
748 if key == 'singleline' and value == None:
749 return (RAGGED_RIGHT, None)
751 m = re.search ('relative\s*([-0-9])', key)
753 return ('relative', m.group (1))
755 m = re.match ('([0-9]+)pt', key)
757 return ('staffsize', m.group (1))
759 if key == 'indent' or key == 'line-width':
760 m = re.match ('([-.0-9]+)(cm|in|mm|pt|staffspace)', value)
762 f = float (m.group (1))
763 return (key, '%f\\%s' % (f, m.group (2)))
767 def find_file (name):
768 for i in global_options.include_path:
769 full = os.path.join (i, name)
770 if os.path.exists (full):
773 error (_ ("file not found: %s") % name + '\n')
777 def verbatim_html (s):
778 return re.sub ('>', '>',
780 re.sub ('&', '&', s)))
782 def split_options (option_string):
784 if global_options.format == HTML:
785 options = re.findall('[\w\.-:]+(?:\s*=\s*(?:"[^"]*"|\'[^\']*\'|\S+))?',option_string)
786 for i in range(len(options)):
787 options[i] = re.sub('^([^=]+=\s*)(?P<q>["\'])(.*)(?P=q)','\g<1>\g<3>',options[i])
790 return re.split (format_res[global_options.format]['option_sep'],
794 def set_default_options (source):
795 global default_ly_options
796 if not default_ly_options.has_key (LINE_WIDTH):
797 if global_options.format == LATEX:
798 textwidth = get_latex_textwidth (source)
799 default_ly_options[LINE_WIDTH] = \
800 '''%.0f\\pt''' % textwidth
801 elif global_options.format == TEXINFO:
802 for (k, v) in texinfo_line_widths.items ():
803 # FIXME: @layout is usually not in
806 # \input texinfo @c -*-texinfo-*-
808 # Bluntly search first K items of
810 # s = chunks[0].replacement_text ()
811 if re.search (k, source[:1024]):
812 default_ly_options[LINE_WIDTH] = v
816 def replacement_text (self):
819 def filter_text (self):
820 return self.replacement_text ()
822 def ly_is_outdated (self):
825 def png_is_outdated (self):
831 class Substring (Chunk):
832 def __init__ (self, source, start, end, line_number):
836 self.line_number = line_number
837 self.override_text = None
842 def replacement_text (self):
843 if self.override_text:
844 return self.override_text
846 return self.source[self.start:self.end]
848 class Snippet (Chunk):
849 def __init__ (self, type, match, format, line_number):
853 self.option_dict = {}
855 self.line_number = line_number
857 def replacement_text (self):
858 return self.match.group ('match')
860 def substring (self, s):
861 return self.match.group (s)
864 return `self.__class__` + ' type = ' + self.type
866 class Include_snippet (Snippet):
867 def processed_filename (self):
868 f = self.substring ('filename')
869 return os.path.splitext (f)[0] + format2ext[global_options.format]
871 def replacement_text (self):
872 s = self.match.group ('match')
873 f = self.substring ('filename')
875 return re.sub (f, self.processed_filename (), s)
877 class Lilypond_snippet (Snippet):
878 def __init__ (self, type, match, format, line_number):
879 Snippet.__init__ (self, type, match, format, line_number)
880 os = match.group ('options')
881 self.do_options (os, self.type)
884 contents = self.substring ('code')
885 return ('\\sourcefileline %d\n%s'
886 % (self.line_number - 1, contents))
891 return self.compose_ly (s)
894 def do_options (self, option_string, type):
895 self.option_dict = {}
897 options = split_options (option_string)
901 (key, value) = re.split ('\s*=\s*', i)
902 self.option_dict[key] = value
904 if i in no_options.keys ():
905 if no_options[i] in self.option_dict.keys ():
906 del self.option_dict[no_options[i]]
908 self.option_dict[i] = None
910 has_line_width = self.option_dict.has_key (LINE_WIDTH)
911 no_line_width_value = 0
913 # If LINE_WIDTH is used without parameter, set it to default.
914 if has_line_width and self.option_dict[LINE_WIDTH] == None:
915 no_line_width_value = 1
916 del self.option_dict[LINE_WIDTH]
918 for i in default_ly_options.keys ():
919 if i not in self.option_dict.keys ():
920 self.option_dict[i] = default_ly_options[i]
922 if not has_line_width:
923 if type == 'lilypond' or FRAGMENT in self.option_dict.keys ():
924 self.option_dict[RAGGED_RIGHT] = None
926 if type == 'lilypond':
927 if LINE_WIDTH in self.option_dict.keys ():
928 del self.option_dict[LINE_WIDTH]
930 if RAGGED_RIGHT in self.option_dict.keys ():
931 if LINE_WIDTH in self.option_dict.keys ():
932 del self.option_dict[LINE_WIDTH]
934 if QUOTE in self.option_dict.keys () or type == 'lilypond':
935 if LINE_WIDTH in self.option_dict.keys ():
936 del self.option_dict[LINE_WIDTH]
938 if not INDENT in self.option_dict.keys ():
939 self.option_dict[INDENT] = '0\\mm'
941 # The QUOTE pattern from ly_options only emits the `line-width'
943 if has_line_width and QUOTE in self.option_dict.keys ():
944 if no_line_width_value:
945 del self.option_dict[LINE_WIDTH]
947 del self.option_dict[QUOTE]
949 def compose_ly (self, code):
950 if FRAGMENT in self.option_dict.keys ():
958 # The original concept of the `exampleindent' option is broken.
959 # It is not possible to get a sane value for @exampleindent at all
960 # without processing the document itself. Saying
968 # causes ugly results with the DVI backend of texinfo since the
969 # default value for @exampleindent isn't 5em but 0.4in (or a smaller
970 # value). Executing the above code changes the environment
971 # indentation to an unknown value because we don't know the amount
972 # of 1em in advance since it is font-dependent. Modifying
973 # @exampleindent in the middle of a document is simply not
974 # supported within texinfo.
976 # As a consequence, the only function of @exampleindent is now to
977 # specify the amount of indentation for the `quote' option.
979 # To set @exampleindent locally to zero, we use the @format
980 # environment for non-quoted snippets.
981 override[EXAMPLEINDENT] = r'0.4\in'
982 override[LINE_WIDTH] = texinfo_line_widths['@smallbook']
983 override.update (default_ly_options)
986 for (key, value) in self.option_dict.items ():
988 option_list.append (key)
990 option_list.append (key + '=' + value)
991 option_string = string.join (option_list, ',')
994 compose_types = [NOTES, PREAMBLE, LAYOUT, PAPER]
995 for a in compose_types:
998 for (key, value) in self.option_dict.items ():
1000 classic_lilypond_book_compatibility (key, value)
1004 (_ ("deprecated ly-option used: %s=%s" \
1007 (_ ("compatibility mode translation: %s=%s" \
1008 % (c_key, c_value)))
1011 (_ ("deprecated ly-option used: %s" \
1014 (_ ("compatibility mode translation: %s" \
1017 (key, value) = (c_key, c_value)
1020 override[key] = value
1022 if not override.has_key (key):
1023 override[key] = None
1026 for type in compose_types:
1027 if ly_options[type].has_key (key):
1028 compose_dict[type].append (ly_options[type][key])
1032 if not found and key not in simple_options:
1033 warning (_ ("ignoring unknown ly option: %s") % key)
1036 if RELATIVE in override.keys () and override[RELATIVE]:
1037 relative = int (override[RELATIVE])
1039 relative_quotes = ''
1043 relative_quotes += ',' * (- relative)
1045 relative_quotes += "'" * relative
1047 paper_string = string.join (compose_dict[PAPER],
1049 layout_string = string.join (compose_dict[LAYOUT],
1051 notes_string = string.join (compose_dict[NOTES],
1053 preamble_string = string.join (compose_dict[PREAMBLE],
1056 font_dump_setting = ''
1057 if FONTLOAD in self.option_dict:
1058 font_dump_setting = '#(define-public force-eps-font-include #t)\n'
1060 d = globals().copy()
1062 return (PREAMBLE_LY + body) % d
1064 def get_hash (self):
1066 hash = md5.md5 (self.relevant_contents (self.full_ly ()))
1068 ## let's not create too long names.
1069 self.hash = hash.hexdigest ()[:10]
1073 def basename (self):
1074 if FILENAME in self.option_dict:
1075 return self.option_dict[FILENAME]
1076 if global_options.use_hash:
1077 return 'lily-%s' % self.get_hash ()
1080 def write_ly (self):
1081 outf = open (self.basename () + '.ly', 'w')
1082 outf.write (self.full_ly ())
1083 open (self.basename () + '.txt', 'w').write ('image of music')
1085 def relevant_contents (self, ly):
1086 return re.sub (r'\\(version|sourcefileline)[^\n]*\n', '', ly)
1088 def ly_is_outdated (self):
1089 base = self.basename ()
1090 ly_file = base + '.ly'
1091 tex_file = base + '.tex'
1092 eps_file = base + '.eps'
1093 systems_file = base + '-systems.tex'
1095 if (os.path.exists (ly_file)
1096 and os.path.exists (systems_file)
1097 and os.stat (systems_file)[stat.ST_SIZE]
1098 and re.match ('% eof', open (systems_file).readlines ()[-1])
1099 and (global_options.use_hash or FILENAME in self.option_dict)
1100 and (self.relevant_contents (self.full_ly ())
1101 == self.relevant_contents (open (ly_file).read ()))):
1103 if global_options.verbose:
1104 print 'OUT OF DATE: ', ly_file
1107 def png_is_outdated (self):
1108 base = self.basename ()
1109 # FIXME: refactor stupid OK stuff
1110 ok = not self.ly_is_outdated ()
1111 if global_options.format in (HTML, TEXINFO):
1112 ok = ok and os.path.exists (base + '.eps')
1116 page_count = ps_page_count (base + '.eps')
1119 ok = ok and os.path.exists (base + '.png')
1121 elif page_count > 1:
1122 for a in range (1, page_count + 1):
1123 ok = ok and os.path.exists (base + '-page%d.png' % a)
1127 def texstr_is_outdated (self):
1131 # FIXME: refactor stupid OK stuff
1132 base = self.basename ()
1133 ok = self.ly_is_outdated ()
1134 ok = ok and (os.path.exists (base + '.texstr'))
1137 def filter_text (self):
1138 code = self.substring ('code')
1139 s = run_filter (code)
1142 'options': self.match.group ('options')
1145 return output[self.format][FILTER] % d
1147 def replacement_text (self):
1148 func = Lilypond_snippet.__dict__['output_' + self.format]
1151 def get_images (self):
1152 base = self.basename ()
1154 single = '%(base)s.png' % vars ()
1155 multiple = '%(base)s-page1.png' % vars ()
1157 if os.path.exists (multiple) \
1158 and (not os.path.exists (single) \
1159 or (os.stat (multiple)[stat.ST_MTIME] \
1160 > os.stat (single)[stat.ST_MTIME])):
1161 count = ps_page_count ('%(base)s.eps' % vars ())
1162 images = ['%s-page%d.png' % (base, a) for a in range (1, count+1)]
1163 images = tuple (images)
1166 def output_docbook (self):
1168 base = self.basename ()
1169 for image in self.get_images ():
1170 (base, ext) = os.path.splitext (image)
1171 str += output[DOCBOOK][OUTPUT] % vars ()
1172 str += self.output_print_filename (DOCBOOK)
1173 if (self.substring('inline') == 'inline'):
1174 str = '<inlinemediaobject>' + str + '</inlinemediaobject>'
1176 str = '<mediaobject>' + str + '</mediaobject>'
1177 if VERBATIM in self.option_dict:
1178 verb = verbatim_html (self.substring ('code'))
1179 str = output[DOCBOOK][VERBATIM] % vars () + str
1182 def output_html (self):
1184 base = self.basename ()
1185 if global_options.format == HTML:
1186 str += self.output_print_filename (HTML)
1187 if VERBATIM in self.option_dict:
1188 verb = verbatim_html (self.substring ('code'))
1189 str += output[HTML][VERBATIM] % vars ()
1190 if QUOTE in self.option_dict:
1191 str = output[HTML][QUOTE] % vars ()
1193 str += output[HTML][BEFORE] % vars ()
1194 for image in self.get_images ():
1195 (base, ext) = os.path.splitext (image)
1196 alt = self.option_dict[ALT]
1197 str += output[HTML][OUTPUT] % vars ()
1198 str += output[HTML][AFTER] % vars ()
1201 def output_info (self):
1203 for image in self.get_images ():
1204 (base, ext) = os.path.splitext (image)
1206 # URG, makeinfo implicitly prepends dot to extension.
1207 # Specifying no extension is most robust.
1209 alt = self.option_dict[ALT]
1210 str += output[TEXINFO][OUTPUTIMAGE] % vars ()
1212 base = self.basename ()
1213 str += output[global_options.format][OUTPUT] % vars ()
1216 def output_latex (self):
1218 base = self.basename ()
1219 if global_options.format == LATEX:
1220 str += self.output_print_filename (LATEX)
1221 if VERBATIM in self.option_dict:
1222 verb = self.substring ('code')
1223 str += (output[LATEX][VERBATIM] % vars ())
1225 str += (output[LATEX][OUTPUT] % vars ())
1227 ## todo: maintain breaks
1229 breaks = self.ly ().count ("\n")
1230 str += "".ljust (breaks, "\n").replace ("\n","%\n")
1232 if QUOTE in self.option_dict:
1233 str = output[LATEX][QUOTE] % vars ()
1236 def output_print_filename (self, format):
1238 if PRINTFILENAME in self.option_dict:
1239 base = self.basename ()
1240 filename = self.substring ('filename')
1241 str = output[global_options.format][PRINTFILENAME] % vars ()
1245 def output_texinfo (self):
1247 if self.output_print_filename (TEXINFO):
1249 + self.output_print_filename (HTML)
1252 + self.output_print_filename (LATEX)
1254 base = self.basename ()
1255 if TEXIDOC in self.option_dict:
1256 texidoc = base + '.texidoc'
1257 if os.path.exists (texidoc):
1258 str += '@include %(texidoc)s\n\n' % vars ()
1260 if VERBATIM in self.option_dict:
1261 verb = self.substring ('code')
1262 str += (output[TEXINFO][VERBATIM] % vars ())
1263 if not QUOTE in self.option_dict:
1264 str = output[TEXINFO][NOQUOTE] % vars ()
1266 str += self.output_info ()
1268 # str += ('@ifinfo\n' + self.output_info () + '\n@end ifinfo\n')
1269 # str += ('@tex\n' + self.output_latex () + '\n@end tex\n')
1270 # str += ('@html\n' + self.output_html () + '\n@end html\n')
1272 if QUOTE in self.option_dict:
1273 str = output[TEXINFO][QUOTE] % vars ()
1275 # need par after image
1280 class Lilypond_file_snippet (Lilypond_snippet):
1282 name = self.substring ('filename')
1283 contents = open (find_file (name)).read ()
1284 return ('\\sourcefilename \"%s\"\n\\sourcefileline 0\n%s'
1287 snippet_type_to_class = {
1288 'lilypond_file': Lilypond_file_snippet,
1289 'lilypond_block': Lilypond_snippet,
1290 'lilypond': Lilypond_snippet,
1291 'include': Include_snippet,
1294 def find_linestarts (s):
1299 i = s.find ('\n', start)
1307 nls.append (len (s))
1310 def find_toplevel_snippets (s, types):
1313 res[i] = ly.re.compile (snippet_res[global_options.format][i])
1317 found = dict ([(t, None) for t in types])
1319 line_starts = find_linestarts (s)
1321 # We want to search for multiple regexes, without searching
1322 # the string multiple times for one regex.
1323 # Hence, we use earlier results to limit the string portion
1325 # Since every part of the string is traversed at most once for
1326 # every type of snippet, this is linear.
1332 if not found[type] or found[type][0] < index:
1335 m = res[type].search (s[index:endex])
1340 if snippet_type_to_class.has_key (type):
1341 cl = snippet_type_to_class[type]
1344 start = index + m.start ('match')
1345 line_number = line_start_idx
1346 while (line_starts[line_number] < start):
1350 snip = cl (type, m, global_options.format, line_number)
1352 found[type] = (start, snip)
1356 or found[type][0] < found[first][0]):
1361 # Limiting the search space is a cute
1362 # idea, but this *requires* to search
1363 # for possible containing blocks
1364 # first, at least as long as we do not
1365 # search for the start of blocks, but
1366 # always/directly for the entire
1367 # @block ... @end block.
1369 endex = found[first][0]
1372 snippets.append (Substring (s, index, len (s), line_start_idx))
1375 while (start > line_starts[line_start_idx+1]):
1378 (start, snip) = found[first]
1379 snippets.append (Substring (s, index, start, line_start_idx + 1))
1380 snippets.append (snip)
1382 index = start + len (snip.match.group ('match'))
1386 def filter_pipe (input, cmd):
1387 if global_options.verbose:
1388 progress (_ ("Opening filter `%s'") % cmd)
1390 (stdin, stdout, stderr) = os.popen3 (cmd)
1392 status = stdin.close ()
1396 output = stdout.read ()
1397 status = stdout.close ()
1398 error = stderr.read ()
1402 signal = 0x0f & status
1403 if status or (not output and error):
1404 exit_status = status >> 8
1405 error (_ ("`%s' failed (%d)") % (cmd, exit_status))
1406 error (_ ("The error log is as follows:"))
1407 sys.stderr.write (error)
1408 sys.stderr.write (stderr.read ())
1411 if global_options.verbose:
1417 return filter_pipe (s, global_options.filter_cmd)
1419 def is_derived_class (cl, baseclass):
1422 for b in cl.__bases__:
1423 if is_derived_class (b, baseclass):
1427 def process_snippets (cmd, ly_snippets, texstr_snippets, png_snippets):
1428 ly_names = filter (lambda x: x,
1429 map (Lilypond_snippet.basename, ly_snippets))
1430 texstr_names = filter (lambda x: x,
1431 map (Lilypond_snippet.basename, texstr_snippets))
1433 png_names = filter (lambda x: x,
1434 map (Lilypond_snippet.basename, png_snippets))
1437 def my_system (cmd):
1438 status = ly.system (cmd,
1439 be_verbose=global_options.verbose,
1442 if global_options.format in (HTML, TEXINFO):
1443 cmd += ' --formats=png '
1444 if global_options.format in (DOCBOOK):
1445 cmd += ' --formats=png,pdf '
1447 # the --process=CMD switch is a bad idea
1448 # it is too generic for lilypond-book.
1450 my_system (string.join ([cmd, '--backend texstr',
1451 'snippet-map.ly'] + texstr_names))
1452 for l in texstr_names:
1453 my_system ('latex %s.texstr' % l)
1456 open ('snippet-names', 'wb').write ('\n'.join (['snippet-map.ly']
1459 my_system (string.join ([cmd, 'snippet-names']))
1462 LATEX_INSPECTION_DOCUMENT = r'''
1466 \typeout{textwidth=\the\textwidth}
1467 \typeout{columnsep=\the\columnsep}
1468 \makeatletter\if@twocolumn\typeout{columns=2}\fi\makeatother
1472 # Do we need anything else besides `textwidth'?
1473 def get_latex_textwidth (source):
1474 m = re.search (r'''(?P<preamble>\\begin\s*{document})''', source)
1476 warning (_ ("cannot find \\begin{document} in LaTeX document"))
1478 ## what's a sensible default?
1481 preamble = source[:m.start (0)]
1482 latex_document = LATEX_INSPECTION_DOCUMENT % vars ()
1484 (handle, tmpfile) = tempfile.mkstemp('.tex')
1485 logfile = os.path.splitext (tmpfile)[0] + '.log'
1486 logfile = os.path.split (logfile)[1]
1488 tmp_handle = os.fdopen (handle,'w')
1489 tmp_handle.write (latex_document)
1492 ly.system ('latex %s' % tmpfile, be_verbose=global_options.verbose)
1493 parameter_string = open (logfile).read()
1499 m = re.search ('columns=([0-9.]*)', parameter_string)
1501 columns = int (m.group (1))
1504 m = re.search ('columnsep=([0-9.]*)pt', parameter_string)
1506 columnsep = float (m.group (1))
1509 m = re.search ('textwidth=([0-9.]*)pt', parameter_string)
1511 textwidth = float (m.group (1))
1513 textwidth = (textwidth - columnsep) / columns
1517 def modify_preamble (chunk):
1518 str = chunk.replacement_text ()
1519 if (re.search (r"\\begin *{document}", str)
1520 and not re.search ("{graphic[sx]", str)):
1521 str = re.sub (r"\\begin{document}",
1522 r"\\usepackage{graphics}" + '\n'
1523 + r"\\begin{document}",
1525 chunk.override_text = str
1537 '.texinfo': TEXINFO,
1544 # TEXINFO: '.texinfo',
1550 class Compile_error:
1553 def write_file_map (lys, name):
1554 snippet_map = open ('snippet-map.ly', 'w')
1555 snippet_map.write ("""
1556 #(define version-seen #t)
1557 #(ly:add-file-name-alist '(
1560 snippet_map.write ('("%s.ly" . "%s")\n'
1564 snippet_map.write ('))\n')
1566 def do_process_cmd (chunks, input_name):
1567 all_lys = filter (lambda x: is_derived_class (x.__class__,
1571 write_file_map (all_lys, input_name)
1572 ly_outdated = filter (lambda x: is_derived_class (x.__class__,
1574 and x.ly_is_outdated (), chunks)
1575 texstr_outdated = filter (lambda x: is_derived_class (x.__class__,
1577 and x.texstr_is_outdated (),
1579 png_outdated = filter (lambda x: is_derived_class (x.__class__,
1581 and x.png_is_outdated (),
1584 outdated = png_outdated + texstr_outdated + ly_outdated
1586 progress (_ ("Writing snippets..."))
1587 map (Lilypond_snippet.write_ly, ly_outdated)
1591 progress (_ ("Processing..."))
1593 process_snippets (global_options.process_cmd, ly_outdated, texstr_outdated, png_outdated)
1595 progress (_ ("All snippets are up to date..."))
1598 def guess_format (input_filename):
1600 e = os.path.splitext (input_filename)[1]
1601 if e in ext2format.keys ():
1603 format = ext2format[e]
1605 error (_ ("cannot determine format for: %s" \
1610 def write_if_updated (file_name, lines):
1612 f = open (file_name)
1614 new_str = string.join (lines, '')
1615 if oldstr == new_str:
1616 progress (_ ("%s is up to date.") % file_name)
1622 progress (_ ("Writing `%s'...") % file_name)
1623 open (file_name, 'w').writelines (lines)
1626 def note_input_file (name, inputs=[]):
1627 ## hack: inputs is mutable!
1628 inputs.append (name)
1631 def samefile (f1, f2):
1633 return os.path.samefile (f1, f2)
1634 except AttributeError: # Windoze
1635 f1 = re.sub ("//*", "/", f1)
1636 f2 = re.sub ("//*", "/", f2)
1639 def do_file (input_filename):
1641 if not input_filename or input_filename == '-':
1642 in_handle = sys.stdin
1643 input_fullname = '<stdin>'
1645 if os.path.exists (input_filename):
1646 input_fullname = input_filename
1647 elif global_options.format == LATEX and ly.search_exe_path ('kpsewhich'):
1648 input_fullname = os.popen ('kpsewhich ' + input_filename).read()[:-1]
1650 input_fullname = find_file (input_filename)
1652 note_input_file (input_fullname)
1653 in_handle = open (input_fullname)
1655 if input_filename == '-':
1656 input_base = 'stdin'
1658 input_base = os.path.basename \
1659 (os.path.splitext (input_filename)[0])
1661 # Only default to stdout when filtering.
1662 if global_options.output_name == '-' or (not global_options.output_name and global_options.filter_cmd):
1663 output_filename = '-'
1664 output_file = sys.stdout
1666 # don't complain when global_options.output_name is existing
1667 output_filename = input_base + format2ext[global_options.format]
1668 if global_options.output_name:
1669 if not os.path.isdir (global_options.output_name):
1670 os.mkdir (global_options.output_name, 0777)
1671 os.chdir (global_options.output_name)
1673 if (os.path.exists (input_filename)
1674 and os.path.exists (output_filename)
1675 and samefile (output_filename, input_fullname)):
1677 _ ("Output would overwrite input file; use --output."))
1681 progress (_ ("Reading %s...") % input_fullname)
1682 source = in_handle.read ()
1685 set_default_options (source)
1688 # FIXME: Containing blocks must be first, see
1689 # find_toplevel_snippets.
1691 'multiline_comment',
1695 'singleline_comment',
1700 progress (_ ("Dissecting..."))
1701 chunks = find_toplevel_snippets (source, snippet_types)
1703 if global_options.format == LATEX:
1705 if (c.is_plain () and
1706 re.search (r"\\begin *{document}", c.replacement_text())):
1711 if global_options.filter_cmd:
1712 write_if_updated (output_filename,
1713 [c.filter_text () for c in chunks])
1714 elif global_options.process_cmd:
1715 do_process_cmd (chunks, input_fullname)
1716 progress (_ ("Compiling %s...") % output_filename)
1718 write_if_updated (output_filename,
1719 [s.replacement_text ()
1722 def process_include (snippet):
1723 os.chdir (original_dir)
1724 name = snippet.substring ('filename')
1725 progress (_ ("Processing include: %s") % name)
1727 return do_file (name)
1729 include_chunks = map (process_include,
1730 filter (lambda x: is_derived_class (x.__class__,
1735 return chunks + reduce (lambda x,y: x + y, include_chunks, [])
1737 except Compile_error:
1738 os.chdir (original_dir)
1739 progress (_ ("Removing `%s'") % output_filename)
1745 global global_options
1747 opt_parser = get_option_parser()
1748 (global_options, args) = opt_parser.parse_args ()
1750 if global_options.format in ('texi-html', 'texi'):
1751 global_options.format = TEXINFO
1752 global_options.use_hash = True
1754 global_options.include_path = map (os.path.abspath, global_options.include_path)
1756 if global_options.warranty:
1759 if not args or len (args) > 1:
1760 opt_parser.print_help ()
1766 # FIXME: 85 lines of `main' macramee??
1767 files = do_options ()
1771 basename = os.path.splitext (file)[0]
1772 basename = os.path.split (basename)[1]
1774 if not global_options.format:
1775 global_options.format = guess_format (files[0])
1778 if global_options.format in (TEXINFO, HTML, DOCBOOK):
1782 if global_options.process_cmd == '':
1783 global_options.process_cmd = (lilypond_binary
1784 + ' --formats=%s --backend eps ' % formats)
1786 if global_options.process_cmd:
1787 global_options.process_cmd += string.join ([(' -I %s' % ly.mkarg (p))
1788 for p in global_options.include_path])
1790 if global_options.format in (TEXINFO, LATEX):
1791 ## prevent PDF from being switched on by default.
1792 global_options.process_cmd += ' --formats=eps '
1794 if (global_options.format in (TEXINFO, LATEX)
1795 and global_options.create_pdf):
1796 global_options.process_cmd += "--pdf -dinclude-eps-fonts -dgs-load-fonts "
1798 if global_options.verbose:
1799 global_options.process_cmd += " --verbose "
1801 global_options.process_cmd += " -dread-file-list -deps-box-padding=-3 "
1806 chunks = do_file (file)
1807 if global_options.psfonts:
1808 fontextract.verbose = global_options.verbose
1809 snippet_chunks = filter (lambda x: is_derived_class (x.__class__,
1813 psfonts_file = basename + '.psfonts'
1814 if not global_options.verbose:
1815 progress (_ ("Writing fonts to %s...") % psfonts_file)
1816 fontextract.extract_fonts (psfonts_file,
1817 [x.basename() + '.eps'
1818 for x in snippet_chunks])
1819 if not global_options.verbose:
1822 except Compile_error:
1825 if global_options.format in (TEXINFO, LATEX):
1826 psfonts_file = os.path.join (global_options.output_name, basename + '.psfonts')
1827 output = os.path.join (global_options.output_name, basename + '.dvi' )
1829 if not global_options.psfonts and not global_options.create_pdf:
1830 warning (_ ("option --psfonts not used"))
1831 warning (_ ("processing with dvips will have no fonts"))
1834 progress (_ ("DVIPS usage:"))
1836 progress (" dvips -h %(psfonts_file)s %(output)s" % vars ())
1839 inputs = note_input_file ('')
1842 base_file_name = os.path.splitext (os.path.basename (file))[0]
1843 dep_file = os.path.join (global_options.output_name, base_file_name + '.dep')
1844 final_output_file = os.path.join (global_options.output_name,
1846 + '.%s' % global_options.format)
1848 os.chdir (original_dir)
1849 open (dep_file, 'w').write ('%s: %s' % (final_output_file, ' '.join (inputs)))
1851 if __name__ == '__main__':