]> git.donarmstrong.com Git - lilypond.git/blob - stepmake/bin/table-to-html.py
d43590f0d7ffaadb6e65467fb86914645dc3d59c
[lilypond.git] / stepmake / bin / table-to-html.py
1 #!@PYTHON@
2
3 # table-to-html.py -- convert char-separated table to html table
4
5 # source file of the GNU LilyPond music typesetter
6
7 # (c) 1998 Jan Nieuwenhuizen <janneke@gnu.org>
8
9 version = '0.1'
10 name = 'table-to-html'
11
12 import os
13 import sys
14
15 import getopt
16 from string import *
17 import regex
18 import regsub
19 import time
20
21 def program_id ():
22     return name + ' version ' + version;
23
24 def identify ():
25     sys.stdout.write (program_id () + '\n')
26
27 def help ():
28     sys.stdout.write ("Usage: table-to-html [OPTION]... TABLE-FILE HTML-FILENAME\n"
29                  + "Generate pretty table from char separated table\n\n"
30                  + "Options:\n"
31                  + "  -h, --help             print this help\n"
32                  + "  -p, --package=DIR      specify package\n"
33                  + "  -s, --separator=SEP    specify separator [:]\n"
34                  + "  -t, --latex            do latex output instead\n"
35                       )
36     
37     sys.exit (0)
38
39
40 def header (html):
41     html.write ('<body bgcolor=white><table cellspacing=10>')
42
43 def footer (html):
44     html.write ('</table></body>')
45
46 def convert_html (inname, outname, separator):
47     # urg, again?
48     from flower import *
49     table = File (inname)
50     # ugh
51     html = File (outname, 'w')
52
53     header (html)
54     i = 0
55     while not table.eof ():
56         line = table.readline ()
57         i = i + 1
58         if not len(line):
59             continue
60         columns = split (line, separator)
61         html_line = '<tr><td>' + join (columns, '</td><td>') + '</td></tr>'
62         html.write (html_line)
63         if len (columns) < 7:
64             print inname + ': ' + str(i) + ':warning: not enough cols\n'
65             continue
66         if len (columns) > 7:
67             print inname + ': ' + str(i) + ':warning: too many cols\n'
68             continue
69
70     table.close ()
71     footer (html)
72     html.close ()
73
74 def convert_tex (inname, outname, separator):
75     # urg, again?
76     from flower import *
77     table = File (inname)
78     # ugh
79     html = File (outname, 'w')
80
81     i = 0
82     while not table.eof ():
83         line = table.readline ()
84         i = i + 1
85         if not len(line):
86             continue
87         columns = split (line, separator)
88         if len (columns) < 7:
89             print inname + ': ' + str(i) + ':warning: not enough cols\n'
90             continue
91         if len (columns) > 7:
92             print inname + ': ' + str(i) + ':warning: too many cols\n'
93             continue
94
95         html_line =  '\\tableentry{' + join (columns, '}{') + '}\n'
96         html.write (html_line)
97     table.close ()
98     html.close ()
99
100 def main ():
101     identify ()
102     (options, files) = getopt.getopt (
103         sys.argv[1:], 'to:hp:s:', ['help', 'latex', 'output=', 'package=', 'separator='])
104     latex = 0
105     separator = ':'
106     output = ''
107     for opt in options:
108         o = opt[0]
109         a = opt[1]
110         if o == '--separator' or o == '-s':
111             separator = a
112         elif o== '--help' or o == '-h':
113             help ()
114         elif o=='--latex' or o == '-t':
115             latex = 1
116         elif o == '--output' or o == '-o':
117             output = a
118         elif o == '--package' or o == '-p':
119             topdir=a
120         else:
121             print o
122             raise getopt.error
123
124     sys.path.append (topdir + '/stepmake/bin')
125     from packagepython import *
126     package = Package (topdir)
127     packager = Packager ()
128
129     from flower import *
130
131     if latex:
132         convert_tex (files[0], output, separator)
133     else:
134         convert_html (files[0], output, separator)
135
136 main ()
137