Class: Standoff::AnnotatedString
- Inherits:
-
Object
- Object
- Standoff::AnnotatedString
- Defined in:
- lib/standoff.rb
Instance Attribute Summary collapse
-
#signal ⇒ Object
Returns the value of attribute signal.
-
#tags(name = nil) ⇒ Object
Returns the value of attribute tags.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ AnnotatedString
constructor
A new instance of AnnotatedString.
- #insert_tag(text, tag) ⇒ Object
-
#inspect ⇒ Object
re-define, otherwise the to_s overrides the default inspect.
- #next_tag(tag) ⇒ Object
- #previous_tag(tag) ⇒ Object
-
#to_s ⇒ Object
return the signal as a string with tags interpolated as inline XML.
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( = {}) if [:signal] && [:tags] @signal = [:signal] = [:tags] end end |
Instance Attribute Details
#signal ⇒ Object
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 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 |
#inspect ⇒ Object
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. = .sort index = .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 < .length-1 ? [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. = .sort index = .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 ? [index - 1] : nil end |
#to_s ⇒ Object
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 = [] = [] oldbegin = xml.length # insert tags starting from the end of the string, so we can rely on the start and end indices .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 |