]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/texi2omf.py
new file.
[lilypond.git] / buildscripts / texi2omf.py
1 import time
2 import re
3 import sys
4 import getopt
5 import os
6
7 def usage ():
8         sys.stderr.write ('''
9 texi2omf [options] texifile
10
11 Options:
12
13 --format=FORM         format is FORM. Supported: HTML, PS, PDF
14 --location=FILE       path to file on disk.
15 --version=VERSION
16
17 Use the following commands (enclose in @ignore)
18
19 @omfsubject . .
20 @omfdescription . .
21 @omftype . . 
22
23 etc.
24
25
26 ''')
27         
28 (options, files) = getopt.getopt (sys.argv[1:], '',
29                                   ['format=', 'location=', 'version='])
30
31 license = 'FDL'
32 location = ''
33 version = ''
34 email = os.getenv ('MAILADDRESS')
35 name = os.getenv ('USERNAME')
36
37 for (o,a) in options:
38         if o == '--format':
39                 format = a
40         elif o == '--location':
41                 location = 'file:%s' % a
42         elif o == '--version':
43                 version = a
44         else:
45                 assert 0
46
47                 
48 if not files:
49         usage()
50         sys.exit (2)
51
52
53 formats = {
54         'html' : 'text/html',
55         'pdf' : 'application/pdf',
56         'ps' : 'application/postscript',
57         }
58
59 if not formats.has_key (format):
60         sys.stderr.write ("Format `%s' unknown\n" % format)
61         sys.exit (1)
62
63
64 infile =files[0]
65
66 today = time.localtime()
67
68 texi = open (infile).read()
69
70 if not location:
71         location = 'file:%s' % re.sub (r'\.*', '.html', infile)
72         
73
74 omf_vars = {
75         'date': '%d-%d-%d' %  today[:3],
76         'mimeformat': formats[format],
77         'maintainer':  "%s (%s)" % (name, email),
78         'version' : version,
79         'location' : location, 
80         }
81
82 for a in ['subject','creator', 'title', 'subtitle', 'version', 'category', 'type',
83           'description',  'license']:
84         
85         m = re.search ('@omf%s (.*)\n'% a, texi)
86         if m:
87                 omf_vars[a] = m.group (1)
88         elif not omf_vars.has_key (a):
89                 omf_vars[a] = ''
90                 
91 if not omf_vars['title']:
92         title = ''
93         m = re.search ('@title (.*)\n', texi)
94         if m:
95                 title = m.group (1)
96
97         subtitle = ''
98         m = re.search ('@subtitle (.*)\n', texi)
99         if m:
100                 subtitle = m.group (1)
101
102         if subtitle:
103                 title  = '%s -- %s' % (title, subtitle)
104
105         omf_vars['title'] = title
106         
107 if not omf_vars['creator']:
108         m = re.search ('@author (.*)\n', texi)
109         if m:
110                 omf_vars['creator'] = m.group (1)
111
112
113
114 print r'''
115 <?xml version="1.0" encoding="UTF-8"?>
116 <!DOCTYPE omf PUBLIC "-//OMF//DTD Scrollkeeper OMF Variant V1.0//EN" "http://scrollkeeper.sourceforge.net/dtds/scrollkeeper-omf-1.0/scrollkeeper-omf.dtd">
117 <omf>
118   <resource>
119     <creator>
120       %(creator)s
121     </creator>
122     <maintainer>
123       %(maintainer)s
124     </maintainer>
125     <title>
126       %(title)s
127     </title>
128     <date>
129       %(date)s
130     </date>
131     <version identifier="%(version)s" />
132     <subject category="%(category)s"/>
133     <description>
134     %(description)s
135     </description>
136     <type>
137     %(type)s
138     </type>
139     <format mime="%(mimeformat)s">
140     <identifier url="%(location)s"/>
141     <language code="C"/>
142     <rights type="%(license)s" />
143   </resource>
144 </omf>
145 ''' % omf_vars
146
147