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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
|
#!/usr/bin/env python
import sys, signal
import xml.parsers.expat
root="source/"
titles = []
# list of elements that we can directly convert to wiki text
replace_element = \
{'start':{'emph': "'''",
'comment': "<!-- "
},
'end': {'emph': "'''",
'comment': " -->"
}
}
replace_paragraph_role = \
{'start':{'code': '<code>',
'codeintip': '<code>',
'example': '<code>',
'heading1': '= ',
'heading2': '== ',
'heading3': '=== ',
'heading4': '==== ',
'heading5': '===== ',
'heading6': '====== ',
'head1': '= ', # used only in one file, probably in error?
'head2': '== ', # used only in one file, probably in error?
'listitem': '',
'note': '{{Note|',
'paragraph': '',
'related': '', # used only in one file, probably in error?
'relatedtopics': '', # used only in one file, probably in error?
'tablecontent': '| ',
'tablehead': '! scope="col" | ',
'variable': '',
'tip': '{{Tip|',
'warning': '{{Warning|',
},
'end':{'code': '</code>\n\n',
'codeintip': '</code>\n\n',
'example': '</code>\n\n',
'heading1': ' =\n\n',
'heading2': ' ==\n\n',
'heading3': ' ===\n\n',
'heading4': ' ====\n\n',
'heading5': ' =====\n\n',
'heading6': ' ======\n\n',
'head1': ' =\n\n', # used only in one file, probably in error?
'head2': ' ==\n\n', # used only in one file, probably in error?
'listitem': '\n',
'note': '}}\n\n',
'paragraph': '\n\n',
'related': '\n\n', # used only in one file, probably in error?
'relatedtopics': '\n\n', # used only in one file, probably in error?
'tablecontent': '\n\n',
'tablehead': '\n\n',
'variable': '',
'tip': '}}\n\n',
'warning': '}}\n\n',
}
}
section_id_mapping = \
{'relatedtopics': 'RelatedTopics'}
# text snippets that we need to convert
replace_text_list = \
[["$[officename]", "{{ProductName}}"],
["%PRODUCTNAME", "{{ProductName}}"]
]
help_id_patterns = [
"HID",
"SID",
"FID",
"RID",
"TP_",
"MD_",
"FN_",
"DLG_",
"SW_",
"UID",
"basctl_",
"basic_",
"chart2_",
"dbaccess_",
"extensions_",
"filter_",
"formula_",
"fpicker_",
"framework_",
"goodies_"
]
help_file_name = ""
all_help_id_mappings = [[]]
def load_all_help_ids():
file = open("helpers/help_hid.lst")
for line in file:
ids = line.strip().upper().split(",")
if len(ids) >= 2:
all_help_id_mappings.append(ids)
def get_help_id_res2(name):
file = open("helpers/hid.lst")
for line in file:
ids = line.strip().upper().split(" ")
if len(ids) >= 2:
if ids[0] == name:
return ids[1]
# if none found
return "0"
def get_help_id(name):
name = name.strip().replace("cui_","svx_").upper()
for i in all_help_id_mappings:
if len(i) >= 2 and i[0].strip() == name:
return i[1].strip()
return get_help_id_res2(name)
def get_link_filename(link, name):
text = link
if link.find("http") >= 0:
text = name
for title in titles:
try:
if title[0].find(text) >= 0:
return title[1].strip()
except:
pass
return link
def get_link_name(link):
for title in titles:
if title[0].find(link) >= 0:
return title[2].strip()
return link
def replace_text(text):
for i in replace_text_list:
if text.find(i[0]) >= 0:
text = text.replace(i[0],i[1])
return text
def href_to_fname_id(href):
link = href.replace('"', '')
fname = link
id = ''
if link.find("#") >= 0:
fname = link[:link.find("#")]
id = link[link.find("#")+1:]
else:
sys.stderr.write('Reference without a "#" in "%s".'% link)
return [fname, id]
# Base class for all the elements
#
# self.name - name of the element, to drop the self.child_parsing flag
# self.objects - collects the child objects that are constructed during
# parsing of the child elements
# self.child_parsing - flag whether we are parsing a child, or the object
# itself
# self.parent - parent object
class ElementBase:
def __init__(self, name, parent):
self.name = name
self.objects = []
self.child_parsing = False
self.parent = parent
def start_element(self, parser, name, attrs):
pass
def end_element(self, parser, name):
if name == self.name:
self.parent.child_parsing = False
def char_data(self, parser, data):
pass
def get_curobj(self):
if self.child_parsing:
return self.objects[len(self.objects)-1].get_curobj()
return self
# start parsing a child element
def parse_child(self, child):
self.child_parsing = True
self.objects.append(child)
# construct the wiki representation of this object, including the objects
# held in self.objects (here only the text of the objects)
def get_all(self):
text = ''
for i in self.objects:
text = text + i.get_all()
return text
# for handling variables, and embedding in general
# id - the variable name we want to get
def get_variable(self, id):
for i in self.objects:
if i != None:
var = i.get_variable(id)
if var != None:
return var
return None
# embed part of another file into current structure
def embed_href(self, fname, id):
# parse another xhp
parser = XhpParser('source/' + fname, False)
var = parser.get_variable(id)
if var != None:
self.objects.append(var)
elif parser.follow_embed:
sys.stderr.write('Cannot find reference "#%s" in "%s".\n'% \
(id, fname))
class XhpFile(ElementBase):
def __init__(self):
ElementBase.__init__(self, None, None)
self.depth=1
def start_element(self, parser, name, attrs):
if name == 'section':
self.parse_child(Section(attrs, self, self.depth))
elif name == 'paragraph':
para = Paragraph(attrs, self, self.depth)
self.depth = para.depth
self.parse_child(para)
elif name == 'table':
self.parse_child(Table(attrs, self))
elif name == 'list':
self.parse_child(List(attrs, self))
elif name == 'bookmark':
self.parse_child(Bookmark(attrs, self))
elif name == 'embed' and parser.follow_embed:
(fname, id) = href_to_fname_id(attrs['href'])
self.embed_href(fname, id)
class Bookmark(ElementBase):
bookmarks_list = []
current_bookmark = ""
def __init__(self, attrs, parent):
ElementBase.__init__(self, 'bookmark', parent)
self.bookmark = ""
text = attrs['branch'].encode('ascii','replace')
# text = text.upper()
for i in help_id_patterns:
if text.find(i) >= 0:
self.bookmark = text[text.rfind("/")+1:].replace(":","_")
break
def get_all(self):
Bookmark.current_bookmark = self.bookmark
return ""
@staticmethod
def set_heading(data):
global help_file_name
if len(Bookmark.current_bookmark) > 0:
if data.find("]]") >= 0:
try:
data = data[data.find("|")+1:data.find("]]")]
except:
pass
help_id = get_help_id(Bookmark.current_bookmark)
bookmark = " { "+help_id+", \""+help_file_name+"#"+data.replace("\"","\\\"")+"\" },"
bookmark = bookmark.encode('ascii','replace')
Bookmark.bookmarks_list.append(bookmark)
Bookmark.current_bookmark = ""
@staticmethod
def save_bookmarks():
file = open("bookmarks.h","a")
for i in Bookmark.bookmarks_list:
file.write(i.encode('ascii','replace')+"\n")
file.close()
class Image(ElementBase):
def __init__(self, attrs, parent):
ElementBase.__init__(self, 'image', parent)
self.src = attrs['src']
try:
self.width = attrs['width']
self.height = attrs['height']
except:
self.width = self.height = ""
self.align = 'left'
self.alt = False
self.alttext = ""
def start_element(self, parser, name, attrs):
if name == 'alt':
self.alt = True
def end_element(self, parser, name):
ElementBase.end_element(self, parser, name)
if name == 'alt':
self.alt = False
def char_data(self, parser, data):
if self.alt:
self.alttext = self.alttext + data
def get_all(self):
wikitext = "[[Image:"+self.src+"|border|"+self.align+"|"
if len(self.width):
wikitext = wikitext + self.width+"x"+self.height+"|"
wikitext = wikitext + self.alttext+"]]"
return wikitext
def get_curobj(self):
return self
class Text:
def __init__(self, text):
self.wikitext = replace_text(text)
def get_all(self):
return self.wikitext
def get_variable(self, id):
return None
class TableCell(ElementBase):
def __init__(self, attrs, parent):
ElementBase.__init__(self, 'tablecell', parent)
def start_element(self, parser, name, attrs):
if name == 'paragraph':
self.parse_child(Paragraph(attrs, self, 0))
class TableRow(ElementBase):
def __init__(self, attrs, parent):
ElementBase.__init__(self, 'tablerow', parent)
def start_element(self, parser, name, attrs):
if name == 'tablecell':
self.parse_child(TableCell(attrs, self))
def get_all(self):
text = '|-\n' + ElementBase.get_all(self)
return text
class Table(ElementBase):
def __init__(self, attrs, parent):
ElementBase.__init__(self, 'table', parent)
def start_element(self, parser, name, attrs):
if name == 'tablerow':
self.parse_child(TableRow(attrs, self))
def get_all(self):
# + ' align="left"' etc.?
text = '{| border="1"\n' + \
ElementBase.get_all(self) + \
'|}\n\n'
return text
class ListItem(ElementBase):
def __init__(self, attrs, parent):
ElementBase.__init__(self, 'listitem', parent)
def start_element(self, parser, name, attrs):
if name == 'paragraph':
self.parse_child(Paragraph(attrs, self, 0))
def get_all(self):
text = ""
prefix = '*'
postfix = ''
if self.parent.startwith > 0:
prefix = '<li>'
postfix = '</li>'
elif self.parent.type == 'ordered':
prefix = '#'
# add the text itself
for i in self.objects:
text = text + prefix + ElementBase.get_all(self)
return text
class List(ElementBase):
def __init__(self, attrs, parent):
ElementBase.__init__(self, 'list', parent)
self.type = attrs['type']
try:
self.startwith = int(attrs['startwith'])
except:
self.startwith = 0
def start_element(self, parser, name, attrs):
if name == 'listitem':
self.parse_child(ListItem(attrs, self))
def get_all(self):
text = ""
if self.startwith > 0:
text = text + '<ol start="%d">\n'% self.startwith
text = text + ElementBase.get_all(self)
if self.startwith > 0:
text = text + '</ol>\n'
else:
text = text + '\n'
return text
class Section(ElementBase):
def __init__(self, attrs, parent, depth):
ElementBase.__init__(self, 'section', parent)
self.depth = depth
self.id = attrs['id']
def start_element(self, parser, name, attrs):
# sections can be nested
if name == 'section':
self.parse_child(Section(attrs, self, self.depth))
elif name == 'embed':
(fname, id) = href_to_fname_id(attrs['href'])
if parser.follow_embed:
self.embed_href(fname, id)
elif name == 'paragraph':
para = Paragraph(attrs, self, self.depth)
self.depth = para.depth
self.parse_child(para)
def get_all(self):
mapping = ''
try:
mapping = section_id_mapping[self.id]
except:
pass
# some of the section ids are used as real id's, some of them have
# function (like relatetopics), and have to be templatized
text = ''
if mapping != '':
text = text + '{{%s|'% mapping
text = text + ElementBase.get_all(self)
if mapping != '':
text = text + '}}\n\n'
return text
def get_variable(self, id):
var = ElementBase.get_variable(self, id)
if var != None:
return var
if id == self.id:
return self
return None
class Link(ElementBase):
def __init__(self, attrs, parent):
ElementBase.__init__(self, 'link', parent)
self.link = attrs['href']
try:
self.lname = attrs['name']
except:
self.lname = self.link[self.link.rfind("/")+1:]
# Override lname
self.default_name = self.lname
self.lname = get_link_filename(self.link, self.lname)
self.wikitext = ""
def char_data(self, parser, data):
self.wikitext = self.wikitext + data
def get_all(self):
if self.wikitext == "":
self.wikitext = self.default_name
self.wikitext = replace_text(self.wikitext)
if self.link.find("http") >= 0:
text = "["+self.link+" "+self.wikitext+"]"
else:
text = "[["+self.lname+"|"+self.wikitext+"]]"
return text
class Paragraph(ElementBase):
def __init__(self, attrs, parent, depth):
ElementBase.__init__(self, 'paragraph', parent)
try:
self.role = attrs['role']
except:
self.role = 'paragraph'
try:
self.level=int(attrs['level'])
except:
self.level=0
if depth > self.level:
self.depth = depth
else:
self.depth = self.level
self.is_first = (len(self.parent.objects) == 0)
def start_element(self, parser, name, attrs):
if name == 'variable':
self.parse_child(Variable(attrs, self, self.depth))
elif name == 'image':
self.parse_child(Image(attrs, self))
elif name == 'link':
self.parse_child(Link(attrs, self))
elif name == 'bookmark':
# This shouldn't occur
print "Warning: Unhandled bookmark content!!!"
try:
global replace_element
self.objects.append(Text(replace_element['start'][name]))
except:
pass
def end_element(self, parser, name):
ElementBase.end_element(self, parser, name)
try:
global replace_element
self.objects.append(Text(replace_element['end'][name]))
except:
pass
def char_data(self, parser, data):
self.objects.append(Text(data))
def get_all(self):
role = self.role
if role == 'heading':
if self.depth < 6:
role = 'heading%d'% self.depth
else:
role = 'heading6'
if ( role == 'tablecontent' or role == 'tablehead' ) and not self.is_first:
role = 'paragraph'
# prepend the markup according to the role
text = ''
if len(self.objects) > 0:
try:
text = text + replace_paragraph_role['start'][role]
except:
sys.stderr.write( "Unknown paragraph role start: " + role + "\n" )
# the text itself
text = text + ElementBase.get_all(self)
# set bookmark info
if self.role == "heading":
Bookmark.set_heading(text)
# append the markup according to the role
if len(self.objects) > 0:
try:
text = text + replace_paragraph_role['end'][role]
except:
sys.stderr.write( "Unknown paragraph role end: " + role + "\n" )
return text
class Variable(Paragraph):
def __init__(self, attrs, parent, depth):
Paragraph.__init__(self, attrs, parent, depth)
self.name = 'variable'
self.role = 'variable'
self.id = attrs['id']
def get_variable(self, id):
if id == self.id:
return self
return None
class XhpParser:
def __init__(self, filename, follow_embed):
self.head_obj = XhpFile()
self.filename = filename
self.follow_embed = follow_embed
file = open(filename, "r")
p = xml.parsers.expat.ParserCreate()
p.StartElementHandler = self.start_element
p.EndElementHandler = self.end_element
p.CharacterDataHandler = self.char_data
buf = file.read()
p.Parse(buf)
file.close()
def start_element(self, name, attrs):
self.head_obj.get_curobj().start_element(self, name, attrs)
def end_element(self, name):
self.head_obj.get_curobj().end_element(self, name)
def char_data(self, data):
self.head_obj.get_curobj().char_data(self, data)
def get_all(self):
return self.head_obj.get_all()
def get_variable(self, id):
return self.head_obj.get_variable(id)
def loadallfiles(filename):
global titles
file = open(filename, "r")
for line in file:
title = line.split(";", 2)
titles.append(title)
def signal_handler(signal, frame):
sys.stderr.write( "Exiting..." )
sys.exit(1)
signal.signal(signal.SIGINT, signal_handler)
if len(sys.argv) < 2:
print "wikiconv2.py <inputfile.xph>"
sys.exit(1)
if len(sys.argv) > 2:
help_file_name = sys.argv[2]
# TODO: Currently the following files are loaded for every
# file which is converted. Combine the batch converter with
# this file to generate quicker help files.
load_all_help_ids()
loadallfiles("alltitles.csv")
parser = XhpParser(sys.argv[1], True)
print parser.get_all().encode('ascii','replace')
Bookmark.save_bookmarks()
|