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
50 program_version = '@TOPLEVEL_VERSION@'
51 program_name = os.path.basename (sys.argv[0])
53 original_dir = os.getcwd ()
57 _ ("Process LilyPond snippets in hybrid HTML, LaTeX, texinfo or DocBook document.")
61 lilypond-book --filter="tr '[a-z]' '[A-Z]'" %(BOOK)s
62 lilypond-book --filter="convert-ly --no-version --from=2.0.0 -" %(BOOK)s
63 lilypond-book --process='lilypond -I include' %(BOOK)s
64 ''' % {'BOOK': _ ("BOOK")})
66 authors = ('Jan Nieuwenhuizen <janneke@gnu.org>',
67 'Han-Wen Nienhuys <hanwen@xs4all.nl>')
69 ################################################################
71 if global_options.verbose:
72 raise _ ('Exiting (%d)...') % i
77 sys.stdout.write ('%s (GNU LilyPond) %s\n' % (program_name, program_version))
83 sys.stderr.write (program_name + ": " + _ ("warning: %s") % s + '\n')
86 sys.stderr.write (program_name + ": " + _ ("error: %s") % s + '\n')
88 def ps_page_count (ps_name):
89 header = open (ps_name).read (1024)
90 m = re.search ('\n%%Pages: ([0-9]+)', header)
92 return string.atoi (m.group (1))
104 ''' % ( _ ('Copyright (c) %s by') % '2001--2007',
106 _ ("Distributed under terms of the GNU General Public License."),
107 _ ("It comes with NO WARRANTY.")))
109 def get_option_parser ():
110 p = ly.get_option_parser (usage=_ ("%s [OPTION]... FILE") % 'lilypond-book',
111 version="@TOPLEVEL_VERSION@",
112 description=help_summary)
114 p.add_option ('-F', '--filter', metavar=_ ("FILTER"),
117 help=_ ("pipe snippets through FILTER [convert-ly -n -]"),
119 p.add_option ('-f', '--format',
120 help=_ ("use output format FORMAT (texi [default], texi-html, latex, html, docbook)"),
123 p.add_option ("-I", '--include', help=_ ("add DIR to include path"),
125 action='append', dest='include_path',
126 default=[os.path.abspath (os.getcwd ())])
128 p.add_option ('--left-padding',
131 help="Pad left side of music to align music inspite of uneven bar numbers. (in mm)",
135 p.add_option ("-o", '--output', help=_ ("write output to DIR"),
137 action='store', dest='output_name',
140 p.add_option ('-P', '--process', metavar=_ ("COMMAND"),
141 help = _ ("process ly_files using COMMAND FILE..."),
143 dest='process_cmd', default='lilypond -dbackend=eps')
144 p.add_option ('--pdf',
147 help=_ ("Create PDF files for use with PDFTeX"),
149 p.add_option ('', '--psfonts', action="store_true", dest="psfonts",
150 help=_ ('''extract all PostScript fonts into INPUT.psfonts for LaTeX
151 must use this with dvips -h INPUT.psfonts'''),
153 p.add_option ('-V', '--verbose', help=_ ("be verbose"),
157 p.add_option ('-w', '--warranty',
158 help=_ ("show warranty and copyright"),
160 p.add_option_group ('bugs',
161 description=(_ ("Report bugs via")
162 + ''' http://post.gmane.org/post.php'''
163 '''?group=gmane.comp.gnu.lilypond.bugs\n'''))
166 lilypond_binary = os.path.join ('@bindir@', 'lilypond')
168 # Only use installed binary when we are installed too.
169 if '@bindir@' == ('@' + 'bindir@') or not os.path.exists (lilypond_binary):
170 lilypond_binary = 'lilypond'
172 global_options = None
175 default_ly_options = { 'alt': "[image of music]" }
178 # Is this pythonic? Personally, I find this rather #define-nesque. --hwn
183 EXAMPLEINDENT = 'exampleindent'
185 FRAGMENT = 'fragment'
190 LINE_WIDTH = 'line-width'
191 LILYQUOTE = 'lilyquote'
192 NOFRAGMENT = 'nofragment'
193 NOINDENT = 'noindent'
198 OUTPUTIMAGE = 'outputimage'
201 PREAMBLE = 'preamble'
202 PRINTFILENAME = 'printfilename'
204 RAGGED_RIGHT = 'ragged-right'
205 RELATIVE = 'relative'
206 STAFFSIZE = 'staffsize'
209 VERBATIM = 'verbatim'
210 FONTLOAD = 'fontload'
211 FILENAME = 'filename'
215 # NOTIME has no opposite so it isn't part of this dictionary.
216 # NOQUOTE is used internally only.
218 NOFRAGMENT: FRAGMENT,
223 # Recognize special sequences in the input.
225 # (?P<name>regex) -- Assign result of REGEX to NAME.
226 # *? -- Match non-greedily.
227 # (?m) -- Multiline regex: Make ^ and $ match at each line.
228 # (?s) -- Make the dot match all characters including newline.
229 # (?x) -- Ignore whitespace in patterns.
240 <(?P<inline>(inline)?)mediaobject>\s*<textobject.*?>\s*<programlisting\s+language="lilypond".*?(role="(?P<options>.*?)")?>(?P<code>.*?)</programlisting\s*>\s*</textobject\s*>\s*</(inline)?mediaobject>)''',
245 <(?P<inline>(inline)?)mediaobject>\s*<textobject.*?>\s*<programlisting\s+language="lilypond".*?(role="(?P<options>.*?)")?>(?P<code>.*?)</programlisting\s*>\s*</textobject\s*>\s*</(inline)?mediaobject>)''',
250 <(?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>)''',
256 (?P<code><!--\s.*?!-->)
259 'singleline_comment':
278 (\s*(?P<options>.*?)\s*:)?\s*
286 \s*(?P<options>.*?)\s*
295 \s*(?P<options>.*?)\s*
297 \s*(?P<filename>.*?)\s*
304 (?P<code><!--\s.*?!-->)
307 'singleline_comment':
313 (?P<code><pre>.*?</pre>))''',
319 (?P<code><pre>\s.*?</pre>\s))''',
338 \s*(?P<options>.*?)\s*
349 \s*(?P<options>.*?)\s*
353 \\end\s*{lilypond})''',
361 \s*(?P<options>.*?)\s*
369 'singleline_comment':
392 \\end\s*{verbatim}))''',
401 (?P<filename>\S+))''',
405 ^[^\n]*?(?!@c\s+)[^\n]*?
409 \s*(?P<options>.*?)\s*
419 \s*(?P<options>.*?)\s*
422 ^@end\s+lilypond)\s''',
429 \s*(?P<options>.*?)\s*
440 @end\s+ignore))\s''',
442 'singleline_comment':
447 @c([ \t][^\n]*|)\n))''',
449 # Don't do this: It interferes with @code{@{}.
450 # 'verb': r'''(?P<code>@code{.*?})''',
458 @end\s+example\s))''',
467 'intertext': r',?\s*intertext=\".*?\"',
471 'intertext': r',?\s*intertext=\".*?\"',
476 'intertext': r',?\s*intertext=\".*?\"',
477 'option_sep': '\s*,\s*',
481 'intertext': r',?\s*intertext=\".*?\"',
482 'option_sep': '\s*,\s*',
486 # Options without a pattern in ly_options.
503 RELATIVE: r'''\relative c%(relative_quotes)s''',
508 INDENT: r'''indent = %(indent)s''',
510 LINE_WIDTH: r'''line-width = %(line-width)s''',
512 QUOTE: r'''line-width = %(line-width)s - 2.0 * %(exampleindent)s''',
514 LILYQUOTE: r'''line-width = %(line-width)s - 2.0 * %(exampleindent)s''',
516 RAGGED_RIGHT: r'''ragged-right = ##t''',
518 PACKED: r'''packed = ##t''',
530 \remove Time_signature_engraver
536 STAFFSIZE: r'''#(set-global-staff-size %(staffsize)s)''',
543 FILTER: r'''<mediaobject><textobject><programlisting language="lilypond" role="%(options)s">%(code)s</programlisting></textobject></mediaobject>''',
546 <imageobject role="latex">
547 <imagedata fileref="%(base)s.pdf" format="PDF"/>
549 <imageobject role="html">
550 <imagedata fileref="%(base)s.png" format="PNG"/></imageobject>''',
552 VERBATIM: r'''<programlisting>%(verb)s</programlisting>''',
554 PRINTFILENAME: '<textobject><simpara><ulink url="%(base)s.ly"><filename>%(filename)s</filename></ulink></simpara></textobject>'
558 FILTER: r'''<lilypond %(options)s>
568 <a href="%(base)s.ly">''',
571 <img align="center" valign="center"
572 border="0" src="%(image)s" alt="%(alt)s">''',
574 PRINTFILENAME: '<p><tt><a href="%(base)s.ly">%(filename)s</a></tt></p>',
576 QUOTE: r'''<blockquote>
589 \ifx\preLilyPondExample \undefined%%
592 \preLilyPondExample%%
594 \def\lilypondbook{}%%
595 \input %(base)s-systems.tex%%
596 \ifx\postLilyPondExample \undefined%%
599 \postLilyPondExample%%
603 PRINTFILENAME: '''\\texttt{%(filename)s}
606 QUOTE: r'''\begin{quotation}%(str)s
609 VERBATIM: r'''\noindent
610 \begin{verbatim}%(verb)s\end{verbatim}''',
612 FILTER: r'''\begin{lilypond}[%(options)s]
619 FILTER: r'''@lilypond[%(options)s]
625 @include %(base)s-systems.texi
629 OUTPUTIMAGE: r'''@noindent
631 @image{%(base)s,,,%(alt)s,%(ext)s}
635 <a href="%(base)s.ly">
636 <img align="center" valign="center"
637 border="0" src="%(image)s" alt="%(alt)s">
645 <a href="%(base)s.ly">
653 QUOTE: r'''@quotation
654 %(str)s@end quotation
661 VERBATIM: r'''@exampleindent 0
663 %(verb)s@end verbatim
669 # Maintain line numbers.
674 for f in [HTML, LATEX]:
675 for s in (QUOTE, VERBATIM):
676 output[f][s] = output[f][s].replace("\n"," ")
679 PREAMBLE_LY = '''%%%% Generated by %(program_name)s
680 %%%% Options: [%(option_string)s]
681 \\include "lilypond-book-preamble.ly"
684 %% ****************************************************************
685 %% Start cut-&-pastable-section
686 %% ****************************************************************
691 #(define dump-extents #t)
692 %(font_dump_setting)s
694 force-assignment = #""
695 line-width = #(- line-width (* mm %(padding_mm)f))
708 %% ****************************************************************
709 %% ly snippet contents follows:
710 %% ****************************************************************
714 %% ****************************************************************
716 %% ****************************************************************
723 %% ****************************************************************
725 %% ****************************************************************
729 %% ****************************************************************
731 %% ****************************************************************
734 texinfo_line_widths = {
735 '@afourpaper': '160\\mm',
736 '@afourwide': '6.5\\in',
737 '@afourlatex': '150\\mm',
738 '@smallbook': '5\\in',
739 '@letterpaper': '6\\in',
742 def classic_lilypond_book_compatibility (key, value):
743 if key == 'singleline' and value == None:
744 return (RAGGED_RIGHT, None)
746 m = re.search ('relative\s*([-0-9])', key)
748 return ('relative', m.group (1))
750 m = re.match ('([0-9]+)pt', key)
752 return ('staffsize', m.group (1))
754 if key == 'indent' or key == 'line-width':
755 m = re.match ('([-.0-9]+)(cm|in|mm|pt|staffspace)', value)
757 f = float (m.group (1))
758 return (key, '%f\\%s' % (f, m.group (2)))
762 def find_file (name):
763 for i in global_options.include_path:
764 full = os.path.join (i, name)
765 if os.path.exists (full):
768 error (_ ("file not found: %s") % name + '\n')
772 def verbatim_html (s):
773 return re.sub ('>', '>',
775 re.sub ('&', '&', s)))
777 def split_options (option_string):
779 if global_options.format == HTML:
780 options = re.findall('[\w\.-:]+(?:\s*=\s*(?:"[^"]*"|\'[^\']*\'|\S+))?',option_string)
781 for i in range(len(options)):
782 options[i] = re.sub('^([^=]+=\s*)(?P<q>["\'])(.*)(?P=q)','\g<1>\g<3>',options[i])
785 return re.split (format_res[global_options.format]['option_sep'],
789 def set_default_options (source):
790 global default_ly_options
791 if not default_ly_options.has_key (LINE_WIDTH):
792 if global_options.format == LATEX:
793 textwidth = get_latex_textwidth (source)
794 default_ly_options[LINE_WIDTH] = \
795 '''%.0f\\pt''' % textwidth
796 elif global_options.format == TEXINFO:
797 for (k, v) in texinfo_line_widths.items ():
798 # FIXME: @layout is usually not in
801 # \input texinfo @c -*-texinfo-*-
803 # Bluntly search first K items of
805 # s = chunks[0].replacement_text ()
806 if re.search (k, source[:1024]):
807 default_ly_options[LINE_WIDTH] = v
811 def replacement_text (self):
814 def filter_text (self):
815 return self.replacement_text ()
817 def ly_is_outdated (self):
820 def png_is_outdated (self):
826 class Substring (Chunk):
827 def __init__ (self, source, start, end, line_number):
831 self.line_number = line_number
832 self.override_text = None
837 def replacement_text (self):
838 if self.override_text:
839 return self.override_text
841 return self.source[self.start:self.end]
843 class Snippet (Chunk):
844 def __init__ (self, type, match, format, line_number):
848 self.option_dict = {}
850 self.line_number = line_number
852 def replacement_text (self):
853 return self.match.group ('match')
855 def substring (self, s):
856 return self.match.group (s)
859 return `self.__class__` + ' type = ' + self.type
861 class Include_snippet (Snippet):
862 def processed_filename (self):
863 f = self.substring ('filename')
864 return os.path.splitext (f)[0] + format2ext[global_options.format]
866 def replacement_text (self):
867 s = self.match.group ('match')
868 f = self.substring ('filename')
870 return re.sub (f, self.processed_filename (), s)
872 class Lilypond_snippet (Snippet):
873 def __init__ (self, type, match, format, line_number):
874 Snippet.__init__ (self, type, match, format, line_number)
875 os = match.group ('options')
876 self.do_options (os, self.type)
879 contents = self.substring ('code')
880 return ('\\sourcefileline %d\n%s'
881 % (self.line_number - 1, contents))
886 return self.compose_ly (s)
889 def do_options (self, option_string, type):
890 self.option_dict = {}
892 options = split_options (option_string)
896 (key, value) = re.split ('\s*=\s*', i)
897 self.option_dict[key] = value
899 if i in no_options.keys ():
900 if no_options[i] in self.option_dict.keys ():
901 del self.option_dict[no_options[i]]
903 self.option_dict[i] = None
905 has_line_width = self.option_dict.has_key (LINE_WIDTH)
906 no_line_width_value = 0
908 # If LINE_WIDTH is used without parameter, set it to default.
909 if has_line_width and self.option_dict[LINE_WIDTH] == None:
910 no_line_width_value = 1
911 del self.option_dict[LINE_WIDTH]
913 for i in default_ly_options.keys ():
914 if i not in self.option_dict.keys ():
915 self.option_dict[i] = default_ly_options[i]
917 if not has_line_width:
918 if type == 'lilypond' or FRAGMENT in self.option_dict.keys ():
919 self.option_dict[RAGGED_RIGHT] = None
921 if type == 'lilypond':
922 if LINE_WIDTH in self.option_dict.keys ():
923 del self.option_dict[LINE_WIDTH]
925 if RAGGED_RIGHT in self.option_dict.keys ():
926 if LINE_WIDTH in self.option_dict.keys ():
927 del self.option_dict[LINE_WIDTH]
929 if QUOTE in self.option_dict.keys () or type == 'lilypond':
930 if LINE_WIDTH in self.option_dict.keys ():
931 del self.option_dict[LINE_WIDTH]
933 if not INDENT in self.option_dict.keys ():
934 self.option_dict[INDENT] = '0\\mm'
936 # The QUOTE pattern from ly_options only emits the `line-width'
938 if has_line_width and QUOTE in self.option_dict.keys ():
939 if no_line_width_value:
940 del self.option_dict[LINE_WIDTH]
942 del self.option_dict[QUOTE]
944 def compose_ly (self, code):
945 if FRAGMENT in self.option_dict.keys ():
953 # The original concept of the `exampleindent' option is broken.
954 # It is not possible to get a sane value for @exampleindent at all
955 # without processing the document itself. Saying
963 # causes ugly results with the DVI backend of texinfo since the
964 # default value for @exampleindent isn't 5em but 0.4in (or a smaller
965 # value). Executing the above code changes the environment
966 # indentation to an unknown value because we don't know the amount
967 # of 1em in advance since it is font-dependent. Modifying
968 # @exampleindent in the middle of a document is simply not
969 # supported within texinfo.
971 # As a consequence, the only function of @exampleindent is now to
972 # specify the amount of indentation for the `quote' option.
974 # To set @exampleindent locally to zero, we use the @format
975 # environment for non-quoted snippets.
976 override[EXAMPLEINDENT] = r'0.4\in'
977 override[LINE_WIDTH] = texinfo_line_widths['@smallbook']
978 override.update (default_ly_options)
981 for (key, value) in self.option_dict.items ():
983 option_list.append (key)
985 option_list.append (key + '=' + value)
986 option_string = string.join (option_list, ',')
989 compose_types = [NOTES, PREAMBLE, LAYOUT, PAPER]
990 for a in compose_types:
993 for (key, value) in self.option_dict.items ():
995 classic_lilypond_book_compatibility (key, value)
999 (_ ("deprecated ly-option used: %s=%s" \
1002 (_ ("compatibility mode translation: %s=%s" \
1003 % (c_key, c_value)))
1006 (_ ("deprecated ly-option used: %s" \
1009 (_ ("compatibility mode translation: %s" \
1012 (key, value) = (c_key, c_value)
1015 override[key] = value
1017 if not override.has_key (key):
1018 override[key] = None
1021 for type in compose_types:
1022 if ly_options[type].has_key (key):
1023 compose_dict[type].append (ly_options[type][key])
1027 if not found and key not in simple_options:
1028 warning (_ ("ignoring unknown ly option: %s") % key)
1031 if RELATIVE in override.keys () and override[RELATIVE]:
1032 relative = int (override[RELATIVE])
1034 relative_quotes = ''
1038 relative_quotes += ',' * (- relative)
1040 relative_quotes += "'" * relative
1042 paper_string = string.join (compose_dict[PAPER],
1044 layout_string = string.join (compose_dict[LAYOUT],
1046 notes_string = string.join (compose_dict[NOTES],
1048 preamble_string = string.join (compose_dict[PREAMBLE],
1050 padding_mm = global_options.padding_mm
1051 font_dump_setting = ''
1052 if FONTLOAD in self.option_dict:
1053 font_dump_setting = '#(define-public force-eps-font-include #t)\n'
1055 d = globals().copy()
1057 return (PREAMBLE_LY + body) % d
1059 def get_hash (self):
1061 hash = md5.md5 (self.relevant_contents (self.full_ly ()))
1063 ## let's not create too long names.
1064 self.hash = hash.hexdigest ()[:10]
1068 def basename (self):
1069 if FILENAME in self.option_dict:
1070 return self.option_dict[FILENAME]
1071 if global_options.use_hash:
1072 return 'lily-%s' % self.get_hash ()
1075 def write_ly (self):
1076 outf = open (self.basename () + '.ly', 'w')
1077 outf.write (self.full_ly ())
1078 open (self.basename () + '.txt', 'w').write ('image of music')
1080 def relevant_contents (self, ly):
1081 return re.sub (r'\\(version|sourcefileline|sourcefilename)[^\n]*\n', '', ly)
1083 def ly_is_outdated (self):
1084 base = self.basename ()
1085 ly_file = base + '.ly'
1086 tex_file = base + '.tex'
1087 eps_file = base + '.eps'
1088 systems_file = base + '-systems.tex'
1090 if (os.path.exists (ly_file)
1091 and os.path.exists (systems_file)
1092 and os.stat (systems_file)[stat.ST_SIZE]
1093 and re.match ('% eof', open (systems_file).readlines ()[-1])
1094 and (global_options.use_hash or FILENAME in self.option_dict)
1095 and (self.relevant_contents (self.full_ly ())
1096 == self.relevant_contents (open (ly_file).read ()))):
1101 def png_is_outdated (self):
1102 base = self.basename ()
1103 # FIXME: refactor stupid OK stuff
1104 ok = not self.ly_is_outdated ()
1105 if global_options.format in (HTML, TEXINFO):
1106 ok = ok and os.path.exists (base + '.eps')
1110 page_count = ps_page_count (base + '.eps')
1113 ok = ok and os.path.exists (base + '.png')
1115 elif page_count > 1:
1116 for a in range (1, page_count + 1):
1117 ok = ok and os.path.exists (base + '-page%d.png' % a)
1121 def texstr_is_outdated (self):
1125 # FIXME: refactor stupid OK stuff
1126 base = self.basename ()
1127 ok = self.ly_is_outdated ()
1128 ok = ok and (os.path.exists (base + '.texstr'))
1131 def filter_text (self):
1132 code = self.substring ('code')
1133 s = run_filter (code)
1136 'options': self.match.group ('options')
1139 return output[self.format][FILTER] % d
1141 def replacement_text (self):
1142 func = Lilypond_snippet.__dict__['output_' + self.format]
1145 def get_images (self):
1146 base = self.basename ()
1148 single = '%(base)s.png' % vars ()
1149 multiple = '%(base)s-page1.png' % vars ()
1151 if os.path.exists (multiple) \
1152 and (not os.path.exists (single) \
1153 or (os.stat (multiple)[stat.ST_MTIME] \
1154 > os.stat (single)[stat.ST_MTIME])):
1155 count = ps_page_count ('%(base)s.eps' % vars ())
1156 images = ['%s-page%d.png' % (base, a) for a in range (1, count+1)]
1157 images = tuple (images)
1160 def output_docbook (self):
1162 base = self.basename ()
1163 for image in self.get_images ():
1164 (base, ext) = os.path.splitext (image)
1165 str += output[DOCBOOK][OUTPUT] % vars ()
1166 str += self.output_print_filename (DOCBOOK)
1167 if (self.substring('inline') == 'inline'):
1168 str = '<inlinemediaobject>' + str + '</inlinemediaobject>'
1170 str = '<mediaobject>' + str + '</mediaobject>'
1171 if VERBATIM in self.option_dict:
1172 verb = verbatim_html (self.substring ('code'))
1173 str = output[DOCBOOK][VERBATIM] % vars () + str
1176 def output_html (self):
1178 base = self.basename ()
1179 if global_options.format == HTML:
1180 str += self.output_print_filename (HTML)
1181 if VERBATIM in self.option_dict:
1182 verb = verbatim_html (self.substring ('code'))
1183 str += output[HTML][VERBATIM] % vars ()
1184 if QUOTE in self.option_dict:
1185 str = output[HTML][QUOTE] % vars ()
1187 str += output[HTML][BEFORE] % vars ()
1188 for image in self.get_images ():
1189 (base, ext) = os.path.splitext (image)
1190 alt = self.option_dict[ALT]
1191 str += output[HTML][OUTPUT] % vars ()
1192 str += output[HTML][AFTER] % vars ()
1195 def output_info (self):
1197 for image in self.get_images ():
1198 (base, ext) = os.path.splitext (image)
1200 # URG, makeinfo implicitly prepends dot to extension.
1201 # Specifying no extension is most robust.
1203 alt = self.option_dict[ALT]
1204 str += output[TEXINFO][OUTPUTIMAGE] % vars ()
1206 base = self.basename ()
1207 str += output[global_options.format][OUTPUT] % vars ()
1210 def output_latex (self):
1212 base = self.basename ()
1213 if global_options.format == LATEX:
1214 str += self.output_print_filename (LATEX)
1215 if VERBATIM in self.option_dict:
1216 verb = self.substring ('code')
1217 str += (output[LATEX][VERBATIM] % vars ())
1219 str += (output[LATEX][OUTPUT] % vars ())
1221 ## todo: maintain breaks
1223 breaks = self.ly ().count ("\n")
1224 str += "".ljust (breaks, "\n").replace ("\n","%\n")
1226 if QUOTE in self.option_dict:
1227 str = output[LATEX][QUOTE] % vars ()
1230 def output_print_filename (self, format):
1232 if PRINTFILENAME in self.option_dict:
1233 base = self.basename ()
1234 filename = self.substring ('filename')
1235 str = output[global_options.format][PRINTFILENAME] % vars ()
1239 def output_texinfo (self):
1241 if self.output_print_filename (TEXINFO):
1243 + self.output_print_filename (HTML)
1246 + self.output_print_filename (LATEX)
1248 base = self.basename ()
1249 if TEXIDOC in self.option_dict:
1250 texidoc = base + '.texidoc'
1251 if os.path.exists (texidoc):
1252 str += '@include %(texidoc)s\n\n' % vars ()
1254 if VERBATIM in self.option_dict:
1255 verb = self.substring ('code')
1256 str += output[TEXINFO][VERBATIM] % vars ()
1257 if not QUOTE in self.option_dict:
1258 str = output[TEXINFO][NOQUOTE] % vars ()
1260 if LILYQUOTE in self.option_dict:
1261 str += output[TEXINFO][QUOTE] % {'str':self.output_info ()}
1263 str += self.output_info ()
1265 # str += ('@ifinfo\n' + self.output_info () + '\n@end ifinfo\n')
1266 # str += ('@tex\n' + self.output_latex () + '\n@end tex\n')
1267 # str += ('@html\n' + self.output_html () + '\n@end html\n')
1269 if QUOTE in self.option_dict:
1270 str = output[TEXINFO][QUOTE] % vars ()
1272 # need par after image
1277 class Lilypond_file_snippet (Lilypond_snippet):
1279 name = self.substring ('filename')
1280 contents = open (find_file (name)).read ()
1281 return ('\\sourcefilename \"%s\"\n\\sourcefileline 0\n%s'
1284 snippet_type_to_class = {
1285 'lilypond_file': Lilypond_file_snippet,
1286 'lilypond_block': Lilypond_snippet,
1287 'lilypond': Lilypond_snippet,
1288 'include': Include_snippet,
1291 def find_linestarts (s):
1296 i = s.find ('\n', start)
1304 nls.append (len (s))
1307 def find_toplevel_snippets (s, types):
1310 res[i] = ly.re.compile (snippet_res[global_options.format][i])
1314 found = dict ([(t, None) for t in types])
1316 line_starts = find_linestarts (s)
1318 # We want to search for multiple regexes, without searching
1319 # the string multiple times for one regex.
1320 # Hence, we use earlier results to limit the string portion
1322 # Since every part of the string is traversed at most once for
1323 # every type of snippet, this is linear.
1329 if not found[type] or found[type][0] < index:
1332 m = res[type].search (s[index:endex])
1337 if snippet_type_to_class.has_key (type):
1338 cl = snippet_type_to_class[type]
1341 start = index + m.start ('match')
1342 line_number = line_start_idx
1343 while (line_starts[line_number] < start):
1347 snip = cl (type, m, global_options.format, line_number)
1349 found[type] = (start, snip)
1353 or found[type][0] < found[first][0]):
1358 # Limiting the search space is a cute
1359 # idea, but this *requires* to search
1360 # for possible containing blocks
1361 # first, at least as long as we do not
1362 # search for the start of blocks, but
1363 # always/directly for the entire
1364 # @block ... @end block.
1366 endex = found[first][0]
1369 snippets.append (Substring (s, index, len (s), line_start_idx))
1372 while (start > line_starts[line_start_idx+1]):
1375 (start, snip) = found[first]
1376 snippets.append (Substring (s, index, start, line_start_idx + 1))
1377 snippets.append (snip)
1379 index = start + len (snip.match.group ('match'))
1383 def filter_pipe (input, cmd):
1384 if global_options.verbose:
1385 progress (_ ("Opening filter `%s'") % cmd)
1387 (stdin, stdout, stderr) = os.popen3 (cmd)
1389 status = stdin.close ()
1393 output = stdout.read ()
1394 status = stdout.close ()
1395 error = stderr.read ()
1399 signal = 0x0f & status
1400 if status or (not output and error):
1401 exit_status = status >> 8
1402 error (_ ("`%s' failed (%d)") % (cmd, exit_status))
1403 error (_ ("The error log is as follows:"))
1404 sys.stderr.write (error)
1405 sys.stderr.write (stderr.read ())
1408 if global_options.verbose:
1414 return filter_pipe (s, global_options.filter_cmd)
1416 def is_derived_class (cl, baseclass):
1419 for b in cl.__bases__:
1420 if is_derived_class (b, baseclass):
1424 def process_snippets (cmd, ly_snippets, texstr_snippets, png_snippets):
1425 ly_names = filter (lambda x: x,
1426 map (Lilypond_snippet.basename, ly_snippets))
1427 texstr_names = filter (lambda x: x,
1428 map (Lilypond_snippet.basename, texstr_snippets))
1430 png_names = filter (lambda x: x,
1431 map (Lilypond_snippet.basename, png_snippets))
1434 def my_system (cmd):
1435 status = ly.system (cmd,
1436 be_verbose=global_options.verbose,
1439 if global_options.format in (HTML, TEXINFO) and '--formats' not in cmd:
1440 cmd += ' --formats=png '
1441 elif global_options.format in (DOCBOOK) and '--formats' not in cmd:
1442 cmd += ' --formats=png,pdf '
1446 # the --process=CMD switch is a bad idea
1447 # it is too generic for lilypond-book.
1449 my_system (string.join ([cmd, '--backend texstr',
1450 'snippet-map.ly'] + texstr_names))
1451 for l in texstr_names:
1452 my_system ('latex %s.texstr' % l)
1455 open ('snippet-names', 'wb').write ('\n'.join (['snippet-map.ly']
1458 my_system (string.join ([cmd, 'snippet-names']))
1461 LATEX_INSPECTION_DOCUMENT = r'''
1465 \typeout{textwidth=\the\textwidth}
1466 \typeout{columnsep=\the\columnsep}
1467 \makeatletter\if@twocolumn\typeout{columns=2}\fi\makeatother
1471 # Do we need anything else besides `textwidth'?
1472 def get_latex_textwidth (source):
1473 m = re.search (r'''(?P<preamble>\\begin\s*{document})''', source)
1475 warning (_ ("cannot find \\begin{document} in LaTeX document"))
1477 ## what's a sensible default?
1480 preamble = source[:m.start (0)]
1481 latex_document = LATEX_INSPECTION_DOCUMENT % vars ()
1483 (handle, tmpfile) = tempfile.mkstemp('.tex')
1484 logfile = os.path.splitext (tmpfile)[0] + '.log'
1485 logfile = os.path.split (logfile)[1]
1487 tmp_handle = os.fdopen (handle,'w')
1488 tmp_handle.write (latex_document)
1491 ly.system ('latex %s' % tmpfile, be_verbose=global_options.verbose)
1492 parameter_string = open (logfile).read()
1498 m = re.search ('columns=([0-9.]*)', parameter_string)
1500 columns = int (m.group (1))
1503 m = re.search ('columnsep=([0-9.]*)pt', parameter_string)
1505 columnsep = float (m.group (1))
1508 m = re.search ('textwidth=([0-9.]*)pt', parameter_string)
1510 textwidth = float (m.group (1))
1512 textwidth = (textwidth - columnsep) / columns
1516 def modify_preamble (chunk):
1517 str = chunk.replacement_text ()
1518 if (re.search (r"\\begin *{document}", str)
1519 and not re.search ("{graphic[sx]", str)):
1520 str = re.sub (r"\\begin{document}",
1521 r"\\usepackage{graphics}" + '\n'
1522 + r"\\begin{document}",
1524 chunk.override_text = str
1536 '.texinfo': TEXINFO,
1543 # TEXINFO: '.texinfo',
1549 class Compile_error:
1552 def write_file_map (lys, name):
1553 snippet_map = open ('snippet-map.ly', 'w')
1554 snippet_map.write ("""
1555 #(define version-seen #t)
1556 #(define output-empty-score-list #f)
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 ()
1765 def psfonts_warning (options, basename):
1766 if options.format in (TEXINFO, LATEX):
1767 psfonts_file = os.path.join (options.output_name, basename + '.psfonts')
1768 output = os.path.join (options.output_name, basename + '.dvi' )
1770 if not options.create_pdf:
1771 if not options.psfonts:
1772 warning (_ ("option --psfonts not used"))
1773 warning (_ ("processing with dvips will have no fonts"))
1776 progress (_ ("DVIPS usage:"))
1778 progress (" dvips -h %(psfonts_file)s %(output)s" % vars ())
1782 # FIXME: 85 lines of `main' macramee??
1783 files = do_options ()
1787 basename = os.path.splitext (file)[0]
1788 basename = os.path.split (basename)[1]
1790 if not global_options.format:
1791 global_options.format = guess_format (files[0])
1794 if global_options.format in (TEXINFO, HTML, DOCBOOK):
1798 if global_options.process_cmd == '':
1799 global_options.process_cmd = (lilypond_binary
1800 + ' --formats=%s --backend eps ' % formats)
1802 if global_options.process_cmd:
1803 global_options.process_cmd += string.join ([(' -I %s' % ly.mkarg (p))
1804 for p in global_options.include_path])
1806 if global_options.format in (TEXINFO, LATEX):
1807 ## prevent PDF from being switched on by default.
1808 global_options.process_cmd += ' --formats=eps '
1809 if global_options.create_pdf:
1810 global_options.process_cmd += "--pdf -dinclude-eps-fonts -dgs-load-fonts "
1812 if global_options.verbose:
1813 global_options.process_cmd += " --verbose "
1815 if global_options.padding_mm:
1816 global_options.process_cmd += " -deps-box-padding=%f " % global_options.padding_mm
1818 global_options.process_cmd += " -dread-file-list "
1823 chunks = do_file (file)
1824 if global_options.psfonts:
1825 fontextract.verbose = global_options.verbose
1826 snippet_chunks = filter (lambda x: is_derived_class (x.__class__,
1830 psfonts_file = basename + '.psfonts'
1831 if not global_options.verbose:
1832 progress (_ ("Writing fonts to %s...") % psfonts_file)
1833 fontextract.extract_fonts (psfonts_file,
1834 [x.basename() + '.eps'
1835 for x in snippet_chunks])
1836 if not global_options.verbose:
1839 except Compile_error:
1842 psfonts_warning (global_options, basename)
1844 inputs = note_input_file ('')
1847 base_file_name = os.path.splitext (os.path.basename (file))[0]
1848 dep_file = os.path.join (global_options.output_name, base_file_name + '.dep')
1849 final_output_file = os.path.join (global_options.output_name,
1851 + '.%s' % global_options.format)
1853 os.chdir (original_dir)
1854 open (dep_file, 'w').write ('%s: %s' % (final_output_file, ' '.join (inputs)))
1856 if __name__ == '__main__':