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.



75
76
77
78
79
80
81
82
# File 'lib/rtf/node.rb', line 75

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

Actual text



63
64
65
# File 'lib/rtf/node.rb', line 63

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.



89
90
91
92
93
94
95
# File 'lib/rtf/node.rb', line 89

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.



106
107
108
109
110
111
112
# File 'lib/rtf/node.rb', line 106

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.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rtf/node.rb', line 116

def to_rtf
  rtf=(@text == nil ? '' : @text.gsub("{", "\\{").gsub("}", "\\}").gsub("\\", "\\\\"))
  # This is from lfarcy / rtf-extensions
  # I don't see the point of coding different 128<n<256 range

  #f1=lambda { |n| n < 128 ? n.chr : n < 256 ? "\\'#{n.to_s(16)}" : "\\u#{n}\\'3f" }
  # Encode as Unicode.

  f=lambda { |n| n < 128 ? n.chr : "\\u#{n}\\'3f" }
  # Ruby 1.9 is safe, cause detect original encoding
  # and convert text to utf-16 first
  if RUBY_VERSION>"1.9.0"
    return rtf.encode("UTF-16LE", :undef=>:replace).each_codepoint.map(&f).join('')
  else
    # You SHOULD use UTF-8 as input, ok?
    return rtf.unpack('U*').map(&f).join('')
  end
end