habakiri 0.1.1 → 0.1.2

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
  SHA256:
3
- metadata.gz: 813ae70ea321d06939608a93265b91ff6ff6ba35d181321bd31572e7f4ca8945
4
- data.tar.gz: 6b5553cac1efee65f7cb5ba7482fb831dd6c11f59d03085d282a8c912c09fe22
3
+ metadata.gz: 755f1d5a970754e4e71f8f41cd9a5aa7f02482aa2cd3b72f2bbd03de122a8d22
4
+ data.tar.gz: 457b9a65289f046f47ca77173baf81695af717a38370dd3a4e0e18897496e703
5
5
  SHA512:
6
- metadata.gz: fe50a267573973e7cd16a511e0c6baea8c75cf64694d44677ee1fbbdbc75f4f3d80f77c1027c46f83992a85c38dc1bac0d72b7ef334b4d6e7ce67a908dd5bf54
7
- data.tar.gz: b408aa0e9c13b10c322e5d91dee71537be448a1374b38db7b61d32b3050537b400a897d1117c9b17d80a1147d87f04adc35f2f515b8ab16f9bd416d80f93982b
6
+ metadata.gz: d48721467c27f00f134ab4186684634b10a9a6827d1e6407838448e8b0020d1f451737742fa3e658eab0142d4e63627d39bcf4e75b46cd0128b430ff245cb112
7
+ data.tar.gz: a03ae57eab368f16362da48bc894b2a4171d246ec4fdcf18fa45b32d752f9f7e29c46a1d1c52d70753dd9259b37c5840cc04dfc32e3d1478dc603de12975c11c
@@ -2,23 +2,23 @@ require 'habakiri/version'
2
2
  require 'diff/lcs'
3
3
 
4
4
  class Habakiri
5
- def initialize(prefix = '{{', suffix = '}}')
6
- @prefix = prefix
7
- @suffix = suffix
5
+ def initialize(template, prefix = '{{', suffix = '}}')
6
+ @prefix = prefix.to_s
7
+ @suffix = suffix.to_s
8
+ @template = template.to_s
9
+ @keywords = keywords
8
10
  end
9
11
 
10
- def exec(template, text)
11
- diffs = Diff::LCS.sdiff(template.to_s, text.to_s)
12
- matches = template.to_enum(:scan, /#{@prefix}.+?#{@suffix}/).map{ Regexp.last_match }
12
+ def exec(text)
13
+ diffs = Diff::LCS.sdiff(@template, text.to_s)
13
14
 
14
- matches.each.with_object({}){|match, object|
15
- start = match.begin(0)
16
- last = match.end(0)
17
- range = start...last
18
- list = diffs.select{|d| range.include?(d.old_position) }
15
+ @keywords.each.with_object({}){|(key, key_begin, key_end), object|
16
+ index_begin = diffs.index{|d| d.old_position == key_begin }
17
+ index_end = diffs.index{|d| d.old_position == key_end }
18
+ list = diffs[index_begin...index_end]
19
19
 
20
20
  # 改行後の追加要素も対応する文字列とみなす
21
- i = diffs.index{|d| d.old_position == last }
21
+ i = index_end
22
22
  if i
23
23
  loop do
24
24
  diff = diffs[i]
@@ -31,14 +31,23 @@ class Habakiri
31
31
  end
32
32
  end
33
33
 
34
- key = match[0].delete_prefix(@prefix).delete_suffix(@suffix).strip
35
34
  object[key] = list.map(&:new_element).join.strip
36
35
  }
37
36
  end
38
37
 
38
+ private
39
+
40
+ def keywords
41
+ @template.to_enum(:scan, /#{@prefix}.+?#{@suffix}/).map{
42
+ m = Regexp.last_match
43
+ key = m[0].delete_prefix(@prefix).delete_suffix(@suffix).strip
44
+ [key, m.begin(0), m.end(0)]
45
+ }
46
+ end
47
+
39
48
  class << self
40
49
  def exec(template, text)
41
- new.exec(template, text)
50
+ new(template).exec(text)
42
51
  end
43
52
  end
44
53
  end
@@ -1,3 +1,3 @@
1
1
  class Habakiri
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.1.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: habakiri
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - diaphragm
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-22 00:00:00.000000000 Z
11
+ date: 2020-07-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Habariki is a template match engine for strings that can extract part
14
14
  of text by template keywords.