Class: RTF::TextNode

Inherits:
Node
  • Object
show all
Defined in:
lib/rtf/node.rb

Overview

This class represents a specialisation of the Node class to refer to a Node that simply contains text.

Instance Attribute Summary collapse

Attributes inherited from Node

#parent

Instance Method Summary collapse

Methods inherited from Node

#is_root?, #next_node, #previous_node, #root

Constructor Details

#initialize(parent, text = nil) ⇒ TextNode

This is the constructor for the TextNode class.

Parameters

parent

A reference to the Node that owns the TextNode. Must not be nil.

text

A String containing the node text. Defaults to nil.

Exceptions

RTFError

Generated whenever an nil parent object is specified to the method.



82
83
84
85
86
87
88
89
# File 'lib/rtf/node.rb', line 82

def initialize(parent, text=nil)
   super(parent)
   if parent == nil
      RTFError.fire("Nil parent specified for text node.")
   end
   @parent = parent
   @text   = text
end

Instance Attribute Details

#textObject

Attribute accessor.



67
68
69
# File 'lib/rtf/node.rb', line 67

def text
  @text
end

Instance Method Details

#append(text) ⇒ Object

This method concatenates a String on to the end of the existing text within a TextNode object.

Parameters

text

The String to be added to the end of the text node.



96
97
98
99
100
101
102
# File 'lib/rtf/node.rb', line 96

def append(text)
   if @text != nil
      @text = @text + text.to_s
   else
      @text = text.to_s
   end
end

#insert(text, offset) ⇒ Object

This method inserts a String into the existing text within a TextNode object. If the TextNode contains no text then it is simply set to the text passed in. If the offset specified is past the end of the nodes text then it is simply appended to the end.

Parameters

text

A String containing the text to be added.

offset

The numbers of characters from the first character to insert the new text at.



113
114
115
116
117
118
119
# File 'lib/rtf/node.rb', line 113

def insert(text, offset)
   if @text != nil
      @text = @text[0, offset] + text.to_s + @text[offset, @text.length]
   else
      @text = text.to_s
   end
end

#to_rtfObject

This method generates the RTF equivalent for a TextNode object. This method escapes any special sequences that appear in the text.



123
124
125
# File 'lib/rtf/node.rb', line 123

def to_rtf
   @text == nil ? '' : @text.gsub("{", "\\{").gsub("}", "\\}").gsub("\\", "\\\\")
end