summaryrefslogtreecommitdiff
path: root/sandbox/edloper/epytext/epytext.py
blob: 52071542adb50ffee443825baac60912994ae01d (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

import epydoc.markup.epytext as epytext
import xml.dom.minidom as minidom
import docutils.nodes as nodes
import docutils.parsers

class Parser(docutils.parsers.Parser):
    """The epytext parser."""

    supported = ('epytext')

    settings_spec = (
        'epytext Parser Options',
        None,
        ())

    def __init__(self):
        pass

    def parse(self, inputstring, document):
        errors = []
        epytext_tree = epytext.parse(inputstring)
        
        self.setup_parse(inputstring, document)
        for child in self._parse(epytext_tree):
            self.document.append(child)
        self.finish_parse()
        
    def _parse(self, tree):
        if isinstance(tree, minidom.Document):
            return self._parse(tree.childNodes[0])
        if isinstance(tree, minidom.Text):
            return nodes.Text(tree.data)

        # Get children.
        children = [self._parse(c) for c in tree.childNodes]

        if tree.tagName == 'epytext':
            return children
        if tree.tagName == 'para':
            return nodes.paragraph('','', *children)
        if tree.tagName == 'section':
            return nodes.section('', *children)
        if tree.tagName == 'heading':
            return nodes.title('','', *children)
        if tree.tagName == 'fieldlist':
            return nodes.field_list('', *children)
        if tree.tagName == 'field':
            return nodes.field('', *self._parse_field(tree, children))
        if tree.tagName == 'literalblock':
            return nodes.literal_block('','', *children)
        if tree.tagName == 'doctestblock':
            return nodes.doctest_block('','', *children)
        if tree.tagName == 'ulist':
            return nodes.bullet_list('', *children)
        if tree.tagName == 'olist':
            return nodes.enumerated_list('', *children)
        if tree.tagName == 'li':
            return nodes.list_item('', *children)
        if tree.tagName == 'link':
            # [XX] discards link target.
            name, target = children
            return nodes.title_reference('','', name)
        if tree.tagName == 'uri':
            name, target = children
            return nodes.reference('','', name, refuid=target.astext())
        if tree.tagName == 'code':
            return nodes.literal('','', *children)
        if tree.tagName == 'math':
            return nodes.emphasis('','', *children)
        if tree.tagName == 'italic':
            return nodes.emphasis('','', *children)
        if tree.tagName == 'bold':
            return nodes.strong('','', *children)
        if tree.tagName == 'indexed': 
            # [XX] doesn't mark the fact that it's indexedd
            return nodes.emphasis('','', *children)
        if tree.tagName == 'symbol':
            # use substitutions.  
            # [XX] this needs to be fixed!
            return nodes.Text(children[0])
        elif tree.tagName in ('tag', 'arg', 'name', 'target'):
            return children[0]
        else:
            raise ValueError, ('unknown %s' % tree.tagName)

    def _parse_field(self, tree, children):
        numargs = 0
        while tree.childNodes[numargs+1].tagName == 'arg': numargs += 1
        tag = children[0]
        args = children[1:1+numargs]
        body = children[1+numargs:]
        name = nodes.field_name('','', *children[:1+numargs])
        body = nodes.field_body('', *children[1+numargs:])
        return (name, body)