summaryrefslogtreecommitdiffabout
path: root/wikimarkup.py
blob: 2ef6be109d830b006f74d2f3c7d94c49d74c5ab3 (plain)
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2008, 2009, 2015 Sergey Poznyakoff
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function
import sys
import re
from types import *

__all__ = [ "BaseWikiMarkup", "WikiMarkup",
            "TagAttributes", "TagAttributeSyntax" ]

class TagAttributeSyntax(Exception):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)

class TagAttributes(object):
    attrstart = re.compile("^(?P<attr>[a-zA-Z0-9_-]+)(?P<eq>=\")?")
    valseg = re.compile("^[^\\\"]+")
    tab = {}
    printable = None
    def __init__(self, string):
        if not string:
            self.printable = ''
            return
        self.printable = string
        s = string
        self.tab = {}
        while s != '':
            s = s.strip()
            m = self.attrstart.match(s)
            if m:
                name = m.group('attr')
                val = ''
                s = s[m.end(0):]
                if m.group('eq'):
                    while 1:
                        m = self.valseg.match(s)
                        val += m.group(0)
                        s = s[m.end(0):]
                        if s[0] == '\\':
                            val += s[1]
                            s += 2
                        elif s[0] == '"':
                            s = s[1:]
                            break
                else:
                    val = 1
                self.tab[name] = val
            else:
                raise TagAttributeSyntax(s)
    def __len__(self):
        return len(self.tab)
    def __getitem__(self, key):
        return self.tab[key]
    def __contains__(self, key):
        return key in self.tab
    def __iter__(self):
        for key in self.tab:
            yield(key)
    def has_key(self, key):
        return self.__contains__(key)
    def __setitem__(self, key, value):
        self.tab[key] = value
    def __delitem__(self, key):
        del self.tab[key]
    def __str__(self):
        return self.printable
    def __repr__(self):
        return self.printable

class BaseWikiMarkup(object):

    delim = re.compile("^==+|==+[ \\t]*$|(^----$)|^\\*+|^#+|^[;:]+|(\\[\\[)|\\[|(\\{\\{)|(\\]\\])|\\]|(\\}\\})|\\||(\\'\\'\\'?)|<")
    otag = re.compile("<(?P<tag>[a-zA-Z0-9_]+)(?:\s+(?P<args>[^>]+))?\s*(?P<closed>/)?>")
    ctag = re.compile("</(?P<tag>[a-zA-Z0-9_]+)\s*>")
    refstart = re.compile("^https?://")
    
    close_delim = {
        '[': ']',
        '[[': ']]',
        '{{': '}}'
    }

    # Environment types:
    envtypes = { "*": [ "unnumbered", 0 ],
                 "#": [ "numbered", 0 ],
                 ";": [ "defn", 0 ],
                 ":": [ "defn", 1 ]
    }

    toklist = None
    tokind = 0
    newline = 0
    tree = None

    tags = [ 'code', 'nowiki', 'tt', 'div' ]
    
    nested = 0
    debug_level = 0
    
    def dprint(self, lev, fmt, *argv):
        if self.debug_level >= lev:
            print("[DEBUG]", fmt % argv)

    def print_dump_prefix(self, level, file):
        file.write("[DUMP]" + ' ' * (2*level + 1))

    def dump_nil(self, node, level, file):
        pass
    
    def dump_text(self, node, level, file):
        self.print_dump_prefix(level, file)
        file.write("CONTENT: \"%s\"\n" % node['content'])
    
    def dump_delim(self, node, level, file):
        file.write("'%s'" % node['content'])
        if 'continuation' in node and node['continuation']:
            file.write(" (cont)")
        file.write("\n")
                       
    def dump_tag(self, node, level, file):
        self.print_dump_prefix(level, file)
        file.write("TAG: %s\n" % node['tag'])
        if 'args' in node:
            self.print_dump_prefix(level, file)
            file.write("ARGS: %s\n" % node['args'])
        if 'content' in node:
            self.dump_node(node['content'], level + 1, file)
    
    def dump_seq(self, node, level, file):
        self.dump(node['content'], level + 1, file)
        
    def dump_ref(self, node, level, file):
        self.print_dump_prefix(level, file)
        file.write("REF: %s\n" % node['ref'])
        self.dump_node(node['content'], level + 1, file)
    
    def dump_hdr(self, node, level, file):
        self.print_dump_prefix(level, file)
        file.write("LEVEL: %s\n" % node['level'])
        self.dump_node(node['content'], level + 1, file)
             
    def dump_elt(self, node, level, file):
        self.print_dump_prefix(level, file)
        file.write("SUBTYPE: %s\n" % node['subtype'])
        self.dump_node(node['content'], level + 1, file)
        
    def dump_env(self, node, level, file):
        self.print_dump_prefix(level, file)
        file.write("ENVTYPE: %s\n" % node['envtype'])
        self.print_dump_prefix(level, file)
        file.write("LEVEL: %s\n" % node['level'])
        self.dump(node['content'], level + 1, file)
    
    def dump_ind(self, node, level, file):
        self.print_dump_prefix(level, file)
        file.write("LEVEL: %s\n" % node['level'])
        self.dump_node(node['content'], level + 1, file)
    
    def dump_link(self, node, level, file):
        self.dump(node['content'], level + 1, file)
    
    dump_type = {
        'NIL': dump_nil,
        'NL': dump_nil,
        'TEXT': dump_text,
        'DELIM': dump_delim,
        'OTAG': dump_tag,
        'CTAG': dump_tag,
        'TAG': dump_tag,
        'SEQ': dump_seq,
        'REF': dump_ref,
        'HDR': dump_hdr,
        'ELT': dump_elt,
        'ENV': dump_env,
        'IND': dump_ind,
        'BAR': dump_nil,
        'PARA': dump_seq,
        'PRE': dump_text,
        'BOLD': dump_seq,
        'IT': dump_seq,
        'LINK': dump_link,
    }
    
    def dump_node(self, node, level, file):
        if type(node) != dict:
            file.write("UNHANDLED NODE: %s, %s\n" % (type(node),node))
            return
         
        self.print_dump_prefix(level, file)
        file.write("NODE " + node['type'] + ":\n")
        if node['type'] in self.dump_type:
            self.dump_type[node['type']](self, node, level, file)
        else:
            self.print_dump_prefix(level, file)
            file.write("(UNHANDLED) ")
            file.write("%s\n" % node)
        self.print_dump_prefix(level, file)
        file.write("END NODE " + node['type'] + "\n")
                
    def dump(self, tree, level=0, file=sys.stdout):
        for node in tree:
            self.dump_node(node, level, file)
            
    def tokread(self):
        line = None
        pos = 0
        while 1:
            if (not line or pos == len(line)):
                try:
                    line = self.input()
                    pos = 0
                except StopIteration:
                    line = u''
                    
            if not line or line == "":
                yield({ 'type': 'NIL' })
                break

            if line == '\n':
                yield({ 'type': 'NL', 'content': line })
                line = None
                continue

            self.dprint(100, "LINE: %s", line[pos:])
            m = self.delim.search(line, pos)
            
            if m:
                if (pos < m.start(0)):
                    yield({'type': 'TEXT', 'content': line[pos:m.start(0)]})
                pos = m.start(0)
                t = None

                if line[m.start(0)] == '<':
                    m = self.otag.match(line, pos)
                    if m:
                        pos = m.end(0)
                        if m.group('tag') == 'nowiki':
                            if not m.group('closed'):
                                while 1:
                                    try:
                                        m = self.ctag.match(line)
                                        if m and m.group('tag') == 'nowiki':
                                            yield({ 'type': 'TEXT',
                                                    'content': line[pos:m.start(0)] })
                                            pos = m.end(0)
                                            break

                                        yield({ 'type': 'TEXT',
                                              'content': line[pos:] })

                                        line = self.input()
                                        pos = 0
                                    except StopIteration:
                                        break
                            continue
                        elif m.group('tag') in self.tags:
                            try:
                                t = { 'type': 'OTAG',
                                      'tag': m.group('tag'),
                                      'args': TagAttributes(m.group('args')) }
                                yield(t)
                                if m.group('closed'):
                                    t['type'] = 'CTAG'
                                    yield(t)
                            except TagAttributeSyntax:
                                yield({'type': 'TEXT',
                                       'content': m.group(0)})
                            continue
                    else:
                        m = self.ctag.match(line, pos)
                        if m:
                            if m.group('tag') in self.tags:
                                yield( { 'type': 'CTAG',
                                         'tag': m.group('tag') } )
                                pos = m.end(0)
                                continue
                        else:
                            yield( { 'type': 'TEXT',
                                     'content': line[pos:pos+1] })
                            pos += 1
                            continue
                else:
                    pos = m.end(0)
                    content = m.group(0)
                    if content[0] in self.envtypes:
                        t = { 'type': 'DELIM',
                              'content': content,
                              'continuation': pos < len(line) and line[pos] == ":" }
                        if t['continuation']:
                            t['content'] += t['content'][0]
                            pos += 1

                        yield(t)

                        while pos < len(line) and line[pos] in [' ', '\t']:
                            pos += 1 
                    else:
                        yield({ 'type': 'DELIM',
                                'content': content,
                                'continuation': False})
                    continue
                
            if line:
                if line[-1] == '\n':
                    if line[pos:-1] != '':
                        yield({ 'type': 'TEXT',
                                'content': line[pos:-1] })
                    yield({ 'type': 'NL',
                            'content': '\n' })
                else:
                    yield({ 'type': 'TEXT',
                            'content': line[pos:] })
                line = None
                

    def input(self):
        return None

    def swaptkn(self, i, j):
        self.dprint(80, "SWAPPING %s <-> %s", i, j)
        x = self.toklist[i] 
        self.toklist[i] = self.toklist[j]
        self.toklist[j] = x

    def tokenize(self):
        self.toklist = []
        for tok in self.tokread():
            self.dprint(100, "TOK: %s", tok)
            self.toklist.append(tok)
        # Determine and fix up the ordering of bold and italic markers
        # There are three possible cases:
        #
        # 1a. '''a b ''c'' d'''
        # 1b. ''a b '''c''' d''
        #
        # 2a. '''''a b'' c d'''
        # 2b. '''''a b''' c d''
        # 
        # 3a. '''a b ''c d'''''
        # 3b. ''a b '''c d'''''
        stack = []
        for i in range(0,len(self.toklist)):
            if self.toklist[i]['type'] == 'DELIM' \
                    and (self.toklist[i]['content'] == "''" \
                             or self.toklist[i]['content'] == "'''"):
                if len(stack) > 0:
                    if self.toklist[stack[-1]]['content'] == self.toklist[i]['content']:
                        # Case 1: just pop the matching delimiter off the stack
                        stack.pop()
                    elif len(stack) == 2 and stack[-2] + 1 == stack[-1]:
                        # Case 2: swap delimiters saved on stack ...
                        self.swaptkn(stack[-2], stack[-1])
                        #         and pop off the matching one
                        stack.pop()
                    elif i < len(self.toklist) \
                            and self.toklist[i+1]['type'] == 'DELIM' \
                            and self.toklist[stack[-1]]['content'] == self.toklist[i+1]['content']:
                        # Case 3: swap current and next tokens
                        self.swaptkn(i, i+1)
                        #         and pop off the matching one 
                        stack.pop()
                    else:
                        # Push the token on stack
                        stack.append(i)
                else:
                    # Push the token on stack
                    stack.append(i)
        # Redefine all non-matched tokens as TEXT
        for i in stack:
            self.toklist[i]['type'] = 'TEXT'

    def peektkn(self, off=0):
        return self.toklist[self.tokind-off]

    def setkn(self,val):
        self.toklist[self.tokind] = val
    
    def getkn(self):
        self.newline = self.tokind == 0 or self.toklist[self.tokind-1]['type'] == 'NL'
        if self.tokind == len(self.toklist):
            return { 'type': 'NIL' }
        tok = self.toklist[self.tokind]
        self.tokind = self.tokind + 1
        return tok
    
    def ungetkn(self):
        self.tokind = self.tokind - 1
        self.newline = self.tokind == 0 or self.toklist[self.tokind-1]['type'] == 'NL'
        return self.toklist[self.tokind]

    def parse_fontmod(self,delim,what):
        self.dprint(80, "ENTER parse_fontmod(%s,%s), tok %s",
                    delim, what, self.peektkn())
        seq = []
        text = ''
        while 1:
            tok = self.getkn()
            if tok['type'] == 'TEXT':
                text += tok['content']
            elif tok['type'] == 'DELIM':
                if tok['content'] == delim:
                    break
                elif self.is_inline_delim(tok):
                    if text:
                        seq.append({ 'type': 'TEXT', 'content': text })
                        text = ''
                    x = self.parse_inline(tok)
                    if x:
                        seq.append(x)
                    else:
                        self.dprint(80, "LEAVE parse_fontmod=%s", "None")
                        return None
                else:
                    self.dprint(80, "LEAVE parse_fontmod=None")
                    return None
            elif tok['type'] == 'NL':
                if self.peektkn()['type'] == 'NL':
                    self.dprint(80, "LEAVE parse_fontmod=None")
                    return None
                seq.append({ 'type': 'TEXT', 'content': '\n' })
            else:
                self.dprint(80, "LEAVE parse_fontmod=None")
                return None
        if text:
            seq.append({ 'type': 'TEXT', 'content': text })
        res = { 'type': what, 'content': seq }
        self.dprint(80, "LEAVE parse_fontmod=%s", res)    
        return res

    def parse_link(self, type, delim):
        self.dprint(80, "ENTER parse_link(%s,%s), tok %s",
                    type, delim, self.peektkn())
        subtree = []
        list = []
        while 1:
            tok = self.getkn()
            if tok['type'] == 'DELIM':
                if tok['content'] == delim:
                    if list:
                        subtree.append({ 'type': 'SEQ', 'content': list })
                    break
                elif tok['content'] == "|":
                    if len(list) > 1:
                        subtree.append({ 'type': 'SEQ', 'content': list })
                    elif list:
                        subtree.append(list[0])
                    list = []
                else:
                    x = self.parse_inline(tok)
                    if x:
                        list.append(x)
                    else:
                        self.dprint(80, "LEAVE parse_link=%s", "None")
                        return None
            elif tok['type'] == 'TEXT':
                list.append(tok)
            else:
                self.dprint(80, "LEAVE parse_link=%s", "None")
                return None
        self.dprint(80, "LEAVE parse_link=(%s,%s)", type, subtree)
        return { 'type': type, 'content': subtree }

    def parse_ref(self):
        tok = self.getkn()
        self.dprint(80, "ENTER parse_ref, tok %s", tok)
        if not (tok['type'] == 'TEXT' and self.refstart.match(tok['content'])):
            self.dprint(80, "LEAVE parse_ref=None")
            return None

        seq = []
        (ref,sep,text) = tok['content'].partition(' ')
        if text:
            seq.insert(0, {'type': 'TEXT', 'content': text })

        while 1:
            tok = self.getkn()
            if tok == None or tok['type'] == 'NIL':
                self.dprint(80, "LEAVE parse_ref=None")
                return None
            if tok['type'] == 'DELIM':
                if tok['content'] == ']':
                    break
                else:
                    tok = self.parse_inline(tok)
                    if tok:
                        seq.append(tok)
                    else:
                        self.dprint(80, "LEAVE parse_ref=None")
                        return None
            elif tok['type'] == 'OTAG':
                list.append(self.parse_til(tok))
            else:
                seq.append(tok)

        ret = { 'type': 'REF', 
                'ref': ref,
                'content': { 'type': 'SEQ', 'content': seq } }
        self.dprint(80, "LEAVE parse_ref= %s", ret)
        return ret

    inline_delims = [ "''", "'''", "[", "[[", "{{", "|" ]

    def is_inline_delim(self, tok):
        return tok['type'] == 'DELIM' and tok['content'] in self.inline_delims
    def is_block_delim(self, tok):
        return tok['type'] == 'DELIM' and tok['content'] not in self.inline_delims
    
    def parse_inline(self, tok):
        self.dprint(80, "ENTER parse_inline(%s), tok %s", tok, self.peektkn())
        tokind = self.tokind
        if tok['content'] == "''":
            x = self.parse_fontmod(tok['content'], 'IT')
        elif tok['content'] == "'''":
            x = self.parse_fontmod(tok['content'], 'BOLD')
        elif tok['content'] == "[":
            x = self.parse_ref()
        elif tok['content'] == "[[":
            x = self.parse_link('LINK', "]]")
        elif tok['content'] == "{{":
            x = self.parse_link('TMPL', "}}")
        else:
            self.dprint(80, "LEAVE parse_inline=%s (unhandled delimiter)", "None")
            x = None
        if not x:
            self.tokind = tokind
            tok['type'] = 'TEXT'
            self.dprint(80, "BEGIN DELIMITER RECOVERY: %s", tok)
            od = tok['content']
            if od in self.close_delim:
                cd = self.close_delim[od]
                lev = 0
                for tok in self.toklist[self.tokind+1:]:
                    if tok['type'] == 'NIL':
                        break
                    elif tok['type'] == 'DELIM':
                        if tok['content'] == od:
                            lev += 1
                        elif tok['content'] == cd:
                            if lev == 0:
                                tok['type'] = 'TEXT'
                            lev -= 1
                            break
            self.dprint(80, "END DELIMITER RECOVERY: %s", tok)

        self.dprint(80, "LEAVE parse_inline=%s", x)
        return x

    def parse_para(self):
        self.dprint(80, "ENTER parse_para, tok %s", self.peektkn())
        seq = []
        textlist = []
        tok = self.peektkn()

        if self.newline:
            if re.match("^\s", tok['content']):
                type = 'PRE'
                rx = re.compile("^\S")
            else:
                type = 'PARA'
                rx = re.compile("^\s")
        else:
            type = 'SEQ'
            rx = None
            
        while 1:
            tok = self.getkn()
            if tok['type'] == 'TEXT':
                if rx and self.newline and rx.match(tok['content']):
                    self.ungetkn()
                    break
                textlist.append(tok['content'])
            elif tok['type'] == 'NL':
                tok = self.getkn()
                if tok['type'] == 'NL' or tok['type'] == 'NIL':
                    break
                else:
                    self.ungetkn()
                    if self.is_block_delim(tok):
                        break
                textlist.append('\n')
            elif tok['type'] == 'NIL':
                break
            elif tok['type'] == 'OTAG' or tok['type'] == 'CTAG' or tok['type'] == 'TAG':
                self.ungetkn()
                break
            elif tok['type'] == 'DELIM':
                if self.is_inline_delim(tok):
                    if textlist:
                        seq.append({ 'type': 'TEXT',
                                     'content': ''.join(textlist) })
                        textlist = []
                    x = self.parse_inline(tok)
                    if x:
                        seq.append(x)
                    else:
                        self.ungetkn()
                        # restart
                else:
                    seq.append({ 'type': 'TEXT', 'content': tok['content'] })
                #    self.ungetkn()
                    break
        if textlist:
            seq.append({ 'type': 'TEXT', 'content': ''.join(textlist) })
        self.dprint(80, "LEAVE parse_para=%s", seq)
        return { 'type': type, 'content': seq }

    def parse_header(self, delim):
        self.dprint(80, "ENTER parse_header(%s), tok %s", delim, self.peektkn())
        list = []
        while 1:
            tok = self.getkn()
            if tok['type'] == 'NIL':
                self.dprint(80, "LEAVE parse_header=%s", "None")        
                return None
            elif tok['type'] == 'TEXT':
                list.append(tok)
            elif tok['type'] == 'DELIM':
                if tok['content'] == delim:
                    if self.peektkn()['type'] == 'NL':
                        break
                    else:
                        self.dprint(80, "LEAVE parse_header=%s", "None")
                        return None
                else:
                    x = self.parse_inline(tok)
                    if x:
                        list.append(x)
                    else:
                        self.ungetkn()
                        self.dprint(80, "LEAVE parse_header=%s", "None")
                        return None #FIXME?
            else:
                self.dprint(80, "LEAVE parse_header=%s", "None")
                return None
        self.dprint(80, "LEAVE parse_header=(HDR, %s, (SEQ,%s))",len(delim)-1,list)
        return { 'type': 'HDR',
                 'level': len(delim)-1,
                 'content': { 'type': 'SEQ', 'content': list } }


    def parse_line(self):
        self.dprint(80, "ENTER parse_line, tok %s", self.peektkn())
        list = []
        while 1:
            tok = self.getkn()
            if tok['type'] == 'NL' or tok['type'] == 'NIL':
                break
            elif tok['type'] == 'TEXT':
                list.append(tok)
            elif tok['type'] == 'DELIM':
                if tok['content'][0] == ":":
                    list.append(self.parse_indent(len(tok['content'])))
                    break
                else:
                    x = self.parse_inline(tok)
                    if x:
                        list.append(x)
                    else:
                        list.append(tok)
            elif tok['type'] == 'OTAG':
                list.append(self.parse_til(tok))
            else:
                list.append(tok)
        self.dprint(80, "LEAVE parse_line=(SEQ, %s)", list)
        return { 'type': 'SEQ', 'content': list }
    
    def parse_env(self, type, lev):
        self.dprint(80, "ENTER parse_env(%s,%s), tok %s",type,lev,self.peektkn())
        list = []
        while 1:
            tok = self.getkn()
            if tok['type'] == 'DELIM' \
               and tok['content'][0] in self.envtypes \
               and type == self.envtypes[tok['content'][0]][0]:
                if len(tok['content']) < lev:
                    self.ungetkn()
                    break
                elif len(tok['content']) > lev:
                    self.ungetkn()
                    elt = self.parse_env(type, len(tok['content']))
                else:
                    elt = self.parse_line()
                    if  not tok['continuation']:
                        list.append({ 'type': 'ELT',
                                      'subtype': self.envtypes[tok['content'][0]][1],
                                      'content': elt })
                        continue

                if list:
                    if list[-1]['content']['type'] != 'SEQ':
                        x = list[-1]['content']['content']
                        # FIXME:
                        list[-1]['content'] = { 'type': 'SEQ', 'content': [x] }
                    list[-1]['content']['content'].append(elt)
            else:
                self.ungetkn()
                break
        self.dprint(80, "LEAVE parse_env=(ENV, %s, %s, %s)", type, lev, list)
        return { 'type': 'ENV', 'envtype': type, 'level': lev, 'content': list }

    def parse_indent(self, lev):
        self.dprint(80, "ENTER parse_indent(%s), tok %s", lev, self.peektkn())
        x = { 'type': 'IND', 'level': lev, 'content': self.parse_line() }
        self.dprint(80, "LEAVE parse_indent=%s", x)
        return x

    def parse_til(self, tag):
        self.dprint(80, "ENTER parse_til(%s)", tag)
        seq = []
        save = self.tokind
        while 1:
            t = self.parse0()
            if t == None or t['type'] == 'NIL':
                self.tokind = save
                s = '<' + tag['tag']
                if 'args' in tag and tag['args']:
                    s += ' ' + str(tag['args'])
                del tag['args']
                s += '>'
                if 'content' in tag:
                    subtree = tag['content']
                else:
                    subtree = None
                tag['type'] = 'TEXT'
                tag['content'] = s
                if subtree:
                    self.tree[self.tokind:self.tokind] = subtree
                self.dprint(80, "LEAVE parse_til = %s (tree modified)", tag)
                self.ungetkn()
                return self.parse0()
            
            if t['type'] == 'CTAG' and tag['tag'] == t['tag']:
                break
            seq.append(t)

        ret = { 'type': 'TAG',
                'tag': tag['tag'],
                'args': tag['args'],
                'content': { 'type': 'SEQ', 'content': seq } }
        self.dprint(80, "LEAVE parse_til = %s", ret)
        return ret
            
    def parse0(self):
        tok = self.getkn()
        self.dprint(80, "ENTER parse0(%s)", tok)
        toktype = tok['type']
        if toktype == 'NIL':
            return None
        elif toktype == 'TEXT':
            self.ungetkn()
            return self.parse_para()
        elif toktype == 'DELIM':
            if tok['content'] == "----":
                return { 'type': 'BAR' }
            elif tok['content'][0:2] == "==":
                return self.parse_header(tok['content'])
            elif tok['content'][0] in self.envtypes:
                type = self.envtypes[tok['content'][0]][0]
                lev = len(tok['content'])
                if tok['content'][0] == ':':
                    t = self.peektkn(2)
                    if not (t['type'] == 'DELIM' and t['content'] == ';'):
                        return self.parse_indent(lev)
                self.ungetkn()
                return self.parse_env(type, lev)
            else:
                self.ungetkn()
                return self.parse_para()
        elif toktype == 'NL':
            return { 'type': 'TEXT', 'content': '\n' }
        elif toktype == 'OTAG':
            return self.parse_til(tok)
        else:
            return tok

    def parse(self):
        if not self.toklist:
            self.tokenize()
        if self.debug_level >= 90:
            print("TOKEN DUMP BEGIN")
            self.dump(self.toklist)
            print("TOKEN DUMP END")

        self.tokind = 0
        self.tree = []
        while 1:
            subtree = self.parse0()
            if subtree == None:
                break
            self.tree.append(subtree)

        if self.nested:
            if self.tree[0]['type'] == 'PARA':
                self.tree[0]['type'] = 'SEQ'
                
        if self.debug_level >= 70:
            print("TREE DUMP BEGIN")
            self.dump(self.tree)
            print("TREE DUMP END")

    def __str__(self):
        return str(self.tree)


class WikiMarkup (BaseWikiMarkup):
    """
    A derived class, that supplies a basic input method.
    
    Three types of inputs are available:

    1. filename=<file>
    The file <file> is opened and used for input.
    2. file=<file>
    The already opened file <file> is used for input.
    3. text=<string>
    Input is taken from <string>, line by line.

    Usage:

    obj = WikiMarkup(arg=val)
    obj.parse
    ... Do whatever you need with obj.tree ...

    """
    file = None
    text = None
    lang = 'en'
    html_base = 'http://%(lang)s.wiktionary.org/wiki/'
    image_base = 'http://upload.wikimedia.org/wikipedia/commons/thumb/a/bf'
    media_base = 'http://www.mediawiki.org/xml/export-0.3'
    
    def __init__(self, *args, **keywords):
        for kw in keywords:
            if kw == 'file':
                self.file = keywords[kw]
            elif kw == 'filename':
                self.file = open(keywords[kw])
            elif kw == 'text':
                self.text = keywords[kw].split("\n")
            elif kw == 'lang':
                self.lang = keywords[kw]
            elif kw == 'html_base':
                self.html_base = keywords[kw]
            elif kw == 'image_base':
                self.image_base = keywords[kw]
            elif kw == 'media_base':
                self.media_base = keywords[kw]
            elif kw == 'nested':
                self.nested = keywords[kw]

    def __del__(self):
        if self.file:
            self.file.close()

    def input(self):
        if self.file:
            return self.file.readline()
        elif self.text:
            return self.text.pop(0) + '\n'
        else:
            return None

    def is_lang_link(self, elt):
        if elt['type'] == 'LINK' \
           and isinstance(elt['content'], list) \
           and len(elt['content']) == 1:
            if elt['content'][0]['type'] == TEXT:
                m = re.match('([\w-]+):', elt['content'][0]['content'])
                if m: # and m.group(1) in self.langtab:
                    return True
            elif elt['content'][0]['type'] == 'SEQ' \
                    and len(elt['content'][0]['content']) == 1 and\
                    elt['content'][0]['content'][0]['type'] == TEXT:
                m = re.match('([\w-]+):',elt['content'][0]['content'][0]['content'])
                if m: # and m.group(1) in self.langtab:
                    return True
        return False
    
    def is_empty_text(self, elt):
        if elt['type'] == 'TEXT':
            if re.search('\w', elt['content']):
                return False
            return True
        return False

    def is_empty_para(self, seq):
        for x in seq:             
            if not (self.is_lang_link(x) or self.is_empty_text(x)):
                return False
        return True
    
    # ISO 639 
    langtab = {
        "aa": "Afar",            # Afar
	"ab": "Аҧсуа",           # Abkhazian
	"ae": None,              # Avestan
	"af": "Afrikaans",       # Afrikaans
	"ak": "Akana",           # Akan
        "als": "Alemannisch",
	"am": "አማርኛ",            # Amharic
	"an": "Aragonés",        # Aragonese
        "ang": "Englisc",
	"ar": "العربية" ,        # Arabic
        "arc": "ܐܪܡܝܐ",
	"as": "অসমীয়া",         # Assamese
        "ast": "Asturian", 
	"av": "Авар",            # Avaric
	"ay": "Aymara",           # Aymara
	"az": "Azərbaycan" ,     # Azerbaijani

	"ba": "Башҡорт",         # Bashkir
        "bar": "Boarisch", 	
        "bat-smg": "Žemaitėška",
        "bcl": "Bikol",
	"be": "Беларуская",      # Byelorussian; Belarusian
        "be-x-old": "Беларуская (тарашкевіца)",
	"bg": "Български",       # Bulgarian
	"bh": "भोजपुरी",  # Bihari
	"bi": "Bislama",         # Bislama
	"bm": "Bamanankan",      # Bambara
	"bn": "বাংলা" ,          # Bengali; Bangla
	"bo": "བོད་སྐད",         # Tibetan
        "bpy": "ইমার ঠার/বিষ্ণুপ্রিয়া মণিপুরী" ,
	"br": "Brezhoneg" ,      # Breton
	"bs": "Bosanski" ,       # Bosnian
        "bug": "Basa Ugi",
        "bxr": "Буряад",

	"ca": "Català" ,         # Catalan
        "cbk-zam": "Chavacano de Zamboanga",
        "cdo": "Mìng-dĕ̤ng-ngṳ̄",
        "cho": "Choctaw",
	"ce": "Нохчийн",         # Chechen
        "ceb": "Sinugboanong Binisaya" , # Cebuano
	"ch": "Chamor",          # Chamorro
        "chr": "ᏣᎳᎩ",
        "chy": "Tsetsêhestâhese",
	"co": "Cors",            # Corsican
	"cr": "Nehiyaw",         # Cree
        "crh": "Qırımtatarca",
	"cs": "Česky" ,          # Czech
        "csb": "Kaszëbsczi",
	"c": "Словѣньскъ",       # Church Slavic
	"cv": "Чăваш",           # Chuvash
	"cy": "Cymraeg" ,        # Welsh

	"da": "Dansk" ,          # Danish
	"de": "Deutsch" ,        # German
        "diq": "Zazaki",         # Dimli (Southern Zazaki)
        "dsb": "Dolnoserbski",
	"dv": "ދިވެހިބަސް",      # Divehi
	"dz": "ཇོང་ཁ",           # Dzongkha; Bhutani

	"ee": "Eʋegbe",          # Ewe
	"el": "Ελληνικά" ,       # Greek
        "eml": "Emiliàn e rumagnòl",
	"en": "English" ,        # English
        "eo": "Esperanto" ,
	"es": "Español" ,        # Spanish
	"et": "Eesti" ,          # Estonian
	"eu": "Euskara" ,         # Basque
        "ext": "Estremeñ",

	"fa": "فارسی" ,          # Persian
	"ff": "Fulfulde",        # Fulah
	"fi": "Suomi" ,          # Finnish
        "fiu-vro": "Võro",
	"fj": "Na Vosa Vakaviti",# Fijian; Fiji
	"fo": "Føroyskt" ,       # Faroese
	"fr": "Français" ,       # French
        "frp": "Arpitan",
        "fur": "Furlan",
	"fy": "Frysk",           # Frisian

	"ga": "Gaeilge",         # Irish
        "gan": "贛語 (Gànyŭ)",
	"gd": "Gàidhlig",        # Scots; Gaelic
	"gl": "Gallego" ,        # Gallegan; Galician
        "glk": "گیلکی",
        "got": "𐌲𐌿𐍄𐌹𐍃𐌺𐍉𐍂𐌰𐌶𐌳𐌰",
	"gn": "Avañe'ẽ",         # Guarani
	"g": "ગુજરાતી",          # Gujarati
	"gv": "Gaelg",           # Manx

	"ha": "هَوُسَ",          # Hausa
        "hak": "Hak-kâ-fa / 客家話",
        "haw": "Hawai`i",
	"he": "עברית" ,          # Hebrew (formerly iw)
	"hi": "हिन्दी" ,   # Hindi
        "hif": "Fiji Hindi",
	"ho": "Hiri Mot",        # Hiri Motu
	"hr": "Hrvatski" ,       # Croatian
        "hsb": "Hornjoserbsce",
	"ht": "Krèyol ayisyen" , # Haitian; Haitian Creole
	"hu": "Magyar" ,         # Hungarian
	"hy": "Հայերեն",         # Armenian
	"hz": "Otsiherero",      # Herero

        "ia": "Interlingua",
        "ie": "Interlingue",
	"id": "Bahasa Indonesia",# Indonesian (formerly in)
	"ig": "Igbo",            # Igbo
	"ii": "ꆇꉙ 	",       # Sichuan Yi
	"ik": "Iñupiak",         # Inupiak
        "ilo": "Ilokano",
        "io": "Ido" ,
	"is": "Íslenska" ,       # Icelandic
	"it": "Italiano" ,       # Italian
	"i": "ᐃᓄᒃᑎᑐᑦ",           # Inuktitut

	"ja": "日本語",          # Japanese
        "jbo": "Lojban",
	"jv": "Basa Jawa",       # Javanese

	"ka": "ქართული" ,        # Georgian
        "kaa": "Qaraqalpaqsha",
        "kab": "Taqbaylit",
	"kg": "KiKongo",         # Kongo
	"ki": "Gĩkũyũ",          # Kikuyu
	"kj": "Kuanyama",        # Kuanyama
	"kk": "Қазақша",         # Kazakh
	"kl": "Kalaallisut",     # Kalaallisut; Greenlandic
	"km": "ភាសាខ្មែរ",       # Khmer; Cambodian
	"kn": "ಕನ್ನಡ",           # Kannada
	"ko": "한국어" ,         # Korean
	"kr": "Kanuri",          # Kanuri
	"ks": "कश्मीरी / كشميري", # Kashmiri
        "ksh": "Ripoarisch",
	"ku": "Kurdî / كوردی",   # Kurdish
	"kv": "Коми",            # Komi
	"kw": "Kernewek/Karnuack", # Cornish
	"ky": "Кыргызча",        # Kirghiz

	"la": "Latina" ,         # Latin
        "lad": "Dzhudezmo",
	"lb": "Lëtzebuergesch" , # Letzeburgesch
        "lbe": "Лакку",
	"lg": "Luganda",         # Ganda
	"li": "Limburgs",        # Limburgish; Limburger; Limburgan
        "lij": "Lígur", 
	"ln": "Lingala", 	 # Lingala
        "lmo": "Lumbaart",
	"lo": "ລາວ",             # Lao; Laotian
	"lt": "Lietuvių" ,       # Lithuanian
	"lua": "Luba",           # Luba
	"lv": "Latvieš" ,        # Latvian; Lettish

        "map-bms": "Basa Banyumasan",
        "mdf": "Мокшень (Mokshanj Kälj)",
	"mg": "Malagasy",        # Malagasy
	"mh": "Ebon",            # Marshall
	"mi": "Māori",           # Maori
	"mk": "Македонски" ,     # Macedonian
	"ml": None,              # Malayalam
	"mn": "Монгол",          # Mongolian
	"mo": "Молдовеняскэ",    # Moldavian
	"mr": "मराठी" ,     # Marathi
	"ms": "Bahasa Melay" ,   # Malay
	"mt": "Malti",           # Maltese
        "mus": "Muskogee",
	"my": "မ္ရန္‌မာစာ",      # Burmese
        "myv": "Эрзянь (Erzjanj Kelj)",
        "mzn": "مَزِروني",

	"na": "dorerin Naoero",  # Nauru
        "nah": "Nāhuatl",
        "nap": "Nnapulitano",
	"nb": "Norsk (Bokmål)",  # Norwegian Bokm@aa{}l
	"nd": None,              # Ndebele, North
        "nds": "Plattdüütsch",
        "nds-nl": "Nedersaksisch",
	"ne": "नेपाली",    # Nepali
        "new": "नेपाल भाषा" , # Nepal Bhasa
	"ng": "Oshiwambo",       # Ndonga
	"nl": "Nederlands" ,     # Dutch
	"nn": "Nynorsk",         # Norwegian Nynorsk
	"no": "Norsk (Bokmål)" , # Norwegian
        "nov": "Novial",
	"nr": None,              # Ndebele, South
        "nrm": "Nouormand/Normaund",
	"nv": "Diné bizaad",     # Navajo
	"ny": "Chi-Chewa",       # Chichewa; Nyanja

	"oc": "Occitan",         # Occitan; Proven@,{c}al
	"oj": None,              # Ojibwa
	"om": "Oromoo",          # (Afan) Oromo
	"or": "ଓଡ଼ିଆ",           # Oriya
	"os": "Иронау",          # Ossetian; Ossetic

	"pa": "ਪੰਜਾਬੀ" ,         # Panjabi; Punjabi
        "pag": "Pangasinan",
        "pam": "Kapampangan",
        "pap": "Papiament",
        "pdc": "Deitsch",
	"pi": "पाऴि",        # Pali
        "pih": "Norfuk",
	"pl": "Polski" ,         # Polish
        "pms": "Piemontèis" ,
	"ps": "پښتو",            # Pashto, Pushto
	"pt": "Português" ,      # Portuguese

	"q": "Runa Simi" ,       # Quechua

	"rm": "Rumantsch",       # Rhaeto-Romance
        "rmy": "romani - रोमानी",
	"rn": "Kirundi",         # Rundi; Kirundi
	"ro": "Română" ,         # Romanian
        "roa-rup": "Armãneashce",
        "roa-tara": "Tarandíne",
	"ru": "Русский" ,        # Russian
	"rw": "Ikinyarwanda",    # Kinyarwanda

	"sa": "संस्कृतम्", # Sanskrit
        "sah": "Саха тыла (Saxa Tyla)",
	"sc": "Sardu",           # Sardinian
        "scn": "Sicilian", 
        "sco": "Scots",
	"sd": "سنڌي، سندھی ، सिन्ध", # Sindhi
	"se": "Sámegiella",      # Northern Sami
	"sg": "Sängö",           # Sango; Sangro
        "sh": "Srpskohrvatski / Српскохрватски" ,
        "si": "සිංහල",
        "simple": "Simple English" ,
	"sk": "Slovenčina" ,     # Slovak
	"sl": "Slovenščina" ,    # Slovenian
	"sm": "Gagana Samoa",    # Samoan
	"sn": "chiShona",        # Shona
	"so": "Soomaaliga",      # Somali
	"sr": "Српски / Srpski", # Serbian
        "srn": "Sranantongo",
	"ss": "SiSwati",         # Swati; Siswati
	"st": "Sesotho",         # Sesotho; Sotho, Southern
        "stk": "Seeltersk",
	"s": "Basa Sunda",       # Sundanese
        "sq": "Shqip" ,          # Albanian
        "szl": "Ślůnski",
	"sv": "Svenska" ,        # Swedish
	"sw": "Kiswahili",       # Swahili 

	"ta": "தமிழ்" ,          # Tamil
	"te": "తెలుగు" ,         # Telugu
        "tet": "Tetun",
	"tg": "Тоҷикӣ",          # Tajik
	"th": "ไทย" ,            # Thai
	"ti": "ትግርኛ",            # Tigrinya
	"tk": "تركمن / Туркмен", # Turkmen
	"tl": "Tagalog" ,        # Tagalog
	"tn": "Setswana",        # Tswana; Setswana
	"to": "faka Tonga",      # Tonga (?) # Also ZW ; MW
        "tokipona": "Tokipona",
        "tpi": "Tok Pisin",
 	"tr": "Türkçe" ,         # Turkish
	"ts": "Xitsonga",        # Tsonga
	"tt": "Tatarça / Татарча", # Tatar
        "tum": "chiTumbuka",
	"tw": "Twi",             # Twi
	"ty": "Reo Mā`ohi",      # Tahitian

        "udm": "Удмурт кыл",
	"ug": "Oyghurque",       # Uighur
	"uk": "Українська" ,     # Ukrainian
	"ur": "اردو",            # Urdu
	"uz": "O‘zbek",          # Uzbek

	"ve": "Tshivenda",       # Venda
        "vec": "Vèneto",
	"vi": "Tiếng Việt" ,     # Vietnamese
        "vls": "West-Vlams",
        "vo": "Volapük" ,
        
	"wa": "Walon",           # Walloon
        "war": "Winaray",
	"wo": "Wolof",           # Wolof
        "w": "吴语",

        "xal": "Хальмг",
	"xh": "isiXhosa",        # Xhosa

	"yi": "ייִדיש",          # Yiddish 
	"yo": "Yorùbá",          # Yoruba

	"za": "Cuengh",          # Zhuang
        "zea": "Zeêuws",
	"zh": "中文" ,           # Chinese
        "zh-classical": "古文 / 文言文",
        "zm-min-nan": "Bân-lâm-gú",
        "zh-yue": "粵語",
	"zu": "isiZulu"          # Zulu
    }

        

    
            
        

    
        

Return to:

Send suggestions and report system problems to the System administrator.