]> www.ginac.de Git - ginac.git/blob - ginsh/ginsh_fcn_help.py
Add LaTeX pretty-print for function derivatives.
[ginac.git] / ginsh / ginsh_fcn_help.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 # Convert help for ginsh functions from man page to C source
4 import sys, re, optparse
5
6 rxStart = re.compile('^.*GINSH_FCN_HELP_START$')
7 rxEnd = re.compile('^.*GINSH_FCN_HELP_END$')
8 fcnRx = re.compile('^[.]BI\s+')
9 hlpRx = re.compile('\\\\[-]')
10 codeFmt = 'insert_help("%s",\n"%s"\n);\n'
11
12 def parseProto(pStr):
13         pStr = fcnRx.sub('', pStr)
14         pStr = re.sub('\s', '', pStr)
15         pStr = re.sub('"', '', pStr)
16         pStr = re.sub(',', ', ', pStr)
17         pStr = re.sub('\\[', ' [', pStr)
18         name = pStr.split('(')[0]
19         return name, pStr
20
21 def extractHelp(inp, out):
22         name, proto, synopsis = None, None, None
23         seenStart = False
24         for l in inp:
25                 l = l.strip()
26                 if not seenStart:
27                         if rxStart.match(l):
28                                 seenStart = True
29                         continue
30                 if rxEnd.match(l):
31                         break
32                 if fcnRx.match(l):
33                         name, proto = parseProto(l)
34                 elif hlpRx.match(l):
35                         l = hlpRx.sub('', l).strip()
36                         l = re.sub('"', "'", l)
37                         synopsis = '%s"\n" - %s' % ( proto, l )
38                 elif l.lower() == '.br':
39                         synopsis = synopsis or proto
40                         out.write(codeFmt % ( name, synopsis ))
41                         name, proto, synopsis = None, None, None
42
43 def main():
44         op = optparse.OptionParser()
45         op.add_option('-o', dest = 'outfile')
46         options, args = op.parse_args()
47         outfile = sys.stdout
48         infile = sys.stdin
49         if not options.outfile is None:
50                 outfile = open(options.outfile, 'wt')
51         if len(args) >= 1:
52                 infile = open(args[0])
53         extractHelp(infile, outfile)
54         if infile != sys.stdin:
55                 infile.close()
56         outfile.flush()
57         if outfile != sys.stdout:
58                 outfile.close()
59
60 if __name__ == '__main__':
61         main()
62         sys.exit(0)
63