-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_to_wiki.py
executable file
·164 lines (104 loc) · 4.68 KB
/
convert_to_wiki.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#! /usr/bin/python
from optparse import OptionParser
import re
parser = OptionParser()
( options, args ) = parser.parse_args()
infilename = "wiki.wtex"
outfilename = None
if len( args ) > 0 :
infilename = args[ 0 ]
if len( args ) > 1 :
outfilename = args[ 1 ]
f = open( infilename, 'r' )
text = f.read()
f.close()
# <wiki>text</wiki> is put into the wiki only
text = re.sub( r"<wiki>\n?(.*?)</wiki>\n?", r"\1", text, flags = re.DOTALL)
# <latex>text</latex> is put into the latex only
text = re.sub( r"<latex>\n?(.*?)</latex>\n?", r"", text, flags = re.DOTALL)
# \section{x} to == x == etc.
text = re.sub( r"\\section{(.*?)}", r"== \1 ==", text, flags = re.DOTALL)
text = re.sub( r"\\subsection{(.*?)}", r"=== \1 ===", text, flags = re.DOTALL)
text = re.sub( r"\\subsubsection{(.*?)}", r"==== \1 ====", text, flags = re.DOTALL)
# itemize
def itemize(match):
return re.sub( r"^\s*\\item", "*", match.group(1), flags = re.MULTILINE)
text = re.sub( r"\\begin{itemize}(.*?)\\end{itemize}", itemize, text, flags = re.DOTALL)
# remove verbatim
def verbatim(match):
return "\n" + re.sub( r"^", " ", match.group(1), flags = re.MULTILINE) + "\n"
text = re.sub( r"\\begin{Verbatim}\n(.*?)\n\\end{Verbatim}", verbatim, text, flags = re.DOTALL)
# remove %comments
text = re.sub( r"^%(.*?)\n", r"", text, flags = re.DOTALL | re.MULTILINE)
text = re.sub( r"([^\\])%(.*?)\n", r"\1\n", text, flags = re.DOTALL)
# ``...'' to "..."
text = re.sub( r"``(.*?)''", r'"\1"', text, flags = re.DOTALL)
# \emph{x} to ''x''
text = re.sub( r"\\emph{(.*?)}", r"''\1''", text, flags = re.DOTALL)
# $x$ to ''x''
text = re.sub( r"\$([^$\n]*?)\$", r"''\1''", text, flags = re.DOTALL)
# \texttt{x} to <tt>x</tt>
text = re.sub( r"\\texttt{(.*?)}", r'<tt style="font-size: 1.25em;">\1</tt>', text, flags = re.DOTALL)
# \dots to ...
text = re.sub( r"\\dots", r"...", text, flags = re.DOTALL)
# \times to ×
text = re.sub( r"\\times\s", r"×", text, flags = re.DOTALL)
# remove \\
text = re.sub( r"\\\\", r"", text, flags = re.DOTALL)
# remove \noindent
text = re.sub( r"\\noindent\n?", r"", text, flags = re.DOTALL)
# \wikilink{...}{...} to [[...|'''...''']]
text = re.sub( r"\\wikilink{(.*?)}{(.*?)}", r"[[\1|'''\2''']]", text, flags = re.DOTALL)
# \keys{...} to {{key press|...}}
def keys(match):
t = match.group(1);
t = re.sub( r"\\cmd", r"Cmd", t );
t = re.sub( r"\\ctrl", r"Ctrl", t );
t = re.sub( r"\\shift", r"Shift", t );
t = re.sub( r"\\arrowkeyup", r"Up", t );
t = re.sub( r"\\arrowkeydown", r"Down", t );
t = re.sub( r"\\arrowkeyleft", r"Left", t );
t = re.sub( r"\\arrowkeyright", r"Right", t );
t = re.sub( r"\s*\+\s*", r"|", t );
t = re.sub( r"LATEXGROUPEDPLUS", r"+", t );
return r"KEYPRESSOPENkey press|" + t + "KEYPRESSCLOSE"
text = re.sub( r"{\+}", r"LATEXGROUPEDPLUS", text, flags = re.DOTALL)
text = re.sub( r"\\keys{(.*?)}", keys, text, flags = re.DOTALL)
text = re.sub( r"LATEXGROUPEDPLUS", r"{+}", text, flags = re.DOTALL)
# \menu{x} to {{bc | ...}}
def menu(match):
t = match.group(1);
t = re.sub( r"\s*\>\s*", r" | ", t );
return r"KEYPRESSOPENbc | " + t + "KEYPRESSCLOSE"
text = re.sub( r"\\menu{(.*?)}", menu, text, flags = re.DOTALL)
# \button{x} to ''x''
text = re.sub( r"\\button{(.*?)}", r"''\1''", text, flags = re.DOTALL)
# \& to &
text = re.sub( r"\\&", r"&", text, flags = re.DOTALL)
# \% to %
text = re.sub( r"\\%", r"%", text, flags = re.DOTALL)
# tables
text = re.sub( r"^\s*\\tablecell(\[.*?\])?{(.*?)}", r'| style="padding: 5px;" | \2', text, flags = re.DOTALL | re.MULTILINE)
text = re.sub( r"\\specialcell{(.*?)}", r"\1", text, flags = re.DOTALL)
text = re.sub( r"^\s*\\hline", r"|-", text, flags = re.DOTALL | re.MULTILINE)
# latex macros
text = re.sub( r"\\ie", r"i.e.", text, flags = re.DOTALL)
text = re.sub( r"\\eg", r"e.g.", text, flags = re.DOTALL)
text = re.sub( r"\\Ie", r"I.e.", text, flags = re.DOTALL)
text = re.sub( r"\\Eg", r"E.g.", text, flags = re.DOTALL)
text = re.sub( r"\\etc", r"etc", text, flags = re.DOTALL)
text = re.sub( r"\\bdv", r"BigDataViewer", text, flags = re.DOTALL)
text = re.sub( r"\\Bdv", r"BigDataViewer", text, flags = re.DOTALL)
text = re.sub( r"\\bds", r"BigDataServer", text, flags = re.DOTALL)
text = re.sub( r"\\Bds", r"BigDataServer", text, flags = re.DOTALL)
text = re.sub( r"\\screenshotA{(.*?)}", r"[[Image:bdv-\1]]", text, flags = re.DOTALL)
text = re.sub( r"\\screenshotB{(.*?)}", r"[[Image:bdv-\1|500px]]", text, flags = re.DOTALL)
text = re.sub( r"\\screenshotTikz{(.*?)}", r"[[Image:bdvTikz-\1.png]]", text, flags = re.DOTALL)
# clean up
text = re.sub( r"KEYPRESSOPEN(.*?)KEYPRESSCLOSE", r"{{\1}}", text, flags = re.DOTALL)
if outfilename is None:
print text
else:
f = open( outfilename, 'w' )
f.write( text )
f.close()