]> git.donarmstrong.com Git - lilypond.git/blob - stepmake/bin/ls-latex.py
release: 1.3.7
[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                 print 'title found! '
90                 header.title = m.group (1)
91         m = re.search( '@author (.*)\n', s)
92         if m:
93                 header.author = m.group (1)
94         
95         header.formats = ['html', 'ps.gz']
96         return header
97
98 # urg
99 # should make a 'next_parens '
100 yo_article_re = re.compile ('article(\\([^)]*\\))[ \t\n]*(\\([^)]*\\))')
101 yo_report_re = re.compile ('report(\\([^)]*\\))[\t\n ]*(\\([^)]*\\))')
102 yo_sect_re  =  re.compile ('sect(\\([^)]*\\))')
103 yo_chapter_re  =  re.compile ('chapter(\\([^)]*\\))')
104
105 def read_yodl_header (s):
106         header = Latex_head ()
107         report = yo_report_re.search (s)
108         article = 0
109         sect = 0
110         chapter = 0
111         if report:
112                 header.author = report.group (2)
113                 header.title = yo_report_re.group (1)
114         else:
115                 article = yo_article_re.search (s)
116                 if article:
117                         header.author = article.group (2)
118                         header.title = article.group (1)
119                 else:
120                         chapter = yo_chapter_re.search (s)
121                         if chapter:
122                                 header.title = chapter.group (1)
123                         else:
124                                 sect = yo_sect_re.search (s)
125                                 if sect:
126                                         header.title = sect.group (1)
127
128         header.formats = ['html']
129         return  header
130
131
132 def print_html_head (l,o,h):
133         pre =o
134         
135         fn = pre + h.basename
136
137         t = h.filename 
138         if h.title :
139                 t = t + ': '+ h.title
140
141         l.write ('<li>%s </a>' % t)
142
143         if h.author:
144                 l.write ('<p>by %s</p>' % h.author)
145
146         for f in h.formats:
147                 l.write ('(<a href=%s.%s>%s</a>)' % (fn, f, format_names [f]))
148         l.write ('</li>\n')
149
150 def help ():
151         sys.stdout.write (r"""Usage: ls-latex [OPTION]... FILE...
152 Generate html index file for FILE...
153
154 Options:
155 -h, --help                print this help
156 """)
157         sys.exit (0)
158
159 import getopt
160
161 (options, files) = getopt.getopt(sys.argv[1:], 
162         'e:h', ['help', 'prefix=',  'title='])
163
164 tex = ''
165 output =''
166 pre = ''
167 title = ''
168 for opt in options:
169         o = opt[0]
170         a = opt[1]
171         if o == '--prefix':
172                 pre = a
173         elif o == '--title':
174                 title = a  
175         elif o == '-h' or o == '--help':
176                         help ()
177
178
179 l = sys.stdout
180
181 l.write (r"""<html><title>%s</title>
182 <body>
183 <h1> %s</h1><ul>
184 """ % (title, title))
185
186
187 read_header_funcs = {
188         'pod' : read_pod_header,
189         'tex' : read_latex_header,
190         'doc' : read_latex_header,
191         'bib': read_bib_header, 
192         'latex' : read_latex_header,
193         'tely' : read_texinfo_header,
194         'texi': read_texinfo_header,
195         'yo': read_yodl_header, 
196 }       
197
198
199 for x in files:
200         m = re.search ('\\.([^.]*)$', x)
201         if m == None:
202                 continue
203
204         s = gulp_file (x)
205         head = read_header_funcs [m.group(1)] (s)
206
207         head.filename = x
208         head.basename = re.sub ("\\.[^.]+", '', x)
209         
210         print_html_head (l, pre, head)
211
212 l.write ('</ul></body></html>')
213