From 0a1fb0e9b496de6bbdee6a2cd8f788c2e62cb802 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Mon, 2 Feb 2004 20:37:36 +0000 Subject: [PATCH] Handle verbatim, latex textwidth. --- ChangeLog | 4 + scripts/filter-lilypond-book.py | 169 +++++++++++++++++++++----------- 2 files changed, 117 insertions(+), 56 deletions(-) diff --git a/ChangeLog b/ChangeLog index ac27236f5c..1a1f3b6a09 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2004-02-02 Jan Nieuwenhuizen + + * scripts/filter-lilypond-book.py: Handle verbatim, latex textwidth. + 2004-02-02 Han-Wen Nienhuys * VERSION: release 2.1.17 diff --git a/scripts/filter-lilypond-book.py b/scripts/filter-lilypond-book.py index a4eb0e4bab..05a1743733 100644 --- a/scripts/filter-lilypond-book.py +++ b/scripts/filter-lilypond-book.py @@ -1,9 +1,11 @@ #!@PYTHON@ ''' -TODO: latex/paper parameters - [verbatim] - \relative ly:export? +TODO: @\ lilypondfile + \input, @include + check latex parameters + papersizes? + Example usage: @@ -77,6 +79,7 @@ option_definitions = [ (_ ("FILTER"), 'F', 'filter', _ ("pipe snippets through FILTER [convert-ly -n -]")), ('', 'h', 'help', _ ("print this help")), (_ ("COMMAND"), 'P', 'process', _ ("process ly_files using COMMAND FILE...")), + (_ ("FILE"), 'o', 'output', _ ("write output to FILE")), ('', 'V', 'verbose', _ ("be verbose")), ('', 'v', 'version', _ ("print version information")), ('', 'w', 'warranty', _ ("show warranty and copyright")), @@ -93,16 +96,22 @@ if '@bindir@' == ('@' + 'bindir@') or not os.path.exists (lilypond_binary): use_hash_p = 1 format = 0 +output_name = 0 +latex_filter_cmd = 'latex "\\nonstopmode \input /dev/stdin"' 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 +default_ly_options = { } LATEX = 'latex' HTML = 'html' TEXINFO = 'texinfo' BEFORE = 'before' +OUTPUT = 'output' AFTER = 'after' +VERBATIM = 'verbatim' +LINEWIDTH = 'linewidth' ## lilypond-book heritage. to be cleaned @@ -124,7 +133,6 @@ re_dict = { 'include': no_match, 'input': no_match, 'header': no_match, - 'preamble-end': no_match, 'landscape': no_match, 'verbatim': r'''(?s)(?P
\s.*?
\s)''', 'verb': r'''(?P
.*?
)''', @@ -141,11 +149,11 @@ re_dict = { }, LATEX: { - 'input': r'(?m)^[^%\n]*?(?P\\mbinput{?([^}\t \n}]*))', - 'include': r'(?m)^[^%\n]*?(?P\\mbinclude{(?P[^}]+)})', + 'junkmeinput': r'(?m)^[^%\n]*?(?P\\mbinput{?([^}\t \n}]*))', + 'junkmeinclude': r'(?m)^[^%\n]*?(?P\\mbinclude{(?P[^}]+)})', + 'include': r'(?m)^[^%\n]*?(?P\\input{(?P[^}]+)})', 'option-sep' : ',\s*', 'header': r"\n*\\documentclass\s*(\[.*?\])?", - 'preamble-end': r'(?P\\begin\s*{document})', 'verbatim': r"(?s)(?P\\begin\s*{verbatim}.*?\\end{verbatim})", 'verb': r"(?P\\verb(?P.).*?(?P=del))", 'lilypond-file': r'(?m)^[^%\n]*?(?P\\lilypondfile\s*(\[(?P.*?)\])?\s*\{(?P.+)})', @@ -166,10 +174,10 @@ re_dict = { # why do we have distinction between @mbinclude and @include? TEXINFO: { - 'include': '(?m)^[^%\n]*?(?P@mbinclude\s+(?P\S*))', + 'junkmeinclude': '(?m)^[^%\n]*?(?P@mbinclude\s+(?P\S*))', + 'include': '(?m)^[^%\n]*?(?P@include\s+(?P\S*))', 'input': no_match, 'header': no_match, - 'preamble-end': no_match, 'landscape': no_match, 'verbatim': r'''(?s)(?P@example\s.*?@end example\s)''', 'verb': r'''(?P@code{.*?})''', @@ -192,7 +200,8 @@ PAPER = 'paper' ly_options = { NOTES: { - 'relative': r'''\relative #(ly:make-pitch %(relative)s 0 0)''' + #'xrelative': r'''\relative #(ly:make-pitch %(relative)s 0 0)''', + 'relative': r'''\relative c%(relative_quotes)s''', }, PAPER: { 'indent' : r''' @@ -207,6 +216,7 @@ ly_options = { \remove Time_signature_engraver }''', 'raggedright' : r''' + indent = 0.0\mm raggedright = ##t''', }, PREAMBLE: { @@ -215,6 +225,36 @@ ly_options = { }, } +# WIP +output = { + HTML : { + AFTER: '', + BEFORE: '', + OUTPUT: r'''''', + VERBATIM: r'''
+%(code)s
''', + }, + + LATEX : { + AFTER: '', + BEFORE: '', + OUTPUT: r'''{\parindent 0pt +\ifx\preLilyPondExample\preLilyPondExample\fi +\def\lilypondbook{} +\input %(base)s.tex +\ifx\preLilyPondExample\postLilyPondExample\fi} +''', + VERBATIM: r'''\begin{verbatim} +%(code)s\end{verbatim} +''', + }, + + TEXINFO : { + BEFORE: '', + AFTER: '', + }, + + } PREAMBLE_LY = r'''%% Generated by %(program_name)s %% Options: [%(option_string)s] @@ -231,8 +271,15 @@ FULL_LY = '%(code)s' def compose_ly (code, option_string): + options = [] + # urg + for i in default_ly_options.keys (): + options.append (i) + vars ()[i] = default_ly_options[i] + + options = options + string.split (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): @@ -241,8 +288,8 @@ def compose_ly (code, option_string): body = FULL_LY # defaults - relative = "0" - staffsize = "16" + relative = 0 + staffsize = '16' notes_options = [] paper_options = [] @@ -256,37 +303,19 @@ def compose_ly (code, option_string): key = i if key in ly_options[NOTES].keys (): - notes_options.append (ly_options[NOTES][key] % vars ()) + notes_options.append (ly_options[NOTES][key]) elif key in ly_options[PREAMBLE].keys (): - preamble_options.append (ly_options[PREAMBLE][key] \ - % vars ()) + preamble_options.append (ly_options[PREAMBLE][key]) elif key in ly_options[PAPER].keys (): - paper_options.append (ly_options[PAPER][key] % vars ()) + paper_options.append (ly_options[PAPER][key]) + relative_quotes = (",,,", ",,", ",", "", "'", "''", "'''")[relative-3] 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 ') + notes_string = string.join (notes_options, '\n ') % vars () + paper_string = string.join (paper_options, '\n ') % vars () + preamble_string = string.join (preamble_options, '\n ') % vars () return (PREAMBLE_LY + body) % vars () -output = { - HTML : { - BEFORE: '', - AFTER: '', - }, - - LATEX : { - BEFORE: '', - AFTER: '', - }, - - TEXINFO : { - BEFORE: '', - AFTER: '', - }, - - } - # BARF # use lilypond-bin for latex (.lytex) books, @@ -343,15 +372,23 @@ class Snippet: def output_html (self, source): base = self.basename (source) + if VERBATIM in self.match.group ('options'): + code = self.substring (source, 'code') + h.write (output[HTML][VERBATIM] % vars ()) h.write (output[HTML][BEFORE]) - h.write ('' % vars ()) + h.write (output[HTML][OUTPUT] % vars ()) h.write (output[HTML][AFTER]) def output_latex (self, source): - h.write (output[HTML][BEFORE]) - name = self.basename (source) + '.tex' - h.write (open (name).read ()) - h.write (output[HTML][AFTER]) + if VERBATIM in self.match.group ('options'): + code = self.substring (source, 'code') + h.write (output[LATEX][VERBATIM] % vars ()) + h.write (output[LATEX][BEFORE]) + base = self.basename (source) + # h.write (open (base + '.tex').read ()) + # output = open (base + '.tex').read () + h.write (output[LATEX][OUTPUT] % vars ()) + h.write (output[LATEX][AFTER]) def output_texinfo (self, source): h.write ('\n@tex\n') @@ -467,17 +504,22 @@ def process_snippets (source, snippets, cmd): LATEX_DOCUMENT = r''' %(preamble)s \begin{document} -\typeout{columnsep=\the\columnsep} \typeout{textwidth=\the\textwidth} +\typeout{columnsep=\the\columnsep} +\makeatletter\if@twocolumn\typeout{columns=2}\fi\makeatother \end{document} ''' #need anything else besides textwidth? -def get_latex_parameters (source): - snippet, = find_snippets (source, 'preamble-end') - latex_cmd = 'latex "\\nonstopmode \input /dev/stdin"' - preamble = source[:snippet.start (0)] +def get_latex_textwidth (source): + m = re.search (r'''(?P\\begin\s*{document})''', source) + preamble = source[:m.start (0)] latex_document = LATEX_DOCUMENT % vars () - parameter_string = filter_pipe (latex_document, latex_cmd) + parameter_string = filter_pipe (latex_document, latex_filter_cmd) + + columns = 0 + m = re.search ('columns=([0-9.]*)', parameter_string) + if m: + columns = string.atoi (m.group (1)) columnsep = 0 m = re.search ('columnsep=([0-9.]*)pt', parameter_string) @@ -488,8 +530,8 @@ def get_latex_parameters (source): m = re.search('textwidth=([0-9.]*)pt', parameter_string) if m: textwidth = string.atof (m.group (1)) - if columnsep: - textwidth -= columnsep + if columns: + textwidth = (textwidth - columnsep) / columns return textwidth @@ -522,7 +564,6 @@ def do_file (input_filename): h = open (input_filename) source = h.read () - #snippet_types = ('lilypond', 'lilypond-block') snippet_types = ('verbatim', 'verb', 'multiline-comment', 'lilypond', 'lilypond-block') snippets = [] @@ -532,6 +573,13 @@ def do_file (input_filename): snippets.sort (compare_index) h = sys.stdout + if output_name and output_name != '-': + if not os.path.isdir (output_name): + os.mkdir (output_name, 0777) + if input_filename != '-': + h = open (output_name + '/' + input_filename + '.out', + 'w') + os.chdir (output_name) def filter_source (snippet, source): global index @@ -560,9 +608,10 @@ def do_file (input_filename): snippet_output (snippet, source) index = snippet.end (0) - if format == LATEX: - textwdith = get_latex_parameters (source) - #TODO: set global option + global default_ly_options + if format == LATEX and LINEWIDTH not in default_ly_options.keys (): + textwidth = get_latex_textwidth (source) + default_ly_options[LINEWIDTH] = '''%.0f\pt''' % textwidth global index if filter_cmd: @@ -575,8 +624,14 @@ def do_file (input_filename): do_snippets (source, snippets, compile_output) h.write (source[index:]) + # misusing snippet bit silly? + includes = find_snippets (source, 'include') + for i in includes: + os.chdir (original_dir) + sys.stderr.write ('DO: ' + sys.argv[0] + ' OPTIONS ' + i.substring (source, 'filename') + '\n') + def do_options (): - global format + global format, output_name global filter_cmd, process_cmd, verbose_p (sh, long) = ly.getopt_args (option_definitions) @@ -610,6 +665,8 @@ def do_options (): elif o == '--help' or o == '-h': ly.help () sys.exit (0) + elif o == '--output' or o == '-o': + output_name = a elif o == '--process' or o == '-P': process_cmd = a filter_cmd = 0 -- 2.39.5