Created
September 6, 2011 14:23
-
-
Save bastien/1197679 to your computer and use it in GitHub Desktop.
Trying to write out files in UTF-8 that TextEdit, Pages, Word and open
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Danish: æåø | |
Chinese: 我真累 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
require 'rubygems' | |
require 'rtf' | |
include RTF | |
module RTF | |
# https://github.com/lfarcy/rtf-extensions | |
# Modifies the TextNode class to manage utf8 character strings | |
class TextNode < Node | |
# 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 = @text || '' | |
_text = _text.gsub("\\", "\\\\\\").gsub("{", "\\{").gsub("}", "\\}") | |
_text.unpack('U*').map { |n| n < 128 ? n.chr : n < 256 ? "\\'#{n.to_s(16)}" : "\\u#{n}\\'3f" }.join('') | |
end | |
end | |
end | |
content = "" | |
f = File.open("input.txt", 'r') | |
content = f.read | |
f.close | |
document = Document.new(Font.new(Font::ROMAN, 'Times New Roman')) | |
document.paragraph << content | |
temp_file = File.open("output.rtf", 'w') do |f| | |
f.puts document.to_rtf | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
puts `file input.txt -I` | |
content = "" | |
f = File.open("input.txt", 'r', encoding:'UTF-8') | |
content = f.read | |
f.close | |
temp_file = File.open("output.txt", 'w') do |f| | |
f.puts content | |
end | |
f = File.open("output.txt", 'r', encoding:'UTF-8') | |
puts f.read | |
puts f.read.encoding | |
f.close | |
puts `file output.txt -I` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment