]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/lilypond-book.py
Add lilypond-book ly verbatim gettext quirk
[lilypond.git] / scripts / lilypond-book.py
index 6d1a949c39ad51a6a19ea032664bdcf201c500c4..b4bd319a690d3b35aa27b969a7d0b4edd2d0435a 100644 (file)
@@ -126,7 +126,7 @@ def warranty ():
 
 %s
 %s
-''' % ( _ ('Copyright (c) %s by') % '2001--2009',
+''' % ( _ ('Copyright (c) %s by') % '2001--2010',
         '\n  '.join (authors),
         _ ("Distributed under terms of the GNU General Public License."),
         _ ("It comes with NO WARRANTY.")))
@@ -164,7 +164,8 @@ def get_option_parser ():
                   default='')
 
     p.add_option ('--latex-program',
-                  help=_ ("run executable PROG instead of latex"),
+                  help=_ ("run executable PROG instead of latex, or in\n\
+case --pdf option is set instead of pdflatex"),
                   metavar=_ ("PROG"),
                   action='store', dest='latex_program',
                   default='latex')
@@ -176,11 +177,28 @@ def get_option_parser ():
                   type="float",
                   default=3.0)
 
+    p.add_option ('--lily-output-dir',
+                  help=_ ("write lily-XXX files to DIR, link into --output dir"),
+                  metavar=_ ("DIR"),
+                  action='store', dest='lily_output_dir',
+                  default=None)
+
     p.add_option ("-o", '--output', help=_ ("write output to DIR"),
                   metavar=_ ("DIR"),
                   action='store', dest='output_dir',
                   default='')
 
+    p.add_option ('--pdf',
+                  action="store_true",
+                  dest="create_pdf",
+                  help=_ ("create PDF files for use with PDFTeX"),
+                  default=False)
+
+    p.add_option ('-P', '--process', metavar=_ ("COMMAND"),
+                  help = _ ("process ly_files using COMMAND FILE..."),
+                  action='store',
+                  dest='process_cmd', default='')
+
     p.add_option ('--skip-lily-check',
                   help=_ ("do not fail if no lilypond output is found"),
                   metavar=_ ("DIR"),
@@ -193,21 +211,9 @@ def get_option_parser ():
                   action='store_true', dest='skip_png_check',
                   default=False)
 
-    p.add_option ('--lily-output-dir',
-                  help=_ ("write lily-XXX files to DIR, link into --output dir"),
-                  metavar=_ ("DIR"),
-                  action='store', dest='lily_output_dir',
-                  default=None)
-
-    p.add_option ('-P', '--process', metavar=_ ("COMMAND"),
-                  help = _ ("process ly_files using COMMAND FILE..."),
-                  action='store',
-                  dest='process_cmd', default='')
-
-    p.add_option ('--pdf',
-                  action="store_true",
-                  dest="create_pdf",
-                  help=_ ("create PDF files for use with PDFTeX"),
+    p.add_option ('--use-source-file-names',
+                  help=_ ("write snippet output files with the same base name as their source file"),
+                  action='store_true', dest='use_source_file_names',
                   default=False)
 
     p.add_option ('-V', '--verbose', help=_ ("be verbose"),
@@ -384,9 +390,9 @@ snippet_res = {
          r'''(?mx)
           (?P<match>
           <lilypond
-           (\s*(?P<options>.*?)\s*:)?\s*
+           (\s+(?P<options>.*?))?\s*:\s*
            (?P<code>.*?)
-          />)''',
+          \s*/>)''',
 
         'lilypond_block':
          r'''(?msx)
@@ -395,7 +401,7 @@ snippet_res = {
            \s*(?P<options>.*?)\s*
           >
           (?P<code>.*?)
-          </lilypond>)''',
+          </lilypond\s*>)''',
 
         'lilypond_file':
          r'''(?mx)
@@ -404,7 +410,7 @@ snippet_res = {
            \s*(?P<options>.*?)\s*
           >
           \s*(?P<filename>.*?)\s*
-          </lilypondfile>)''',
+          </lilypondfile\s*>)''',
 
         'multiline_comment':
          r'''(?smx)
@@ -954,7 +960,7 @@ def verb_ly_gettext (s):
 
     if langdefs.LANGDICT[document_language].enable_ly_identifier_l10n:
         for v in ly_var_def_re.findall (s):
-            s = re.sub (r"(?m)(^|[' \\#])%s([^a-zA-Z])" % v,
+            s = re.sub (r"(?m)(?<!\\clef)(^|[' \\#])%s([^a-zA-Z])" % v,
                         "\\1" + t (v) + "\\2",
                         s)
         for id in ly_context_id_re.findall (s):
@@ -1199,10 +1205,11 @@ left-margin-default right-margin-default)"
         option_list = []
         for option in self.get_option_list ():
             for name in PROCESSING_INDEPENDENT_OPTIONS:
-                if not option.startswith (name):
-                    option_list.append (option)
+                if option.startswith (name):
+                    break
+            else:
+                option_list.append (option)
         option_string = ','.join (option_list)
-
         compose_dict = {}
         compose_types = [NOTES, PREAMBLE, LAYOUT, PAPER]
         for a in compose_types:
@@ -1279,8 +1286,10 @@ left-margin-default right-margin-default)"
             hash = md5 (self.relevant_contents (self.ly ()))
             for option in self.get_option_list ():
                 for name in PROCESSING_INDEPENDENT_OPTIONS:
-                    if not option.startswith (name):
-                        hash.update (option)
+                    if option.startswith (name):
+                        break
+                else:
+                    hash.update (option)
 
             ## let's not create too long names.
             self.checksum = hash.hexdigest ()[:10]
@@ -1292,6 +1301,8 @@ left-margin-default right-margin-default)"
         name = '%s/lily-%s' % (cs[:2], cs[2:])
         return name
 
+    final_basename = basename
+
     def write_ly (self):
         base = self.basename ()
         path = os.path.join (global_options.lily_output_dir, base)
@@ -1301,7 +1312,7 @@ left-margin-default right-margin-default)"
         filename = path + '.ly'
         if os.path.exists (filename):
             diff_against_existing = filter_pipe (self.full_ly (), 'diff -u %s -' % filename)
-            if not diff_against_existing.startswith ('\n'):
+            if diff_against_existing:
                 warning ("%s: duplicate filename but different contents of orginal file,\n\
 printing diff against existing file." % filename)
                 ly.stderr_write (diff_against_existing)
@@ -1319,13 +1330,25 @@ printing diff against existing file." % filename)
             print '\nMissing', missing
             raise CompileError(self.basename())
         for name in existing:
+            if (global_options.use_source_file_names
+                and isinstance (self, LilypondFileSnippet)):
+                base, ext = os.path.splitext (name)
+                components = base.split ('-')
+                # ugh, assume filenames with prefix with one dash (lily-xxxx)
+                if len (components) > 2:
+                    base_suffix = '-' + components[-1]
+                else:
+                    base_suffix = ''
+                final_name = self.final_basename () + base_suffix + ext
+            else:
+                final_name = name
             try:
-                os.unlink (os.path.join (destination, name))
+                os.unlink (os.path.join (destination, final_name))
             except OSError:
                 pass
 
             src = os.path.join (output_dir, name)
-            dst = os.path.join (destination, name)
+            dst = os.path.join (destination, final_name)
             dst_path = os.path.split(dst)[0]
             if not os.path.isdir (dst_path):
                 os.makedirs (dst_path)
@@ -1426,7 +1449,7 @@ printing diff against existing file." % filename)
         return func (self)
 
     def get_images (self):
-        base = self.basename ()
+        base = self.final_basename ()
 
         single = '%(base)s.png' % vars ()
         multiple = '%(base)s-page1.png' % vars ()
@@ -1443,7 +1466,7 @@ printing diff against existing file." % filename)
 
     def output_docbook (self):
         str = ''
-        base = self.basename ()
+        base = self.final_basename ()
         for image in self.get_images ():
             (base, ext) = os.path.splitext (image)
             str += output[DOCBOOK][OUTPUT] % vars ()
@@ -1459,7 +1482,7 @@ printing diff against existing file." % filename)
 
     def output_html (self):
         str = ''
-        base = self.basename ()
+        base = self.final_basename ()
         if self.format == HTML:
             str += self.output_print_filename (HTML)
             if VERBATIM in self.option_dict:
@@ -1488,13 +1511,13 @@ printing diff against existing file." % filename)
             info_image_path = os.path.join (global_options.info_images_dir, base)
             str += output[TEXINFO][OUTPUTIMAGE] % vars ()
 
-        base = self.basename ()
+        base = self.final_basename ()
         str += output[self.format][OUTPUT] % vars ()
         return str
 
     def output_latex (self):
         str = ''
-        base = self.basename ()
+        base = self.final_basename ()
         if self.format == LATEX:
             str += self.output_print_filename (LATEX)
             if VERBATIM in self.option_dict:
@@ -1515,7 +1538,7 @@ printing diff against existing file." % filename)
     def output_print_filename (self, format):
         str = ''
         if PRINTFILENAME in self.option_dict:
-            base = self.basename ()
+            base = self.final_basename ()
             filename = os.path.basename (self.substring ('filename'))
             str = output[format][PRINTFILENAME] % vars ()
 
@@ -1523,7 +1546,7 @@ printing diff against existing file." % filename)
 
     def output_texinfo (self):
         str = self.output_print_filename (TEXINFO)
-        base = self.basename ()
+        base = self.final_basename ()
         if DOCTITLE in self.option_dict:
             doctitle = base + '.doctitle'
             translated_doctitle = doctitle + document_language
@@ -1586,6 +1609,13 @@ class LilypondFileSnippet (LilypondSnippet):
         return ('\\sourcefilename \"%s\"\n\\sourcefileline 0\n%s'
                 % (name, self.contents))
 
+    def final_basename (self):
+        if global_options.use_source_file_names:
+            base = os.path.splitext (os.path.basename (self.substring ('filename')))[0]
+            return base
+        else:
+            return self.basename ()
+
 
 class LilyPondVersionString (Snippet):
     """A string that does not require extra memory."""
@@ -2124,6 +2154,8 @@ def main ():
         global_options.process_cmd += ' --formats=eps '
         if global_options.create_pdf:
             global_options.process_cmd += "--pdf -dinclude-eps-fonts -dgs-load-fonts "
+            if global_options.latex_program == 'latex':
+                global_options.latex_program = 'pdflatex'
 
     if global_options.verbose:
         global_options.process_cmd += " --verbose "