Class: TagAlong::TaggedText

Inherits:
Object
  • Object
show all
Defined in:
lib/tag_along/tagged_text.rb

Constant Summary collapse

CHR =
{ '<' => 60, '>' => 62 }
SPACES =
{ 9 => true, 10 => true, 11 => true, 12 => true,
13 => true, 32 => true, 133 => true, 160 => true,
5760 => true, 6158 => true, 8192 => true,
8193 => true, 8194 => true, 8195 => true,
8196 => true, 8197 => true, 8198 => true,
8199 => true, 8200 => true, 8201 => true,
8202 => true, 8232 => true, 8233 => true,
8239 => true, 8287 => true, 12288 => true }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tagged_text, opts = {}) ⇒ TaggedText

Returns a new instance of TaggedText.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tag_along/tagged_text.rb', line 16

def initialize(tagged_text, opts = {})
  @normalize_spaces = true if opts[:normalize_spaces]
  @tagged_text = tagged_text
  @inside_tag = false
  @inside_space = false
  @offsets  = []
  @text = []
  @text_offset = 0
  @current_offset = { type: :text, start: 0, end: nil,
                      text_start: 0, text_end: nil }
  process_tagged_text
end

Instance Attribute Details

#offsetsObject (readonly)

Returns the value of attribute offsets.



14
15
16
# File 'lib/tag_along/tagged_text.rb', line 14

def offsets
  @offsets
end

#tagged_textObject (readonly)

Returns the value of attribute tagged_text.



14
15
16
# File 'lib/tag_along/tagged_text.rb', line 14

def tagged_text
  @tagged_text
end

Instance Method Details

#adjust_offsets(plain_text_offsets) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tag_along/tagged_text.rb', line 33

def adjust_offsets(plain_text_offsets)
  plain_text_offsets = plain_text_offsets.is_a?(Offsets) ?
                        plain_text_offsets :
                        Offsets.new(plain_text_offsets)
  adjusted_offsets = TagAlong::Offsets.new([])
  @offsets.each do |offset|
    next if offset[:type] == :tag
    process_offset(plain_text_offsets, offset, adjusted_offsets)
    break if plain_text_offsets.empty?
  end
  adjusted_offsets
end

#plain_textObject



29
30
31
# File 'lib/tag_along/tagged_text.rb', line 29

def plain_text
  @text.pack('U*')
end