]> git.donarmstrong.com Git - lilypond.git/commitdiff
Handle verbatim, latex textwidth.
authorJan Nieuwenhuizen <janneke@gnu.org>
Mon, 2 Feb 2004 20:37:36 +0000 (20:37 +0000)
committerJan Nieuwenhuizen <janneke@gnu.org>
Mon, 2 Feb 2004 20:37:36 +0000 (20:37 +0000)
ChangeLog
scripts/filter-lilypond-book.py

index ac27236f5cf8c0c1778d037b8cf4e9c818587c0e..1a1f3b6a095907ffdd770f07c04adf638c4d8a9c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2004-02-02  Jan Nieuwenhuizen  <janneke@gnu.org>
+
+       * scripts/filter-lilypond-book.py: Handle verbatim, latex textwidth.
+
 2004-02-02  Han-Wen Nienhuys   <hanwen@xs4all.nl>
 
        * VERSION: release 2.1.17
index a4eb0e4bab96347440ece7114c615232da538c24..05a1743733fe2dafe937bd7d53c1ec4a57fd7875 100644 (file)
@@ -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<code><pre>\s.*?</pre>\s)''',
                'verb': r'''(?P<code><pre>.*?</pre>)''',
@@ -141,11 +149,11 @@ re_dict = {
                },
 
        LATEX: {
-               'input': r'(?m)^[^%\n]*?(?P<match>\\mbinput{?([^}\t \n}]*))',
-               'include': r'(?m)^[^%\n]*?(?P<match>\\mbinclude{(?P<filename>[^}]+)})',
+               'junkmeinput': r'(?m)^[^%\n]*?(?P<match>\\mbinput{?([^}\t \n}]*))',
+               'junkmeinclude': r'(?m)^[^%\n]*?(?P<match>\\mbinclude{(?P<filename>[^}]+)})',
+               'include': r'(?m)^[^%\n]*?(?P<match>\\input{(?P<filename>[^}]+)})',
                'option-sep' : ',\s*',
                'header': r"\n*\\documentclass\s*(\[.*?\])?",
-               'preamble-end': r'(?P<code>\\begin\s*{document})',
                'verbatim': r"(?s)(?P<code>\\begin\s*{verbatim}.*?\\end{verbatim})",
                'verb': r"(?P<code>\\verb(?P<del>.).*?(?P=del))",
                'lilypond-file': r'(?m)^[^%\n]*?(?P<match>\\lilypondfile\s*(\[(?P<options>.*?)\])?\s*\{(?P<filename>.+)})',
@@ -166,10 +174,10 @@ re_dict = {
        # why do we have distinction between @mbinclude and @include?
 
        TEXINFO: {
-               'include':  '(?m)^[^%\n]*?(?P<match>@mbinclude\s+(?P<filename>\S*))',
+               'junkmeinclude':  '(?m)^[^%\n]*?(?P<match>@mbinclude\s+(?P<filename>\S*))',
+               'include':  '(?m)^[^%\n]*?(?P<match>@include\s+(?P<filename>\S*))',
                'input': no_match,
                'header': no_match,
-               'preamble-end': no_match,
                'landscape': no_match,
                'verbatim': r'''(?s)(?P<code>@example\s.*?@end example\s)''',
                'verb': r'''(?P<code>@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'''<src image="%(base)s.png">''',
+       VERBATIM: r'''<pre>
+%(code)s</pre>''',
+       },
+       
+       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 ('<src image="%(base)s.png">' % 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<preabmle>\\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