xrt 0.0.5 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9fe8c82b81533d58ad55d63fc95dedddbfff782a
4
- data.tar.gz: 437782711e48511fe351953897985544b3b0309f
3
+ metadata.gz: 60911a855be15e5cc634ea8efaf464ff478ba591
4
+ data.tar.gz: e57a1f273278eb63443708444b3f239902d124d6
5
5
  SHA512:
6
- metadata.gz: ea18450231e65f911b90975f96612e83b256811f09afe44dd6a19674a4d65abc3918168851c44cba4deb48ded8379cecb6c9b1fb8bf48d9027e1131e44a3cab7
7
- data.tar.gz: dd845b2ccd015828d7aaa15934f1079ec6bd6b885a4cc0ccdbde8fd18f637e83944e998ee079a5ca371ca18f3fec0ff4933b33acc87757a6311ca9ccb0913ec8
6
+ metadata.gz: 47470f9a585967c8410c0062a49f8caf7981bf89f5ceb12d3580e7953957a9f86656805f49b28bdc23d8501998e2d37219c3d5b3b444b25e98f1cff325396d8e
7
+ data.tar.gz: 99fd86545e72f3d627566cc211d4a83fe6ce58341ecc4c74c92983395d26b30996ace9dfd3ad77e28b9a8eb1d8854f85e80f164d889dab7eba76c1951d14d1ef
@@ -9,6 +9,9 @@ module XRT
9
9
  when 'dump'
10
10
  success = XRT::Command::Dump.new.execute(args)
11
11
  exit success ? 0 : 1
12
+ when 'dump_blocks'
13
+ XRT::Command::DumpBlocks.new.execute(args)
14
+ exit 0
12
15
  when 'extract'
13
16
  success = XRT::Command::Extract.new.execute(*args)
14
17
  exit success ? 0 : 1
@@ -0,0 +1,22 @@
1
+ require 'xrt/depth_checker'
2
+
3
+ module XRT
4
+ module Command
5
+ class DumpBlocks
6
+ def execute(files)
7
+ files.each{|file|
8
+ puts dump_file file
9
+ }
10
+ end
11
+
12
+ def dump_file file
13
+ blocks = find_blocks file
14
+ puts blocks.join("\n=====\n")
15
+ end
16
+
17
+ def find_blocks(file)
18
+ XRT::Parser.new(open(file).read).document.find_blocks_with_directive.map{|s| s.auto_indent }
19
+ end
20
+ end
21
+ end
22
+ end
@@ -22,6 +22,7 @@ module XRT
22
22
  found_blocks = from_doc.find_block_texts.select{|block|
23
23
  block.content.index(target_block) == 0
24
24
  }
25
+ found_blocks = resolve_inclusions(found_blocks)
25
26
 
26
27
  if found_blocks.length == 0
27
28
  raise "target_block not found"
@@ -46,6 +47,17 @@ module XRT
46
47
 
47
48
  transaction
48
49
  end
50
+
51
+ # input: [<h1>a</h1>, <h1>]
52
+ # output: [<h1>a</h1>]
53
+ def resolve_inclusions(items)
54
+ items.combination(2){|parent, child|
55
+ if parent.include? child
56
+ items.delete(child)
57
+ end
58
+ }
59
+ items
60
+ end
49
61
  end
50
62
  end
51
63
  end
@@ -1,4 +1,5 @@
1
1
  require 'xrt/depth_checker'
2
2
  require 'xrt/command/dump'
3
+ require 'xrt/command/dump_blocks'
3
4
  require 'xrt/command/extract'
4
5
  require 'xrt/command/lcs'
@@ -15,9 +15,32 @@ module XRT
15
15
  doc
16
16
  end
17
17
 
18
+ # read tokens from tokenized tokens
19
+ # push contents to (container) node
20
+ # returns parsed container node
21
+ # return when tokenized is empty, or node is closed
18
22
  def parse_contents(tokenized, node)
19
23
  while tokenized.length > 0
20
24
  statement = XRT::Statement::Factory.new_from_content(tokenized.shift)
25
+
26
+ case statement
27
+ when XRT::Statement::Tag
28
+ parse_contents(tokenized, statement)
29
+ if statement.tag_opening?
30
+ statement = XRT::Statement::TagPair.new(statement)
31
+ parse_contents(tokenized, statement)
32
+ node << statement
33
+ next
34
+ elsif statement.tag_closing?
35
+ statement = XRT::Statement::TagPairEnd.new(statement)
36
+ node << statement
37
+ break
38
+ else
39
+ node << statement
40
+ next
41
+ end
42
+ end
43
+
21
44
  case statement
22
45
  when XRT::Statement::Block
23
46
  parse_contents(tokenized, statement)
@@ -22,6 +22,14 @@ module XRT
22
22
  []
23
23
  end
24
24
 
25
+ def include? statement
26
+ children.each{|child|
27
+ return true if child.equal? statement
28
+ return true if child.include?(statement)
29
+ }
30
+ return false
31
+ end
32
+
25
33
  def replace_child(new_child, old_child)
26
34
  children.each_with_index{|child, index|
27
35
  if child.equal? old_child
@@ -165,7 +173,7 @@ module XRT
165
173
  end
166
174
 
167
175
  def << statement
168
- raise 'trying to push_child to closed block' if closed?
176
+ raise "trying to push_child to closed block: #{self.inspect} << #{statement.inspect}" if closed?
169
177
  @children << statement
170
178
  end
171
179
 
@@ -188,6 +196,44 @@ module XRT
188
196
  tag_start = XRT::Statement::TagStart.new content
189
197
  self << tag_start
190
198
  end
199
+
200
+ def tag_void_element?
201
+ # https://www.w3.org/TR/html5/syntax.html#void-elements
202
+ void_element_names = %w( area base br col embed hr img input keygen link meta param source track wbr )
203
+ void_element_names.include?(self.tag_name)
204
+ end
205
+
206
+ def tag_name
207
+ @children[1].content.match(%r{\A/?(\S+)})[1].downcase
208
+ end
209
+
210
+ def tag_opening?
211
+ !tag_independent? && !tag_closing? && !tag_void_element?
212
+ end
213
+
214
+ def tag_closing?
215
+ @children[1].content[0] == '/'
216
+ end
217
+
218
+ def tag_independent?
219
+ @children[-2].content[-1] == '/'
220
+ end
221
+ end
222
+
223
+ class TagPair < Block
224
+ def initialize tag
225
+ @children = [ tag ]
226
+ end
227
+ end
228
+
229
+ class TagPairEnd < End
230
+ def content
231
+ @content.content
232
+ end
233
+
234
+ def inspect
235
+ "(#{self.class}:#{@content.inspect})"
236
+ end
191
237
  end
192
238
  end
193
239
  end
@@ -1,3 +1,3 @@
1
1
  module Xrt
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -32,7 +32,7 @@ class TestCommandExtract < Test::Unit::TestCase
32
32
  }
33
33
  end
34
34
 
35
- def _todo_est_extract_text
35
+ def test_extract_text
36
36
  Dir.mktmpdir{|dir|
37
37
  templates_dir = Pathname(dir).join('templates')
38
38
  templates_dir.mkdir
@@ -65,14 +65,42 @@ class TestParser < Test::Unit::TestCase
65
65
  end
66
66
 
67
67
  def test_document_tag
68
- parser = XRT::Parser.new('<div>')
68
+ parser = XRT::Parser.new('<div>a</div>')
69
69
  doc = parser.document
70
70
  assert doc.kind_of? XRT::Statement::Document
71
+
72
+ tag_start = XRT::Statement::Tag.new '<'
73
+ tag_start << XRT::Statement::Text.new('div')
74
+ tag_start << XRT::Statement::TagEnd.new('>')
75
+
76
+ text = XRT::Statement::Text.new('a')
77
+
78
+ tag_close = XRT::Statement::Tag.new '<'
79
+ tag_close << XRT::Statement::Text.new('/div')
80
+ tag_close << XRT::Statement::TagEnd.new('>')
81
+
82
+ tag_pair = XRT::Statement::TagPair.new(tag_start)
83
+ tag_pair << text
84
+ tag_pair << XRT::Statement::TagPairEnd.new(tag_close)
85
+ assert_equal [
86
+ tag_pair,
87
+ ], doc.children
88
+ end
89
+
90
+ def test_void_element
91
+ parser = XRT::Parser.new('<img>a')
92
+ doc = parser.document
93
+ assert doc.kind_of? XRT::Statement::Document
94
+
71
95
  tag = XRT::Statement::Tag.new '<'
72
- tag << XRT::Statement::Text.new('div')
96
+ tag << XRT::Statement::Text.new('img')
73
97
  tag << XRT::Statement::TagEnd.new('>')
98
+
99
+ text = XRT::Statement::Text.new('a')
100
+
74
101
  assert_equal [
75
- tag
102
+ tag,
103
+ text
76
104
  ], doc.children
77
105
  end
78
106
 
@@ -125,6 +125,23 @@ HTML
125
125
  assert_equal '2', document.content, 'replaced'
126
126
  end
127
127
 
128
+ def test_include?
129
+ document = XRT::Statement::Document.new
130
+ block = XRT::Statement::Block.new('[% IF 1 %]')
131
+ s1 = XRT::Statement::Text.new('1')
132
+ s2 = XRT::Statement::Text.new('2')
133
+ s3 = XRT::Statement::Text.new('3')
134
+ block << s1
135
+ document << block
136
+ document << s2
137
+
138
+ assert document.include? s1
139
+ assert document.include? s2
140
+ assert_false document.include? s3
141
+ assert block.include? s1
142
+ assert_false block.include? s2
143
+ end
144
+
128
145
  def test_replace_child_for_descendant
129
146
  document = XRT::Statement::Document.new
130
147
  if_block = XRT::Statement::Block.new('[% IF a %]')
@@ -210,3 +227,61 @@ class TestBlock < Test::Unit::TestCase
210
227
  assert_equal block.content, %q{[% IF 1 %]ok[% END %]}
211
228
  end
212
229
  end
230
+
231
+ class TestTag < Test::Unit::TestCase
232
+ def test_opening_tag
233
+ tag = XRT::Statement::Tag.new('<')
234
+ tag << XRT::Statement::Text.new('div')
235
+ tag << XRT::Statement::TagEnd.new('>')
236
+ assert tag.tag_opening?
237
+ assert_false tag.tag_closing?
238
+ end
239
+
240
+ def test_closing_tag
241
+ tag = XRT::Statement::Tag.new('<')
242
+ tag << XRT::Statement::Text.new('/div')
243
+ tag << XRT::Statement::TagEnd.new('>')
244
+ assert_false tag.tag_opening?
245
+ assert tag.tag_closing?
246
+ end
247
+
248
+ def test_independent_tag
249
+ tag = XRT::Statement::Tag.new('<')
250
+ tag << XRT::Statement::Text.new('div/')
251
+ tag << XRT::Statement::TagEnd.new('>')
252
+ assert_false tag.tag_opening?
253
+ assert_false tag.tag_closing?
254
+ assert tag.tag_independent?
255
+ end
256
+
257
+ def test_tag_name
258
+ [
259
+ 'div',
260
+ 'DIV',
261
+ '/div',
262
+ 'div class="title"',
263
+ ].each{|test_case|
264
+ tag = XRT::Statement::Tag.new('<')
265
+ tag << XRT::Statement::Text.new(test_case)
266
+ tag << XRT::Statement::TagEnd.new('>')
267
+ assert_equal 'div', tag.tag_name
268
+ }
269
+ end
270
+
271
+ def test_void_element?
272
+ [
273
+ ['div', false],
274
+ ['img', true],
275
+ ].each{|test_case|
276
+ tag_name, expect = *test_case
277
+
278
+ tag = XRT::Statement::Tag.new('<')
279
+ tag << XRT::Statement::Text.new(tag_name)
280
+ tag << XRT::Statement::TagEnd.new('>')
281
+ assert_equal expect, tag.tag_void_element?
282
+
283
+ assert_equal !expect, tag.tag_opening?, 'void element is not open'
284
+ }
285
+ end
286
+
287
+ 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.5
4
+ version: 0.0.6
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-13 00:00:00.000000000 Z
11
+ date: 2016-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,6 +70,7 @@ files:
70
70
  - lib/xrt.rb
71
71
  - lib/xrt/cli.rb
72
72
  - lib/xrt/command/dump.rb
73
+ - lib/xrt/command/dump_blocks.rb
73
74
  - lib/xrt/command/extract.rb
74
75
  - lib/xrt/command/lcs.rb
75
76
  - lib/xrt/commands.rb