]> git.donarmstrong.com Git - lilypond.git/blob - stepmake/bin/table-to-html.py
release: 1.3.68
[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 import getopt
15 import string
16 import time
17 import re
18
19 format = 'html'
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 (
29 r"""Usage: table-to-html [OPTION]... TABLE-FILE HTML-FILENAME
30 Generate pretty table from char separated table
31 Options:
32   -h, --help                     print this help
33   -p, --package=DIR       specify package
34   -s, --separator=SEP   specify separator [:]
35   -t, --latex                   do latex output instead
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 (lines, outname, cols, separator, linesep):
47         # ugh
48         html = open (outname, 'w')
49
50         header (html)
51         i = 0
52         for line in lines:
53                 i = i + 1
54                 if not len(line):
55                         continue
56                 columns = string.split (line, separator)
57                 html_line = '<tr><td>' + string.join (columns, '</td><td>') + '</td></tr>'
58                 html_line= re.sub (linesep, ' ', html_line)
59                 html.write (html_line)
60
61                 if len (columns) <> cols:
62                                 print i
63                                 raise 'not enough cols'
64
65         footer (html)
66         html.close ()
67
68
69 def convert_tex (lines, outname, cols, separator, linesep):
70         html = open(outname, 'w')
71         header = r"""\documentclass{article}
72 \begin{document}
73 {\parindent -1pc
74         \parskip 0pc\parsep 0pc
75         %  COMMENT( from the texbook)
76         \def\length#1{\count0=0 \getlength#1\end}
77         \def\getlength#1{\ifx#1\end \let\next=\relax
78           \else\advance\count0 by1 \let\next=\getlength\fi \next}
79           \def\inlanguage#1#2{{\length{#2}%
80                 \ifnum\count0=0
81                 \else
82                 \emph{#1}: #2.
83                 \fi}}
84         \small
85
86         \def\tableentry#1#2#3#4#5#6#7{\par{\bf #1}: #7
87           \inlanguage{Fran\c cais}{#2}
88            \inlanguage{British}{#4}  \inlanguage{Deutsch}{#3}
89            \inlanguage{Nederlands}{#5}\inlanguage{Italiano}{#6}}
90 """
91
92         html.write (header)
93         i = 0
94         for line in lines:
95                 i = i + 1
96                 if not len(line):
97                         continue
98                 columns = string.split (line, separator)
99                 if len (columns) <> cols:
100                                 print i
101                                 raise 'not enough cols'
102
103                 tex_line =  '\\tableentry{' + string.join (columns, '}{') + '}\n'
104                 tex_line = re.sub (linesep, ' ', tex_line)
105                 html.write (tex_line)
106                 
107         html.write(r"""}\end{document}""")
108         html.close ()
109
110 def convert_texinfo (lines, outname, cols, separator, linesep):
111                 pass
112
113
114
115 identify ()
116 (options, files) = getopt.getopt (
117         sys.argv[1:], 'f:tl:o:h:c:s:', ['columns=', 'help', 'format=', 'output=', 'separator=', 'linesep='])
118 latex = 0
119 separator = '@'
120 output = ''
121 linesep = '\r'
122 for opt in options:
123         o = opt[0]
124         a = opt[1]
125         if o == '--separator' or o == '-s':
126                 separator = a
127         elif o== '--help' or o == '-h':
128                 help ()
129         elif o=='--format' or o == '-f':
130                 format = a
131         elif o == '--output' or o == '-o':
132                 output = a
133         elif o == '--linesep' or o == '-l':
134                 linesep = a
135         elif o == '--columns' or o == '-c':
136                 cols =  string.atoi(a)
137         else:
138                 print o
139                 raise getopt.error
140
141 lines = open (files[0]).readlines ()
142
143 if format == 'latex':
144         convert_tex (lines, output,  cols, separator, linesep)
145 elif format == 'html':
146         convert_html (lines, output, cols, separator, linesep)
147 elif format == 'texi':
148         convert_texinfo (lines, output, cols, separator, linesep)
149