]> git.donarmstrong.com Git - lilypond.git/blob - stepmake/bin/ls-latex.py
(INSTALL): Assign INSTALL_PY
[lilypond.git] / stepmake / bin / ls-latex.py
1 #!@PYTHON@
2
3 # name isn't really appropriate now ...
4
5 name = 'ls-latex'
6 version = '0.1'
7
8 import sys
9 import os
10 import string
11 import __main__
12 import glob
13 import re
14
15 format_names  = {'ps.gz': 'Compressed PostScript',
16                                  'html' : 'HTML'
17                                  }
18
19 def gulp_file(f):
20         try:
21                 i = open(f)
22                 i.seek (0, 2)
23                 n = i.tell ()
24                 i.seek (0,0)
25         except:
26                 sys.stderr.write ("can't open file: %s\n" % f)
27                 return ''
28         s = i.read (n)
29         if len (s) <= 0:
30                 sys.stderr.write ("gulped empty file: %s\n" % f)
31         i.close ()
32         return s
33
34 class Latex_head:
35         def __init__ (self):
36                 self.author = ''
37                 self.title = ''
38                 self.date = ''
39                 self.format = ''
40                 
41 def read_latex_header (s):
42         header = Latex_head()
43         m = re.search(r'\\author{([^}]+)}', s)
44         if m:
45                 header.author = m.group (1)
46
47         m = re.search (r'\\title{([^}]+)}',s )
48         if m:
49                 header.title = m.group (1)
50
51         header.formats = ['ps.gz']
52         return  header
53
54
55 def read_bib_header (s):
56         m = re.search ('% *AUTHOR *= *(.*)\n',s)
57
58         header = Latex_head()
59
60         if m:
61                 header.author = m.group (1)
62
63         m = re.search ('% *TITLE *= *(.*)\n',s )
64         if m:
65                 header.title = m.group (1)
66
67         header.formats = ['html']
68         return header
69
70
71 def read_pod_header (s):
72         header = Latex_head ()
73
74         i = re.search( '[^\n \t]', s)
75         s = s[i:]
76         i = re.search( '\n\n', s)
77         s = s[i+2:]     
78         i = re.search( '\n\n', s)
79         header.title = s[:i]
80
81         header.formats = ['html']
82         return  header
83
84 def read_texinfo_header (s):
85         header = Latex_head ()
86
87         m = re.search( '@settitle (.*)\n', s)
88         if m:
89                 header.title = m.group (1)
90         m = re.search( '@author (.*)\n', s)
91         if m:
92                 header.author = m.group (1)
93         
94         header.formats = ['html', 'ps.gz']
95         return header
96
97 # urg
98 # should make a 'next_parens '
99 yo_article_re = re.compile ('article(\\([^)]*\\))[ \t\n]*(\\([^)]*\\))')
100 yo_report_re = re.compile ('report(\\([^)]*\\))[\t\n ]*(\\([^)]*\\))')
101 yo_sect_re  =  re.compile ('sect(\\([^)]*\\))')
102 yo_chapter_re  =  re.compile ('chapter(\\([^)]*\\))')
103
104 def read_yodl_header (s):
105         header = Latex_head ()
106         report = yo_report_re.search (s)
107         article = 0
108         sect = 0
109         chapter = 0
110         if report:
111                 header.author = report.group (2)
112                 header.title = yo_report_re.group (1)
113         else:
114                 article = yo_article_re.search (s)
115                 if article:
116                         header.author = article.group (2)
117                         header.title = article.group (1)
118                 else:
119                         chapter = yo_chapter_re.search (s)
120                         if chapter:
121                                 header.title = chapter.group (1)
122                         else:
123                                 sect = yo_sect_re.search (s)
124                                 if sect:
125                                         header.title = sect.group (1)
126
127         header.formats = ['html']
128         return  header
129
130
131 def print_html_head (l,o,h):
132         pre =o
133         
134         fn = pre + h.basename
135
136         t = h.filename 
137         if h.title :
138                 t = t + ': '+ h.title
139
140         l.write ('<li>%s </a>' % t)
141
142         if h.author:
143                 l.write ('<p>by %s</p>' % h.author)
144
145         for f in h.formats:
146                 l.write ('(<a href=%s.%s>%s</a>)' % (fn, f, format_names [f]))
147         l.write ('</li>\n')
148
149 def help ():
150         sys.stdout.write (r"""Usage: ls-latex [OPTIONS]... FILE...
151 Generate html index file for FILE...
152
153 Options:
154 -h, --help                print this help
155 """)
156         sys.exit (0)
157
158 import getopt
159
160 (options, files) = getopt.getopt(sys.argv[1:], 
161         'e:h', ['help', 'prefix=',  'title='])
162
163 tex = ''
164 output =''
165 pre = ''
166 title = ''
167 for opt in options:
168         o = opt[0]
169         a = opt[1]
170         if o == '--prefix':
171                 pre = a
172         elif o == '--title':
173                 title = a  
174         elif o == '-h' or o == '--help':
175                         help ()
176
177
178 l = sys.stdout
179
180 l.write (r"""<html><title>%s</title>
181 <body>
182 <h1> %s</h1><ul>
183 """ % (title, title))
184
185
186 read_header_funcs = {
187         'pod' : read_pod_header,
188         'tex' : read_latex_header,
189         'doc' : read_latex_header,
190         'bib': read_bib_header, 
191         'latex' : read_latex_header,
192         'tely' : read_texinfo_header,
193         'texi': read_texinfo_header,
194         'yo': read_yodl_header, 
195 }       
196
197
198 for x in files:
199         m = re.search ('\\.([^.]*)$', x)
200         if m == None:
201                 continue
202
203         s = gulp_file (x)
204         head = read_header_funcs [m.group(1)] (s)
205
206         head.filename = x
207         head.basename = re.sub ("\\.[^.]+", '', x)
208         
209         print_html_head (l, pre, head)
210
211 l.write ('</ul></body></html>')
212