xrt 0.0.7 → 0.0.8
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/parser.rb +89 -18
- data/lib/xrt/statement.rb +19 -30
- data/lib/xrt/version.rb +1 -1
- data/test/test-parser.rb +97 -6
- data/test/test-statement.rb +27 -12
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e794554d3ea1a41e6b8baf7b319eef351a0e75c
|
4
|
+
data.tar.gz: 6f474d42d839fcabd1d80bedd8e730c9033f4cb2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d9c72d02fe07ecaa4175ea58f1f690dc9226824965320e875e31a928a41b54f3c28b745f4a2aff834c5bb2e4e81082a5e6ecfafd420d6759fb1da3ec0ebf46e
|
7
|
+
data.tar.gz: 5da38ce8f05c3f78548d09a8d9221c89ba7b9815e20feb19b5979aed68f212ca5ccceab26ee3d8bc690ed43663a6032648e8588502db9b2804b6981ad20d4dac
|
data/lib/xrt/parser.rb
CHANGED
@@ -19,31 +19,44 @@ module XRT
|
|
19
19
|
# push contents to (container) node
|
20
20
|
# returns parsed container node
|
21
21
|
# return when tokenized is empty, or node is closed
|
22
|
-
def parse_contents(tokenized, node)
|
22
|
+
def parse_contents(tokenized, node, in_raw_text = false)
|
23
|
+
if node.kind_of?(XRT::Statement::TagPair) && node.children[0].tag_raw_text_element?
|
24
|
+
parse_raw_text_element(tokenized, node)
|
25
|
+
return
|
26
|
+
end
|
27
|
+
|
23
28
|
while tokenized.length > 0
|
24
|
-
statement =
|
29
|
+
statement = new_statement(tokenized.shift)
|
25
30
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
if statement.tag_opening?
|
30
|
-
statement = XRT::Statement::TagPair.new(statement)
|
31
|
+
unless in_raw_text
|
32
|
+
case statement
|
33
|
+
when XRT::Statement::Tag
|
31
34
|
parse_contents(tokenized, statement)
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
35
|
+
if statement.tag_opening?
|
36
|
+
statement = XRT::Statement::TagPair.new(statement)
|
37
|
+
parse_contents(tokenized, statement, in_raw_text)
|
38
|
+
node << statement
|
39
|
+
next
|
40
|
+
elsif statement.tag_closing?
|
41
|
+
statement = XRT::Statement::TagPairEnd.new(statement)
|
42
|
+
node << statement
|
43
|
+
break
|
44
|
+
else
|
45
|
+
node << statement
|
46
|
+
next
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
if in_raw_text
|
52
|
+
if statement.kind_of?(XRT::Statement::Tag) || statement.kind_of?(XRT::Statement::TagEnd)
|
53
|
+
statement = XRT::Statement::Text.new(statement.content)
|
41
54
|
end
|
42
55
|
end
|
43
56
|
|
44
57
|
case statement
|
45
|
-
when XRT::Statement::
|
46
|
-
parse_contents(tokenized, statement)
|
58
|
+
when XRT::Statement::ControlBlock
|
59
|
+
parse_contents(tokenized, statement, in_raw_text)
|
47
60
|
node << statement
|
48
61
|
when XRT::Statement::End
|
49
62
|
node << statement
|
@@ -56,6 +69,64 @@ module XRT
|
|
56
69
|
node
|
57
70
|
end
|
58
71
|
|
72
|
+
# parse all statements as texts and whitespaces until </script> will appear.
|
73
|
+
def parse_raw_text_element(tokenized, node)
|
74
|
+
syntax = XRT::Syntax.new
|
75
|
+
tag_name = node.children[0].tag_name
|
76
|
+
|
77
|
+
while tokenized.length > 0
|
78
|
+
maybe_close_tag = new_statement(tokenized[0])
|
79
|
+
maybe_closing_content = new_statement(tokenized[1])
|
80
|
+
|
81
|
+
if maybe_close_tag.kind_of?(XRT::Statement::Tag)
|
82
|
+
maybe_close_tag << maybe_closing_content
|
83
|
+
if maybe_close_tag.tag_closing? && maybe_close_tag.tag_name == tag_name
|
84
|
+
close_tag = new_statement(tokenized.shift)
|
85
|
+
parse_contents(tokenized, close_tag)
|
86
|
+
statement = XRT::Statement::TagPairEnd.new(close_tag)
|
87
|
+
node << statement
|
88
|
+
return
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
content = tokenized.shift
|
93
|
+
block_level = syntax.block_level content
|
94
|
+
if block_level == 1
|
95
|
+
statement = XRT::Statement::ControlBlock.new content
|
96
|
+
parse_contents(tokenized, statement, true)
|
97
|
+
node << statement
|
98
|
+
elsif syntax.block? content
|
99
|
+
node << XRT::Statement::Directive.new(content)
|
100
|
+
elsif syntax.whitespace? content
|
101
|
+
node << XRT::Statement::Whitespace.new(content)
|
102
|
+
else
|
103
|
+
node << XRT::Statement::Text.new(content)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def new_statement content
|
109
|
+
syntax = XRT::Syntax.new
|
110
|
+
|
111
|
+
block_level = syntax.block_level content
|
112
|
+
|
113
|
+
if block_level == 1
|
114
|
+
XRT::Statement::ControlBlock.new content
|
115
|
+
elsif block_level == -1
|
116
|
+
XRT::Statement::End.new content
|
117
|
+
elsif syntax.block? content
|
118
|
+
XRT::Statement::Directive.new content
|
119
|
+
elsif syntax.tag_start? content
|
120
|
+
XRT::Statement::Tag.new content
|
121
|
+
elsif syntax.tag_end? content
|
122
|
+
XRT::Statement::TagEnd.new content
|
123
|
+
elsif syntax.whitespace? content
|
124
|
+
XRT::Statement::Whitespace.new content
|
125
|
+
else
|
126
|
+
XRT::Statement::Text.new content
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
59
130
|
def tokens
|
60
131
|
reading = @source.dup
|
61
132
|
result = []
|
data/lib/xrt/statement.rb
CHANGED
@@ -11,13 +11,17 @@ module XRT
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def == other
|
14
|
-
self.class == other.class && self.
|
14
|
+
self.class == other.class && self.to_s == other.to_s
|
15
15
|
end
|
16
16
|
|
17
17
|
def inspect
|
18
18
|
"(#{self.class}:#{self.content})"
|
19
19
|
end
|
20
20
|
|
21
|
+
def to_s
|
22
|
+
inspect
|
23
|
+
end
|
24
|
+
|
21
25
|
def children
|
22
26
|
[]
|
23
27
|
end
|
@@ -92,30 +96,6 @@ module XRT
|
|
92
96
|
end
|
93
97
|
|
94
98
|
class Statement
|
95
|
-
module Factory
|
96
|
-
def self.new_from_content content
|
97
|
-
syntax = XRT::Syntax.new
|
98
|
-
|
99
|
-
block_level = syntax.block_level content
|
100
|
-
|
101
|
-
if block_level == 1
|
102
|
-
XRT::Statement::Block.new content
|
103
|
-
elsif block_level == -1
|
104
|
-
XRT::Statement::End.new content
|
105
|
-
elsif syntax.block? content
|
106
|
-
XRT::Statement::Directive.new content
|
107
|
-
elsif syntax.tag_start? content
|
108
|
-
XRT::Statement::Tag.new content
|
109
|
-
elsif syntax.tag_end? content
|
110
|
-
XRT::Statement::TagEnd.new content
|
111
|
-
elsif syntax.whitespace? content
|
112
|
-
XRT::Statement::Whitespace.new content
|
113
|
-
else
|
114
|
-
XRT::Statement::Text.new content
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
end
|
119
99
|
|
120
100
|
class Document < Statement
|
121
101
|
def initialize
|
@@ -134,10 +114,6 @@ module XRT
|
|
134
114
|
children.map{|c| c.content }.join
|
135
115
|
end
|
136
116
|
|
137
|
-
def == other
|
138
|
-
self.content == other.content && self.children.zip(other.children).all{|a, b| p [a, b]; a == b }
|
139
|
-
end
|
140
|
-
|
141
117
|
def inspect
|
142
118
|
"(#{self.class}:#{self.children})"
|
143
119
|
end
|
@@ -190,6 +166,9 @@ module XRT
|
|
190
166
|
end
|
191
167
|
end
|
192
168
|
|
169
|
+
class ControlBlock < Block
|
170
|
+
end
|
171
|
+
|
193
172
|
class Tag < Block
|
194
173
|
def initialize content
|
195
174
|
@children = []
|
@@ -203,6 +182,12 @@ module XRT
|
|
203
182
|
void_element_names.include?(self.tag_name)
|
204
183
|
end
|
205
184
|
|
185
|
+
def tag_raw_text_element?
|
186
|
+
# https://www.w3.org/TR/html5/syntax.html#raw-text-elements
|
187
|
+
raw_element_names = %w( script style )
|
188
|
+
raw_element_names.include?(self.tag_name)
|
189
|
+
end
|
190
|
+
|
206
191
|
def tag_name
|
207
192
|
return nil if @children.empty?
|
208
193
|
matched = @children[1].content.match(%r{\A/?(\w+)})
|
@@ -211,7 +196,7 @@ module XRT
|
|
211
196
|
end
|
212
197
|
|
213
198
|
def tag_opening?
|
214
|
-
!tag_independent? && !tag_closing? && !tag_void_element?
|
199
|
+
!tag_independent? && !tag_closing? && !tag_void_element? && !tag_comment?
|
215
200
|
end
|
216
201
|
|
217
202
|
def tag_closing?
|
@@ -221,6 +206,10 @@ module XRT
|
|
221
206
|
def tag_independent?
|
222
207
|
@children[-2].content[-1] == '/'
|
223
208
|
end
|
209
|
+
|
210
|
+
def tag_comment?
|
211
|
+
@children[1].content[0] == '!'
|
212
|
+
end
|
224
213
|
end
|
225
214
|
|
226
215
|
class TagPair < Block
|
data/lib/xrt/version.rb
CHANGED
data/test/test-parser.rb
CHANGED
@@ -6,6 +6,18 @@ class TestParser < Test::Unit::TestCase
|
|
6
6
|
@parser = XRT::Parser.new
|
7
7
|
end
|
8
8
|
|
9
|
+
def test_new_statement
|
10
|
+
parser = XRT::Parser.new('')
|
11
|
+
|
12
|
+
assert parser.new_statement('hi').kind_of? XRT::Statement::Text
|
13
|
+
assert parser.new_statement(' ').kind_of? XRT::Statement::Whitespace
|
14
|
+
assert parser.new_statement('[% foo %]').kind_of? XRT::Statement::Directive
|
15
|
+
assert parser.new_statement('[% foo IF 1 %]').kind_of? XRT::Statement::Directive
|
16
|
+
assert parser.new_statement('[% IF 1 %]').kind_of? XRT::Statement::ControlBlock
|
17
|
+
assert parser.new_statement('<').kind_of? XRT::Statement::Tag
|
18
|
+
assert parser.new_statement('>').kind_of? XRT::Statement::TagEnd
|
19
|
+
end
|
20
|
+
|
9
21
|
def test_document_empty
|
10
22
|
parser = XRT::Parser.new('')
|
11
23
|
doc = parser.document
|
@@ -41,9 +53,9 @@ class TestParser < Test::Unit::TestCase
|
|
41
53
|
parser = XRT::Parser.new('[% IF a %]1[% END %]')
|
42
54
|
doc = parser.document
|
43
55
|
assert doc.kind_of? XRT::Statement::Document
|
44
|
-
if_block = XRT::Statement::
|
56
|
+
if_block = XRT::Statement::ControlBlock.new('[% IF a %]')
|
45
57
|
if_block << XRT::Statement::Text.new('1')
|
46
|
-
if_block << XRT::Statement::
|
58
|
+
if_block << XRT::Statement::End.new('[% END %]')
|
47
59
|
assert_equal [
|
48
60
|
if_block
|
49
61
|
], doc.children
|
@@ -53,12 +65,12 @@ class TestParser < Test::Unit::TestCase
|
|
53
65
|
parser = XRT::Parser.new('[% IF a %][% IF b %]1[% END %][% END %]')
|
54
66
|
doc = parser.document
|
55
67
|
assert doc.kind_of? XRT::Statement::Document
|
56
|
-
if_block1 = XRT::Statement::
|
57
|
-
if_block2 = XRT::Statement::
|
68
|
+
if_block1 = XRT::Statement::ControlBlock.new('[% IF a %]')
|
69
|
+
if_block2 = XRT::Statement::ControlBlock.new('[% IF b %]')
|
58
70
|
if_block2 << XRT::Statement::Text.new('1')
|
59
|
-
if_block2 << XRT::Statement::
|
71
|
+
if_block2 << XRT::Statement::End.new('[% END %]')
|
60
72
|
if_block1 << if_block2
|
61
|
-
if_block1 << XRT::Statement::
|
73
|
+
if_block1 << XRT::Statement::End.new('[% END %]')
|
62
74
|
assert_equal [
|
63
75
|
if_block1
|
64
76
|
], doc.children
|
@@ -104,6 +116,85 @@ class TestParser < Test::Unit::TestCase
|
|
104
116
|
], doc.children
|
105
117
|
end
|
106
118
|
|
119
|
+
def test_block_in_raw_text_element
|
120
|
+
parser = XRT::Parser.new('<script>[% IF a %]1[% END %][% b %]</script>')
|
121
|
+
doc = parser.document
|
122
|
+
assert doc.kind_of? XRT::Statement::Document
|
123
|
+
|
124
|
+
tag_start = XRT::Statement::Tag.new '<'
|
125
|
+
tag_start << XRT::Statement::Text.new('script')
|
126
|
+
tag_start << XRT::Statement::TagEnd.new('>')
|
127
|
+
|
128
|
+
tag_close = XRT::Statement::Tag.new '<'
|
129
|
+
tag_close << XRT::Statement::Text.new('/script')
|
130
|
+
tag_close << XRT::Statement::TagEnd.new('>')
|
131
|
+
|
132
|
+
if_block = XRT::Statement::ControlBlock.new('[% IF a %]')
|
133
|
+
if_block << XRT::Statement::Text.new('1')
|
134
|
+
if_block << XRT::Statement::End.new('[% END %]')
|
135
|
+
|
136
|
+
directive = XRT::Statement::Directive.new('[% b %]')
|
137
|
+
|
138
|
+
tag_pair = XRT::Statement::TagPair.new(tag_start)
|
139
|
+
tag_pair << if_block
|
140
|
+
tag_pair << directive
|
141
|
+
tag_pair << XRT::Statement::TagPairEnd.new(tag_close)
|
142
|
+
|
143
|
+
assert_equal [
|
144
|
+
tag_pair,
|
145
|
+
], doc.children
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_simple_raw_text_element
|
149
|
+
parser = XRT::Parser.new('<script>1<2</script>')
|
150
|
+
doc = parser.document
|
151
|
+
assert doc.kind_of? XRT::Statement::Document
|
152
|
+
|
153
|
+
tag_start = XRT::Statement::Tag.new '<'
|
154
|
+
tag_start << XRT::Statement::Text.new('script')
|
155
|
+
tag_start << XRT::Statement::TagEnd.new('>')
|
156
|
+
|
157
|
+
tag_close = XRT::Statement::Tag.new '<'
|
158
|
+
tag_close << XRT::Statement::Text.new('/script')
|
159
|
+
tag_close << XRT::Statement::TagEnd.new('>')
|
160
|
+
|
161
|
+
tag_pair = XRT::Statement::TagPair.new(tag_start)
|
162
|
+
tag_pair << XRT::Statement::Text.new('1')
|
163
|
+
tag_pair << XRT::Statement::Text.new('<')
|
164
|
+
tag_pair << XRT::Statement::Text.new('2')
|
165
|
+
tag_pair << XRT::Statement::TagPairEnd.new(tag_close)
|
166
|
+
|
167
|
+
assert_equal [
|
168
|
+
tag_pair,
|
169
|
+
], doc.children
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_raw_text_in_block_in_raw_text_element
|
173
|
+
parser = XRT::Parser.new('<script>[% IF a %]<[% END %]</script>')
|
174
|
+
doc = parser.document
|
175
|
+
assert doc.kind_of? XRT::Statement::Document
|
176
|
+
|
177
|
+
tag_start = XRT::Statement::Tag.new '<'
|
178
|
+
tag_start << XRT::Statement::Text.new('script')
|
179
|
+
tag_start << XRT::Statement::TagEnd.new('>')
|
180
|
+
|
181
|
+
tag_close = XRT::Statement::Tag.new '<'
|
182
|
+
tag_close << XRT::Statement::Text.new('/script')
|
183
|
+
tag_close << XRT::Statement::TagEnd.new('>')
|
184
|
+
|
185
|
+
if_block = XRT::Statement::ControlBlock.new('[% IF a %]')
|
186
|
+
if_block << XRT::Statement::Text.new('<')
|
187
|
+
if_block << XRT::Statement::End.new('[% END %]')
|
188
|
+
|
189
|
+
tag_pair = XRT::Statement::TagPair.new(tag_start)
|
190
|
+
tag_pair << if_block
|
191
|
+
tag_pair << XRT::Statement::TagPairEnd.new(tag_close)
|
192
|
+
|
193
|
+
assert_equal [
|
194
|
+
tag_pair,
|
195
|
+
], doc.children
|
196
|
+
end
|
197
|
+
|
107
198
|
def test_read_directive
|
108
199
|
assert_equal '[% %]', @parser.read_directive('[% %]')
|
109
200
|
assert_equal '[% [ ] %]', @parser.read_directive('[% [ ] %]')
|
data/test/test-statement.rb
CHANGED
@@ -1,18 +1,6 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require 'xrt/statement'
|
3
3
|
|
4
|
-
class TestStatementFactory < Test::Unit::TestCase
|
5
|
-
def test_new_from_string
|
6
|
-
assert XRT::Statement::Factory.new_from_content('hi').kind_of? XRT::Statement::Text
|
7
|
-
assert XRT::Statement::Factory.new_from_content(' ').kind_of? XRT::Statement::Whitespace
|
8
|
-
assert XRT::Statement::Factory.new_from_content('[% foo %]').kind_of? XRT::Statement::Directive
|
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
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
4
|
class TestStatement < Test::Unit::TestCase
|
17
5
|
def test_text
|
18
6
|
text = XRT::Statement::Text.new('hi')
|
@@ -254,6 +242,17 @@ class TestTag < Test::Unit::TestCase
|
|
254
242
|
assert tag.tag_independent?
|
255
243
|
end
|
256
244
|
|
245
|
+
def test_comment
|
246
|
+
tag = XRT::Statement::Tag.new('<')
|
247
|
+
tag << XRT::Statement::Text.new('!--')
|
248
|
+
tag << XRT::Statement::Whitespace.new(' ')
|
249
|
+
tag << XRT::Statement::Text.new('--')
|
250
|
+
tag << XRT::Statement::TagEnd.new('>')
|
251
|
+
assert_false tag.tag_opening?
|
252
|
+
assert_false tag.tag_closing?
|
253
|
+
assert tag.tag_comment?
|
254
|
+
end
|
255
|
+
|
257
256
|
def test_tag_name
|
258
257
|
[
|
259
258
|
'div',
|
@@ -297,4 +296,20 @@ class TestTag < Test::Unit::TestCase
|
|
297
296
|
}
|
298
297
|
end
|
299
298
|
|
299
|
+
def test_raw_text_element?
|
300
|
+
[
|
301
|
+
['div', false],
|
302
|
+
['script', true],
|
303
|
+
['style', true],
|
304
|
+
].each{|test_case|
|
305
|
+
tag_name, expect = *test_case
|
306
|
+
|
307
|
+
tag = XRT::Statement::Tag.new('<')
|
308
|
+
tag << XRT::Statement::Text.new(tag_name)
|
309
|
+
tag << XRT::Statement::TagEnd.new('>')
|
310
|
+
assert_equal expect, tag.tag_raw_text_element?
|
311
|
+
}
|
312
|
+
end
|
313
|
+
|
314
|
+
|
300
315
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xrt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hitode909
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|