]> git.donarmstrong.com Git - lilypond.git/blobdiff - buildscripts/mutopia-index.py
Merge branch 'lilypond/translation' of ssh://jomand@git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / buildscripts / mutopia-index.py
index bf7ed7a62845e85b6eca831e2f5403c791586772..50e4ebbf29b5b9355694937acbf228dde47fdd1e 100644 (file)
-#!@PYTHON@
+#!/usr/bin/env python
 # mutopia-index.py
 
-name = 'mutopia-index'
-
-import regex
+import fnmatch
+import getopt
 import os
+import re
+import stat
 import sys
-sys.path.append ('@abs-step-bindir@')
 
+def find (pat, dir):
+    f = os.popen ('find %s -name "%s"'% (dir, pat))
+    lst = []
+    for a in f.readlines():
+        a = a[:-1]
+        lst.append (a)
+    return lst
 
 
-header_regex = regex.compile('\\header[ \t\n]*{\([^}]*\)}')
-header_entry_regex = regex.compile('[\n\t ]*\([^\n\t ]+\)[\n\t ]*=[\n \t]*\([^;]+\)[\n \t]*;')
+junk_prefix = 'out-www/'
 
-#
-# FIXME breaks on multiple strings.
-#
-def read_mudela_header (fn):
-       s = gulp_file(fn)
-       s = regsub.gsub('%.*$', '', s)
-       s = regsub.gsub('\n', ' ', s)   
+headertext= r"""
 
-       dict = {}
-       if header_regex.search(s) <> -1:
-               h = header_regex.group(1)
-       else:
-               return dict
+<h1>LilyPond samples</h1>
 
-       while regex.search('=', h) <> -1: 
 
-               if header_entry_regex.search (h) == -1:
+<p>You are looking at a page with some LilyPond samples.  These files
+are also included in the distribution. The output is completely
+generated from the source file, without any further touch up.
 
-                       raise 'format error'
+<p>
 
-               h = regsub.sub(header_entry_regex, '', h)
-               left = header_entry_regex.group(1)
-               right = header_entry_regex.group(2)
-
-               right = regsub.gsub('\([^\\]\)\"', '\\1', right)
-               right = regsub.gsub('^"', '', right)            
-               left = regsub.gsub('\([^\\]\)\"', '', left)
-               left = regsub.gsub('^"', '', left)
-
-               dict[left] = right
-
-       return dict
-   
+The pictures are 90 dpi anti-aliased snapshots of the printed output.
+For a good impression of the quality print out the PDF file.
+"""
 
+headertext_nopics= r"""
+<p>No examples were found in this directory. 
+"""
 
+#
+# FIXME breaks on multiple strings.
+#
+def read_lilypond_header (fn):
+    s = open (fn).read ()
+    s = re.sub ('%.*$', '', s)
+    s = re.sub ('\n', ' ', s)                
+
+    dict = {}
+    m = re.search (r"""\\header\s*{([^}]*)}""", s)
+
+    if m:
+            s = m.group (1)
+    else:
+            return dict
+
+    while s:
+        m = re.search (r'''\s*(\S+)\s*=\s*"([^"]+)"''', s)
+        if m == None:
+            s = ''
+        else:
+            s = s[m.end (0):]
+            left  = m.group         (1)
+            right = m.group (2)
+
+            left = re.sub ('"', '', left)
+            right = re.sub ('"', '', right)
+            dict[left] = right
+
+    return dict
 
 def help ():
-    sys.stdout.write ("Usage: " + name + " [options] INFILE OUTFILE\n"
-                + "Generate index for mutopia\n\n"
-                + "Options:\n"
-                + "  -h, --help             print this help\n"
-                + "  -p, --package=DIR      specify package\n"
-                + "  --prefix=PRE           specify prefix\n"
-                + "  -s, --subdirs=DIR      add subdir\n"
-                + "  --suffix=SUF           specify suffix\n"
-                     )
+    sys.stdout.write (r'''Usage: mutopia-index [OPTIONS] INFILE OUTFILE
+Generate index for mutopia.
+
+Options:
+ -h, --help                 print this help
+ -o, --output=FILE          write output to file
+ -s, --subdirs=DIR             add subdir
+   --suffix=SUF             specify suffix
+   
+''')
     sys.exit (0)
 
-def gen_list(inputs, subdir, filename):
-    (pre, subdirs, post)=subdir
-    print "generating HTML list %s\n" % filename
-    list = open(filename, 'w')
-    list.write ('<html><TITLE>Rendered Examples</TITLE>\n')
-    list.write ('<body bgcolor=white>')
-    if len(subdirs):
-       list.write  ('<h2>subdirectories</h2>')
-       list.write  ('<ul>')    
-        for ex in subdirs:
-           print 'subdir %s ' % ex
-           list.write ('<li><a href=%s/index.html>Subdirectory: %s</a></li>\n' % (pre + ex + post , ex))
-
-       list.write ('</ul>')
-
-    list.write('<h2>Contents of this directory</h2>\n');
-    list.write ('These example files are taken from the LilyPond distribution.\n'
-     'LilyPond currently only outputs TeX and MIDI. The pictures and\n'
-     'PostScript files were generated using TeX, Ghostscript and some\n'
-     'graphics tools.  The papersize used for these examples is A4.  The GIF\n'
-     'files have been scaled to eliminate aliasing.\n');
-
+# ugh.
+def gen_list (inputs, file_name):
+    sys.stderr.write ("generating HTML list %s" % file_name)
+    sys.stderr.write ('\n')
+    if file_name:
+        list = open (file_name, 'w')
+    else:
+        list = sys.stdout
+    list.write ('''<html><head><title>Rendered Examples</title>
+<style type="text/css">
+hr { border:0; height:1; color: #000000; background-color: #000000; }\n
+</style>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+</head>''')
+
+    list.write ('<body bgcolor=white>\n')
+    
+    if inputs:
+        list.write (headertext)
+    else:
+        list.write (headertext_nopics)
 
     for ex in inputs:
-       ex_ext = '.ly'
-       print '%s, ' % ex
-       try:
-           header = read_mudela_header(ex + ex_ext + '.txt')
-       except:
-           ex_ext = '.fly'
-           header = read_mudela_header(ex + ex_ext + '.txt')
-       
-       def read_dict(s, default, h =header):
-               try:
-                   ret = h[s]
-               except KeyError:
-                   ret = default
-               return ret
-       head = read_dict('title', ex)
-       composer = read_dict('composer', '')
-       desc = read_dict('description', '')
-       list.write('<hr>')
-       list.write('<h1>example file: %s</h1>' % head);
-       if composer <> '':
-           list.write('<h2>%s</h2>\n' % composer)
-       if desc <> '':
-           list.write('%s<p>' % desc)
-       list.write ('<ul>')
-       def list_item(filename, desc, l = list):
-           if file_exist_b(filename):
-               l.write ('<li><a href=%s>%s</a>\n' % (filename, desc))
-           
-       list_item(ex + ex_ext + '.txt', 'The input')
-       for pageno in range(1,100):
-           f  = ex + '-page%d.gif' % pageno
-           if not file_exist_b (f):
-               break
-           list_item(f, 'The output (picture, page %d)' % pageno)
-       list_item(ex + '.ps.gz', 'The output (gzipped PostScript)')
-       list_item(ex + '.midi', 'The output (MIDI)')
-       list.write ("</ul>");
-
-    list.write( "</BODY></HTML>");
-    list.close()
-
-import getopt
-
-(options, files) = getopt.getopt(sys.argv[1:], 
-  'hp:s:', ['help', 'subdirs=', 'suffix=', 'package=', 'prefix='])
-subdir_pre=''
-subdir_suf =''
-
-subdirs =[]
-for opt in options:
-    o = opt[0]
-    a = opt[1]
-    if o == '--subdirs' or o == '-s':
-       subdirs.append (a)
-    elif o == '--prefix':
-       subdir_pre = a
-    elif o == '-p' or o == '--package':
-       topdir = a
-    elif o == '--suffix':
-       subdir_suf = a
-
-    sys.path.append (topdir + '/stepmake/bin')
-    from packagepython import *
-    package = Package (topdir)
-    packager = Packager ()
-
-    from flower import *
-
-       
-# huh?
-allfiles = multiple_find (['*.*ly.txt'], '.')
-
-gen_list (files, (subdir_pre, subdirs, subdir_suf), 'index.html')
-
+        print ex
+        
+        (base, ext) = os.path.splitext (ex)
+        (base, ext2) = os.path.splitext (base)                
+        ext = ext2 + ext
+        
+        header = read_lilypond_header (ex)
+        head = header.get ('title', os.path.basename (base))
+        composer = header.get ('composer', '')
+        desc = header.get ('description', '')
+        list.write ('<hr>\n')
+        list.write ('<h1>%s</h1>\n' % head);
+        if composer:
+            list.write ('<h2>%s</h2>\n' % composer)
+        if desc:
+            list.write ('%s<p>' % desc)
+        list.write ('<ul>\n')
+
+        def list_item (file_name, desc, type, lst = list):
+            if os.path.isfile (file_name):
+                lst.write ('<li><a href="%s">%s</a>'
+                     % (re.sub (junk_prefix, '', file_name), desc))
+
+                # FIXME: include warning if it uses \include
+                # files.
+                
+                size = os.stat (file_name)[stat.ST_SIZE]
+                kB = (size + 512) / 1024
+                if kB:
+                    lst.write (' (%s %d kB)' % (type, kB))
+                else:
+                    lst.write (' (%s %d characters)'
+                         % (type, size))
+                pictures = ['jpeg', 'png', 'xpm']
+                lst.write ('\n')
+            else:
+                print "cannot find" , `file_name`
+
+        list_item (base + ext, 'The input', 'ASCII')
+
+        pages_found = 0
+        for page in range (1, 100):
+            f = base + '-page%d.png' % page
+            
+            if not os.path.isfile (f):
+                break
+            pages_found += 1
+            list_item (f, 'See a picture of page %d' % page, 'png')
+
+        if pages_found == 0 and os.path.exists (base + '.png'):
+            list_item (base + ".png",
+                 'See a picture', 'png')
+
+            
+        list_item (base + '.pdf', 'Print', 'PDF')
+        list_item (base + '.midi', 'Listen', 'MIDI')
+        list.write ('</ul>\n');
+
+    list.write ('</body></html>\n');
+    list.close ()
+
+(options, files) = getopt.getopt (sys.argv[1:],
+ 'ho:', ['help', 'output='])
+outfile = 'examples.html'
+
+subdirs = []
+for (o, a) in options:
+    if o == '--help' or o == '-h':
+        help ()
+    elif o == '--output' or o == '-o':
+        outfile = a
+
+dirs  = []
+for f in files:
+    dirs += find ('out-www', f)
+
+if not dirs:
+    dirs = ['.']
+
+allfiles = []
+
+for d in dirs:
+    allfiles += find ('*.ly', d)
+
+allfiles = [f for f in allfiles
+            if not f.endswith ('snippet-map.ly')
+            and not re.search ('lily-[0-9a-f]+', f)
+            and 'musicxml' not in f]
+
+gen_list (allfiles, outfile)