Class: Standoff::AnnotatedString

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AnnotatedString

Returns a new instance of AnnotatedString.



22
23
24
25
26
27
# File 'lib/standoff.rb', line 22

def initialize(options = {})
  if options[:signal] && options[:tags]
    @signal = options[:signal]
    @tags = options[:tags]
  end
end

Instance Attribute Details

#signalObject

Returns the value of attribute signal.



21
22
23
# File 'lib/standoff.rb', line 21

def signal
  @signal
end

#tags(name = nil) ⇒ Object

Returns the value of attribute tags.



21
22
23
# File 'lib/standoff.rb', line 21

def tags
  @tags
end

Instance Method Details

#insert_tag(text, tag) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/standoff.rb', line 60

def insert_tag(text,tag)
    end_tag_form = '</' + tag.name + '>'
    text.insert(tag.end, end_tag_form)
    start_tag_form = '<' + tag.name + tag.attributes.map{|k, v| " #{k}=\'#{v}\'"}.join + '>'
    text.insert(tag.start, start_tag_form)
    return text
end

#inspectObject

re-define, otherwise the to_s overrides the default inspect



53
54
55
56
57
# File 'lib/standoff.rb', line 53

def inspect # re-define, otherwise the to_s overrides the default inspect
  vars = self.instance_variables. 
    map{|v| "#{v}=#{instance_variable_get(v).inspect}"}.join(", ") 
  "<#{self.class}: #{vars}>" 
end

#next_tag(tag) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/standoff.rb', line 77

def next_tag (tag)
  # it's too bad we have to sort these every time. we should make @tags always be sorted.
  tags = @tags.sort
  index = tags.index tag
  # we assume tag is a tag on self
  raise "error in Standoff::AnnotatedString#previous_tag: argument should be a member of self.tags" if index.nil?
  index < tags.length-1 ? tags[index + 1] : nil
end

#previous_tag(tag) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/standoff.rb', line 68

def previous_tag (tag)
  # it's too bad we have to sort these every time. we should make @tags always be sorted.
  tags = @tags.sort
  index = tags.index tag
  # we assume tag is a tag on self
  raise "error in Standoff::AnnotatedString#previous_tag: argument should be a member of self.tags" if index.nil?
  index > 0 ? tags[index - 1] : nil
end

#to_sObject

return the signal as a string with tags interpolated as inline XML



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/standoff.rb', line 36

def to_s # return the signal as a string with tags interpolated as inline XML
  #takes into consideration the ordering of tags
  xml = @signal.dup
  datags = []
  latetags = []
  oldbegin = xml.length
  
  # insert tags starting from the end of the string, so we can rely on the start and end indices
  @tags.sort.reverse.each do |tag|
    next if tag.end > oldbegin # AS allows overlapping tags, but we have to filter them when serializing to inline
    oldbegin = tag.start
    insert_tag(xml,tag)
  end
  
  xml
end