]> git.donarmstrong.com Git - lilypond.git/blob - bin/ls-latex.py
partial: 1.0.1.jcn
[lilypond.git] / bin / ls-latex.py
1 #!@PYTHON@
2
3 import sys
4 import os
5
6 lilypath =''
7 try:
8     lilypath = os.environ['LILYPOND_SOURCEDIR'] + '/'
9 except KeyError:
10     print 'Please set LILYPOND_SOURCEDIR to the toplevel source, eg LILYPOND_SOURCEDIR=/home/foobar/lilypond-1.2.3/'
11     sys.exit(1)
12
13 lilypath = lilypath + '/bin/'
14 sys.path.append(lilypath)
15  
16 from lilypython import *
17 import __main__
18 import glob
19
20
21
22 latex_author_re = regex.compile('\\author{\([^}]+\)}')
23 latex_title_re = regex.compile('\\title{\([^}]+\)}')
24
25 class Latex_head:
26     def __init__ (self):
27         self.author = ''
28         self.title = ''
29         self.date = ''
30         self.site = ''
31     
32
33 def read_latex_header (fn):
34     s = gulp_file (fn)
35     i = regex.search( '\\\\begin{document}', s)
36     if i < 0:
37         raise 'huh?'
38     s = s[:i]
39     s = regsub.gsub('%.*$', '', s)
40     s = regsub.gsub('\n', ' ', s)
41     if latex_author_re.search (s) == -1 :
42         raise 'huh?'
43
44     header = Latex_head()
45     header.filename= fn;
46     header.author = latex_author_re.group (1)
47     if latex_title_re.search (s) == -1:
48         raise 'huh?'
49     header.title = latex_title_re.group (1)
50     header.outfile = regsub.gsub ('\.doc+$', '.ps.gz', fn)
51     return  header
52
53
54 bib_author_re = regex.compile('% *AUTHOR *= *\(.*\)$')
55 bib_title_re = regex.compile('% *TITLE *= *\(.*\)$')
56
57 def bib_header (fn):
58     s = gulp_file (fn)
59     if bib_author_re.search (s) == -1 :
60         raise 'huh?'
61
62     header = Latex_head()
63     header.filename= fn;
64     header.author = bib_author_re.group (1)
65     if bib_title_re.search (s) == -1:
66         raise 'huh?'    
67     header.title = bib_title_re.group (1)
68     header.outfile = regsub.gsub ( '\.bib$', '.html' , fn)
69     return header
70
71
72 def read_pod_header (fn):
73     header = Latex_head ()
74     s = gulp_file (fn)
75     i = regex.search( '[^\n \t]', s)
76     s = s[i:]
77     i = regex.search( '\n\n', s)
78     s = s[i+2:]    
79     if i < 0:
80         raise 'huh?'
81     i = regex.search( '\n\n', s)
82     header.title = s[:i]
83     header.filename = fn
84     header.outfile = regsub.gsub ('\.pod$', '.html', fn)
85     return  header
86
87
88 def print_html_head (l,o,h):
89     pre =o
90
91     l.write ('<li><a href=%s>%s</a>' % (pre + h.outfile, h.title ))
92     if h.author:
93         l.write ('<p>by %s</p>' % h.author)
94     l.write ('</li>\n')
95
96
97 import getopt
98
99 (cl_options, files) = getopt.getopt(sys.argv[1:], 
100                                     'e:h', ['help', 'prefix=' 
101                                             , 'title='])
102
103 tex = ''
104 output =''
105 pre = ''
106 title = ''
107 for opt in cl_options:
108     o = opt[0]
109     a = opt[1]
110     if o == '--prefix' or o == '-p':
111         pre = a
112     if o == '--title' or o == '-t':
113         title = a  
114
115 l = sys.stdout
116
117 l.write ('<html><title>%s</title><h1> %s</h1><ul>\n' % (title, title))
118
119
120 for x in files:
121     if regex.search ('\\.bib$', x) <> -1:
122         head = bib_header (x)
123     elif regex.search ('\\.pod$', x) <> -1:
124         head = read_pod_header (x)
125     else:
126         head = read_latex_header (x)
127     print_html_head (l, pre, head)
128 l.write ('</ul></html>')