Sha256: 41617a786ecdfccfc4379bfb106609dfa302610296446573c266793f481bf1c2

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require 'ostruct'
require 'tag_along/version'
require 'tag_along/offsets'

class TagAlong

  attr :text, :tagged_text

  def self.version
    VERSION
  end

  def initialize(text, offsets)
    @offsets = offsets.is_a?(Offsets) ? offsets : Offsets.new(offsets)
    @text = text
    @split_text = nil
    @tagged_text = nil
    split_text
  end

  def tag(open_tag, close_tag)
    @tagged_text = @split_text.inject([]) do |res, t|
      if t[:tagged]
        [open_tag, t[:text], close_tag].each { |text| res << text }
      else
        res << t[:text]
      end
      res
    end.join('')
  end

  private

  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 }
      res << { tagged: false, text: fragment }
      fragment = []
      cursor = item.offset_start
      chars_num = item.offset_end + 1 - cursor
      chars_num.times { fragment << text_ary.shift }
      res << { tagged: true, text: fragment }
      fragment = []
      cursor = item.offset_end + 1
    end

    res.each do |r|
      r[:text] = r[:text].pack('U*')
    end
    @split_text = res
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tag_along-0.6.1 lib/tag_along.rb