moin-2.0 / MoinMoin / converter / rst_out.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
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
# Copyright: 2008 MoinMoin:BastianBlank
# Copyright: 2010 MoinMoin:DmitryAndreev
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
MoinMoin - reStructuredText markup output converter

Converts an internal document tree into reStructuredText markup.

This converter based on ReStructuredText 2006-09-22.
"""


from __future__ import absolute_import, division

from MoinMoin.util.tree import moin_page, xlink

from emeraldtree import ElementTree as ET

import re

from werkzeug.utils import unescape


class Cell(object):

    def __init__(self, text):
        self.text = text

    def __call__(self):
        return self.text

    def height(self):
        return len(self.text.split('\n'))

    def width(self):
        max = 0
        for i in self.text.split('\n'):
            if len(i) > max:
                max = len(i)
        return max


class Table(object):
    """
    An object of this class collects the structure of a table
    and represent it in ReStructuredText syntax.
    """

    def __init__(self):
        self.i = -1
        self.j = -1
        self.table = []
        self.header_count = 0
        self.rowclass = ''

    def add_row(self):
        """
        Add new row to the table.
        """
        if self.rowclass == 'table-header':
            self.header_count += 1
        row = []
        self.i += 1
        self.j = 0
        self.table.append(row)
        if self.i > 0:
            if len(self.table[-2]) > (self.j):
                self.add_cell(self.table[-2][self.j][0],
                                self.table[-2][self.j][1] - 1, Cell(''))
        return row

    def end_row(self):
        """
        Adds empyt cells to current row if it's too short.

        Moves the row to the head of the table if it is table header.
        """
        if len(self.table) > 1:
            if len(self.table[-2]) > len(self.table[-1]):
                self.add_cell(1, 1, Cell(''))
                self.end_row()
            if self.rowclass == 'table-header':
                self.table.insert(self.header_count - 1, self.table.pop())

    def add_cell(self, cs, rs, cell):
        """
        Adds cell to the row.

        :param cs: number of columns spanned
        """
        if cs < 1 or rs < 1:
            return
        self.table[-1].append((cs, rs, cell))
        for i in range(cs-1):
            self.table[-1].append((cs-i-1, rs, Cell('')))
        self.j += cs
        if self.i > 0:
            if len(self.table[-2]) > self.j:
                self.add_cell(self.table[-2][self.j][0],
                                self.table[-2][self.j][1] - 1, Cell(''))
        return

    def height(self):
        """
        :returns: number of rows in the table
        """
        return len(self.table)

    def width(self):
        """
        :returns: width of rows in the table or zero if rows have different width
        """
        if not self.table:
            return 0
        width = len(self.table[0])
        for row in self.table:
            if len(row) != width:
                return 0
        return width

    def col_width(self, col):
        """
        Counts the width of the column in ReSturcturedText representation.

        :param col: index of the column
        :returns: number of characters
        """
        if self.width() <= col:
            return 0
        width = 0
        for row in self.table:
            if row[col][2].width() > width:
                width = row[col][2].width()
        return width

    def row_height(self, row):
        """
        Counts lines in ReSturcturedText representation of the row

        :param row: index of the row
        :returns: number of lines
        """
        if self.height() <= row:
            return 0
        height = 0
        for col in self.table[row]:
            if col[2].height() > height:
                height = col[2].height()
        return height

    def __repr__(self):
        """
        Represent table using ReStructuredText syntax.
        """
        ret = []
        if self.height() and self.width():
            cols = []
            rows = []
            row = self.table[0]
            for col in range(self.width()):
                cols.append(self.col_width(col))
            for row in range(self.height()):
                rows.append(self.row_height(row))
            ret = []
            line = [u'+']
            row = self.table[0]
            for col in range(len(cols)):
                line.append(u'-'*cols[col])
                if self.table[0][col][0] > 1:
                    line.append(u'-')
                else:
                    line.append(u'+')
            ret.append(''.join(line))
            for row in range(len(rows)):
                for i in range(rows[row]):
                    line = []
                    line.append(u'|')
                    for col in range(len(cols)):
                        if self.table[row][col][2].height() <= i:
                            line.append(''.ljust(cols[col])[:cols[col]])
                        else:
                            line.append(
                                self.table[row][col][2]().split(
                                    u'\n')[i].ljust(cols[col])[:cols[col]])
                        if self.table[row][col][0] > 1:
                            line.append(' ')
                        else:
                            line.append(u'|')

                    ret.append(''.join(line))
                line = [u'+']
                for col in range(len(cols)):
                    if self.table[row][col][1] > 1:
                        line.append(u' '*cols[col])
                    elif row == self.header_count - 1:
                        line.append(u'='*cols[col])
                    else:
                        line.append(u'-'*cols[col])
                    if self.table[row][col][0] > 1:
                        if row + 1 < len(rows)\
                                and self.table[row + 1][col][0] > 1\
                                or row + 1 >= len(rows):
                            line.append(u'-')
                        else:
                            line.append(u'+')
                    else:
                        line.append(u'+')
                ret.append(''.join(line))
        return u'\n'.join(ret)


class ReST(object):
    """
    ReST syntax elements
    It's dummy
    """
    h = u"""= - ` : ' " ~ ^ _ * + # < >""".split()
    a_separator = u'|'
    verbatim = u'::'
    monospace = u'``'
    strong = u"**"
    emphasis = u"*"
    p = u'\n'
    linebreak = u'\n\n'
    separator = u'----'
    list_type = {
        (u'definition', None): u'',
        (u'ordered', None): u'1.',
        (u'ordered', u'lower-alpha'): u'a.',
        (u'ordered', u'upper-alpha'): u'A.',
        (u'ordered', u'lower-roman'): u'i.',
        (u'ordered', u'upper-roman'): u'I.',
        (u'unordered', None): u'*',
        (None, None): u' ',
        }


class Converter(object):
    """
    Converter application/x.moin.document -> text/x.moin.rst
    """
    namespaces = {
        moin_page.namespace: 'moinpage'}

    supported_tag = {
        'moinpage': (
                'a',
                'blockcode',
                'break_line',
                'code',
                'div',
                'emphasis',
                'h',
                'list',
                'list_item',
                'list_item_label',
                'list_item_body',
                'p',
                'page',
                'separator',
                'span',
                'strong',
                'object',
                'table',
                'table_header',
                'teble_footer',
                'table_body',
                'table_row',
                'table_cell')}

    @classmethod
    def factory(cls, input, output, **kw):
        return cls()

    def __init__(self):
        # TODO: create class containing all table attributes
        self.table_tableclass = u''
        self.table_tablestyle = u''
        self.table_rowsclass = u''
        self.table_rowsstyle = u''
        self.table_rowstyle = u''
        self.table_rowclass = u''

        self.list_item_labels = []
        self.list_item_label = u''
        self.list_level = -1

        # 'text' - default status - <p> = '/n' and </p> = '/n'
        # 'table' - text inside table - <p> = '<<BR>>' and </p> = ''
        # 'list' - text inside list -
        #       <p> if after </p> = '<<BR>>' and </p> = ''
        # status added because of
        #  differences in interpretation of <p> in different places
    def __call__(self, root):
        self.status = ['text', ]
        self.last_closed = None
        self.list_item_label = []
        self.footnotes = []
        self.objects = []
        self.all_used_references = []
        self.anonymous_reference = None
        self.used_references = []
        self.delete_newlines = False
        ret = self.open(root)
        notes = u"\n\n".join(u".. [#] {0}".format(note.replace(u"\n", u"\n  ")) for note in self.footnotes)
        if notes:
            return ret + self.define_references() + u"\n\n{0}\n\n".format(notes)

        return ret + self.define_references()

    def open_children(self, elem):
        childrens_output = []
        self.delete_newlines = False
        delete_newlines = False
        for child in elem:
            if isinstance(child, ET.Element):
                childs_output = self.open(child)
                if self.delete_newlines:
                    while childrens_output and re.match(r'(\n*)\Z', childrens_output[-1]):
                        childrens_output.pop()
                    if childrens_output:
                        last_newlines = r'(\n*)\Z'
                        i = -len(re.search(last_newlines, childrens_output[-1]).groups(1)[0])
                        if i:
                            childrens_output[-1] = childrens_output[-1][:i]
                    else:
                        delete_newlines = True
                self.delete_newlines = False
                childrens_output.append(childs_output)
            else:
                if self.status[-1] == "table":
                    if self.last_closed == "p":
                        childrens_output.append(u'\n\n')
                elif self.status[-1] == "list":
                    child =\
                        re.sub(r"\n(.)", lambda m: u"\n{0}{1}".format(u' '*(len(u''.join(self.list_item_labels)) + len(self.list_item_labels)), m.group(1)), child)
                    if self.last_closed == "p":
                        childrens_output.append(u'\n'
                                + u' '
                                * (len(''.join(self.list_item_labels))
                                   + len(self.list_item_labels)))
                elif self.status[-1] == "text":
                    if self.last_closed == "p":
                        childrens_output.append(self.define_references())
                        childrens_output.append(u'\n')
                elif self.status[-2] == "list":
                    child =\
                        re.sub(r"\n(.)", lambda m: u"\n{0}{1}".format(u' '*(len(u''.join(self.list_item_labels)) + len(self.list_item_labels)), m.group(1)), child)
                childrens_output.append(child)
                self.last_closed = 'text'
        self.delete_newlines = delete_newlines
        return u''.join(childrens_output)

    def open(self, elem):
        uri = elem.tag.uri
        name = self.namespaces.get(uri, None)
        if name is not None:
            n = 'open_' + name
            f = getattr(self, n, None)
            if f is not None:
                return f(elem)
        return self.open_children(elem)

    def open_moinpage(self, elem):
        n = 'open_moinpage_' + elem.tag.name.replace('-', '_')
        f = getattr(self, n, None)
        if f:
            ret = f(elem)
            self.last_closed = elem.tag.name.replace('-', '_')
            return ret
        return self.open_children(elem)

    def open_moinpage_a(self, elem):
        href = elem.get(xlink.href, None)
        text = u''.join(elem.itertext()).replace(u'\n', u' ')
        # TODO: check that links have different alt texts
        if text in [t for (t, h) in self.all_used_references]:
            if (text, href) in self.all_used_references:
                return u"`{0}`_".format(text)
            if not self.anonymous_reference:
                self.anonymous_reference = href
                self.used_references.insert(0, (u"_", href))
                return u"`{0}`__".format(text)
            else:
                while text in [t for (t, h) in self.all_used_references]:
                    text = text + u"~"
        self.used_references.append((text, href))
        self.all_used_references.append((text, href))
        #self.objects.append("\n\n.. _%s: %s\n\n" % (text, href))
        return u"`{0}`_".format(text)

    def open_moinpage_blockcode(self, elem):
        text = u''.join(elem.itertext())
        max_subpage_lvl = 3
        text = text.replace(u'\n', u'\n  '
                                  + u' ' * (len(u''.join(self.list_item_labels))
                                         + len(self.list_item_labels)))

        if self.list_level >= 0:
            self.delete_newlines = True
            """
            while self.output and re.match(r'(\n*)\Z', self.output[-1]):
                self.output.pop()
            last_newlines = r'(\n*)\Z'
            if self.output:
                i = -len(re.search(last_newlines, self.output[-1]).groups(1)[0])
                if i:
                    self.output[-1] = self.output[-1][:i]
            """
        return u"::\n\n  {0}{1}\n\n".format(u' ' * (len(u''.join(self.list_item_labels)) + len(self.list_item_labels)), text)

    def open_moinpage_code(self, elem):
        ret = u"{0}{1}{2}".format(ReST.monospace, u''.join(elem.itertext()), ReST.monospace)
        return ret

    def open_moinpage_emphasis(self, elem):
        childrens_output = self.open_children(elem)
        return u"{0}{1}{2}".format(ReST.emphasis, childrens_output, ReST.emphasis)

    def open_moinpage_h(self, elem):
        level = elem.get(moin_page.outline_level, 1)
        text = u''.join(elem.itertext())
        try:
            level = int(level)
        except ValueError:
            raise ElementException(u'page:outline-level needs to be an integer')
        if level < 1:
            level = 1
        elif level > 6:
            level = 6
        ret = u"\n\n{0}\n{1}\n{2}\n\n".format(ReST.h[level] * len(text), text, ReST.h[level] * len(text))
        return ret

    def open_moinpage_line_break(self, elem):
        if self.status[-1] == "list":
            return (ReST.linebreak
                    + u' '
                      * (len(u''.join(self.list_item_labels))
                         + len(self.list_item_labels)))
        if self.last_closed == 'p':
            return u'\n\n'
        return ReST.linebreak

    def open_moinpage_list(self, elem):
        label_type = (elem.get(moin_page.item_label_generate, None),
                        elem.get(moin_page.list_style_type, None))
        self.list_item_labels.append(
            ReST.list_type.get(label_type, u' '))
        self.list_level += 1
        ret = u''
        if self.status[-1] == 'text' and self.last_closed:
            ret = u'\n\n'
        elif self.status[-1] != 'text' or self.last_closed:
            ret = u'\n'
        self.status.append('list')
        self.last_closed = None
        ret += self.open_children(elem)
        self.list_item_labels.pop()
        self.list_level -= 1
        self.status.pop()
        if self.status[-1] == 'list':
            return ret + u''
        return ret + u'\n'

    def open_moinpage_list_item(self, elem):
        self.list_item_label = self.list_item_labels[-1] + u' '
        return self.open_children(elem)

    def open_moinpage_list_item_label(self, elem):
        ret = u''
        if self.list_item_labels[-1] == u'' or self.list_item_labels[-1] == u' ':
            self.list_item_labels[-1] = u' '
            self.list_item_label = self.list_item_labels[-1] + u' '
            ret = (u' '
                   * (len(u''.join(self.list_item_labels[:-1]))
                      + len(self.list_item_labels[:-1])))
            if self.last_closed and self.last_closed != 'list':
                ret = u'\n{0}'.format(ret)
            return ret + self.open_children(elem)
        return self.open_children(elem)

    def open_moinpage_list_item_body(self, elem):
        ret = u''
        if self.last_closed:
            ret = u'\n'
        ret += (u' ' * (len(u''.join(self.list_item_labels[:-1]))
                       + len(self.list_item_labels[:-1]))
                + self.list_item_label)
        if self.list_item_labels[-1] in [u'1.', u'i.', u'I.', u'a.', u'A.']:
            self.list_item_labels[-1] = u'#.'

        ret = self.define_references() + ret + self.open_children(elem)
        if self.last_closed == "text":
            return ret + u'\n'
        return ret

    def open_moinpage_note(self, elem):
        class_ = elem.get(moin_page.note_class, u"")
        if class_:
            self.status.append('list')
            if class_ == u"footnote":
                self.footnotes.append(self.open_children(elem))
            self.status.pop()
        return u' [#]_ '

    def open_moinpage_object(self, elem):
        # TODO: object parameters support
        href = elem.get(xlink.href, u'')
        href = href.split(u'?')
        args = u''
        if len(href) > 1:
            args =[s for s in re.findall(r'(?:^|;|,|&|)(\w+=\w+)(?:,|&|$)',
                                            href[1]) if s[:3] != u'do=']
        href = href[0]
        alt = elem.get(moin_page.alt, u'')
        if not alt:
            ret = u''
        else:
            ret = u'|{0}|'.format(alt)
        args_text = u''
        if args:
            args_text = u"\n  {0}".format(u'\n  '.join(u':{0}: {1}'.format(arg.split(u'=')[0], arg.split(u'=')[1]) for arg in args))
        self.objects.append(u".. {0} image:: {1}{2}".format(ret, href, args_text))
        return ret

    def open_moinpage_p(self, elem):
        ret = u''
        if self.status[-1] == 'text':
            self.status.append('p')
            set = self.define_references()
            if self.last_closed == 'text':
                ret = ReST.p * 2 + self.open_children(elem) + ReST.p + set
            elif self.last_closed:
                ret = ReST.p + self.open_children(elem) + ReST.p + set
            else:
                ret = self.open_children(elem) + ReST.p + set
        elif self.status[-1] == 'table':
            self.status.append('p')
            if self.last_closed and self.last_closed != 'table_cell'\
                                and self.last_closed != 'table_row'\
                                and self.last_closed != 'table_header'\
                                and self.last_closed != 'table_footer'\
                                and self.last_closed != 'table_body'\
                                and self.last_closed != 'line_break':
          #                      and self.last_closed != 'p':
                ret = ReST.linebreak + self.open_children(elem)
            elif self.last_closed == 'p' or self.last_closed == 'line_break':
                ret = self.open_children(elem)
            else:
                ret = self.open_children(elem)
        elif self.status[-1] == 'list':
            self.status.append('p')
            if self.last_closed and self.last_closed == 'list_item_label':
                ret = self.open_children(elem)
            elif self.last_closed and self.last_closed != 'list_item'\
                                and self.last_closed != 'list_item_header'\
                                and self.last_closed != 'list_item_footer'\
                                and self.last_closed != 'p':
                ret = (ReST.linebreak + u' '
                                        * (len(u''.join(self.list_item_labels))
                                           + len(self.list_item_labels)) + self.open_children(elem))
            elif self.last_closed and self.last_closed == 'p':
                #return ReST.p +\
                ret = (u"\n" + u' ' * (len(u''.join(self.list_item_labels))
                                    + len(self.list_item_labels)) + self.open_children(elem))
            else:
                ret = self.open_children(elem)
            if not self.delete_newlines:
                ret += u"\n"
        else:
            self.status.append('p')
            ret = self.open_children(elem)
        self.status.pop()
        return ret

    def open_moinpage_page(self, elem):
        self.last_closed = None
        return self.open_children(elem)

    def open_moinpage_body(self, elem):
        return self.open_children(elem)

    def open_moinpage_part(self, elem):
        type = elem.get(moin_page.content_type, u"").split(u';')
        if len(type) == 2:
            if type[0] == u"x-moin/macro":
                if len(elem) and iter(elem).next().tag.name == "arguments":
                    alt = u"<<{0}({1})>>".format(type[1].split(u'=')[1], u','.join([u''.join(c.itertext()) for c in iter(elem).next() if c.tag.name == "argument"]))
                else:
                    alt = u"<<{0}()>>".format(type[1].split(u'=')[1])

                obj = u".. |{0}| macro:: {1}".format(alt, alt)
                self.objects.append(obj)
                return u" |{0}| ".format(alt)
            elif type[0] == u"x-moin/format":
                elem_it = iter(elem)
                ret = u"\n\n.. parser:{0}".format(type[1].split(u'=')[1])
                if len(elem) and elem_it.next().tag.name == "arguments":
                    args = []
                    for arg in iter(elem).next():
                        if arg.tag.name == "argument":
                            args.append(u"{0}=\"{1}\"".format(arg.get(moin_page.name, u""), u' '.join(arg.itertext())))
                    ret = u'{0} {1}'.format(ret, u' '.join(args))
                    elem = elem_it.next()
                ret = u"{0}\n  {1}".format(ret, u' '.join(elem.itertext()))
                return ret
        return elem.get(moin_page.alt, u'') + u"\n"

    def open_moinpage_inline_part(self, elem):
        return self.open_moinpage_part(elem)

    def open_moinpage_separator(self, elem):
        return u'\n\n' + ReST.separator + u'\n\n'

    def open_moinpage_span(self, elem):
        baseline_shift = elem.get(moin_page.baseline_shift, u'')

        # No text decoration and text size in rst, this can be deleted
        """
        text_decoration = elem.get(moin_page.text_decoration, u'')
        font_size = elem.get(moin_page.font_size, u'')
        if text_decoration == 'line-through':
            self.children.append(iter(elem))
            self.opened.append(elem)
            return ''
        if text_decoration == 'underline':
            self.children.append(iter(elem))
            self.opened.append(elem)
            return ''
        if font_size:
            self.children.append(iter(elem))
            self.opened.append(elem)
            return ''
        """
        if baseline_shift == 'super':
            return u"\\ :sup:`{0}`\\ ".format(u''.join(elem.itertext()))
        if baseline_shift == 'sub':
            return u"\\ :sub:`{0}`\\ ".format(u''.join(elem.itertext()))
        return self.open_children(elem)

    def open_moinpage_strong(self, elem):
        return ReST.strong + self.open_children(elem) + ReST.strong

    def open_moinpage_table(self, elem):
        self.table_tableclass = elem.attrib.get('class', u'')
        self.table_tablestyle = elem.attrib.get('style', u'')
        self.table_rowsstyle = u''
        self.table_rowsclass = u''
        self.status.append('table')
        self.last_closed = None
        self.table = []
        self.tablec = Table()
        self.open_children(elem)
        self.status.pop()
        table = repr(self.tablec)
        if self.status[-1] == "list":
            table =\
                re.sub(r"\n(.)", lambda m: u"\n{0}{1}".format(u' '*(len(u''.join(self.list_item_labels)) + len(self.list_item_labels)), m.group(1)), u"\n" + table)
            return table + ReST.p
        return table + ReST.linebreak

    def open_moinpage_table_header(self, elem):
        # is this correct rowclass?
        self.tablec.rowclass = 'table-header'
        return self.open_children(elem)

    # No table footer support, TODO if needed
    """
    def open_moinpage_table_footer(self, elem):
        self.tablec.rowclass = 'table-footer'
        self.children.append(iter(elem))
        self.opened.append(elem)
        return ''

    def close_moinpage_table_footer(self, elem):
        self.table_rowsclass = ''
        return ''
    """

    def open_moinpage_table_body(self, elem):
        self.tablec.rowclass = 'table-body'
        return self.open_children(elem)

    def open_moinpage_table_row(self, elem):
        self.table_rowclass = elem.attrib.get('class', u'')
        self.table_rowclass = u' '.join([s for s in [self.table_rowsclass,
                                                    self.table_rowclass] if s])
        self.table_rowstyle = elem.attrib.get('style', u'')
        self.table_rowstyle = u' '.join([s for s in [self.table_rowsstyle,
                                                    self.table_rowstyle] if s])
        self.table.append([])
        self.tablec.add_row()
        ret = self.open_children(elem)
        self.table_rowstyle = ''
        self.table_rowclass = ''
        self.tablec.end_row()
        return ret

    def open_moinpage_table_cell(self, elem):
        table_cellclass = elem.attrib.get('class', u'')
        table_cellstyle = elem.attrib.get('style', u'')
        number_cols_spanned\
                = int(elem.get(moin_page.number_cols_spanned, 1))
        number_rows_spanned\
                = int(elem.get(moin_page.number_rows_spanned, 1))

        attrib = []

        # TODO: styles and classes
        """
        if self.table_tableclass:
            attrib.append('tableclass="{0}"'.format(self.table_tableclass))
            self.table_tableclass = ''
        if self.table_tablestyle:
            attrib.append('tablestyle="{0}"'.format(self.table_tablestyle))
            self.table_tableclass = ''
        if self.table_rowclass:
            attrib.append('rowclass="{0}"'.format(self.table_rowclass))
            self.table_rowclass = ''
        if self.table_rowstyle:
            attrib.append('rowclass="{0}"'.format(self.table_rowstyle))
            self.table_rowstyle = ''
        if table_cellclass:
            attrib.append('class="{0}"'.format(table_cellclass))
        if table_cellstyle:
            attrib.append('style="{0}"'.format(table_cellstyle))
        if number_rows_spanned:
            attrib.append('|'+str(number_rows_spanned))

        attrib = ' '.join(attrib)
        """
        self.table[-1].append((number_cols_spanned,
                                number_rows_spanned,
                                [self.open_children(elem)]))
        cell = self.table[-1][-1]
        self.tablec.add_cell(cell[0], cell[1], Cell(u''.join(cell[2])))
        return u''

    def open_moinpage_table_of_content(self, elem):
        depth = elem.get(moin_page.outline_level, u"")
        ret = u"\n\n.. contents::"
        if depth:
            ret += u"\n   :depth: {0}".format(depth)
        return ret + u"\n\n"

    def define_references(self):
        """
        Adds defenitions of founded links and objects to the converter output.
        """
        ret = u''
        self.all_used_references.extend(self.used_references)
        definitions = [u" " * (len(u''.join(self.list_item_labels))
                                    + len(self.list_item_labels))
                                  + u".. _{0}: {1}".format(t, h) for t, h in self.used_references]
        definitions.extend(u" " * (len(u''.join(self.list_item_labels))
                                     + len(self.list_item_labels))
                                  + link for link in self.objects)
        definition_block = u"\n\n".join(definitions)

        if definitions:
            if self.last_closed == 'list_item_label':
                ret += u"\n{0}\n\n".format(definition_block)
            else:
                ret += u"\n\n{0}\n\n".format(definition_block)

        self.used_references = []
        self.objects = []
        self.anonymous_reference = None
        return ret


from . import default_registry
from MoinMoin.util.mime import Type, type_moin_document
default_registry.register(Converter.factory,
                          type_moin_document,
                          Type('text/x-rst'))
default_registry.register(Converter.factory,
                          type_moin_document,
                          Type('x-moin/format;name=rst'))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.