From 4161791f0cfad9c6bec25f2fba7e359821cd2ca9 Mon Sep 17 00:00:00 2001 From: janneke Date: Mon, 2 Feb 2004 13:39:06 +0000 Subject: [PATCH] Handle snippet options. --- ChangeLog | 4 + scripts/filter-lilypond-book.py | 132 +++++++++++++++++++++++++++----- 2 files changed, 117 insertions(+), 19 deletions(-) diff --git a/ChangeLog b/ChangeLog index 664795e83c..9091adb42e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2004-02-02 Jan Nieuwenhuizen + + * scripts/filter-lilypond-book.py: Handle snippet options. + 2004-02-02 Han-Wen Nienhuys * lily/new-lyric-combine-music-iterator.cc (construct_children): diff --git a/scripts/filter-lilypond-book.py b/scripts/filter-lilypond-book.py index 70bf4cba0d..1eab948d1a 100644 --- a/scripts/filter-lilypond-book.py +++ b/scripts/filter-lilypond-book.py @@ -1,6 +1,23 @@ #!@PYTHON@ +''' +Exampel usage: + +test: + filter-lilypond-book --filter="tr '[a-z]' '[A-Z]'" BOOK + +convert-ly on book: + filter-lilypond-book --filter="convert-ly --no-version --from=1.6.11 -" BOOK + +minimal classic lilypond-book (WIP): + filter-lilypond-book --process="lilypond-bin" BOOK.tely + + (([0-9][0-9])*pt) -> staffsize=\2 + +''' + import string +import __main__ ################################################################ # Users of python modules should include this snippet @@ -47,7 +64,7 @@ help_summary = _ ("""Process ly snippets from lilypond-book source. Example usa filter-lilypond-book --filter="convert-ly --no-version --from=1.6.11 -" BOOK """) -copyright = ('Tom Cato Amundsen ', +copyright = ('Jan Nieuwenhuizen >', 'Han-Wen Nienhuys ') option_definitions = [ @@ -69,12 +86,21 @@ if '@bindir@' == ('@' + 'bindir@') or not os.path.exists (lilypond_binary): lilypond_binary = 'lilypond-bin' +use_hash_p = 1 format = 0 filter_cmd = 'convert-ly --no-version --from=2.0.0 -' #filter_cmd = 0 #process_cmd = 'convert-ly --no-version --from=2.0.0' process_cmd = 0 +LATEX = 'latex' +HTML = 'html' +TEXINFO = 'texinfo' +BEFORE = 'before' +AFTER = 'after' + +## lilypond-book heritage. to be cleaned + ################################################################ # Recognize special sequences in the input @@ -89,7 +115,7 @@ process_cmd = 0 # (?s) -- make the dot match all characters including newline no_match = 'a\ba' re_dict = { - 'html': { + HTML: { 'include': no_match, 'input': no_match, 'header': no_match, @@ -109,7 +135,7 @@ re_dict = { 'ly2dvi': r'(?m)(?P[^>]+)?>\s*(?P[^<]+)\s*)', }, - 'latex': { + LATEX: { 'input': r'(?m)^[^%\n]*?(?P\\mbinput{?([^}\t \n}]*))', 'include': r'(?m)^[^%\n]*?(?P\\mbinclude{(?P[^}]+)})', 'option-sep' : ',\s*', @@ -134,7 +160,7 @@ re_dict = { # why do we have distinction between @mbinclude and @include? - 'texinfo': { + TEXINFO: { 'include': '(?m)^[^%\n]*?(?P@mbinclude\s+(?P\S*))', 'input': no_match, 'header': no_match, @@ -155,23 +181,88 @@ re_dict = { } } -def compose_ly (code, options): +NOTES = 'body' +PREAMBLE = 'preamble' +PAPER = 'paper' + +ly_options = { + NOTES: { + 'relative': r'''\relative #(ly:make-pitch %(relative)s 0 0)''' + }, + PAPER: { + 'indent' : r''' + indent = %(indent)s''', + 'linewidth' : r''' + linewidth = %(linewidth)s''', + 'noindent' : r''' + indent = 0.0\mm''', + 'notime' : r''' + \translator { + \StaffContext + \remove Time_signature_engraver + }''', + 'raggedright' : r''' + raggedright = ##t''', + }, + PREAMBLE: { + 'staffsize': r''' +#(set-global-staff-size %(staffsize)s)''', + }, + } + + +PREAMBLE_LY = r'''%% Generated by %(program_name)s +%% Options: [%(option_string)s] +%(preamble_string)s +\paper {%(paper_string)s +} +''' + +FRAGMENT_LY = r'''\score{ + \notes%(notes_string)s{ + %(code)s } +}''' +FULL_LY = '%(code)s' + + +def compose_ly (code, option_string): m = re.search (r'''\\score''', code) + options = string.split (option_string, ',') if not m and (not options \ or not 'nofragment' in options \ or 'fragment' in options): - body = r'''\score{\notes{ %(code)s }}''' % vars () + body = FRAGMENT_LY else: - body = code - ### todo: add options - return body - - -LATEX = 'latex' -HTML = 'html' -TEXINFO = 'texinfo' -BEFORE = 'before' -AFTER = 'after' + body = FULL_LY + + # defaults + relative = "0" + staffsize = "16" + + notes_options = [] + paper_options = [] + preamble_options = [] + for i in options: + if string.find (i, '=') > 0: + key, value = string.split (i, '=') + # hmm + vars ()[key] = value + else: + key = i + + if key in ly_options[NOTES].keys (): + notes_options.append (ly_options[NOTES][key] % vars ()) + elif key in ly_options[PREAMBLE].keys (): + preamble_options.append (ly_options[PREAMBLE][key] \ + % vars ()) + elif key in ly_options[PAPER].keys (): + paper_options.append (ly_options[PAPER][key] % vars ()) + + program_name = __main__.program_name + notes_string = string.join (notes_options, '\n ') + paper_string = string.join (paper_options, '\n ') + preamble_string = string.join (preamble_options, '\n ') + return (PREAMBLE_LY + body) % vars () output = { HTML : { @@ -236,7 +327,9 @@ class Snippet: return self.hash def basename (self, source): - return 'lily-%d' % self.get_hash (source) + if use_hash_p: + return 'lily-%d' % self.get_hash (source) + raise 'to be done' def write_ly (self, source): h = open (self.basename (source) + '.ly', 'w') @@ -268,9 +361,10 @@ class Snippet: base = self.basename (source) if os.path.exists (base + '.ly') \ and os.path.exists (base + '.tex') \ - and self.ly (source) == open (base + '.ly').read (): + and (not use_hash_p \ + or self.ly (source) == open (base + '.ly').read ()): # TODO: something smart with target formats - # (ps, png) and m/atimes + # (ps, png) and m/ctimes return None return self -- 2.39.2