]> git.donarmstrong.com Git - lilypond.git/blob - stepmake/bin/ls-latex.py
release: 1.2.13
[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
12 def gulp_file (fn):
13         f = open (fn)
14         return f.read ()
15
16 import __main__
17 import glob
18
19 import re
20
21 format_names  = {'ps.gz': 'Compressed PostScript',
22                  'html' : 'HTML'
23                  }
24
25 class Latex_head:
26     def __init__ (self):
27         self.author = ''
28         self.title = ''
29         self.date = ''
30         self.format = ''
31         
32 def read_latex_header (s):
33     header = Latex_head()
34     m = re.search(r'\\author{([^}]+)}', s)
35     if m:
36         header.author = m.group (1)
37
38     m = re.search (r'\\title{([^}]+)}',s )
39     if m:
40         header.title = m.group (1)
41
42     header.formats = ['ps.gz']
43     return  header
44
45
46 def read_bib_header (s):
47
48     m = re.search ('% *AUTHOR *= *(.*)\n',s)
49
50     header = Latex_head()
51
52     if m:
53         header.author = m.group (1)
54
55     m = re.search ('% *TITLE *= *(.*)\n',s )
56     if m:
57         header.title = m.group (1)
58
59     header.formats = ['html']
60     return header
61
62
63 def read_pod_header (s):
64     header = Latex_head ()
65
66     i = re.search( '[^\n \t]', s)
67     s = s[i:]
68     i = re.search( '\n\n', s)
69     s = s[i+2:]    
70     i = re.search( '\n\n', s)
71     header.title = s[:i]
72
73     return  header
74
75 def read_texinfo_header (s):
76     header = Latex_head ()
77
78     m = re.search( '@settitle (.*$)', s)
79     if m:
80         header.title = m.group (1)
81
82     header.formats = ['html', 'ps.gz']
83     return header
84
85
86
87 def print_html_head (l,o,h):
88     pre =o
89
90     
91     fn = pre + h.basename
92
93     t = h.filename 
94     if h.title :
95         t = t + ': '+ h.title
96
97     l.write ('<li>%s </a>' % t)
98
99     if h.author:
100         l.write ('<p>by %s</p>' % h.author)
101
102     for f in h.formats:
103         l.write ('(<a href=%s.%s>%s</a>)' % (fn, f, format_names [f]))
104     l.write ('</li>\n')
105
106 def help ():
107     sys.stdout.write ("Usage: ls-latex [OPTION]... FILE...\n"
108                  "Generate html index file for FILE...\n\n"
109                  + "Options:\n"
110                  + "  -h, --help             print this help\n"
111                       )
112     sys.exit (0)
113
114 import getopt
115
116 (options, files) = getopt.getopt(sys.argv[1:], 
117     'e:h', ['help', 'prefix=',  'title='])
118
119 tex = ''
120 output =''
121 pre = ''
122 title = ''
123 for opt in options:
124     o = opt[0]
125     a = opt[1]
126     if o == '--prefix':
127         pre = a
128     elif o == '--title':
129         title = a  
130     elif o == '-h' or o == '--help':
131         help ()
132
133
134 l = sys.stdout
135
136 l.write (r"""<html><title>%s</title>
137 <body>
138 <h1> %s</h1><ul>
139 """ % (title, title))
140
141
142 read_header_funcs = {
143     'pod' : read_pod_header,
144     'tex' : read_latex_header,
145     'doc' : read_latex_header,
146     'bib': read_bib_header, 
147     'latex' : read_latex_header,
148     'tely' : read_texinfo_header,
149     'texi': read_texinfo_header,
150 }    
151
152
153 for x in files:
154     m = re.search ('\\.([^.]*)$', x)
155     if m == None:
156         continue
157
158     s = gulp_file (x)
159     head = read_header_funcs [m.group(1)] (s)
160
161     head.filename = x
162     head.basename = re.sub ("\\.[^.]+", '', x)
163     
164     print_html_head (l, pre, head)
165
166 l.write ('</ul></body></html>')
167