Class: RTF::TextNode
Overview
This class represents a specialisation of the Node class to refer to a Node that simply contains text.
Instance Attribute Summary (collapse)
-
- (Object) text
Actual text.
Attributes inherited from Node
Instance Method Summary (collapse)
-
- (Object) append(text)
This method concatenates a String on to the end of the existing text within a TextNode object.
-
- (TextNode) initialize(parent, text = nil)
constructor
This is the constructor for the TextNode class.
-
- (Object) insert(text, offset)
This method inserts a String into the existing text within a TextNode object.
-
- (Object) to_rtf
This method generates the RTF equivalent for a TextNode object.
Methods inherited from Node
#is_root?, #next_node, #previous_node, #root
Constructor Details
- (TextNode) initialize(parent, text = nil)
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. |
79 80 81 82 83 84 85 86 |
# File 'lib/rtf/node.rb', line 79 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
- (Object) text
Actual text
67 68 69 |
# File 'lib/rtf/node.rb', line 67 def text @text end |
Instance Method Details
- (Object) append(text)
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. |
93 94 95 96 97 98 99 |
# File 'lib/rtf/node.rb', line 93 def append(text) if @text != nil @text = @text + text.to_s else @text = text.to_s end end |
- (Object) insert(text, offset)
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. |
110 111 112 113 114 115 116 |
# File 'lib/rtf/node.rb', line 110 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 |
- (Object) to_rtf
This method generates the RTF equivalent for a TextNode object. This method escapes any special sequences that appear in the text.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/rtf/node.rb', line 120 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 |