xrt 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/xrt/command/lcs.rb +1 -1
- data/lib/xrt/parser.rb +19 -5
- data/lib/xrt/statement.rb +40 -7
- data/lib/xrt/syntax.rb +8 -0
- data/lib/xrt/version.rb +1 -1
- data/test/test-command-extract.rb +1 -1
- data/test/test-parser.rb +42 -16
- data/test/test-statement.rb +49 -7
- data/test/test-syntax.rb +10 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9fe8c82b81533d58ad55d63fc95dedddbfff782a
|
4
|
+
data.tar.gz: 437782711e48511fe351953897985544b3b0309f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea18450231e65f911b90975f96612e83b256811f09afe44dd6a19674a4d65abc3918168851c44cba4deb48ded8379cecb6c9b1fb8bf48d9027e1131e44a3cab7
|
7
|
+
data.tar.gz: dd845b2ccd015828d7aaa15934f1079ec6bd6b885a4cc0ccdbde8fd18f637e83944e998ee079a5ca371ca18f3fec0ff4933b33acc87757a6311ca9ccb0913ec8
|
data/lib/xrt/command/lcs.rb
CHANGED
data/lib/xrt/parser.rb
CHANGED
@@ -25,11 +25,7 @@ module XRT
|
|
25
25
|
when XRT::Statement::End
|
26
26
|
node << statement
|
27
27
|
break
|
28
|
-
|
29
|
-
node << statement
|
30
|
-
when XRT::Statement::Whitespace
|
31
|
-
node << statement
|
32
|
-
when XRT::Statement::Directive
|
28
|
+
else
|
33
29
|
node << statement
|
34
30
|
end
|
35
31
|
end
|
@@ -44,6 +40,10 @@ module XRT
|
|
44
40
|
while reading.length > 0
|
45
41
|
if got = read_directive(reading)
|
46
42
|
result << got
|
43
|
+
elsif got = read_tag_start(reading)
|
44
|
+
result << got
|
45
|
+
elsif got = read_tag_end(reading)
|
46
|
+
result << got
|
47
47
|
elsif got = read_text(reading)
|
48
48
|
result.concat(split_whitespace(got))
|
49
49
|
else
|
@@ -80,10 +80,14 @@ module XRT
|
|
80
80
|
|
81
81
|
def read_text source
|
82
82
|
return nil if source[0...2] == '[%'
|
83
|
+
return nil if source[0] == '<'
|
84
|
+
return nil if source[0] == '>'
|
83
85
|
|
84
86
|
buffer = ''
|
85
87
|
while true
|
86
88
|
return buffer if source[0...2] == '[%'
|
89
|
+
return buffer if source[0] == '<'
|
90
|
+
return buffer if source[0] == '>'
|
87
91
|
break if source.length < 2
|
88
92
|
buffer << source.slice!(0)
|
89
93
|
end
|
@@ -92,5 +96,15 @@ module XRT
|
|
92
96
|
|
93
97
|
buffer
|
94
98
|
end
|
99
|
+
|
100
|
+
def read_tag_start source
|
101
|
+
return nil unless source[0] == '<'
|
102
|
+
source.slice!(0)
|
103
|
+
end
|
104
|
+
|
105
|
+
def read_tag_end source
|
106
|
+
return nil unless source[0] == '>'
|
107
|
+
source.slice!(0)
|
108
|
+
end
|
95
109
|
end
|
96
110
|
end
|
data/lib/xrt/statement.rb
CHANGED
@@ -15,7 +15,7 @@ module XRT
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def inspect
|
18
|
-
"
|
18
|
+
"(#{self.class}:#{self.content})"
|
19
19
|
end
|
20
20
|
|
21
21
|
def children
|
@@ -49,12 +49,26 @@ module XRT
|
|
49
49
|
children.concat(children.map{|child| child.children }.flatten)
|
50
50
|
end
|
51
51
|
|
52
|
+
def contains_directive?
|
53
|
+
children.any?{|s|
|
54
|
+
s.kind_of? XRT::Statement::Directive
|
55
|
+
} || children.any?{|c|
|
56
|
+
c.contains_directive?
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
52
60
|
def find_blocks
|
53
61
|
children.select{|child|
|
54
62
|
child.kind_of? XRT::Statement::Block
|
55
63
|
}.concat(children.map{|child| child.find_blocks }.flatten)
|
56
64
|
end
|
57
65
|
|
66
|
+
def find_blocks_with_directive
|
67
|
+
children.select{|child|
|
68
|
+
child.kind_of?(XRT::Statement::Block) && child.contains_directive?
|
69
|
+
}.concat(children.map{|child| child.find_blocks_with_directive }.flatten)
|
70
|
+
end
|
71
|
+
|
58
72
|
def find_block_texts
|
59
73
|
children.select{|child|
|
60
74
|
child.kind_of?(XRT::Statement::Block) || child.kind_of?(XRT::Statement::Text)
|
@@ -82,6 +96,10 @@ module XRT
|
|
82
96
|
XRT::Statement::End.new content
|
83
97
|
elsif syntax.block? content
|
84
98
|
XRT::Statement::Directive.new content
|
99
|
+
elsif syntax.tag_start? content
|
100
|
+
XRT::Statement::Tag.new content
|
101
|
+
elsif syntax.tag_end? content
|
102
|
+
XRT::Statement::TagEnd.new content
|
85
103
|
elsif syntax.whitespace? content
|
86
104
|
XRT::Statement::Whitespace.new content
|
87
105
|
else
|
@@ -113,7 +131,7 @@ module XRT
|
|
113
131
|
end
|
114
132
|
|
115
133
|
def inspect
|
116
|
-
"
|
134
|
+
"(#{self.class}:#{self.children})"
|
117
135
|
end
|
118
136
|
end
|
119
137
|
|
@@ -123,16 +141,23 @@ module XRT
|
|
123
141
|
class Whitespace < Statement
|
124
142
|
end
|
125
143
|
|
144
|
+
class TagStart < Statement
|
145
|
+
end
|
146
|
+
|
126
147
|
class Directive < Statement
|
127
148
|
end
|
128
149
|
|
129
|
-
class End <
|
150
|
+
class End < Statement
|
130
151
|
end
|
131
152
|
|
132
|
-
class
|
153
|
+
class TagEnd < End
|
154
|
+
end
|
155
|
+
|
156
|
+
class Block < Statement
|
133
157
|
def initialize(content)
|
134
|
-
super
|
135
158
|
@children = []
|
159
|
+
|
160
|
+
self << Directive.new(content)
|
136
161
|
end
|
137
162
|
|
138
163
|
def closed?
|
@@ -149,11 +174,19 @@ module XRT
|
|
149
174
|
end
|
150
175
|
|
151
176
|
def content
|
152
|
-
|
177
|
+
children.map{|c| c.content }.join
|
153
178
|
end
|
154
179
|
|
155
180
|
def inspect
|
156
|
-
"
|
181
|
+
"(#{self.class}:#{self.children})"
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
class Tag < Block
|
186
|
+
def initialize content
|
187
|
+
@children = []
|
188
|
+
tag_start = XRT::Statement::TagStart.new content
|
189
|
+
self << tag_start
|
157
190
|
end
|
158
191
|
end
|
159
192
|
end
|
data/lib/xrt/syntax.rb
CHANGED
data/lib/xrt/version.rb
CHANGED
data/test/test-parser.rb
CHANGED
@@ -49,20 +49,32 @@ class TestParser < Test::Unit::TestCase
|
|
49
49
|
], doc.children
|
50
50
|
end
|
51
51
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
52
|
+
def test_document_nested_block
|
53
|
+
parser = XRT::Parser.new('[% IF a %][% IF b %]1[% END %][% END %]')
|
54
|
+
doc = parser.document
|
55
|
+
assert doc.kind_of? XRT::Statement::Document
|
56
|
+
if_block1 = XRT::Statement::Block.new('[% IF a %]')
|
57
|
+
if_block2 = XRT::Statement::Block.new('[% IF b %]')
|
58
|
+
if_block2 << XRT::Statement::Text.new('1')
|
59
|
+
if_block2 << XRT::Statement::Text.new('[% END %]')
|
60
|
+
if_block1 << if_block2
|
61
|
+
if_block1 << XRT::Statement::Text.new('[% END %]')
|
62
|
+
assert_equal [
|
63
|
+
if_block1
|
64
|
+
], doc.children
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_document_tag
|
68
|
+
parser = XRT::Parser.new('<div>')
|
69
|
+
doc = parser.document
|
70
|
+
assert doc.kind_of? XRT::Statement::Document
|
71
|
+
tag = XRT::Statement::Tag.new '<'
|
72
|
+
tag << XRT::Statement::Text.new('div')
|
73
|
+
tag << XRT::Statement::TagEnd.new('>')
|
74
|
+
assert_equal [
|
75
|
+
tag
|
76
|
+
], doc.children
|
77
|
+
end
|
66
78
|
|
67
79
|
def test_read_directive
|
68
80
|
assert_equal '[% %]', @parser.read_directive('[% %]')
|
@@ -75,9 +87,23 @@ class TestParser < Test::Unit::TestCase
|
|
75
87
|
assert_equal 'hi', @parser.read_text('hi')
|
76
88
|
assert_equal 'hi[', @parser.read_text('hi[')
|
77
89
|
assert_equal 'hi', @parser.read_text('hi[%')
|
90
|
+
assert_equal 'hi', @parser.read_text('hi<')
|
91
|
+
assert_equal 'hi', @parser.read_text('hi>')
|
78
92
|
assert_nil @parser.read_text('[% %]')
|
79
93
|
end
|
80
94
|
|
95
|
+
def test_read_tag_start
|
96
|
+
assert_equal '<', @parser.read_tag_start('<')
|
97
|
+
assert_equal '<', @parser.read_tag_start('<div')
|
98
|
+
assert_nil @parser.read_tag_start('hi')
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_read_tag_end
|
102
|
+
assert_equal '>', @parser.read_tag_end('>')
|
103
|
+
assert_equal '>', @parser.read_tag_end('>>')
|
104
|
+
assert_nil @parser.read_tag_end('hi')
|
105
|
+
end
|
106
|
+
|
81
107
|
def test_split_whitespace
|
82
108
|
assert_equal([], @parser.split_whitespace(""))
|
83
109
|
assert_equal([" "], @parser.split_whitespace(" "))
|
@@ -91,7 +117,7 @@ class TestParser < Test::Unit::TestCase
|
|
91
117
|
|
92
118
|
def test_tokens
|
93
119
|
test_cases = [
|
94
|
-
['<html>', ['<html>']],
|
120
|
+
['<html>', ['<', 'html', '>']],
|
95
121
|
['a [% b %] c', ['a', ' ', '[% b %]', ' ', 'c']],
|
96
122
|
['[% a %] [% b %] [% c %]', ['[% a %]', ' ', '[% b %]', ' ', '[% c %]']],
|
97
123
|
['[% FOR k IN [1, 2, 3] %]', ['[% FOR k IN [1, 2, 3] %]']],
|
@@ -99,7 +125,7 @@ class TestParser < Test::Unit::TestCase
|
|
99
125
|
%q([% WRAPPER "wrapper.tt" WITH args = [1,2,3] %]<div></div>[% END %]),
|
100
126
|
[
|
101
127
|
%q([% WRAPPER "wrapper.tt" WITH args = [1,2,3] %]),
|
102
|
-
|
128
|
+
*%w(< div > < /div >),
|
103
129
|
%q([% END %]),
|
104
130
|
]
|
105
131
|
],
|
data/test/test-statement.rb
CHANGED
@@ -6,8 +6,10 @@ class TestStatementFactory < Test::Unit::TestCase
|
|
6
6
|
assert XRT::Statement::Factory.new_from_content('hi').kind_of? XRT::Statement::Text
|
7
7
|
assert XRT::Statement::Factory.new_from_content(' ').kind_of? XRT::Statement::Whitespace
|
8
8
|
assert XRT::Statement::Factory.new_from_content('[% foo %]').kind_of? XRT::Statement::Directive
|
9
|
-
assert XRT::Statement::Factory.new_from_content('[% IF 1 %]').kind_of? XRT::Statement::Block
|
10
9
|
assert XRT::Statement::Factory.new_from_content('[% foo IF 1 %]').kind_of? XRT::Statement::Directive
|
10
|
+
assert XRT::Statement::Factory.new_from_content('[% IF 1 %]').kind_of? XRT::Statement::Block
|
11
|
+
assert XRT::Statement::Factory.new_from_content('<').kind_of? XRT::Statement::Tag
|
12
|
+
assert XRT::Statement::Factory.new_from_content('>').kind_of? XRT::Statement::TagEnd
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
@@ -44,6 +46,43 @@ HTML
|
|
44
46
|
assert_equal text.children, []
|
45
47
|
end
|
46
48
|
|
49
|
+
def test_tag_start
|
50
|
+
tag_start = XRT::Statement::TagStart.new('<')
|
51
|
+
assert tag_start.kind_of? XRT::Statement
|
52
|
+
assert_equal tag_start.content, '<'
|
53
|
+
assert_equal tag_start.children, []
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_tag_end
|
57
|
+
tag_end = XRT::Statement::TagEnd.new('<')
|
58
|
+
assert tag_end.kind_of? XRT::Statement::End
|
59
|
+
assert_equal tag_end.content, '<'
|
60
|
+
assert_equal tag_end.children, []
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_tag
|
64
|
+
tag = XRT::Statement::Tag.new('<')
|
65
|
+
assert tag.kind_of? XRT::Statement::Tag
|
66
|
+
assert_equal tag.children, [
|
67
|
+
XRT::Statement::TagStart.new('<')
|
68
|
+
]
|
69
|
+
assert_false tag.closed?
|
70
|
+
tag << XRT::Statement::TagEnd.new('>')
|
71
|
+
assert tag.closed?
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_tag_contains_directive?
|
75
|
+
tag = XRT::Statement::Tag.new('<')
|
76
|
+
assert_false tag.contains_directive?
|
77
|
+
|
78
|
+
tag << XRT::Statement::Text.new('div')
|
79
|
+
assert_false tag.contains_directive?
|
80
|
+
|
81
|
+
tag << XRT::Statement::Directive.new('[% foo %]')
|
82
|
+
assert tag.contains_directive?
|
83
|
+
end
|
84
|
+
|
85
|
+
|
47
86
|
def test_end
|
48
87
|
text = XRT::Statement::End.new('[% END %]')
|
49
88
|
assert text.kind_of? XRT::Statement
|
@@ -52,10 +91,11 @@ HTML
|
|
52
91
|
end
|
53
92
|
|
54
93
|
def test_block
|
55
|
-
|
56
|
-
assert
|
57
|
-
assert_equal
|
58
|
-
|
94
|
+
block = XRT::Statement::Block.new('[% IF 1 %]')
|
95
|
+
assert block.kind_of? XRT::Statement
|
96
|
+
assert_equal block.children, [
|
97
|
+
XRT::Statement::Directive.new('[% IF 1 %]')
|
98
|
+
]
|
59
99
|
end
|
60
100
|
|
61
101
|
def test_document
|
@@ -123,8 +163,9 @@ HTML
|
|
123
163
|
document << text_block
|
124
164
|
if_block = XRT::Statement::Block.new('[% IF a %]')
|
125
165
|
document << if_block
|
166
|
+
if_directive = XRT::Statement::Directive.new('[% IF a %]')
|
126
167
|
|
127
|
-
assert_equal [ text_block, if_block ], document.statements, 'returns statements'
|
168
|
+
assert_equal [ text_block, if_block, if_directive ], document.statements, 'returns statements'
|
128
169
|
end
|
129
170
|
|
130
171
|
def test_find_blocks
|
@@ -144,6 +185,7 @@ class TestBlock < Test::Unit::TestCase
|
|
144
185
|
block = XRT::Statement::Block.new('[% IF 1 %]')
|
145
186
|
assert_equal false, block.closed?
|
146
187
|
|
188
|
+
statement_if = XRT::Statement::Directive.new('[% IF 1 %]')
|
147
189
|
statement_ok = XRT::Statement::Text.new('ok')
|
148
190
|
statement_end = XRT::Statement::End.new('[% END %]')
|
149
191
|
|
@@ -153,7 +195,7 @@ class TestBlock < Test::Unit::TestCase
|
|
153
195
|
block << statement_end
|
154
196
|
assert_equal true, block.closed?
|
155
197
|
|
156
|
-
assert_equal block.children, [statement_ok, statement_end]
|
198
|
+
assert_equal block.children, [statement_if, statement_ok, statement_end]
|
157
199
|
|
158
200
|
assert_raises {
|
159
201
|
block << statement_ok
|
data/test/test-syntax.rb
CHANGED
@@ -17,6 +17,16 @@ class TestSyntax < Test::Unit::TestCase
|
|
17
17
|
assert_nil @syntax.whitespace? 'a'
|
18
18
|
end
|
19
19
|
|
20
|
+
def test_tag_start?
|
21
|
+
assert @syntax.tag_start? '<'
|
22
|
+
assert_false @syntax.tag_start? 'a'
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_tag_end?
|
26
|
+
assert @syntax.tag_end? '>'
|
27
|
+
assert_false @syntax.tag_end? 'a'
|
28
|
+
end
|
29
|
+
|
20
30
|
def test_beginning_block?
|
21
31
|
assert @syntax.beginning_block? '[% IF 1 %]'
|
22
32
|
assert @syntax.beginning_block? '[% if 1 %]'
|