Method: RTF::TextNode#to_rtf

Defined in:
lib/rtf/node.rb

#to_rtfObject

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