-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmm2doku.py
executable file
·273 lines (236 loc) · 9.5 KB
/
mm2doku.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/python
# -*- coding: utf-8 -*-
# ----------------------------------------------------
# Name : mm2doku
# Author : Bruno Valentin ([email protected])
# date : 10/08/2016
# Revision : 1.0
# Purpose : Convert freemind maps to dokuwiki
# ----------------------------------------------------
import re,sys
all_lines=list() #global array containing all the lines for mm file
#-------------------------
def usage():
"""Usage"""
print """Usage: mm2dokuwiki.py freemind_map.mm output_file.txt
"""
#-------------------------
def get_rid_of_html(mystring):
"""replace HTML characters in strings"""
html_table = {"&":"&",""":'"',"'":"'",">":">",
"<":"<", "
":"\n","	":"\t","§":"§",
"À":"À","Á":"Á","Â":"Â","Ã":"Ã",
"Ä":"Ä","Å":"Å","Ç":"Ç","È":"È",
"É":"É","Ê":"Ê","Ë":"Ë","Ì":"Ì",
"Í":"Í","Î":"Î","Ï":"Ï","Ñ":"Ñ",
"Ò":"Ò","Ó":"Ó","Ô":"Ô","Õ":"Õ",
"Ö":"Ö","Ø":"Ø","Ù":"Ù","Ú":"Ú",
"Û":"Û","Ü":"Ü","Ÿ":"Ÿ","à":"à",
"á":"á","â":"â","ã":"ã","ä":"ä",
"å":"å","ç":"ç","è":"è","é":"é",
"ê":"ê","ë":"ë","ì":"ì","í":"í",
"î":"î","ï":"ï","ı":"ı","ñ":"ñ",
"ò":"ò","ó":"ó","ô":"ô","õ":"õ",
"ö":"ö","ø":"ø","ù":"ù","ú":"ú",
"£":"£","€":"€", "û":"û","ü":"ü",
"ÿ":"ÿ","#xb0;":"°"}
for src,dst in html_table.iteritems():
mystring=mystring.replace(src,dst)
return (mystring)
#-------------------------
def process_option(line):
"""gets the options for a node"""
options=list()
# font
if re.match("^<font", line, re.IGNORECASE):
if re.search("BOLD=\"true\"",line, re.IGNORECASE):
options.append("bold")
if re.search("ITALIC=\"true\"",line, re.IGNORECASE):
options.append("italic")
# icon
elif re.match("^<icon BUILTIN=\".+\"", line, re.IGNORECASE):
values=re.search("BUILTIN=\"(.+)\"",line, re.IGNORECASE)
options.append(values.group(1))
return(options)
#-------------------------
def export_dokuwiki(f):
"""exports dokuwiki-style output file"""
global all_lines
titles=["====== {0} ======","====== {0} ======","===== {0} =====", \
"==== {0} ====","=== {0} ===","== {0} =="]
# process all the lines
for line in all_lines:
node_id,texte,creation,modification,level,node_type,node_options=line
# Title in capital letters
if node_type=="root":
texte=texte.upper()
title=titles[level].format(get_rid_of_html(texte))
f.write("{0}\n".format(title))
# opennode
elif node_type in ['opennode']:
# This is a title
if 'forward' in node_options:
title=titles[level].format(get_rid_of_html(texte))
f.write("{0}\n".format(title))
# this is a bulletpoint
else:
# node options
if 'messagebox_warning' in node_options:
texte="{0} :!:".format(texte)
if 'yes' in node_options:
texte="{0} :!:".format(texte)
if 'help' in node_options:
texte="{0} :?:".format(texte)
if 'button_cancel' in node_options:
texte="{0} DELETEME".format(texte)
if 'idea' in node_options:
texte="{0} FIXME".format(texte)
if 'bold' in node_options:
texte="**{0}**".format(texte)
if 'italic' in node_options:
texte="//{0}//".format(texte)
# identation
for i in range(2,level):
f.write(" ")
if re.match("^script", texte, re.IGNORECASE):
script=texte.split(" ")
f.write(" * {0}".format(get_rid_of_html(script[0])))
else:
f.write(" * {0}\n".format(get_rid_of_html(texte)))
# openscript
elif node_type in ['openscript']:
f.write(" *")
# openfile
elif node_type in ['openfile']:
f.write(" *")
# bulletpoint
elif node_type=="bulletpoint":
for i in range(2,level):
f.write(" ")
f.write(" * {0}\n".format(get_rid_of_html(texte)))
# bloc/file/script
elif node_type=="bloc":
bloc_type=node_options[0]
if bloc_type=='script':
language=node_options[1]
texte=get_rid_of_html(texte)
f.write("<code {1}>{0}</code>\n".format(texte,language))
elif bloc_type=='file':
file_name=node_options[1]
language=node_options[2]
texte=get_rid_of_html(texte)
f.write("<file {2} {1}>{0}</file>\n".\
format(texte,file_name,language))
#-------------------------
def process_lines(f):
"""split each and every line and append them in all_lines[]"""
global all_lines
level=line_nb=0
lines = read_lines_from_file(f)
while line_nb < len(lines):
line=lines[line_nb]
# SINGLE LINE NODE
if re.match("^<node.+/>", line, re.IGNORECASE):
values=re.search(\
"CREATED=\"([^\"]+)\".+ID=\"([^\"]+)\".+MODIFIED=\"([^\"]+)\".+TEXT=\"([^\"]*)\"",\
line, re.IGNORECASE)
# if multiline bloc else bulletpoint
if re.match(".*
.*", values.group(4), re.IGNORECASE):
node_type="bloc"
if 'bloc_options' in locals():
node_options=bloc_options
bloc_options=[]
else:
node_type="bulletpoint"
node_options=[]
all_lines.append([ \
values.group(2),values.group(4), values.group(1), \
values.group(3),level, node_type,node_options])
# NEW NODE
elif re.match("^<node.+[^/]>", line, re.IGNORECASE):
node_options=list()
# read until opening other node or closing the current node
while not re.match("^</?node",lines[line_nb+1], re.IGNORECASE):
line_nb+=1
node_options=node_options+process_option(lines[line_nb])
# parses the line
values=re.search(\
"CREATED=\"([^\"]+)\".+ID=\"([^\"]+)\".+MODIFIED=\"([^\"]+)\".+TEXT=\"([^\"]*)\"",\
line, re.IGNORECASE)
text_node=values.group(4)
# type bloc
if re.match(".*
.*", text_node, re.IGNORECASE):
node_type="bloc"
if 'bloc_options' in locals():
node_options=bloc_options
bloc_options=[]
# type script
elif re.match("^script", text_node, re.IGNORECASE):
node_type="openscript"
# keeps script options for the next bloc in bloc_options
bloc_options=text_node.split(" ")
if len(bloc_options) < 2:
bloc_options.append("raw")
# type file
elif re.match("^file", text_node, re.IGNORECASE):
node_type="openfile"
# keeps file options for the next bloc in bloc_options
bloc_options=text_node.split(" ")
if len(bloc_options) < 2:
bloc_options.append("file")
if len(bloc_options) < 3:
bloc_options.append("raw")
# root node
elif level==0:
node_type="root"
# opennode
else:
node_type="opennode"
all_lines.append([\
values.group(2),text_node,values.group(1),\
values.group(3),level,node_type,node_options])
level+=1
# END OF CURRENT NODE
elif re.match("^</node>", line, re.IGNORECASE):
level-=1
line_nb+=1
#-------------------------
def read_lines_from_file(f):
"""parses signe-line files"""
lignes=f.readlines()
nblines=len(lignes)
if len(lignes)==1:
final_lines=lignes[0].replace("><", ">\n<").split("\n")
else:
final_lines=lignes
return final_lines
#-------------------------
def dump_all_lines():
"""Display all the lines from the file"""
for line in all_lines:
print line
#-------------------------
def main():
if not len(sys.argv) == 3 or sys.argv[1]=="--help":
usage()
else:
del sys.argv[0]
input_file,output_file=sys.argv
# Imports data from freemind map
try:
ifh = open(input_file, 'r')
except Exception as e:
print "The input file does not exist"
else:
process_lines(ifh)
ifh.close
# Exports data to dokuwiki file
try:
ofh = open(output_file, 'w')
except Exception as e:
print "problem creating the destination file"
else:
export_dokuwiki(ofh)
ofh.close
if __name__ == '__main__':
main()