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 +4 -4
- data/lib/habakiri.rb +23 -14
- data/lib/habakiri/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 755f1d5a970754e4e71f8f41cd9a5aa7f02482aa2cd3b72f2bbd03de122a8d22
|
4
|
+
data.tar.gz: 457b9a65289f046f47ca77173baf81695af717a38370dd3a4e0e18897496e703
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d48721467c27f00f134ab4186684634b10a9a6827d1e6407838448e8b0020d1f451737742fa3e658eab0142d4e63627d39bcf4e75b46cd0128b430ff245cb112
|
7
|
+
data.tar.gz: a03ae57eab368f16362da48bc894b2a4171d246ec4fdcf18fa45b32d752f9f7e29c46a1d1c52d70753dd9259b37c5840cc04dfc32e3d1478dc603de12975c11c
|
data/lib/habakiri.rb
CHANGED
@@ -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(
|
11
|
-
diffs = Diff::LCS.sdiff(template
|
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
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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 =
|
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(
|
50
|
+
new(template).exec(text)
|
42
51
|
end
|
43
52
|
end
|
44
53
|
end
|
data/lib/habakiri/version.rb
CHANGED
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.
|
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-
|
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.
|