Skip to content

Commit

Permalink
added dynamic tags
Browse files Browse the repository at this point in the history
  • Loading branch information
dimus committed Sep 10, 2013
1 parent fb51e65 commit 4a194d3
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 15 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ Example of dynamic information in a closing tag:
tg = TagAlong.new(text, offsets)
tg.tag('<my_tag>', "</my_tag value=\"%s\">")

Versioning
----------

This gem is following practices of [Semantic Versioning][11]

Contributing
------------

Expand All @@ -184,7 +189,7 @@ Contributing
Copyright
---------

Authors: [Dmitry Mozzherin][11]
Authors: [Dmitry Mozzherin][12]

Copyright (c) 2013 Marine Biological Laboratory. See LICENSE for
further details.
Expand All @@ -199,4 +204,5 @@ further details.
[8]: https://codeclimate.com/github/GlobalNamesArchitecture/tag_along
[9]: https://gemnasium.com/GlobalNamesArchitecture/tag_along.png
[10]: https://gemnasium.com/GlobalNamesArchitecture/tag_along
[11]: https://github.com/dimus
[11]: http://semver.org/
[12]: https://github.com/dimus
17 changes: 14 additions & 3 deletions lib/tag_along.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ def initialize(text, offsets)
end

def tag(open_tag, close_tag)
open_tag_subs = open_tag.scan(/%s/).size
close_tag_subs = close_tag.scan(/%s/).size
@tagged_text = @split_text.inject([]) do |res, t|
if t[:tagged]
[open_tag, t[:text], close_tag].each { |text| res << text }
ot = dyn_tag(open_tag, t[:data_start], open_tag_subs)
ct = dyn_tag(close_tag, t[:data_end], close_tag_subs)
[ot, t[:text], ct].each { |text| res << text }
else
res << t[:text]
end
Expand All @@ -31,14 +35,18 @@ def tag(open_tag, close_tag)

private

def dyn_tag(tag, data, subs_num)
return tag if subs_num == 0
tag % data
end

def split_text
return if @split_text

text_ary = @text.unpack('U*')
cursor = 0
fragment = []
res = []

@offsets.each do |item|
chars_num = item.offset_start - cursor
chars_num.times { fragment << text_ary.shift }
Expand All @@ -47,7 +55,10 @@ def split_text
cursor = item.offset_start
chars_num = item.offset_end + 1 - cursor
chars_num.times { fragment << text_ary.shift }
res << { tagged: true, text: fragment }
res << { tagged: true,
text: fragment,
data_start: item.data_start,
data_end: item.data_end}
fragment = []
cursor = item.offset_end + 1
end
Expand Down
28 changes: 23 additions & 5 deletions lib/tag_along/offsets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def initialize(offsets, opts = {})
@offsets = offsets
@offset_start = (opts[:offset_start] || 'offset_start').to_sym
@offset_end = (opts[:offset_end] || 'offset_end').to_sym
@data_start = (opts[:data_start] || 'data_start').to_sym
@data_end = (opts[:data_end] || 'data_end').to_sym

item = @offsets.first
if item.is_a?(Array)
Expand All @@ -31,27 +33,43 @@ def process_array
@offsets = @offsets.map do |o|
offset_start = o[0]
offset_end = o[1]
instantiate(offset_start, offset_end)
data_start = o[2]
data_end = o[3]
instantiate(offset_start, offset_end, data_start, data_end)
end
end

def process_hash
@offsets.each { |h| symbolize_keys(h) }
@offsets = @offsets.map do |o|
instantiate(o[@offset_start], o[@offset_end])
instantiate(o[@offset_start],
o[@offset_end],
o[@data_start],
o[@data_end])
end
end

def process_obj
@offsets = @offsets.map do |o|
instantiate(o.send(@offset_start),
o.send(@offset_end),)
o.send(@offset_end),
o.send(@data_start),
o.send(@data_end))
end
end

def instantiate(offset_start, offset_end)
def instantiate(offset_start, offset_end, data_start = nil, data_end = nil)
data_start = data_to_ary(data_start)
data_end = data_to_ary(data_end)
OpenStruct.new(offset_start: to_int(offset_start),
offset_end: to_int(offset_end),)
offset_end: to_int(offset_end),
data_start: data_start,
data_end: data_end)
end

def data_to_ary(data)
return data unless data
data.is_a?(Array) ? data : [data]
end

def to_int(val)
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def self.process_spec_data(dir)
start: h[:offsetStart],
end: h[:offsetEnd])
end
offset_ary = offset_obj.map { |h| [h.start, h.end] }
offset_ary = offset_obj.map { |h| [h.start, h.end, h.name] }
[text, offset_ary, offset_hash, offset_obj]
end
end
Expand Down
51 changes: 48 additions & 3 deletions spec/tag_along/offsets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
o.is_a?(TagAlong::Offsets).should be_true
o.first.offset_start.should == 61
o.first.offset_end.should == 68
o.first.item_string.should be_nil
-> { TagAlong::Offsets.new([['a','b']]) }.should
raise_error(TypeError, 'Offsets must be integers')
end
Expand All @@ -24,7 +23,6 @@
o.is_a?(TagAlong::Offsets).should be_true
o.first.offset_start.should == 61
o.first.offset_end.should == 68
o.first.item_string.should be_nil
end

it 'should process object' do
Expand All @@ -34,6 +32,53 @@
o.is_a?(TagAlong::Offsets).should be_true
o.first.offset_start.should == 61
o.first.offset_end.should == 68
o.first.item_string.should be_nil
end

it 'should process arrays with dynamic start tag' do
o = TagAlong::Offsets.new(OFFSETS_ARY)
o.is_a?(TagAlong::Offsets).should be_true
o.first.offset_start.should == 61
o.first.offset_end.should == 68
o.first.data_start.should == ["Pundulus"]
o.first.data_end.should be_nil
end

it 'should process dynamic hash' do
o = TagAlong::Offsets.new(OFFSETS_HASH,
offset_start: :offsetStart,
offset_end: :offsetEnd,
data_start: :verbatim,
data_end: :verbatim)
o.is_a?(TagAlong::Offsets).should be_true
o.first.offset_start.should == 61
o.first.offset_end.should == 68
o.first.data_start.should == ["Pundulus"]
o.first.data_end.should == ["Pundulus"]
o = TagAlong::Offsets.new(OFFSETS_HASH,
offset_start: :offsetStart,
offset_end: :offsetEnd,
data_end: :verbatim)
o.first.data_start.should be_nil
o.first.data_end.should == ["Pundulus"]
end

it 'should process dynamic object' do
o = TagAlong::Offsets.new(OFFSETS_OBJ,
offset_start: :start,
offset_end: :end,
data_start: :name,
data_end: :name)
o.is_a?(TagAlong::Offsets).should be_true
o.first.offset_start.should == 61
o.first.offset_end.should == 68
o.first.data_start.should == ["Pundulus"]
o.first.data_end.should == ["Pundulus"]
o = TagAlong::Offsets.new(OFFSETS_OBJ,
offset_start: :start,
offset_end: :end,
data_end: :name)
o.first.data_start.should be_nil
o.first.data_end.should == ["Pundulus"]
end

end
26 changes: 25 additions & 1 deletion spec/tag_along_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,36 @@
tg.tagged_text.should
include('<another_tag>Lebistes reticulatus</another_tag>')
end

it 'should tag' do
text = 'There\'s Sunday and there\'s Monday'
offsets = [[8,13], [27,32]]
tg = TagAlong.new(text, offsets)
tg.tag('<em>', '</em>').should ==
%q{There's <em>Sunday</em> and there's <em>Monday</em>}
end

it 'should tag dynamicly' do
tg = TagAlong.new(TEXT, OFFSETS_ARY)
tagged_text = tg.tag("<my_tag name=\"%s\">", '</my_tag>')
tg.tagged_text.should == tagged_text
tg.tagged_text.should include('<my_tag name="Lebistes reticulatus">' +
'Lebistes reticulatus</my_tag>')
end

it 'should tag dynamicly end tag' do
offsets = OFFSETS_ARY.each {|i| i.insert(-2, nil)}
tg = TagAlong.new(TEXT, OFFSETS_ARY)
tagged_text = tg.tag('<my_tag>', "</my_tag name=\"%s\">")
tg.tagged_text.should == tagged_text
tg.tagged_text.should include("</my_tag name=\"Pundulus\">")
end

it 'should break dynamic taging if there is problem with data' do
offsets = OFFSETS_ARY.each {|i| i.insert(-2, nil)}
tg = TagAlong.new(TEXT, OFFSETS_ARY)
-> { tg.tag('<my_tag>', "</my_tag val=\"%s\" name=\"%s\">") }.
should raise_error
end

end

0 comments on commit 4a194d3

Please sign in to comment.