From 5263eebdba56b8b4d4cff703798032b14e6611fd Mon Sep 17 00:00:00 2001 From: fred Date: Tue, 26 Mar 2002 22:25:15 +0000 Subject: [PATCH] lilypond-1.2.4 --- scripts/mudela-book.py | 120 ++++++++++++++++++++++++----------------- 1 file changed, 71 insertions(+), 49 deletions(-) diff --git a/scripts/mudela-book.py b/scripts/mudela-book.py index 29e6f29ff5..8021e51818 100644 --- a/scripts/mudela-book.py +++ b/scripts/mudela-book.py @@ -14,9 +14,12 @@ program_version = '@TOPLEVEL_VERSION@' cwd = os.getcwd () include_path = [cwd] -# TODO: use splitting iso. \mudelagraphic. +# TODO: Figure out clean set of options. +# BUG: does not handle \verb|\begin{verbatim}\end{verbatim}| correctly. +# Should make a joint RE for \verb and \begin, \end{verbatim} # + default_music_fontsize = 16 default_text_fontsize = 12 @@ -83,8 +86,17 @@ output_dict= { %s @end example """, + +# do some tweaking: @ is needed in some ps stuff. +# override EndLilyPondOutput, since @tex is done +# in a sandbox, you can't do \input lilyponddefs at the +# top of the document. 'output-all': r"""@tex +\catcode`\@=12 +\input lilyponddefs +\def\EndLilyPondOutput{} \input %s.tex +\catcode`\@=0 @end tex @html @@ -93,6 +105,11 @@ output_dict= { } } +def output_verbatim (body): + if __main__.format == 'texi': + body = re.sub ('([@{}])', '@\\1', body) + return get_output ('output-verbatim') % body + re_dict = { 'latex': {'input': '\\\\input{?([^}\t \n}]*)', 'include': '\\\\include{([^}]+)}', @@ -132,6 +149,16 @@ for r in re_dict.keys (): re_dict[r] = newdict +def uniq (list): + list.sort () + s = list + list = [] + for x in s: + if x not in list: + list.append (x) + return list + + def get_output (name): return output_dict[format][name] @@ -193,16 +220,18 @@ def compose_full_body (body, opts): if 'twocolumn' in opts: cols = 2 - if 'fragment' or 'singleline' in opts: - l = -1.0; - else: - l = latex_linewidths[cols][paper][latex_size] # urg: breaks on \include of full score # Use nofly option if you want to \include full score. if not 'nofly' in opts and not re.search ('\\\\score', body): opts.append ('fly') + if 'fragment' in opts or 'singleline' in opts: + l = -1.0; + else: + l = latex_linewidths[cols][paper][latex_size] + + if 'fly' in opts: body = r"""\score { \notes\relative c { @@ -211,26 +240,18 @@ def compose_full_body (body, opts): \paper { } }""" % body + opts = uniq (opts) + optstring = string.join (opts, ' ') + optstring = re.sub ('\n', ' ', optstring) body = r""" -%% Generated by mudela-book.py +%% Generated by mudela-book.py; options are %s \include "paper%d.ly" \paper { linewidth = %f \pt; } -""" % (music_size, l) + body +""" % (optstring, music_size, l) + body return body -def inclusion_func (match, surround): - insert = match.group (0) - try: - (insert, d) = read_doc_file (match.group(1)) - deps = deps + d - insert = surround + insert + surround - except: - sys.stderr.write("warning: can't find %s, let's hope latex will\n" % m.group(1)) - - return (insert, deps) - def find_inclusion_chunks (regex, surround, str): chunks = [] while str: @@ -258,16 +279,16 @@ def find_input_chunks (str): def read_doc_file (filename): """Read the input file, substituting for \input, \include, \mudela{} and \mudelafile""" str = '' - for fn in [filename, filename+'.tex', filename+'.doc']: + for ext in ['', '.tex', '.doc', '.tely']: try: - f = open(fn) + f = open(filename+ ext) str = f.read (-1) except: pass if not str: - raise IOError + error ("File not found `%s'\n" % filename) retdeps = [filename] @@ -281,17 +302,17 @@ def read_doc_file (filename): else: __main__.format = 'latex' - chunks = find_verbatim_chunks (str) - newchunks = [] + chunks = [('input', str)] - for func in (find_include_chunks, find_input_chunks): + for func in (find_verbatim_chunks, find_verb_chunks, find_include_chunks, find_input_chunks): + newchunks = [] for c in chunks: if c[0] == 'input': ch = func (c[1]) newchunks = newchunks + ch else: newchunks.append (c) - + chunks = newchunks return chunks @@ -343,29 +364,35 @@ def find_verbatim_chunks (str): (TYPE_STR, CONTENT_STR), where TYPE_STR is one of 'input' and 'verbatim' """ - chunks = [] - while str: m = get_re ('verbatim').search( str) - m2 = get_re ("verb").search( str) + if m == None: + chunks.append( ('input', str)) + str = '' + else: + chunks.append (('input', str[:m.start (0)])) + chunks.append (('verbatim', m.group (0))) + + str = str [m.end(0):] + + return chunks - if m == None and m2 == None: +def find_verb_chunks (str): + + chunks = [] + while str: + m = get_re ("verb").search(str) + if m == None: chunks.append (('input', str)) str = '' - break - - if m == None: - m = m2 + else: + chunks.append (('input', str[:m.start (0)])) + chunks.append (('verbatim', m.group (0))) + str = str [m.end(0):] - if m2 and m2.start (0) < m.start (0): - m = m2 + return chunks - chunks.append (('input', str[:m.start (0)])) - chunks.append (('verbatim', m.group (0))) - - str = str [m.end(0):] - return chunks def find_mudela_shorthand_chunks (str): @@ -480,7 +507,6 @@ def advance_counters (counter, opts, str): def schedule_mudela_block (base, chunk, extra_opts): - """Take the body and options from CHUNK, figure out how the real .ly should look, and what should be left MAIN_STR (meant for the main file). The .ly is written, and scheduled in @@ -498,10 +524,7 @@ def schedule_mudela_block (base, chunk, extra_opts): newbody = '' if 'verbatim' in opts: - verbatim_mangle = body - if __main__.format == 'texi': - verbatim_mangle = re.sub ('([{}])', '@\\1', body) - newbody = get_output ('output-verbatim') % verbatim_mangle + newbody = output_verbatim (body) file_body = compose_full_body (body, opts) updated = update_file (file_body, base + '.ly') @@ -609,9 +632,8 @@ def transform_input_file (in_filename, out_filename): newchunks.append (('mudela', body)) else: newchunks.append (c) - - - chunks = newchunks + chunks = newchunks + if chunks and chunks[0][0] == 'input': chunks[0] = ('input', completize_preamble (chunks[0][1])) @@ -621,7 +643,7 @@ def system (cmd): sys.stderr.write ("invoking `%s'\n" % cmd) st = os.system (cmd) if st: - sys.stderr.write ('Error command exited with value %d\n' % st) + error ('Error command exited with value %d\n' % st) return st def compile_all_files (chunks): -- 2.39.5