-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdocstrings2rest.py
More file actions
40 lines (32 loc) · 1.45 KB
/
docstrings2rest.py
File metadata and controls
40 lines (32 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'''
Converts the math in the docstrings created with doxy2swig in reSt math
philipp kraft
'''
from __future__ import division, print_function
import re
import sys
from traceback import format_exc as traceback
if len(sys.argv)<2:
sys.stderr.write("Usage: python docstrings2rest.py docstrings.i >docstrings.i")
exit(1)
# Regular expression to find inline math
patterninline =re.compile( r'\$(.*?)\$',flags=re.DOTALL)
# Regular expression to find block math
patternblock = re.compile(r'\\\\\\\\\[(.*?)\\\\\\\\\]\s*',flags=re.DOTALL)
# Regular expression to find eqnarray math
patternblock2 = re.compile(r'\\\\\\\\begin\{eqnarray\*\}(.*?)\\\\\\\\end\{eqnarray\*\}',flags=re.DOTALL)
# function to replace inline math with reSt syntax
def replinline(matchobj):
# Convert all white space in inline to single space, remove outer white space
# and remove whitespace before {. Otherwise reSt cannot handle the math.
res = (' '.join(matchobj.group(1).split())).strip().replace(' {','{')
return ':math:`' + res + '`'
# function to replace block math with reSt syntax
def replblock(matchobj):
return '\n\n.. math::\n\n ' + '\n '.join(matchobj.group(1).split('\n')) + '\n\n'
# Read text from file
text = open(sys.argv[1]).read()
text = re.sub(patternblock2, replblock, text)
text = re.sub(patternblock, replblock, text)
text = re.sub(patterninline, replinline, text)
print(text)