tag_along 0.6.1 → 0.7.0

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: 391d3d4d89b92c4d71b051eb6545a485e4210669
4
- data.tar.gz: 7356ca9775ccbbce9fce5c2078669994dc5212f2
3
+ metadata.gz: 8c98d99128ba637e0d57ebc32e4be9e3fa7d3b60
4
+ data.tar.gz: a2f520902d8581be2dde44d2e0ace3ae4ceaf2b3
5
5
  SHA512:
6
- metadata.gz: bff906e414af8b88c001e8f532421aa6850744982489f1fbaa59108fe453642963779ebb4f1902ae7ad050258201e11541469f229c2820c1eeb7a93c65c19918
7
- data.tar.gz: 4edd2b7e8856823343b586dc826ca8c8850ea597daf89d27730a09a622386e6c59bb93a14c7319c8f268f0a042195c3c186514a051824def5d115f9d83d04378
6
+ metadata.gz: 427e207f9312002bd4459f6668ffda195e7db75b3b098512027d558f47677c7efb2d076a7c7fef02f4069972a9f52d349132da401ed96e57a354e36cb988c3ca
7
+ data.tar.gz: 1785b8fd997010c9f36469778940e6dfaac605ccf87786d0b83a4e22af75b05d6c18d7bc2f9159f0e33f5c2ad76634648a244c75ca132115af9173ef9b4986e9
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ 0.7.0 -- added dynamic tagging
2
+
1
3
  0.6.1 -- fixed a bug which would crash the gem if an object without
2
4
  item_string is entered
3
5
 
data/README.md CHANGED
@@ -40,7 +40,7 @@ For example you want to tag days of week from a text:
40
40
 
41
41
  To add tags to a text:
42
42
 
43
- offsets = [[8,13], [27,32]]
43
+ offsets = [[8, 13], [27, 32]]
44
44
  tg = TagAlong.new(text, offsets)
45
45
 
46
46
  tg.tag('<my_tag>', '</my_tag>')
@@ -53,18 +53,43 @@ To add tags to a text:
53
53
 
54
54
  Notice that you can retag the text as many times as you want.
55
55
 
56
+ ### Dynamic tags
57
+
58
+ Sometimes you tags contain changeable component. To add dynamic data to tags:
59
+
60
+ #one substitution in opening tag:
61
+ offsets = [[8, 13, 'sunday'], [27, 32, 'monday']
62
+ tg.tag("<my_tag value=\"%s\">", '</my_tag>')
63
+ puts tg.tagged_text
64
+ # There's <my_tag value="sunday">Sunday</my_tag> \
65
+ # and there's <my_tag value="monday">Monday</my_tag>
66
+
67
+ #one substitution in closing tag tag:
68
+ offsets = [[8, 13, nil, 'sunday'], [27, 32, nil, 'monday']
69
+ tg.tag('<my_tag', "</my_tag value=\"%s\">")
70
+ puts tg.tagged_text
71
+ # There's <my_tag>Sunday</my_tag value="sunday"> \
72
+ # and there's <my_tag>Monday</my_tag value="monday">
73
+
74
+ #several substitutions
75
+ offsets = [[8, 13, nil, ['sunday', 'http://en.wikipedia.org/wiki/Sunday'],
76
+ [27, 32, nil, ['monday', http://en.wikipedia.org/wiki/Monday']]
77
+
78
+ tg.tag("<my_tag value=\"%s\", url=\"%s\">", '</my_tag>')
79
+
80
+
56
81
  ### Offsets
57
82
 
58
83
  To prepare offsets from an arbitrary object:
59
84
 
60
85
  # Array of arrays
61
- my_ary = [[8,13], [27,32]]
86
+ my_ary = [[8, 13], [27, 32]]
62
87
  offsets = TagAlong::Offsets.new(my_ary)
63
88
 
64
89
  # Array of hashes
65
- my_hash = [{ start: 8, end:13 }, { start:27, end:32 }]
90
+ my_hash = [{ start: 8, end: 13 }, { start: 27, end: 32 }]
66
91
  offsets = TagAlong::Offsets.new(my_hash,
67
- offset_start: 'start'
92
+ offset_start: 'start',
68
93
  offset_end: 'end')
69
94
  or
70
95
  offsets = TagAlong::Offsets.new(my_hash,
@@ -83,6 +108,75 @@ In all cases you can instantiate TagAlong with resulting offsets:
83
108
  tg = TagAlong.new(text, offsets)
84
109
  tg.tag('|hi|', '|bye|')
85
110
 
111
+ Examples of dynamic tags:
112
+
113
+ # Array of arrays
114
+ my_ary = [[8, 13, ['sunday', 'http://en.wikipedia.org/wiki/Sunday']],
115
+ [27, 32, ['monday', 'http://en.wikipedia.org/wiki/Monday']]]
116
+ offsets = TagAlong::Offsets.new(my_ary)
117
+
118
+ # Array of hashes
119
+ my_hash = [
120
+ { start: 8,
121
+ end: 13,
122
+ subst: ['sunday', 'http://en.wikipedia.org/wiki/Sunday'] },
123
+ { start: 27,
124
+ end: 32,
125
+ subst: ['monday', 'http://en.wikipedia.org/wiki/Monday'] }
126
+ ]
127
+ offsets = TagAlong::Offsets.new(my_hash,
128
+ offset_start: 'start',
129
+ offset_end: 'end',
130
+ data_start: 'subst')
131
+ or
132
+ offsets = TagAlong::Offsets.new(my_hash,
133
+ offset_start: :start,
134
+ offset_end: :end,
135
+ data_start: :subst)
136
+
137
+ # Array of objects
138
+ require 'ostruct'
139
+ my_obj = [OpenStruct.new(s: 8,
140
+ e: 13,
141
+ values: ['sunday',
142
+ 'http://en.wikipedia.org/wiki/Sunday']),
143
+ OpenStruct.new(s: 27,
144
+ e: 32,
145
+ values: ['monday',
146
+ 'http://en.wikipedia.org/wiki/Monday'])]
147
+ offsets = TagAlong::Offsets.new(my_obj,
148
+ offset_start: :s,
149
+ offset_end: :e,
150
+ data_start: :values)
151
+
152
+ In all cases you can instantiate TagAlong with resulting offsets:
153
+
154
+ tg = TagAlong.new(text, offsets)
155
+ tg.tag("<my_tag value=\"%s\", href=\"%s\">", '</my_tag>')
156
+
157
+ Example of dynamic information in a closing tag:
158
+
159
+ # Array of hashes
160
+ my_hash = [
161
+ { start: 8,
162
+ end: 13,
163
+ subst: 'sunday' },
164
+ { start: 27,
165
+ end: 32,
166
+ subst: 'monday' }
167
+ ]
168
+ offsets = TagAlong::Offsets.new(my_hash,
169
+ offset_start: 'start',
170
+ offset_end: 'end',
171
+ data_end: 'subst')
172
+ tg = TagAlong.new(text, offsets)
173
+ tg.tag('<my_tag>', "</my_tag value=\"%s\">")
174
+
175
+ Versioning
176
+ ----------
177
+
178
+ This gem is following practices of [Semantic Versioning][11]
179
+
86
180
  Contributing
87
181
  ------------
88
182
 
@@ -95,7 +189,7 @@ Contributing
95
189
  Copyright
96
190
  ---------
97
191
 
98
- Authors: [Dmitry Mozzherin][11]
192
+ Authors: [Dmitry Mozzherin][12]
99
193
 
100
194
  Copyright (c) 2013 Marine Biological Laboratory. See LICENSE for
101
195
  further details.
@@ -110,4 +204,5 @@ further details.
110
204
  [8]: https://codeclimate.com/github/GlobalNamesArchitecture/tag_along
111
205
  [9]: https://gemnasium.com/GlobalNamesArchitecture/tag_along.png
112
206
  [10]: https://gemnasium.com/GlobalNamesArchitecture/tag_along
113
- [11]: https://github.com/dimus
207
+ [11]: http://semver.org/
208
+ [12]: https://github.com/dimus
@@ -8,7 +8,8 @@ class TagAlong
8
8
  @offsets = offsets
9
9
  @offset_start = (opts[:offset_start] || 'offset_start').to_sym
10
10
  @offset_end = (opts[:offset_end] || 'offset_end').to_sym
11
- @item_string = (opts[:item_string] || 'item_string').to_sym
11
+ @data_start = (opts[:data_start] || 'data_start').to_sym
12
+ @data_end = (opts[:data_end] || 'data_end').to_sym
12
13
 
13
14
  item = @offsets.first
14
15
  if item.is_a?(Array)
@@ -32,33 +33,43 @@ class TagAlong
32
33
  @offsets = @offsets.map do |o|
33
34
  offset_start = o[0]
34
35
  offset_end = o[1]
35
- item_string = o[2]
36
- instantiate(offset_start, offset_end, item_string)
36
+ data_start = o[2]
37
+ data_end = o[3]
38
+ instantiate(offset_start, offset_end, data_start, data_end)
37
39
  end
38
40
  end
39
41
 
40
42
  def process_hash
41
43
  @offsets.each { |h| symbolize_keys(h) }
42
44
  @offsets = @offsets.map do |o|
43
- instantiate(o[@offset_start], o[@offset_end], o[@item_string])
45
+ instantiate(o[@offset_start],
46
+ o[@offset_end],
47
+ o[@data_start],
48
+ o[@data_end])
44
49
  end
45
50
  end
46
51
 
47
52
  def process_obj
48
53
  @offsets = @offsets.map do |o|
49
- item_string = o.respond_to?(@item_string) ?
50
- o.send(@item_string) :
51
- nil
52
54
  instantiate(o.send(@offset_start),
53
- o.send(@offset_end),
54
- item_string)
55
+ o.send(@offset_end),
56
+ o.send(@data_start),
57
+ o.send(@data_end))
55
58
  end
56
59
  end
57
60
 
58
- def instantiate(offset_start, offset_end, item_string)
61
+ def instantiate(offset_start, offset_end, data_start = nil, data_end = nil)
62
+ data_start = data_to_ary(data_start)
63
+ data_end = data_to_ary(data_end)
59
64
  OpenStruct.new(offset_start: to_int(offset_start),
60
65
  offset_end: to_int(offset_end),
61
- item_string: item_string)
66
+ data_start: data_start,
67
+ data_end: data_end)
68
+ end
69
+
70
+ def data_to_ary(data)
71
+ return data unless data
72
+ data.is_a?(Array) ? data : [data]
62
73
  end
63
74
 
64
75
  def to_int(val)
@@ -1,3 +1,3 @@
1
1
  class TagAlong
2
- VERSION = '0.6.1'
2
+ VERSION = '0.7.0'
3
3
  end
data/lib/tag_along.rb CHANGED
@@ -19,9 +19,13 @@ class TagAlong
19
19
  end
20
20
 
21
21
  def tag(open_tag, close_tag)
22
+ open_tag_subs = open_tag.scan(/%s/).size
23
+ close_tag_subs = close_tag.scan(/%s/).size
22
24
  @tagged_text = @split_text.inject([]) do |res, t|
23
25
  if t[:tagged]
24
- [open_tag, t[:text], close_tag].each { |text| res << text }
26
+ ot = dyn_tag(open_tag, t[:data_start], open_tag_subs)
27
+ ct = dyn_tag(close_tag, t[:data_end], close_tag_subs)
28
+ [ot, t[:text], ct].each { |text| res << text }
25
29
  else
26
30
  res << t[:text]
27
31
  end
@@ -31,6 +35,11 @@ class TagAlong
31
35
 
32
36
  private
33
37
 
38
+ def dyn_tag(tag, data, subs_num)
39
+ return tag if subs_num == 0
40
+ tag % data
41
+ end
42
+
34
43
  def split_text
35
44
  return if @split_text
36
45
 
@@ -38,7 +47,6 @@ class TagAlong
38
47
  cursor = 0
39
48
  fragment = []
40
49
  res = []
41
-
42
50
  @offsets.each do |item|
43
51
  chars_num = item.offset_start - cursor
44
52
  chars_num.times { fragment << text_ary.shift }
@@ -47,7 +55,10 @@ class TagAlong
47
55
  cursor = item.offset_start
48
56
  chars_num = item.offset_end + 1 - cursor
49
57
  chars_num.times { fragment << text_ary.shift }
50
- res << { tagged: true, text: fragment }
58
+ res << { tagged: true,
59
+ text: fragment,
60
+ data_start: item.data_start,
61
+ data_end: item.data_end}
51
62
  fragment = []
52
63
  cursor = item.offset_end + 1
53
64
  end
data/spec/spec_helper.rb CHANGED
@@ -3,7 +3,7 @@ Coveralls.wear!
3
3
 
4
4
  require 'json'
5
5
  require 'ostruct'
6
- require_relative '../lib/tag_along'
6
+ require 'tag_along'
7
7
 
8
8
  RSpec.configure do |c|
9
9
  c.mock_with :rr
@@ -16,11 +16,11 @@ module TagAlongSpec
16
16
  text = data[:content]
17
17
  offset_hash = data[:names]
18
18
  offset_obj = offset_hash.map do |h|
19
- OpenStruct.new(name: h[:verbatim],
19
+ OpenStruct.new(name: h[:verbatim],
20
20
  start: h[:offsetStart],
21
- end: h[:offsetEnd])
21
+ end: h[:offsetEnd])
22
22
  end
23
- offset_ary = offset_obj.map { |h| [h.start, h.end] }
23
+ offset_ary = offset_obj.map { |h| [h.start, h.end, h.name] }
24
24
  [text, offset_ary, offset_hash, offset_obj]
25
25
  end
26
26
  end
@@ -12,14 +12,6 @@ describe TagAlong::Offsets do
12
12
  o.is_a?(TagAlong::Offsets).should be_true
13
13
  o.first.offset_start.should == 61
14
14
  o.first.offset_end.should == 68
15
- o.first.item_string.should be_nil
16
- ary_with_item = OFFSETS_HASH.map do |h|
17
- [h[:offsetStart], h[:offsetEnd], h[:verbatim]]
18
- end
19
- o = TagAlong::Offsets.new(ary_with_item)
20
- o.first.offset_start.should == 61
21
- o.first.offset_end.should == 68
22
- o.first.item_string.should == 'Pundulus'
23
15
  -> { TagAlong::Offsets.new([['a','b']]) }.should
24
16
  raise_error(TypeError, 'Offsets must be integers')
25
17
  end
@@ -31,14 +23,6 @@ describe TagAlong::Offsets do
31
23
  o.is_a?(TagAlong::Offsets).should be_true
32
24
  o.first.offset_start.should == 61
33
25
  o.first.offset_end.should == 68
34
- o.first.item_string.should be_nil
35
- o = TagAlong::Offsets.new(OFFSETS_HASH,
36
- offset_start: 'offsetStart',
37
- offset_end: 'offsetEnd',
38
- item_string: 'verbatim')
39
- o.first.offset_start.should == 61
40
- o.first.offset_end.should == 68
41
- o.first.item_string.should == 'Pundulus'
42
26
  end
43
27
 
44
28
  it 'should process object' do
@@ -48,14 +32,53 @@ describe TagAlong::Offsets do
48
32
  o.is_a?(TagAlong::Offsets).should be_true
49
33
  o.first.offset_start.should == 61
50
34
  o.first.offset_end.should == 68
51
- o.first.item_string.should be_nil
35
+ end
36
+
37
+ it 'should process arrays with dynamic start tag' do
38
+ o = TagAlong::Offsets.new(OFFSETS_ARY)
39
+ o.is_a?(TagAlong::Offsets).should be_true
40
+ o.first.offset_start.should == 61
41
+ o.first.offset_end.should == 68
42
+ o.first.data_start.should == ["Pundulus"]
43
+ o.first.data_end.should be_nil
44
+ end
45
+
46
+ it 'should process dynamic hash' do
47
+ o = TagAlong::Offsets.new(OFFSETS_HASH,
48
+ offset_start: :offsetStart,
49
+ offset_end: :offsetEnd,
50
+ data_start: :verbatim,
51
+ data_end: :verbatim)
52
+ o.is_a?(TagAlong::Offsets).should be_true
53
+ o.first.offset_start.should == 61
54
+ o.first.offset_end.should == 68
55
+ o.first.data_start.should == ["Pundulus"]
56
+ o.first.data_end.should == ["Pundulus"]
57
+ o = TagAlong::Offsets.new(OFFSETS_HASH,
58
+ offset_start: :offsetStart,
59
+ offset_end: :offsetEnd,
60
+ data_end: :verbatim)
61
+ o.first.data_start.should be_nil
62
+ o.first.data_end.should == ["Pundulus"]
63
+ end
64
+
65
+ it 'should process dynamic object' do
52
66
  o = TagAlong::Offsets.new(OFFSETS_OBJ,
53
67
  offset_start: :start,
54
68
  offset_end: :end,
55
- item_string: 'name')
69
+ data_start: :name,
70
+ data_end: :name)
56
71
  o.is_a?(TagAlong::Offsets).should be_true
57
72
  o.first.offset_start.should == 61
58
73
  o.first.offset_end.should == 68
59
- o.first.item_string.should == 'Pundulus'
74
+ o.first.data_start.should == ["Pundulus"]
75
+ o.first.data_end.should == ["Pundulus"]
76
+ o = TagAlong::Offsets.new(OFFSETS_OBJ,
77
+ offset_start: :start,
78
+ offset_end: :end,
79
+ data_end: :name)
80
+ o.first.data_start.should be_nil
81
+ o.first.data_end.should == ["Pundulus"]
60
82
  end
83
+
61
84
  end
@@ -22,7 +22,7 @@ describe TagAlong do
22
22
  tg.tagged_text.should
23
23
  include('<another_tag>Lebistes reticulatus</another_tag>')
24
24
  end
25
-
25
+
26
26
  it 'should tag' do
27
27
  text = 'There\'s Sunday and there\'s Monday'
28
28
  offsets = [[8,13], [27,32]]
@@ -30,4 +30,28 @@ describe TagAlong do
30
30
  tg.tag('<em>', '</em>').should ==
31
31
  %q{There's <em>Sunday</em> and there's <em>Monday</em>}
32
32
  end
33
+
34
+ it 'should tag dynamicly' do
35
+ tg = TagAlong.new(TEXT, OFFSETS_ARY)
36
+ tagged_text = tg.tag("<my_tag name=\"%s\">", '</my_tag>')
37
+ tg.tagged_text.should == tagged_text
38
+ tg.tagged_text.should include('<my_tag name="Lebistes reticulatus">' +
39
+ 'Lebistes reticulatus</my_tag>')
40
+ end
41
+
42
+ it 'should tag dynamicly end tag' do
43
+ offsets = OFFSETS_ARY.each {|i| i.insert(-2, nil)}
44
+ tg = TagAlong.new(TEXT, OFFSETS_ARY)
45
+ tagged_text = tg.tag('<my_tag>', "</my_tag name=\"%s\">")
46
+ tg.tagged_text.should == tagged_text
47
+ tg.tagged_text.should include("</my_tag name=\"Pundulus\">")
48
+ end
49
+
50
+ it 'should break dynamic taging if there is problem with data' do
51
+ offsets = OFFSETS_ARY.each {|i| i.insert(-2, nil)}
52
+ tg = TagAlong.new(TEXT, OFFSETS_ARY)
53
+ -> { tg.tag('<my_tag>', "</my_tag val=\"%s\" name=\"%s\">") }.
54
+ should raise_error
55
+ end
56
+
33
57
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tag_along
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Mozzherin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-03 00:00:00.000000000 Z
11
+ date: 2013-09-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  Tags a text with arbitrary tags