lilypond-1.3.121
[lilypond.git] / bin / ls-latex.py
blobef9977a20709f07e09e086c12897c14aa9b5664c
1 #!@PYTHON@
3 import sys
4 import os
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)
13 lilypath = lilypath + '/bin/'
14 sys.path.append(lilypath)
16 from lilypython import *
17 import __main__
18 import glob
22 latex_author_re = regex.compile('\\author{\([^}]+\)}')
23 latex_title_re = regex.compile('\\title{\([^}]+\)}')
25 class Latex_head:
26 def __init__ (self):
27 self.author = ''
28 self.title = ''
29 self.date = ''
30 self.site = ''
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?'
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
54 bib_author_re = regex.compile('% *AUTHOR *= *\(.*\)$')
55 bib_title_re = regex.compile('% *TITLE *= *\(.*\)$')
57 def bib_header (fn):
58 s = gulp_file (fn)
59 if bib_author_re.search (s) == -1 :
60 raise 'huh?'
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
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
88 def print_html_head (l,o,h):
89 pre =o
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')
97 import getopt
99 (cl_options, files) = getopt.getopt(sys.argv[1:],
100 'e:h', ['help', 'prefix='
101 , 'title='])
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
115 l = sys.stdout
117 l.write ('<html><title>%s</title><h1> %s</h1><ul>\n' % (title, title))
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>')