summaryrefslogtreecommitdiffstats
path: root/debian/dconv/parser/arguments.py
blob: 096b269f0feff1114e73ee8f3f37d17f86c51516 (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
import sys
import re
import parser

'''
TODO: Allow inner data parsing (this will allow to parse the examples provided in an arguments block)
'''
class Parser(parser.Parser):
    def __init__(self, pctxt):
        parser.Parser.__init__(self, pctxt)
        #template = pctxt.templates.get_template("parser/arguments.tpl")
        #self.replace = template.render().strip()

    def parse(self, line):
        #return re.sub(r'(Arguments *:)', self.replace, line)
        pctxt = self.pctxt

        result = re.search(r'(Arguments? *:)', line)
        if result:
            label = result.group(0)
            content = []

            desc_indent = False
            desc = re.sub(r'.*Arguments? *:', '', line).strip()

            indent = parser.get_indent(line)

            pctxt.next()
            pctxt.eat_empty_lines()

            arglines = []
            if desc != "none":
                add_empty_lines = 0
                while pctxt.has_more_lines() and (parser.get_indent(pctxt.get_line()) > indent):
                    for j in range(0, add_empty_lines):
                        arglines.append("")
                    arglines.append(pctxt.get_line())
                    pctxt.next()
                    add_empty_lines = pctxt.eat_empty_lines()
                    '''
                    print line

                    if parser.get_indent(line) == arg_indent:
                        argument = re.sub(r' *([^ ]+).*', r'\1', line)
                        if argument:
                            #content.append("<b>%s</b>" % argument)
                            arg_desc = [line.replace(argument, " " * len(self.unescape(argument)), 1)]
                            #arg_desc = re.sub(r'( *)([^ ]+)(.*)', r'\1<b>\2</b>\3', line)
                            arg_desc_indent = parser.get_indent(arg_desc[0])
                            arg_desc[0] = arg_desc[0][arg_indent:]
                            pctxt.next()
                            add_empty_lines = 0
                            while pctxt.has_more_lines and parser.get_indent(pctxt.get_line()) >= arg_indent:
                                for i in range(0, add_empty_lines):
                                    arg_desc.append("")
                                arg_desc.append(pctxt.get_line()[arg_indent:])
                                pctxt.next()
                                add_empty_lines = pctxt.eat_empty_lines()
                            # TODO : reduce space at the beginnning
                            content.append({
                                'name': argument,
                                'desc': arg_desc
                            })
                    '''

                if arglines:
                    new_arglines = []
                    #content = self.parse_args(arglines)
                    parser.remove_indent(arglines)
                    '''
                    pctxt2 = parser.PContext(pctxt.templates)
                    pctxt2.set_content_list(arglines)
                    while pctxt2.has_more_lines():
                        new_arglines.append(parser.example.Parser(pctxt2).parse(pctxt2.get_line()))
                        pctxt2.next()
                    arglines = new_arglines
                    '''

            pctxt.stop = True

            template = pctxt.templates.get_template("parser/arguments.tpl")
            return template.render(
                pctxt=pctxt,
                label=label,
                desc=desc,
                content=arglines
                #content=content
            )
            return line

        return line

'''
    def parse_args(self, data):
        args = []

        pctxt = parser.PContext()
        pctxt.set_content_list(data)

        while pctxt.has_more_lines():
            line = pctxt.get_line()
            arg_indent = parser.get_indent(line)
            argument = re.sub(r' *([^ ]+).*', r'\1', line)
            if True or argument:
                arg_desc = []
                trailing_desc = line.replace(argument, " " * len(self.unescape(argument)), 1)[arg_indent:]
                if trailing_desc.strip():
                    arg_desc.append(trailing_desc)
                pctxt.next()
                add_empty_lines = 0
                while pctxt.has_more_lines() and parser.get_indent(pctxt.get_line()) > arg_indent:
                    for i in range(0, add_empty_lines):
                        arg_desc.append("")
                    arg_desc.append(pctxt.get_line()[arg_indent:])
                    pctxt.next()
                    add_empty_lines = pctxt.eat_empty_lines()

                parser.remove_indent(arg_desc)

                args.append({
                    'name': argument,
                    'desc': arg_desc
                })
        return args

    def unescape(self, s):
        s = s.replace("&lt;", "<")
        s = s.replace("&gt;", ">")
        # this has to be last:
        s = s.replace("&amp;", "&")
        return s
'''