Debian Codegraph @

mallard/ducktype/extensions/if.py

raw
1 # Copyright (c) 2018 Shaun McCance <[email protected]>
2 #
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
4 # of this software and associated documentation files (the "Software"), to deal
5 # in the Software without restriction, including without limitation the rights
6 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 # copies of the Software, and to permit persons to whom the Software is
8 # furnished to do so, subject to the following conditions:
9 #
10 # The above copyright notice and this permission notice shall be included in
11 # all copies or substantial portions of the Software.
12 #
13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 # THE SOFTWARE.
20
21 import mallard.ducktype
22
23 IFURI = 'http://projectmallard.org/if/1.0/'
24
25 class IfExtension(mallard.ducktype.parser.ParserExtension):
26 def __init__(self, parser, prefix, version):
27 if version == 'experimental':
28 self.version = version
29 else:
30 raise mallard.ducktype.parser.SyntaxError(
31 'Unsupported if extension version: ' + version,
32 parser)
33 self.parser = parser
34 self.prefix = prefix
35 self.version = version
36 self.parser.document.add_namespace('if', IFURI)
37
38 def parse_line_block(self, line):
39 indent = mallard.ducktype.parser.DuckParser.get_indent(line)
40 iline = line[indent:]
41
42 if iline.strip() == '??':
43 self.parser.push_text()
44 self.parser.unravel_for_block(indent)
45
46 if self.parser.current.is_name('choose', IFURI):
47 elname = 'if:else'
48 else:
49 elname = 'if:choose'
50
51 qnode = mallard.ducktype.parser.Block(elname, indent, parser=self.parser)
52 self.parser.current.add_child(qnode)
53 self.parser.current = qnode
54
55 self.parser.state = mallard.ducktype.parser.DuckParser.STATE_BLOCK_READY
56 return True
57
58 if iline.startswith('? '):
59 self.parser.push_text()
60 self.parser.unravel_for_block(indent)
61
62 if self.parser.current.is_name('choose', IFURI):
63 elname = 'if:when'
64 else:
65 elname = 'if:if'
66
67 qnode = mallard.ducktype.parser.Block(elname, indent, parser=self.parser)
68 qnode.attributes = mallard.ducktype.parser.Attributes()
69 qnode.attributes.add_attribute('test', iline[2:].strip())
70 self.parser.current.add_child(qnode)
71 self.parser.current = qnode
72
73 self.parser.state = mallard.ducktype.parser.DuckParser.STATE_BLOCK_READY
74 return True
75
76 return False