]> git.donarmstrong.com Git - lilypond.git/blob - stepmake/bin/table-to-html.py
release: 1.0.13
[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, cols, separator, linesep):
47     table = open (inname)
48     # ugh
49     html = open (outname, 'w')
50
51     header (html)
52     i = 0
53     for line in table.readlines ():
54         i = i + 1
55         if not len(line):
56             continue
57         columns = split (line, separator)
58         html_line = '<tr><td>' + join (columns, '</td><td>') + '</td></tr>'
59         html_line= regsub.gsub (linesep, ' ',html_line)
60         html.write (html_line)
61
62         if len (columns) <> cols:
63                 print i
64                 raise 'not enough cols'
65
66     table.close ()
67     footer (html)
68     html.close ()
69
70
71 def convert_tex (inname, outname, cols, separator, linesep):
72     table = open (inname)
73     html = open(outname, 'w')
74
75     i = 0
76     for line in table.readlines ():    
77         i = i + 1
78         if not len(line):
79             continue
80         columns = split (line, separator)
81         if len (columns) <> cols:
82                 print i
83                 raise 'not enough cols'
84
85         tex_line =  '\\tableentry{' + join (columns, '}{') + '}\n'
86         tex_line = regsub.gsub (linesep, ' ', tex_line)
87         html.write (tex_line)
88         
89     table.close ()
90     html.close ()
91
92 def main ():
93     identify ()
94     (options, files) = getopt.getopt (
95         sys.argv[1:], 'tl:o:hp:c:s:', ['columns=', 'help', 'latex', 'output=', 'package=', 'separator=', 'linesep='])
96     latex = 0
97     separator = '@'
98     output = ''
99     linesep = '\r'
100     for opt in options:
101         o = opt[0]
102         a = opt[1]
103         if o == '--separator' or o == '-s':
104             separator = a
105         elif o== '--help' or o == '-h':
106             help ()
107         elif o=='--latex' or o == '-t':
108             latex = 1
109         elif o == '--output' or o == '-o':
110             output = a
111         elif o == '--package' or o == '-p':
112             topdir=a
113         elif o == '--linesep' or o == '-l':
114                 linesep = a
115         elif o == '--columns' or o == '-c':
116                 cols =  atoi(a)
117         else:
118             print o
119             raise getopt.error
120
121     sys.path.append (topdir + '/stepmake/bin')
122     from packagepython import *
123     package = Package (topdir)
124     packager = Packager ()
125
126     from flower import *
127
128     if latex:
129         convert_tex (files[0], output,  cols, separator, linesep)
130     else:
131         convert_html (files[0], output, cols, separator, linesep)
132
133 main ()
134