xrt 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4ebf55387c6ecfe4b9df158408b759f0b1ee8106
4
- data.tar.gz: 82976c5d15a8b57a072272f727a913e60c1c3737
3
+ metadata.gz: bd3b9dd012073b47b0a1bf0109540273f16d8f5d
4
+ data.tar.gz: caf93a2e9702add56ff30b94f8165acf399c0741
5
5
  SHA512:
6
- metadata.gz: 1af7ab32ea83cdc03d6af6da7b10099efb26bdb7ecb1621477981e41b6de063b7795429eb845d15f9c6cc6313a292f908bba6014438560dc809b03e637eef0ab
7
- data.tar.gz: 89609aba1c70bb9856b0c8ed65de38b9724c275e889acbd076275c6998dea00bfa11a689944a5f89d090e95beb853acdd6f10c532312d8beda8ed81ab8b45220
6
+ metadata.gz: 3b6276557387abf00c81765fdf85ccb81b7dfa983af136306190a9a603f6141845702e2f21ae3851b3628919466c692fe0be5dcf44fe751c9e4c483a3ced9862
7
+ data.tar.gz: 18a297a71be188b03114e32e8e4f74e1ed3ad846e198758657b45cbf7b18e45c108a5661be83d2d922b7484262cdc06e02cc4d8d9eee26214ea38ba9497dc1ba
@@ -19,7 +19,7 @@ module XRT
19
19
  parser = XRT::Parser.new(from_source)
20
20
  from_doc = parser.document
21
21
 
22
- found_blocks = from_doc.find_blocks.select{|block|
22
+ found_blocks = from_doc.find_block_texts.select{|block|
23
23
  block.content.index(target_block) == 0
24
24
  }
25
25
 
data/lib/xrt/parser.rb CHANGED
@@ -27,6 +27,8 @@ module XRT
27
27
  break
28
28
  when XRT::Statement::Text
29
29
  node << statement
30
+ when XRT::Statement::Whitespace
31
+ node << statement
30
32
  when XRT::Statement::Directive
31
33
  node << statement
32
34
  end
@@ -40,16 +42,31 @@ module XRT
40
42
  result = []
41
43
 
42
44
  while reading.length > 0
43
- got = read_directive(reading) || read_text(reading)
44
- unless got
45
+ if got = read_directive(reading)
46
+ result << got
47
+ elsif got = read_text(reading)
48
+ result.concat(split_whitespace(got))
49
+ else
45
50
  raise "failed to parse #{@source}"
46
51
  end
47
- result << got
48
52
  end
49
53
 
50
54
  result
51
55
  end
52
56
 
57
+ def split_whitespace(text)
58
+ prefix, suffix=nil
59
+ text.sub!(/\A(\s+)/) {|matched|
60
+ prefix = matched
61
+ ''
62
+ }
63
+ text.sub!(/(\s+)\Z/) {|matched|
64
+ suffix = matched
65
+ ''
66
+ }
67
+ [prefix, text, suffix].compact.delete_if{|s| s.empty?}
68
+ end
69
+
53
70
  def read_directive source
54
71
  return nil unless source[0...2] == '[%'
55
72
 
data/lib/xrt/statement.rb CHANGED
@@ -55,6 +55,12 @@ module XRT
55
55
  }.concat(children.map{|child| child.find_blocks }.flatten)
56
56
  end
57
57
 
58
+ def find_block_texts
59
+ children.select{|child|
60
+ child.kind_of?(XRT::Statement::Block) || child.kind_of?(XRT::Statement::Text)
61
+ }.concat(children.map{|child| child.find_block_texts }.flatten)
62
+ end
63
+
58
64
  def auto_indent
59
65
  lines = content.split(/\n/)[1..-1]
60
66
  whitespaces = lines.map{|line| line.scan(/^\s+/).first }.compact
@@ -76,6 +82,8 @@ module XRT
76
82
  XRT::Statement::End.new content
77
83
  elsif syntax.block? content
78
84
  XRT::Statement::Directive.new content
85
+ elsif syntax.whitespace? content
86
+ XRT::Statement::Whitespace.new content
79
87
  else
80
88
  XRT::Statement::Text.new content
81
89
  end
@@ -112,6 +120,9 @@ module XRT
112
120
  class Text < Statement
113
121
  end
114
122
 
123
+ class Whitespace < Statement
124
+ end
125
+
115
126
  class Directive < Statement
116
127
  end
117
128
 
data/lib/xrt/syntax.rb CHANGED
@@ -1,5 +1,9 @@
1
1
  module XRT
2
2
  class Syntax
3
+ def whitespace? statement
4
+ statement.match(/\A\s*\Z/)
5
+ end
6
+
3
7
  def block? statement
4
8
  statement =~ /\A\[%.+%\]\Z/m
5
9
  end
data/lib/xrt/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Xrt
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -2,7 +2,7 @@ require 'test/unit'
2
2
  require 'tmpdir'
3
3
  require 'xrt/command/dump'
4
4
 
5
- class TestParser < Test::Unit::TestCase
5
+ class TestCommandDump < Test::Unit::TestCase
6
6
  def test_annotate
7
7
  Dir.mktmpdir{|dir|
8
8
  Pathname(dir).join('if1.pm').open('w'){ |f| f.write %q{[% IF 1 %]nested[% END %]} }
@@ -31,4 +31,39 @@ class TestCommandExtract < Test::Unit::TestCase
31
31
  }, transaction.files)
32
32
  }
33
33
  end
34
+
35
+ def test_extract_text
36
+ Dir.mktmpdir{|dir|
37
+ templates_dir = Pathname(dir).join('templates')
38
+ templates_dir.mkdir
39
+ templates_dir.join('a.html').open('w'){ |f| f.write <<'HTML' }
40
+ <html>
41
+ [% a %]
42
+ <h1>
43
+ hi
44
+ </h1>
45
+ [% b %]
46
+ </html>
47
+ HTML
48
+
49
+ command = XRT::Command::Extract.new
50
+ transaction = command.as_transaction(templates_dir.join('a.html').to_s, %q{<h1>}, templates_dir.to_s, '_h1.tt')
51
+ assert_equal({
52
+ templates_dir.join('a.html').to_s => <<'HTML',
53
+ <html>
54
+ [% a %]
55
+ [% INCLUDE "_h1.tt" %]
56
+ [% b %]
57
+ </html>
58
+ HTML
59
+
60
+ templates_dir.join('_h1.tt').to_s => <<'HTML',
61
+ <h1>
62
+ hi
63
+ </h1>
64
+ HTML
65
+
66
+ }, transaction.files)
67
+ }
68
+ end
34
69
  end
data/test/test-parser.rb CHANGED
@@ -24,6 +24,19 @@ class TestParser < Test::Unit::TestCase
24
24
  ], doc.children
25
25
  end
26
26
 
27
+ def test_document_text_and_whitespace_directive
28
+ parser = XRT::Parser.new("1 [% 2 %]\n3")
29
+ doc = parser.document
30
+ assert doc.kind_of? XRT::Statement::Document
31
+ assert_equal [
32
+ XRT::Statement::Text.new('1'),
33
+ XRT::Statement::Whitespace.new(' '),
34
+ XRT::Statement::Directive.new('[% 2 %]'),
35
+ XRT::Statement::Whitespace.new("\n"),
36
+ XRT::Statement::Text.new('3'),
37
+ ], doc.children
38
+ end
39
+
27
40
  def test_document_block
28
41
  parser = XRT::Parser.new('[% IF a %]1[% END %]')
29
42
  doc = parser.document
@@ -58,16 +71,28 @@ class TestParser < Test::Unit::TestCase
58
71
  end
59
72
 
60
73
  def test_read_text
74
+ assert_equal '', @parser.read_text('')
61
75
  assert_equal 'hi', @parser.read_text('hi')
62
76
  assert_equal 'hi[', @parser.read_text('hi[')
63
77
  assert_equal 'hi', @parser.read_text('hi[%')
64
78
  assert_nil @parser.read_text('[% %]')
65
79
  end
66
80
 
81
+ def test_split_whitespace
82
+ assert_equal([], @parser.split_whitespace(""))
83
+ assert_equal([" "], @parser.split_whitespace(" "))
84
+ assert_equal(["\n"], @parser.split_whitespace("\n"))
85
+ assert_equal(["hi"], @parser.split_whitespace("hi"))
86
+ assert_equal(["hi there"], @parser.split_whitespace("hi there"))
87
+ assert_equal([" ", "hi", " "], @parser.split_whitespace(" hi "))
88
+ assert_equal([" \n", "hi", "\n "], @parser.split_whitespace(" \nhi\n "))
89
+ assert_equal(["\n", "xxx", "\n"], @parser.split_whitespace("\nxxx\n"))
90
+ end
91
+
67
92
  def test_tokens
68
93
  test_cases = [
69
94
  ['<html>', ['<html>']],
70
- ['a [% b %] c', ['a ', '[% b %]', ' c']],
95
+ ['a [% b %] c', ['a', ' ', '[% b %]', ' ', 'c']],
71
96
  ['[% a %] [% b %] [% c %]', ['[% a %]', ' ', '[% b %]', ' ', '[% c %]']],
72
97
  ['[% FOR k IN [1, 2, 3] %]', ['[% FOR k IN [1, 2, 3] %]']],
73
98
  [
@@ -4,6 +4,7 @@ require 'xrt/statement'
4
4
  class TestStatementFactory < Test::Unit::TestCase
5
5
  def test_new_from_string
6
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
7
8
  assert XRT::Statement::Factory.new_from_content('[% foo %]').kind_of? XRT::Statement::Directive
8
9
  assert XRT::Statement::Factory.new_from_content('[% IF 1 %]').kind_of? XRT::Statement::Block
9
10
  assert XRT::Statement::Factory.new_from_content('[% foo IF 1 %]').kind_of? XRT::Statement::Directive
data/test/test-syntax.rb CHANGED
@@ -10,6 +10,13 @@ class TestSyntax < Test::Unit::TestCase
10
10
  assert_equal 'foo', @syntax.remove_comment('foo # bar')
11
11
  end
12
12
 
13
+ def test_whitespace?
14
+ assert @syntax.whitespace? ''
15
+ assert @syntax.whitespace? ' '
16
+ assert @syntax.whitespace? " \n"
17
+ assert_nil @syntax.whitespace? 'a'
18
+ end
19
+
13
20
  def test_beginning_block?
14
21
  assert @syntax.beginning_block? '[% IF 1 %]'
15
22
  assert @syntax.beginning_block? '[% if 1 %]'
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.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - hitode909
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-25 00:00:00.000000000 Z
11
+ date: 2016-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler