lib/rtf/node.rb in clbustos-rtf-0.3.0 vs lib/rtf/node.rb in clbustos-rtf-0.3.1
- old
+ new
@@ -4,18 +4,14 @@
module RTF
# This class represents an element within an RTF document. The class provides
# a base class for more specific node types.
class Node
- # Attribute accessor.
- attr_reader :parent
-
- # Attribute mutator.
- attr_writer :parent
-
-
- # This is the constructor for the Node class.
+ # Node parent.
+ attr_accessor :parent
+
+ # Constructor for the Node class.
#
# ==== Parameters
# parent:: A reference to the Node that owns the new Node. May be nil
# to indicate a base or root node.
def initialize(parent)
@@ -116,11 +112,17 @@
end
# This method generates the RTF equivalent for a TextNode object. This
# method escapes any special sequences that appear in the text.
def to_rtf
- @text == nil ? '' : @text.gsub("{", "\\{").gsub("}", "\\}").gsub("\\", "\\\\")
+ rtf=(@text == nil ? '' : @text.gsub("{", "\\{").gsub("}", "\\}").gsub("\\", "\\\\"))
+ # Encode as Unicode.
+ if RUBY_VERSION>"1.9.0"
+ rtf.encode("UTF-16LE").each_codepoint.map {|cp|
+ cp < 128 ? cp.chr : "\\u#{cp}\\'3f"
+ }.join("")
+ end
end
end # End of the TextNode class.
# This class represents a Node that can contain other Node objects. Its a
@@ -390,10 +392,25 @@
else
apply(style)
end
end
+ # This method provides a short cut means of creating a subscript command
+ # node. The method accepts a block that will be passed a single parameter
+ # which will be a reference to the subscript node created. After the
+ # block is complete the subscript node is appended to the end of the
+ # child nodes on the object that the method is call against.
+ def subscript
+ style = CharacterStyle.new
+ style.subscript = true
+ if block_given?
+ apply(style) {|node| yield node}
+ else
+ apply(style)
+ end
+ end
+
# This method provides a short cut means of creating a superscript command
# node. The method accepts a block that will be passed a single parameter
# which will be a reference to the superscript node created. After the
# block is complete the superscript node is appended to the end of the
# child nodes on the object that the method is call against.
@@ -405,10 +422,25 @@
else
apply(style)
end
end
+ # This method provides a short cut means of creating a strike command
+ # node. The method accepts a block that will be passed a single parameter
+ # which will be a reference to the strike node created. After the
+ # block is complete the strike node is appended to the end of the
+ # child nodes on the object that the method is call against.
+ def strike
+ style = CharacterStyle.new
+ style.strike = true
+ if block_given?
+ apply(style) {|node| yield node}
+ else
+ apply(style)
+ end
+ end
+
# This method provides a short cut means of creating a font command node.
# The method accepts a block that will be passed a single parameter which
# will be a reference to the font node created. After the block is
# complete the font node is appended to the end of the child nodes on the
# object that the method is called against.
@@ -1658,6 +1690,6 @@
text << "\n}"
text.string
end
end # End of the Document class.
-end # End of the RTF module.
\ No newline at end of file
+end # End of the RTF module.