]> git.donarmstrong.com Git - lilypond.git/blobdiff - python/book_snippets.py
Lilypond-book: Improve comments
[lilypond.git] / python / book_snippets.py
index b4849a0dbba79c2953c543a5dbe66138677ad824..cbf09a7a28f59bcf47cb8103883c2d511bedc510 100644 (file)
@@ -38,14 +38,11 @@ EXAMPLEINDENT = 'exampleindent'
 FILENAME = 'filename'
 FILTER = 'filter'
 FRAGMENT = 'fragment'
-LANG = 'lang'    ## TODO: This is handled nowhere!
 LAYOUT = 'layout'
-LILYQUOTE = 'lilyquote'
 LINE_WIDTH = 'line-width'
 NOFRAGMENT = 'nofragment'
 NOGETTEXT = 'nogettext'
 NOINDENT = 'noindent'
-NOQUOTE = 'noquote'
 INDENT = 'indent'
 NORAGGED_RIGHT = 'noragged-right'
 NOTES = 'body'
@@ -68,7 +65,6 @@ VERSION = 'lilypondversion'
 
 # NOTIME and NOGETTEXT have no opposite so they aren't part of this
 # dictionary.
-# NOQUOTE is used internally only.
 no_options = {
     NOFRAGMENT: FRAGMENT,
     NOINDENT: INDENT,
@@ -92,7 +88,6 @@ simple_options = [
     PRINTFILENAME,
     DOCTITLE,
     TEXIDOC,
-    LANG,
     VERBATIM,
     FILENAME,
     ALT,
@@ -117,7 +112,6 @@ snippet_options = {
         INDENT: r'''indent = %(indent)s''',
         LINE_WIDTH: r'''line-width = %(line-width)s''',
         QUOTE: r'''line-width = %(line-width)s - 2.0 * %(exampleindent)s''',
-        LILYQUOTE: r'''line-width = %(line-width)s - 2.0 * %(exampleindent)s''',
         RAGGED_RIGHT: r'''ragged-right = ##t''',
         NORAGGED_RIGHT: r'''ragged-right = ##f''',
     },
@@ -145,24 +139,9 @@ snippet_options = {
 
 
 
-FRAGMENT_LY = r'''
-%(notes_string)s
-{
-
-
-%% ****************************************************************
-%% ly snippet contents follows:
-%% ****************************************************************
-%(code)s
-
-
-%% ****************************************************************
-%% end ly snippet
-%% ****************************************************************
-}
-'''
-
 def classic_lilypond_book_compatibility (key, value):
+    if key == 'lilyquote':
+        return (QUOTE, value)
     if key == 'singleline' and value == None:
         return (RAGGED_RIGHT, None)
 
@@ -183,6 +162,8 @@ def classic_lilypond_book_compatibility (key, value):
     return (None, None)
 
 
+# TODO: Remove the 1mm additional padding in the line-width, once lilypond
+#       creates tighter cropped images!
 PREAMBLE_LY = '''%%%% Generated by %(program_name)s
 %%%% Options: [%(option_string)s]
 \\include "lilypond-book-preamble.ly"
@@ -196,8 +177,9 @@ PREAMBLE_LY = '''%%%% Generated by %(program_name)s
 
 \paper {
   %(paper_string)s
-  force-assignment = #""
-  line-width = #(- line-width (* mm  %(padding_mm)f))
+  %% offset the left padding, also add 1mm as lilypond creates cropped
+  %% images with a little space on the right
+  line-width = #(- line-width (* mm  %(padding_mm)f) (* mm 1))
 }
 
 \layout {
@@ -222,10 +204,23 @@ FULL_LY = '''
 %% ****************************************************************
 '''
 
+FRAGMENT_LY = r'''
+%(notes_string)s
+{
 
 
+%% ****************************************************************
+%% ly snippet contents follows:
+%% ****************************************************************
+%(code)s
 
 
+%% ****************************************************************
+%% end ly snippet
+%% ****************************************************************
+}
+'''
+
 
 
 
@@ -400,13 +395,13 @@ class LilypondSnippet (Snippet):
                     self.option_dict[option] = None
 
 
-        # If LINE_WIDTH is used without parameter, set it to default.
+        # Store if we have an explicit line-width given
         has_line_width = self.option_dict.has_key (LINE_WIDTH)
         if has_line_width and self.option_dict[LINE_WIDTH] == None:
             has_line_width = False
             del self.option_dict[LINE_WIDTH]
 
-        # TODO: Can't we do that more efficiently (built-in python func?)
+        # Use default options (i.e. auto-detected line-width, etc)
         for k in self.formatter.default_snippet_options:
             if k not in self.option_dict:
                 self.option_dict[k] = self.formatter.default_snippet_options[k]
@@ -417,22 +412,6 @@ class LilypondSnippet (Snippet):
         if has_relative and not self.option_dict.has_key (FRAGMENT):
             self.option_dict[FRAGMENT] = None
 
-        if not has_line_width:
-            if type == 'lilypond' or FRAGMENT in self.option_dict:
-                self.option_dict[RAGGED_RIGHT] = None
-
-            if type == 'lilypond':
-                if LINE_WIDTH in self.option_dict:
-                    del self.option_dict[LINE_WIDTH]
-            else:
-                if RAGGED_RIGHT in self.option_dict:
-                    if LINE_WIDTH in self.option_dict:
-                        del self.option_dict[LINE_WIDTH]
-
-            if QUOTE in self.option_dict or type == 'lilypond':
-                if LINE_WIDTH in self.option_dict:
-                    del self.option_dict[LINE_WIDTH]
-
         if not INDENT in self.option_dict:
             self.option_dict[INDENT] = '0\\mm'
 
@@ -443,8 +422,7 @@ class LilypondSnippet (Snippet):
         # all settings before writing them in the \paper block.
         if not LINE_WIDTH in self.option_dict:
             if not QUOTE in self.option_dict:
-                if not LILYQUOTE in self.option_dict:
-                    self.option_dict[LINE_WIDTH] = "#(- paper-width \
+                self.option_dict[LINE_WIDTH] = "#(- paper-width \
 left-margin-default right-margin-default)"
 
     def get_option_list (self):
@@ -460,10 +438,6 @@ left-margin-default right-margin-default)"
         return self.option_list
 
     def compose_ly (self, code):
-        if FRAGMENT in self.option_dict:
-            body = FRAGMENT_LY
-        else:
-            body = FULL_LY
 
         # Defaults.
         relative = 1
@@ -574,6 +548,10 @@ left-margin-default right-margin-default)"
         d = globals().copy()
         d.update (locals())
         d.update (self.global_options.information)
+        if FRAGMENT in self.option_dict:
+            body = FRAGMENT_LY
+        else:
+            body = FULL_LY
         return (PREAMBLE_LY + body) % d
 
     def get_checksum (self):
@@ -686,7 +664,8 @@ printing diff against existing file." % filename)
 
         # UGH - junk self.global_options
         skip_lily = self.global_options.skip_lilypond_run
-        for required in [base + '.ly']:
+        for required in [base + '.ly',
+                         base + '.txt']:
             require_file (required)
         if not skip_lily:
             require_file (base + '-systems.count')
@@ -739,7 +718,7 @@ printing diff against existing file." % filename)
         """Pass input through cmd, and return the result."""
 
         if self.global_options.verbose:
-            progress (_ ("Opening filter `%s'\n") % cmd)
+            progress (_ ("Running through filter `%s'\n") % cmd)
 
         # TODO: Use Popen once we resolve the problem with msvcrt in Windows:
         (stdin, stdout, stderr) = os.popen3 (cmd)
@@ -867,19 +846,20 @@ class MusicXMLFileSnippet (LilypondFileSnippet):
 
     def convert_from_musicxml (self):
         name = self.filename
-        option_list = []
+        xml2ly_option_list = []
         for (key, value) in self.option_dict.items ():
             cmd_key = self.musicxml_options_dict.get (key, None)
             if cmd_key == None:
                 continue
             if value == None:
-                option_list.append (cmd_key)
+                xml2ly_option_list.append (cmd_key)
             else:
-                option_list.append (cmd_key + '=' + value)
-        if ('.mxl' in name) and ('--compressed' not in option_list):
-            option_list.append ('--compressed')
+                xml2ly_option_list.append (cmd_key + '=' + value)
+        if ('.mxl' in name) and ('--compressed' not in xml2ly_option_list):
+            xml2ly_option_list.append ('--compressed')
             self.compressed = True
-        opts = " ".join (option_list)
+        opts = " ".join (xml2ly_option_list)
+        progress (_ ("Converting MusicXML file `%s'...\n") % self.filename)
 
         ly_code = self.filter_pipe (self.contents, 'musicxml2ly %s --out=- - ' % opts)
         return ly_code
@@ -914,8 +894,8 @@ class MusicXMLFileSnippet (LilypondFileSnippet):
         if os.path.exists (xmlfilename):
             diff_against_existing = self.filter_pipe (self.contents, 'diff -u %s - ' % xmlfilename)
             if diff_against_existing:
-                warning ("%s: duplicate filename but different contents of orginal file,\n\
-printing diff against existing file." % xmlfilename)
+                warning (_ ("%s: duplicate filename but different contents of orginal file,\n\
+printing diff against existing file.") % xmlfilename)
                 ly.stderr_write (diff_against_existing)
         else:
             out = file (xmlfilename, 'w')
@@ -927,13 +907,15 @@ printing diff against existing file." % xmlfilename)
         if os.path.exists (filename):
             diff_against_existing = self.filter_pipe (self.full_ly (), 'diff -u %s -' % filename)
             if diff_against_existing:
-                warning ("%s: duplicate filename but different contents of converted lilypond file,\n\
-printing diff against existing file." % filename)
+                warning (_ ("%s: duplicate filename but different contents of converted lilypond file,\n\
+printing diff against existing file.") % filename)
                 ly.stderr_write (diff_against_existing)
         else:
             out = file (filename, 'w')
             out.write (self.full_ly ())
             out.close ()
+            file (path + '.txt', 'w').write ('image of music')
+
 
 
 class LilyPondVersionString (Snippet):