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 Exception (_ ('Exiting (%d)...') % i)
77 ly.encoded_write (sys.stdout, '%s (GNU LilyPond) %s\n' % (program_name, program_version))
79 progress = ly.progress
82 ly.stderr_write (program_name + ": " + _ ("warning: %s") % s + '\n')
85 ly.stderr_write (program_name + ": " + _ ("error: %s") % s + '\n')
87 def ps_page_count (ps_name):
88 header = open (ps_name).read (1024)
89 m = re.search ('\n%%Pages: ([0-9]+)', header)
91 return int (m.group (1))
96 ly.encoded_write (sys.stdout, '''
103 ''' % ( _ ('Copyright (c) %s by') % '2001--2007',
105 _ ("Distributed under terms of the GNU General Public License."),
106 _ ("It comes with NO WARRANTY.")))
108 def get_option_parser ():
109 p = ly.get_option_parser (usage=_ ("%s [OPTION]... FILE") % 'lilypond-book',
110 description=help_summary,
111 add_help_option=False)
113 p.add_option ('-F', '--filter', metavar=_ ("FILTER"),
116 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("-h", "--help",
125 help=_ ("show this help and exit"))
127 p.add_option ("-I", '--include', help=_ ("add DIR to include path"),
129 action='append', dest='include_path',
130 default=[os.path.abspath (os.getcwd ())])
132 p.add_option ('--info-images-dir',
133 help=_ ("format Texinfo output so that Info will "
134 "look for images of music in DIR"),
136 action='store', dest='info_images_dir',
139 p.add_option ('--left-padding',
142 help=_ ("pad left side of music to align music inspite of uneven bar numbers (in mm)"),
146 p.add_option ("-o", '--output', help=_ ("write output to DIR"),
148 action='store', dest='output_name',
151 p.add_option ('-P', '--process', metavar=_ ("COMMAND"),
152 help = _ ("process ly_files using COMMAND FILE..."),
154 dest='process_cmd', default='lilypond -dbackend=eps')
156 p.add_option ('--pdf',
159 help=_ ("create PDF files for use with PDFTeX"),
162 p.add_option ('', '--psfonts', action="store_true", dest="psfonts",
163 help=_ ('''extract all PostScript fonts into INPUT.psfonts for LaTeX
164 must use this with dvips -h INPUT.psfonts'''),
167 p.add_option ('-V', '--verbose', help=_ ("be verbose"),
172 p.version = "@TOPLEVEL_VERSION@"
173 p.add_option("--version",
175 help=_ ("show version number and exit"))
177 p.add_option ('-w', '--warranty',
178 help=_ ("show warranty and copyright"),
180 p.add_option_group (ly.display_encode (_ ('Bugs')),
181 description=(_ ("Report bugs via")
182 + ''' http://post.gmane.org/post.php'''
183 '''?group=gmane.comp.gnu.lilypond.bugs\n'''))
186 lilypond_binary = os.path.join ('@bindir@', 'lilypond')
188 # Only use installed binary when we are installed too.
189 if '@bindir@' == ('@' + 'bindir@') or not os.path.exists (lilypond_binary):
190 lilypond_binary = 'lilypond'
192 global_options = None
195 default_ly_options = { 'alt': "[image of music]" }
198 # Is this pythonic? Personally, I find this rather #define-nesque. --hwn
200 ADDVERSION = 'addversion'
204 EXAMPLEINDENT = 'exampleindent'
206 FRAGMENT = 'fragment'
211 LINE_WIDTH = 'line-width'
212 LILYQUOTE = 'lilyquote'
213 NOFRAGMENT = 'nofragment'
214 NOINDENT = 'noindent'
219 OUTPUTIMAGE = 'outputimage'
222 PREAMBLE = 'preamble'
223 PRINTFILENAME = 'printfilename'
225 RAGGED_RIGHT = 'ragged-right'
226 RELATIVE = 'relative'
227 STAFFSIZE = 'staffsize'
230 VERBATIM = 'verbatim'
231 FONTLOAD = 'fontload'
232 FILENAME = 'filename'
236 # NOTIME has no opposite so it isn't part of this dictionary.
237 # NOQUOTE is used internally only.
239 NOFRAGMENT: FRAGMENT,
244 # Recognize special sequences in the input.
246 # (?P<name>regex) -- Assign result of REGEX to NAME.
247 # *? -- Match non-greedily.
248 # (?m) -- Multiline regex: Make ^ and $ match at each line.
249 # (?s) -- Make the dot match all characters including newline.
250 # (?x) -- Ignore whitespace in patterns.
261 <(?P<inline>(inline)?)mediaobject>\s*<textobject.*?>\s*<programlisting\s+language="lilypond".*?(role="(?P<options>.*?)")?>(?P<code>.*?)</programlisting\s*>\s*</textobject\s*>\s*</(inline)?mediaobject>)''',
266 <(?P<inline>(inline)?)mediaobject>\s*<textobject.*?>\s*<programlisting\s+language="lilypond".*?(role="(?P<options>.*?)")?>(?P<code>.*?)</programlisting\s*>\s*</textobject\s*>\s*</(inline)?mediaobject>)''',
271 <(?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>)''',
277 (?P<code><!--\s.*?!-->)
280 'singleline_comment':
299 (\s*(?P<options>.*?)\s*:)?\s*
307 \s*(?P<options>.*?)\s*
316 \s*(?P<options>.*?)\s*
318 \s*(?P<filename>.*?)\s*
325 (?P<code><!--\s.*?!-->)
328 'singleline_comment':
334 (?P<code><pre>.*?</pre>))''',
340 (?P<code><pre>\s.*?</pre>\s))''',
359 \s*(?P<options>.*?)\s*
370 \s*(?P<options>.*?)\s*
374 \\end\s*{lilypond})''',
382 \s*(?P<options>.*?)\s*
390 'singleline_comment':
413 \\end\s*{verbatim}))''',
422 (?P<filename>\S+))''',
426 ^[^\n]*?(?!@c\s+)[^\n]*?
430 \s*(?P<options>.*?)\s*
440 \s*(?P<options>.*?)\s*
443 ^@end\s+lilypond)\s''',
450 \s*(?P<options>.*?)\s*
461 @end\s+ignore))\s''',
463 'singleline_comment':
468 @c([ \t][^\n]*|)\n))''',
470 # Don't do this: It interferes with @code{@{}.
471 # 'verb': r'''(?P<code>@code{.*?})''',
479 @end\s+example\s))''',
488 'intertext': r',?\s*intertext=\".*?\"',
492 'intertext': r',?\s*intertext=\".*?\"',
497 'intertext': r',?\s*intertext=\".*?\"',
498 'option_sep': '\s*,\s*',
502 'intertext': r',?\s*intertext=\".*?\"',
503 'option_sep': '\s*,\s*',
507 # Options without a pattern in ly_options.
525 RELATIVE: r'''\relative c%(relative_quotes)s''',
530 INDENT: r'''indent = %(indent)s''',
532 LINE_WIDTH: r'''line-width = %(line-width)s''',
534 QUOTE: r'''line-width = %(line-width)s - 2.0 * %(exampleindent)s''',
536 LILYQUOTE: r'''line-width = %(line-width)s - 2.0 * %(exampleindent)s''',
538 RAGGED_RIGHT: r'''ragged-right = ##t''',
540 PACKED: r'''packed = ##t''',
552 \remove Time_signature_engraver
558 STAFFSIZE: r'''#(set-global-staff-size %(staffsize)s)''',
565 FILTER: r'''<mediaobject><textobject><programlisting language="lilypond" role="%(options)s">%(code)s</programlisting></textobject></mediaobject>''',
568 <imageobject role="latex">
569 <imagedata fileref="%(base)s.pdf" format="PDF"/>
571 <imageobject role="html">
572 <imagedata fileref="%(base)s.png" format="PNG"/></imageobject>''',
574 VERBATIM: r'''<programlisting>%(verb)s</programlisting>''',
576 PRINTFILENAME: '<textobject><simpara><ulink url="%(base)s.ly"><filename>%(filename)s</filename></ulink></simpara></textobject>'
580 FILTER: r'''<lilypond %(options)s>
590 <a href="%(base)s.ly">''',
594 border="0" src="%(image)s" alt="%(alt)s">''',
596 PRINTFILENAME: '<p><tt><a href="%(base)s.ly">%(filename)s</a></tt></p>',
598 QUOTE: r'''<blockquote>
611 \ifx\preLilyPondExample \undefined%%
614 \preLilyPondExample%%
616 \def\lilypondbook{}%%
617 \input %(base)s-systems.tex%%
618 \ifx\postLilyPondExample \undefined%%
621 \postLilyPondExample%%
625 PRINTFILENAME: '''\\texttt{%(filename)s}
628 QUOTE: r'''\begin{quotation}%(str)s
631 VERBATIM: r'''\noindent
632 \begin{verbatim}%(verb)s\end{verbatim}''',
634 FILTER: r'''\begin{lilypond}[%(options)s]
641 FILTER: r'''@lilypond[%(options)s]
647 @include %(base)s-systems.texi
651 OUTPUTIMAGE: r'''@noindent
653 @image{%(info_image_path)s,,,%(alt)s,%(ext)s}
657 <a href="%(base)s.ly">
659 border="0" src="%(image)s" alt="%(alt)s">
667 <a href="%(base)s.ly">
675 QUOTE: r'''@quotation
676 %(str)s@end quotation
683 VERBATIM: r'''@exampleindent 0
685 %(verb)s@end verbatim
688 ADDVERSION: r'''@example
689 \version @w{"@version{}"}
696 # Maintain line numbers.
701 for f in [HTML, LATEX]:
702 for s in (QUOTE, VERBATIM):
703 output[f][s] = output[f][s].replace("\n"," ")
706 PREAMBLE_LY = '''%%%% Generated by %(program_name)s
707 %%%% Options: [%(option_string)s]
708 \\include "lilypond-book-preamble.ly"
711 %% ****************************************************************
712 %% Start cut-&-pastable-section
713 %% ****************************************************************
718 #(define dump-extents #t)
719 %(font_dump_setting)s
721 force-assignment = #""
722 line-width = #(- line-width (* mm %(padding_mm)f))
735 %% ****************************************************************
736 %% ly snippet contents follows:
737 %% ****************************************************************
741 %% ****************************************************************
743 %% ****************************************************************
750 %% ****************************************************************
752 %% ****************************************************************
756 %% ****************************************************************
758 %% ****************************************************************
761 texinfo_line_widths = {
762 '@afourpaper': '160\\mm',
763 '@afourwide': '6.5\\in',
764 '@afourlatex': '150\\mm',
765 '@smallbook': '5\\in',
766 '@letterpaper': '6\\in',
769 def classic_lilypond_book_compatibility (key, value):
770 if key == 'singleline' and value == None:
771 return (RAGGED_RIGHT, None)
773 m = re.search ('relative\s*([-0-9])', key)
775 return ('relative', m.group (1))
777 m = re.match ('([0-9]+)pt', key)
779 return ('staffsize', m.group (1))
781 if key == 'indent' or key == 'line-width':
782 m = re.match ('([-.0-9]+)(cm|in|mm|pt|staffspace)', value)
784 f = float (m.group (1))
785 return (key, '%f\\%s' % (f, m.group (2)))
789 def find_file (name, raise_error=True):
790 for i in global_options.include_path:
791 full = os.path.join (i, name)
792 if os.path.exists (full):
796 error (_ ("file not found: %s") % name + '\n')
800 def verbatim_html (s):
801 return re.sub ('>', '>',
803 re.sub ('&', '&', s)))
805 def split_options (option_string):
807 if global_options.format == HTML:
808 options = re.findall('[\w\.-:]+(?:\s*=\s*(?:"[^"]*"|\'[^\']*\'|\S+))?',option_string)
809 for i in range(len(options)):
810 options[i] = re.sub('^([^=]+=\s*)(?P<q>["\'])(.*)(?P=q)','\g<1>\g<3>',options[i])
813 return re.split (format_res[global_options.format]['option_sep'],
817 def set_default_options (source):
818 global default_ly_options
819 if not default_ly_options.has_key (LINE_WIDTH):
820 if global_options.format == LATEX:
821 textwidth = get_latex_textwidth (source)
822 default_ly_options[LINE_WIDTH] = \
823 '''%.0f\\pt''' % textwidth
824 elif global_options.format == TEXINFO:
825 for (k, v) in texinfo_line_widths.items ():
826 # FIXME: @layout is usually not in
829 # \input texinfo @c -*-texinfo-*-
831 # Bluntly search first K items of
833 # s = chunks[0].replacement_text ()
834 if re.search (k, source[:1024]):
835 default_ly_options[LINE_WIDTH] = v
839 def replacement_text (self):
842 def filter_text (self):
843 return self.replacement_text ()
845 def ly_is_outdated (self):
848 def png_is_outdated (self):
854 class Substring (Chunk):
855 def __init__ (self, source, start, end, line_number):
859 self.line_number = line_number
860 self.override_text = None
865 def replacement_text (self):
866 if self.override_text:
867 return self.override_text
869 return self.source[self.start:self.end]
871 class Snippet (Chunk):
872 def __init__ (self, type, match, format, line_number):
876 self.option_dict = {}
878 self.line_number = line_number
880 def replacement_text (self):
881 return self.match.group ('match')
883 def substring (self, s):
884 return self.match.group (s)
887 return `self.__class__` + ' type = ' + self.type
889 class Include_snippet (Snippet):
890 def processed_filename (self):
891 f = self.substring ('filename')
892 return os.path.splitext (f)[0] + format2ext[global_options.format]
894 def replacement_text (self):
895 s = self.match.group ('match')
896 f = self.substring ('filename')
898 return re.sub (f, self.processed_filename (), s)
900 class Lilypond_snippet (Snippet):
901 def __init__ (self, type, match, format, line_number):
902 Snippet.__init__ (self, type, match, format, line_number)
903 os = match.group ('options')
904 self.do_options (os, self.type)
907 return self.substring ('code')
910 contents = self.substring ('code')
911 return ('\\sourcefileline %d\n%s'
912 % (self.line_number - 1, contents))
917 return self.compose_ly (s)
920 def do_options (self, option_string, type):
921 self.option_dict = {}
923 options = split_options (option_string)
927 (key, value) = re.split ('\s*=\s*', i)
928 self.option_dict[key] = value
930 if i in no_options.keys ():
931 if no_options[i] in self.option_dict.keys ():
932 del self.option_dict[no_options[i]]
934 self.option_dict[i] = None
936 has_line_width = self.option_dict.has_key (LINE_WIDTH)
937 no_line_width_value = 0
939 # If LINE_WIDTH is used without parameter, set it to default.
940 if has_line_width and self.option_dict[LINE_WIDTH] == None:
941 no_line_width_value = 1
942 del self.option_dict[LINE_WIDTH]
944 for i in default_ly_options.keys ():
945 if i not in self.option_dict.keys ():
946 self.option_dict[i] = default_ly_options[i]
948 if not has_line_width:
949 if type == 'lilypond' or FRAGMENT in self.option_dict.keys ():
950 self.option_dict[RAGGED_RIGHT] = None
952 if type == 'lilypond':
953 if LINE_WIDTH in self.option_dict.keys ():
954 del self.option_dict[LINE_WIDTH]
956 if RAGGED_RIGHT in self.option_dict.keys ():
957 if LINE_WIDTH in self.option_dict.keys ():
958 del self.option_dict[LINE_WIDTH]
960 if QUOTE in self.option_dict.keys () or type == 'lilypond':
961 if LINE_WIDTH in self.option_dict.keys ():
962 del self.option_dict[LINE_WIDTH]
964 if not INDENT in self.option_dict.keys ():
965 self.option_dict[INDENT] = '0\\mm'
967 # The QUOTE pattern from ly_options only emits the `line-width'
969 if has_line_width and QUOTE in self.option_dict.keys ():
970 if no_line_width_value:
971 del self.option_dict[LINE_WIDTH]
973 del self.option_dict[QUOTE]
975 def compose_ly (self, code):
976 if FRAGMENT in self.option_dict.keys ():
984 # The original concept of the `exampleindent' option is broken.
985 # It is not possible to get a sane value for @exampleindent at all
986 # without processing the document itself. Saying
994 # causes ugly results with the DVI backend of texinfo since the
995 # default value for @exampleindent isn't 5em but 0.4in (or a smaller
996 # value). Executing the above code changes the environment
997 # indentation to an unknown value because we don't know the amount
998 # of 1em in advance since it is font-dependent. Modifying
999 # @exampleindent in the middle of a document is simply not
1000 # supported within texinfo.
1002 # As a consequence, the only function of @exampleindent is now to
1003 # specify the amount of indentation for the `quote' option.
1005 # To set @exampleindent locally to zero, we use the @format
1006 # environment for non-quoted snippets.
1007 override[EXAMPLEINDENT] = r'0.4\in'
1008 override[LINE_WIDTH] = texinfo_line_widths['@smallbook']
1009 override.update (default_ly_options)
1012 for (key, value) in self.option_dict.items ():
1014 option_list.append (key)
1016 option_list.append (key + '=' + value)
1017 option_string = ','.join (option_list)
1020 compose_types = [NOTES, PREAMBLE, LAYOUT, PAPER]
1021 for a in compose_types:
1022 compose_dict[a] = []
1024 for (key, value) in self.option_dict.items ():
1025 (c_key, c_value) = \
1026 classic_lilypond_book_compatibility (key, value)
1030 (_ ("deprecated ly-option used: %s=%s" \
1033 (_ ("compatibility mode translation: %s=%s" \
1034 % (c_key, c_value)))
1037 (_ ("deprecated ly-option used: %s" \
1040 (_ ("compatibility mode translation: %s" \
1043 (key, value) = (c_key, c_value)
1046 override[key] = value
1048 if not override.has_key (key):
1049 override[key] = None
1052 for type in compose_types:
1053 if ly_options[type].has_key (key):
1054 compose_dict[type].append (ly_options[type][key])
1058 if not found and key not in simple_options:
1059 warning (_ ("ignoring unknown ly option: %s") % key)
1062 if RELATIVE in override.keys () and override[RELATIVE]:
1063 relative = int (override[RELATIVE])
1065 relative_quotes = ''
1069 relative_quotes += ',' * (- relative)
1071 relative_quotes += "'" * relative
1073 paper_string = '\n '.join (compose_dict[PAPER]) % override
1074 layout_string = '\n '.join (compose_dict[LAYOUT]) % override
1075 notes_string = '\n '.join (compose_dict[NOTES]) % vars ()
1076 preamble_string = '\n '.join (compose_dict[PREAMBLE]) % override
1077 padding_mm = global_options.padding_mm
1078 font_dump_setting = ''
1079 if FONTLOAD in self.option_dict:
1080 font_dump_setting = '#(define-public force-eps-font-include #t)\n'
1082 d = globals().copy()
1084 return (PREAMBLE_LY + body) % d
1086 def get_hash (self):
1088 hash = md5.md5 (self.relevant_contents (self.full_ly ()))
1090 ## let's not create too long names.
1091 self.hash = hash.hexdigest ()[:10]
1095 def basename (self):
1096 if FILENAME in self.option_dict:
1097 return self.option_dict[FILENAME]
1098 if global_options.use_hash:
1099 return 'lily-%s' % self.get_hash ()
1102 def write_ly (self):
1103 outf = open (self.basename () + '.ly', 'w')
1104 outf.write (self.full_ly ())
1105 open (self.basename () + '.txt', 'w').write ('image of music')
1107 def relevant_contents (self, ly):
1108 return re.sub (r'\\(version|sourcefileline|sourcefilename)[^\n]*\n', '', ly)
1110 def ly_is_outdated (self):
1111 base = self.basename ()
1112 ly_file = find_file (base + '.ly', raise_error=False)
1113 tex_file = find_file (base + '.tex', raise_error=False)
1114 systems_file = find_file (base + '-systems.tex', raise_error=False)
1116 if (os.path.exists (ly_file)
1117 and os.path.exists (systems_file)
1118 and os.stat (systems_file)[stat.ST_SIZE]
1119 and re.match ('% eof', open (systems_file).readlines ()[-1])
1120 and (global_options.use_hash or FILENAME in self.option_dict)
1121 and (self.relevant_contents (self.full_ly ())
1122 == self.relevant_contents (open (ly_file).read ()))):
1127 def png_is_outdated (self):
1128 base = self.basename ()
1129 eps_file = find_file (base + '.eps', raise_error=False)
1130 png_file = find_file (base + '.png', raise_error=False)
1131 if not self.ly_is_outdated () and global_options.format in (HTML, TEXINFO):
1132 if os.path.exists (eps_file):
1133 page_count = ps_page_count (eps_file)
1135 return not os.path.exists (png_file)
1137 return not reduce (operator.or_,
1138 [find_file (base + '-page%d.png' % a, raise_error=False)
1139 for a in range (1, page_count + 1)])
1142 def texstr_is_outdated (self):
1146 base = self.basename ()
1147 return not (self.ly_is_outdated ()
1148 and find_file (base + '.texstr', raise_error=False))
1150 def filter_text (self):
1151 code = self.substring ('code')
1152 s = run_filter (code)
1155 'options': self.match.group ('options')
1158 return output[self.format][FILTER] % d
1160 def replacement_text (self):
1161 func = Lilypond_snippet.__dict__['output_' + self.format]
1164 def get_images (self):
1165 base = self.basename ()
1167 single = '%(base)s.png' % vars ()
1168 multiple = '%(base)s-page1.png' % vars ()
1170 if os.path.exists (multiple) \
1171 and (not os.path.exists (single) \
1172 or (os.stat (multiple)[stat.ST_MTIME] \
1173 > os.stat (single)[stat.ST_MTIME])):
1174 count = ps_page_count ('%(base)s.eps' % vars ())
1175 images = ['%s-page%d.png' % (base, a) for a in range (1, count+1)]
1176 images = tuple (images)
1179 def output_docbook (self):
1181 base = self.basename ()
1182 for image in self.get_images ():
1183 (base, ext) = os.path.splitext (image)
1184 str += output[DOCBOOK][OUTPUT] % vars ()
1185 str += self.output_print_filename (DOCBOOK)
1186 if (self.substring('inline') == 'inline'):
1187 str = '<inlinemediaobject>' + str + '</inlinemediaobject>'
1189 str = '<mediaobject>' + str + '</mediaobject>'
1190 if VERBATIM in self.option_dict:
1191 verb = verbatim_html (self.verb_ly ())
1192 str = output[DOCBOOK][VERBATIM] % vars () + str
1195 def output_html (self):
1197 base = self.basename ()
1198 if global_options.format == HTML:
1199 str += self.output_print_filename (HTML)
1200 if VERBATIM in self.option_dict:
1201 verb = verbatim_html (self.verb_ly ())
1202 str += output[HTML][VERBATIM] % vars ()
1203 if QUOTE in self.option_dict:
1204 str = output[HTML][QUOTE] % vars ()
1206 str += output[HTML][BEFORE] % vars ()
1207 for image in self.get_images ():
1208 (base, ext) = os.path.splitext (image)
1209 alt = self.option_dict[ALT]
1210 str += output[HTML][OUTPUT] % vars ()
1211 str += output[HTML][AFTER] % vars ()
1214 def output_info (self):
1216 for image in self.get_images ():
1217 (base, ext) = os.path.splitext (image)
1219 # URG, makeinfo implicitly prepends dot to extension.
1220 # Specifying no extension is most robust.
1222 alt = self.option_dict[ALT]
1223 info_image_path = os.path.join (global_options.info_images_dir, base)
1224 str += output[TEXINFO][OUTPUTIMAGE] % vars ()
1226 base = self.basename ()
1227 str += output[global_options.format][OUTPUT] % vars ()
1230 def output_latex (self):
1232 base = self.basename ()
1233 if global_options.format == LATEX:
1234 str += self.output_print_filename (LATEX)
1235 if VERBATIM in self.option_dict:
1236 verb = self.verb_ly ()
1237 str += (output[LATEX][VERBATIM] % vars ())
1239 str += (output[LATEX][OUTPUT] % vars ())
1241 ## todo: maintain breaks
1243 breaks = self.ly ().count ("\n")
1244 str += "".ljust (breaks, "\n").replace ("\n","%\n")
1246 if QUOTE in self.option_dict:
1247 str = output[LATEX][QUOTE] % vars ()
1250 def output_print_filename (self, format):
1252 if PRINTFILENAME in self.option_dict:
1253 base = self.basename ()
1254 filename = os.path.basename (self.substring ('filename'))
1255 str = output[format][PRINTFILENAME] % vars ()
1259 def output_texinfo (self):
1260 str = self.output_print_filename (TEXINFO)
1261 base = self.basename ()
1262 if TEXIDOC in self.option_dict:
1263 texidoc = base + '.texidoc'
1264 if os.path.exists (texidoc):
1265 str += '@include %(texidoc)s\n\n' % vars ()
1268 if VERBATIM in self.option_dict:
1270 if ADDVERSION in self.option_dict:
1271 version = output[TEXINFO][ADDVERSION]
1272 verb = self.verb_ly ()
1273 substr = output[TEXINFO][VERBATIM] % vars ()
1274 substr += self.output_info ()
1275 if LILYQUOTE in self.option_dict:
1276 substr = output[TEXINFO][QUOTE] % {'str':substr}
1279 # str += ('@ifinfo\n' + self.output_info () + '\n@end ifinfo\n')
1280 # str += ('@tex\n' + self.output_latex () + '\n@end tex\n')
1281 # str += ('@html\n' + self.output_html () + '\n@end html\n')
1283 if QUOTE in self.option_dict:
1284 str = output[TEXINFO][QUOTE] % vars ()
1286 # need par after image
1291 re_begin_verbatim = re.compile (r'\s+%.*?begin verbatim.*\n*', re.M)
1292 re_end_verbatim = re.compile (r'\s+%.*?end verbatim.*$', re.M)
1294 class Lilypond_file_snippet (Lilypond_snippet):
1295 def __init__ (self, type, match, format, line_number):
1296 Lilypond_snippet.__init__ (self, type, match, format, line_number)
1297 self.contents = open (find_file (self.substring ('filename'))).read ()
1301 s = re_begin_verbatim.split (s)[-1]
1302 s = re_end_verbatim.split (s)[0]
1306 name = self.substring ('filename')
1307 return ('\\sourcefilename \"%s\"\n\\sourcefileline 0\n%s'
1308 % (name, self.contents))
1311 snippet_type_to_class = {
1312 'lilypond_file': Lilypond_file_snippet,
1313 'lilypond_block': Lilypond_snippet,
1314 'lilypond': Lilypond_snippet,
1315 'include': Include_snippet,
1318 def find_linestarts (s):
1323 i = s.find ('\n', start)
1331 nls.append (len (s))
1334 def find_toplevel_snippets (s, types):
1337 res[i] = ly.re.compile (snippet_res[global_options.format][i])
1341 found = dict ([(t, None) for t in types])
1343 line_starts = find_linestarts (s)
1345 # We want to search for multiple regexes, without searching
1346 # the string multiple times for one regex.
1347 # Hence, we use earlier results to limit the string portion
1349 # Since every part of the string is traversed at most once for
1350 # every type of snippet, this is linear.
1356 if not found[type] or found[type][0] < index:
1359 m = res[type].search (s[index:endex])
1364 if snippet_type_to_class.has_key (type):
1365 cl = snippet_type_to_class[type]
1368 start = index + m.start ('match')
1369 line_number = line_start_idx
1370 while (line_starts[line_number] < start):
1374 snip = cl (type, m, global_options.format, line_number)
1376 found[type] = (start, snip)
1380 or found[type][0] < found[first][0]):
1385 # Limiting the search space is a cute
1386 # idea, but this *requires* to search
1387 # for possible containing blocks
1388 # first, at least as long as we do not
1389 # search for the start of blocks, but
1390 # always/directly for the entire
1391 # @block ... @end block.
1393 endex = found[first][0]
1396 snippets.append (Substring (s, index, len (s), line_start_idx))
1399 while (start > line_starts[line_start_idx+1]):
1402 (start, snip) = found[first]
1403 snippets.append (Substring (s, index, start, line_start_idx + 1))
1404 snippets.append (snip)
1406 index = start + len (snip.match.group ('match'))
1410 def filter_pipe (input, cmd):
1411 if global_options.verbose:
1412 progress (_ ("Opening filter `%s'") % cmd)
1414 (stdin, stdout, stderr) = os.popen3 (cmd)
1416 status = stdin.close ()
1420 output = stdout.read ()
1421 status = stdout.close ()
1422 error = stderr.read ()
1426 signal = 0x0f & status
1427 if status or (not output and error):
1428 exit_status = status >> 8
1429 error (_ ("`%s' failed (%d)") % (cmd, exit_status))
1430 error (_ ("The error log is as follows:"))
1431 ly.stderr_write (error)
1432 ly.stderr_write (stderr.read ())
1435 if global_options.verbose:
1441 return filter_pipe (s, global_options.filter_cmd)
1443 def is_derived_class (cl, baseclass):
1446 for b in cl.__bases__:
1447 if is_derived_class (b, baseclass):
1451 def process_snippets (cmd, ly_snippets, texstr_snippets, png_snippets):
1452 ly_names = filter (lambda x: x,
1453 map (Lilypond_snippet.basename, ly_snippets))
1454 texstr_names = filter (lambda x: x,
1455 map (Lilypond_snippet.basename, texstr_snippets))
1457 png_names = filter (lambda x: x,
1458 map (Lilypond_snippet.basename, png_snippets))
1461 def my_system (cmd):
1462 status = ly.system (cmd,
1463 be_verbose=global_options.verbose,
1466 if global_options.format in (HTML, TEXINFO) and '--formats' not in cmd:
1467 cmd += ' --formats=png '
1468 elif global_options.format in (DOCBOOK) and '--formats' not in cmd:
1469 cmd += ' --formats=png,pdf '
1473 # the --process=CMD switch is a bad idea
1474 # it is too generic for lilypond-book.
1476 my_system (' '.join ([cmd, '--backend texstr',
1477 'snippet-map.ly'] + texstr_names))
1478 for l in texstr_names:
1479 my_system ('latex %s.texstr' % l)
1482 open ('snippet-names', 'wb').write ('\n'.join (['snippet-map.ly']
1485 my_system (' '.join ([cmd, 'snippet-names']))
1488 LATEX_INSPECTION_DOCUMENT = r'''
1492 \typeout{textwidth=\the\textwidth}
1493 \typeout{columnsep=\the\columnsep}
1494 \makeatletter\if@twocolumn\typeout{columns=2}\fi\makeatother
1498 # Do we need anything else besides `textwidth'?
1499 def get_latex_textwidth (source):
1500 m = re.search (r'''(?P<preamble>\\begin\s*{document})''', source)
1502 warning (_ ("cannot find \\begin{document} in LaTeX document"))
1504 ## what's a sensible default?
1507 preamble = source[:m.start (0)]
1508 latex_document = LATEX_INSPECTION_DOCUMENT % vars ()
1510 (handle, tmpfile) = tempfile.mkstemp('.tex')
1511 logfile = os.path.splitext (tmpfile)[0] + '.log'
1512 logfile = os.path.split (logfile)[1]
1514 tmp_handle = os.fdopen (handle,'w')
1515 tmp_handle.write (latex_document)
1518 ly.system ('latex %s' % tmpfile, be_verbose=global_options.verbose)
1519 parameter_string = open (logfile).read()
1525 m = re.search ('columns=([0-9.]*)', parameter_string)
1527 columns = int (m.group (1))
1530 m = re.search ('columnsep=([0-9.]*)pt', parameter_string)
1532 columnsep = float (m.group (1))
1535 m = re.search ('textwidth=([0-9.]*)pt', parameter_string)
1537 textwidth = float (m.group (1))
1539 textwidth = (textwidth - columnsep) / columns
1543 def modify_preamble (chunk):
1544 str = chunk.replacement_text ()
1545 if (re.search (r"\\begin *{document}", str)
1546 and not re.search ("{graphic[sx]", str)):
1547 str = re.sub (r"\\begin{document}",
1548 r"\\usepackage{graphics}" + '\n'
1549 + r"\\begin{document}",
1551 chunk.override_text = str
1563 '.texinfo': TEXINFO,
1570 # TEXINFO: '.texinfo',
1576 class Compile_error:
1579 def write_file_map (lys, name):
1580 snippet_map = open ('snippet-map.ly', 'w')
1581 snippet_map.write ("""
1582 #(define version-seen #t)
1583 #(define output-empty-score-list #f)
1584 #(ly:add-file-name-alist '(
1587 snippet_map.write ('("%s.ly" . "%s")\n'
1591 snippet_map.write ('))\n')
1593 def do_process_cmd (chunks, input_name):
1594 all_lys = filter (lambda x: is_derived_class (x.__class__,
1598 write_file_map (all_lys, input_name)
1599 ly_outdated = filter (lambda x: is_derived_class (x.__class__,
1601 and x.ly_is_outdated (), chunks)
1602 texstr_outdated = filter (lambda x: is_derived_class (x.__class__,
1604 and x.texstr_is_outdated (),
1606 png_outdated = filter (lambda x: is_derived_class (x.__class__,
1608 and x.png_is_outdated (),
1611 outdated = png_outdated + texstr_outdated + ly_outdated
1613 progress (_ ("Writing snippets..."))
1614 map (Lilypond_snippet.write_ly, ly_outdated)
1618 progress (_ ("Processing..."))
1620 process_snippets (global_options.process_cmd, ly_outdated, texstr_outdated, png_outdated)
1622 progress (_ ("All snippets are up to date..."))
1625 def guess_format (input_filename):
1627 e = os.path.splitext (input_filename)[1]
1628 if e in ext2format.keys ():
1630 format = ext2format[e]
1632 error (_ ("cannot determine format for: %s" \
1637 def write_if_updated (file_name, lines):
1639 f = open (file_name)
1641 new_str = ''.join (lines)
1642 if oldstr == new_str:
1643 progress (_ ("%s is up to date.") % file_name)
1646 # this prevents make from always rerunning lilypond-book:
1647 # output file must be touched in order to be up to date
1648 os.utime (file_name, None)
1652 progress (_ ("Writing `%s'...") % file_name)
1653 open (file_name, 'w').writelines (lines)
1656 def note_input_file (name, inputs=[]):
1657 ## hack: inputs is mutable!
1658 inputs.append (name)
1661 def samefile (f1, f2):
1663 return os.path.samefile (f1, f2)
1664 except AttributeError: # Windoze
1665 f1 = re.sub ("//*", "/", f1)
1666 f2 = re.sub ("//*", "/", f2)
1669 def do_file (input_filename):
1671 if not input_filename or input_filename == '-':
1672 in_handle = sys.stdin
1673 input_fullname = '<stdin>'
1675 if os.path.exists (input_filename):
1676 input_fullname = input_filename
1677 elif global_options.format == LATEX and ly.search_exe_path ('kpsewhich'):
1678 input_fullname = os.popen ('kpsewhich ' + input_filename).read()[:-1]
1680 input_fullname = find_file (input_filename)
1682 note_input_file (input_fullname)
1683 in_handle = open (input_fullname)
1685 if input_filename == '-':
1686 input_base = 'stdin'
1688 input_base = os.path.basename \
1689 (os.path.splitext (input_filename)[0])
1691 # Only default to stdout when filtering.
1692 if global_options.output_name == '-' or (not global_options.output_name and global_options.filter_cmd):
1693 output_filename = '-'
1694 output_file = sys.stdout
1696 # don't complain when global_options.output_name is existing
1697 output_filename = input_base + format2ext[global_options.format]
1698 if global_options.output_name:
1699 if not os.path.isdir (global_options.output_name):
1700 os.mkdir (global_options.output_name, 0777)
1701 os.chdir (global_options.output_name)
1703 if (os.path.exists (input_filename)
1704 and os.path.exists (output_filename)
1705 and samefile (output_filename, input_fullname)):
1707 _ ("Output would overwrite input file; use --output."))
1711 progress (_ ("Reading %s...") % input_fullname)
1712 source = in_handle.read ()
1715 set_default_options (source)
1718 # FIXME: Containing blocks must be first, see
1719 # find_toplevel_snippets.
1721 'multiline_comment',
1725 'singleline_comment',
1730 progress (_ ("Dissecting..."))
1731 chunks = find_toplevel_snippets (source, snippet_types)
1733 if global_options.format == LATEX:
1735 if (c.is_plain () and
1736 re.search (r"\\begin *{document}", c.replacement_text())):
1741 if global_options.filter_cmd:
1742 write_if_updated (output_filename,
1743 [c.filter_text () for c in chunks])
1744 elif global_options.process_cmd:
1745 do_process_cmd (chunks, input_fullname)
1746 progress (_ ("Compiling %s...") % output_filename)
1748 write_if_updated (output_filename,
1749 [s.replacement_text ()
1752 def process_include (snippet):
1753 os.chdir (original_dir)
1754 name = snippet.substring ('filename')
1755 progress (_ ("Processing include: %s") % name)
1757 return do_file (name)
1759 include_chunks = map (process_include,
1760 filter (lambda x: is_derived_class (x.__class__,
1765 return chunks + reduce (lambda x,y: x + y, include_chunks, [])
1767 except Compile_error:
1768 os.chdir (original_dir)
1769 progress (_ ("Removing `%s'") % output_filename)
1775 global global_options
1777 opt_parser = get_option_parser()
1778 (global_options, args) = opt_parser.parse_args ()
1780 if global_options.format in ('texi-html', 'texi'):
1781 global_options.format = TEXINFO
1782 global_options.use_hash = True
1784 global_options.include_path = map (os.path.abspath, global_options.include_path)
1786 if global_options.warranty:
1789 if not args or len (args) > 1:
1790 opt_parser.print_help ()
1795 def psfonts_warning (options, basename):
1796 if options.format in (TEXINFO, LATEX):
1797 psfonts_file = os.path.join (options.output_name, basename + '.psfonts')
1798 output = os.path.join (options.output_name, basename + '.dvi' )
1800 if not options.create_pdf:
1801 if not options.psfonts:
1802 warning (_ ("option --psfonts not used"))
1803 warning (_ ("processing with dvips will have no fonts"))
1806 progress (_ ("DVIPS usage:"))
1808 progress (" dvips -h %(psfonts_file)s %(output)s" % vars ())
1812 # FIXME: 85 lines of `main' macramee??
1813 files = do_options ()
1817 basename = os.path.splitext (file)[0]
1818 basename = os.path.split (basename)[1]
1820 if not global_options.format:
1821 global_options.format = guess_format (files[0])
1824 if global_options.format in (TEXINFO, HTML, DOCBOOK):
1828 if global_options.process_cmd == '':
1829 global_options.process_cmd = (lilypond_binary
1830 + ' --formats=%s --backend eps ' % formats)
1832 if global_options.process_cmd:
1833 global_options.process_cmd += ' '.join ([(' -I %s' % ly.mkarg (p))
1834 for p in global_options.include_path])
1836 if global_options.format in (TEXINFO, LATEX):
1837 ## prevent PDF from being switched on by default.
1838 global_options.process_cmd += ' --formats=eps '
1839 if global_options.create_pdf:
1840 global_options.process_cmd += "--pdf -dinclude-eps-fonts -dgs-load-fonts "
1842 if global_options.verbose:
1843 global_options.process_cmd += " --verbose "
1845 if global_options.padding_mm:
1846 global_options.process_cmd += " -deps-box-padding=%f " % global_options.padding_mm
1848 global_options.process_cmd += " -dread-file-list "
1853 chunks = do_file (file)
1854 if global_options.psfonts:
1855 fontextract.verbose = global_options.verbose
1856 snippet_chunks = filter (lambda x: is_derived_class (x.__class__,
1860 psfonts_file = basename + '.psfonts'
1861 if not global_options.verbose:
1862 progress (_ ("Writing fonts to %s...") % psfonts_file)
1863 fontextract.extract_fonts (psfonts_file,
1864 [x.basename() + '.eps'
1865 for x in snippet_chunks])
1866 if not global_options.verbose:
1869 except Compile_error:
1872 psfonts_warning (global_options, basename)
1874 inputs = note_input_file ('')
1877 base_file_name = os.path.splitext (os.path.basename (file))[0]
1878 dep_file = os.path.join (global_options.output_name, base_file_name + '.dep')
1879 final_output_file = os.path.join (global_options.output_name,
1881 + '.%s' % global_options.format)
1883 os.chdir (original_dir)
1884 open (dep_file, 'w').write ('%s: %s' % (final_output_file, ' '.join (inputs)))
1886 if __name__ == '__main__':