3 # table-to-html.py -- convert char-separated table to html table
5 # source file of the GNU LilyPond music typesetter
7 # (c) 1998 Jan Nieuwenhuizen <janneke@gnu.org>
10 name = 'table-to-html'
22 return name + ' version ' + version;
25 sys.stdout.write (program_id () + '\n')
29 r"""Usage: table-to-html [OPTION]... TABLE-FILE HTML-FILENAME
30 Generate pretty table from char separated table
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
41 html.write ('<body bgcolor=white><table cellspacing=10>')
44 html.write ('</table></body>')
46 def convert_html (lines, outname, cols, separator, linesep):
48 html = open (outname, 'w')
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)
61 if len (columns) <> cols:
63 raise 'not enough cols'
69 def convert_tex (lines, outname, cols, separator, linesep):
70 html = open(outname, 'w')
71 header = r"""\documentclass{article}
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}%
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}}
98 columns = string.split (line, separator)
99 if len (columns) <> cols:
101 raise 'not enough cols'
103 tex_line = '\\tableentry{' + string.join (columns, '}{') + '}\n'
104 tex_line = re.sub (linesep, ' ', tex_line)
105 html.write (tex_line)
107 html.write(r"""}\end{document}""")
110 def convert_texinfo (lines, outname, cols, separator, linesep):
116 (options, files) = getopt.getopt (
117 sys.argv[1:], 'f:tl:o:h:c:s:', ['columns=', 'help', 'format=', 'output=', 'separator=', 'linesep='])
125 if o == '--separator' or o == '-s':
127 elif o== '--help' or o == '-h':
129 elif o=='--format' or o == '-f':
131 elif o == '--output' or o == '-o':
133 elif o == '--linesep' or o == '-l':
135 elif o == '--columns' or o == '-c':
136 cols = string.atoi(a)
141 lines = open (files[0]).readlines ()
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)