summaryrefslogtreecommitdiffabout
path: root/wikimarkup.py
blob: e2a1cab7d9a0b327301c6a147af733090a0af642 (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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2008 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/>.

import sys
import re
from types import *

__all__ = [ "BaseWikiMarkup", "WikiMarkup" ]

eltbeg = re.compile("=+|(^----$)|^[\\*#:]+")
eltre = re.compile("(\\[\\[)|(\\{\\{)|\\[|(\\'\\'\\'?)")
delims = { "[[" : re.compile("\\||(\\]\\])"),
           "{{" : re.compile("\\||(\\}\\})") }
term   = { "[[" : "]]" , "{{" : "}}" }                             
ends   = { "[[" : re.compile("\\]\\]"),
           "{{" : re.compile("\\}\\}") }                  
itend  = re.compile("\\'\\'($|[^\\'])")
boend  = re.compile("\\'\\'\\'($|[^\\'])")

class BaseWikiMarkup:
    """
A base class for handling Wiki markups.
It handles:
   1. paragraphs;
   2. basic block markup (headers, numbered and unnumbered lists,
      indentations);
   3. basic inline markup (bold, italic);
   4. basic reference markup (links, templates, external links).
 It does NOT handle:
   1. pseudo-html markup (<nowiki></nowiki>, and similar);
   2. leading spaces meaning ``preserve formatting'';
   3. tables and math.
 The above rests for FIXME.

 This class relies on its derived classes for providing input. They must
 overload method `input', which must return one physical line of input for
 each call.

 Variables:

   1. tree
   The parse tree.  Valid after parse() finishes (see below).
   
 Methods:

   1. parse()
   Parse the input and build parse tree

   2. input()
   Virtual function. Return next line of input or None on EOF.

   3. output()
   Print the tree in internal representation.
   """
    ## Token classes
    # NIL: nothing
    NIL  = 0 
    # TEXT: text
    TEXT = 1
    # LINK: target, text
    LINK = 2
    # Template: target, text
    TMPL = 3
    # External ref: target, text
    REF  = 4
    # Italics: text
    IT   = 5
    # Bold: text
    BOLD = 6
    # Header: level, text
    HDR  = 7
    # Horizontal bar:
    BAR  = 8
    # Environment: type, level
    ENV  = 9
    # Item: text
    ITEM = 10
    # Sequence: seq
    SEQ  = 11
    # Paragraph
    PARA = 12
    
    # Environment types:
    # Unnumbered list
    ENVUNNUM = 0
    # Numbered list
    ENVNUM   = 1
    # Indent
    INDENT   = 2
    envtypes = [ "*", "#", ":" ]

    tree = None
    
    def itend(self, line, pos):
        while 1:
            d = itend.search(line, pos)
            if not d:
                return -1
            elif d.start(0) == pos or line[d.start(0)-1] != "'":
                return d.start(0)
            else:
                pos = d.start(0) + 1

    la = None
    def putback(self, line):
        self.la = line
        
    def nextkn(self, curlev=0, type = -1):
        while 1:
            if self.la:
                line = self.la
                self.putback(None)
            else:
                try:
                    line = self.input()
                except StopIteration:
                    line = u''
            if not line or line == "":
                self.putback(line)
                break

            if line == '\n':
                yield(self.PARA,)
                continue
            
            m = eltbeg.match(line)
            if m:
                if m.group(0)[0] in self.envtypes:
                    btype = self.envtypes.index(m.group(0)[0])
                    lev = len(m.group(0))
                    if btype == type:
                        if lev == curlev:
                            yield(self.ITEM,
                                  (self.SEQ, self.getkn(line[m.end(0):])))
                        elif lev > curlev:
                            self.putback(line)
                            yield(self.ENV, btype, curlev + 1,
                                  (self.SEQ, self.nextkn(curlev + 1, btype)))
                        else:
                            self.putback(line)
                            break
                    else:
                        self.putback(line)
                        yield(self.ENV, btype, 1, self.nextkn(1, btype))
                
                else:
                    if curlev > 0:
                        self.putback(line)
                        break
                    elif m.group(0)[0:2] == "==" \
                            and line.rstrip('\n').endswith(m.group(0)):
                        yield(self.HDR, len(m.group(0))-1,
                              self.getkn(line[m.end(0):-(1+len(m.group(0)))]))
                    elif m.group(0) == "----":
                        yield(self.BAR,)
            else:
                if curlev > 0:
                    self.putback(line)
                    break
                yield(self.getkn(line))
                
                                            
    def getkn(self, line):
        pos = 0
        while 1:
            if pos == len(line):
                break;
            m = eltre.search(line, pos)
            if not m:
                yield(self.TEXT, line[pos:])
                pos = len(line)
            else:
                yield(self.TEXT, line[pos:m.start(0)])
                pos = m.end(0)
                if m.group(0) == "[[" or m.group(0) == "{{":
                    d = delims[m.group(0)].search(line, pos)
                    if d.group(0) == "|":
                        e = ends[m.group(0)].search(line, d.end(0))
                        target = (self.TEXT, line[pos:d.start(0)])
                        text = (self.SEQ, self.getkn(line[d.end(0):e.start(0)]))
                        pos = e.end(0)
                    elif d.group(0) == term[m.group(0)]:
                        target = (self.TEXT, line[pos:d.start(0)])
                        text = (self.NIL,)
                        pos = d.end(0)
                    if m.group(0) == "[[":
                        yield(self.LINK, target, text)
                    else:
                        yield(self.TMPL, target, text)
                elif m.group(0) == "[":
                    i = line.find("]", m.end(0))
                    if i == -1:
                        i = len(line)
                    (target,sep,text) = line[m.end(0):i].partition(' ')
                    yield(self.REF,
                          (self.TEXT, target),
                          (self.SEQ, self.getkn(text)))
                    pos = i + 1
                elif m.group(0) == "'''":
                    e = boend.search(line, m.end(0))
                    if e:
                        i = e.start(0)
                        pos = e.end(0)
                    else:
                        pos = len(line)
                        i = pos
                    yield(self.BOLD,
                          (self.SEQ, self.getkn(line[m.end(0):i])))
                    pos = e.end(0)
                elif m.group(0) == "''":
                    i = self.itend(line, m.end(0))    
                    if i == -1:
                        i = len(line)
                    yield(self.IT,
                          (self.SEQ, self.getkn(line[m.end(0):i])))
                    pos = i + 2

    def input(self):
        return None

    def expandtok(self, tok):
        if type(tok) == GeneratorType:
            subtree = [self.SEQ]
            for t in tok:
                x = self.expandtok(t)
                if x:
                    subtree.append(x)
            return tuple(subtree) if len(subtree) > 2 else \
                   subtree[1] if len(subtree) == 2 else None
        toktype = tok[0]
        if toktype == self.NIL:
            return None
        if toktype == self.TEXT:
            return tok if tok[1] != '' else None
        elif toktype == self.LINK or toktype == self.TMPL \
                or toktype == self.REF:
            return toktype, self.expandtok(tok[1]), self.expandtok(tok[2])
        elif toktype == self.IT or toktype == self.BOLD \
                or toktype == self.ITEM:
            return toktype, self.expandtok(tok[1])
        elif toktype == self.HDR:
            return toktype, tok[1], self.expandtok(tok[2])
        elif toktype == self.ENV:
            return toktype,tok[1],tok[2],self.expandtok(tok[3])
        elif toktype == self.SEQ:
            if len(tok) == 2:
                return self.expandtok(tok[1])
            elif len(tok) == 1:
                return None
            else:
                subtree = [self.SEQ]
                for t in tok[1:]:
                    x = self.expandtok(t)
                    if x:
                        subtree.append(x)
                return tuple(subtree) if len(subtree) > 2 else \
                       subtree[1] if len(subtree) == 2 else None
        else:
            return tok

    def parse(self):
        tree = [self.SEQ]
        for tok in self.nextkn():
            tree.append(self.expandtok(tok))
        self.tree = tuple(tree)

    def prtok(self, tok, indent):
        if not tok:
            print " " * indent, "None"
            return
        toktype = tok[0]
        if toktype == self.SEQ:
            for t in tok[1:]:
                self.prtok(t, indent)
        else:
            print " " * indent,
            if toktype == self.NIL:
                print "NIL"
            if toktype == self.TEXT:
                print "TEXT \"%s\"" % (tok[1].encode('string_escape'))
            elif toktype == self.LINK:
                print "LINK "
                self.prtok(tok[1], indent+1) # target
                self.prtok(tok[2], indent+1) # text
            elif toktype == self.TMPL:
                print "TMPL"
                self.prtok(tok[1], indent+1) # target
                self.prtok(tok[2], indent+1) # text
            elif toktype == self.REF:
                print "REF"
                self.prtok(tok[1], indent+1) # target
                self.prtok(tok[2], indent+1) # text
            elif toktype == self.IT:
                print "IT"
                self.prtok(tok[1], indent+1)
            elif toktype == self.BOLD:
                print "BOLD"
                self.prtok(tok[1], indent+1)
            elif toktype == self.HDR:
                print "HDR", tok[1]
                self.prtok(tok[2], indent+1)
            elif toktype == self.BAR:
                print "BAR"
            elif toktype == self.ENV:
                print "ENV ",self.envtypes[tok[1]],tok[2]
                self.prtok(tok[3], indent+1)
            elif toktype == self.ITEM:
                print "ITEM"
                self.prtok(tok[1], indent+1)
            elif toktype == self.PARA:
                print "PARA"
    
    def output(self):
        self.prtok(self.tree, 0)


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'
    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):
        if 'file' in keywords:
            self.file = keywords['file']
        elif 'filename' in keywords:
            self.file = open(keywords['filename'])
        elif 'text' in keywords:
            self.text = keywords['text'].split("\n")
        elif 'lang' in keywords:
            self.lang = keywords['lang']
        elif 'html_base' in keywords:
            self.html_base = keywords['html_base']
        elif 'image_base' in keywords:
            self.image_base = keywords['image_base']
        elif 'media_base' in keywords:
            self.media_base = keywords['media_base']

    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)
        else:
            return None


    # 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": "Aymar",           # 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
	"e": "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
    }

    def str_nil(self, tok, env):
        return None

    def str_text(self, tok, env):
        return tok[1]

    def str_seq(self, tok, env):
            str = ""
            for t in tok[1:]:
                s = self.fmtok(t, env)
                if s:
                    str += s
            return str

    def fmtok(self, tok, env):
        if type(tok) != TupleType:
            return ""
        toktype = tok[0]
        if toktype == self.NIL:
            return self.str_nil(tok, env)
        if toktype == self.TEXT:
            return self.str_text(tok, env)
        elif toktype == self.LINK:
            return self.str_link(tok, env)
        elif toktype == self.TMPL:
            return self.str_tmpl(tok, env)
        elif toktype == self.REF:
            return self.str_ref(tok, env)
        elif toktype == self.IT:
            return self.str_it(tok, env)
        elif toktype == self.BOLD:
            return self.str_bold(tok, env)
        elif toktype == self.HDR:
            return self.str_hdr(tok, env)
        elif toktype == self.BAR:
            return self.str_bar(tok, env)
        elif toktype == self.ENV:
            return self.str_env(tok, env)
        elif toktype == self.ITEM:
            return self.str_item(tok, env)
        elif toktype == self.SEQ:
            return self.str_seq(tok, env)
        elif toktype == self.PARA:
            return self.str_para(tok, env)

    def __str__(self):
        return self.fmtok(self.tree, None)
    
        

Return to:

Send suggestions and report system problems to the System administrator.