红宝石下的Serenity-odt 2.0编码:: CompatibilityError

时间:2013-06-27 09:23:03

标签: ruby encoding

这是一个问题:serenity-gem ruby​​ 1.8.7 下完美运行,但在 ruby​​ 2.0.0p195 下发生了一些事情。使用相同的模板和ruby代码时,它给出了以下错误:

incompatible character encodings: ASCII-8BIT and UTF-8 (Encoding::CompatibilityError)
/home/michael/.rvm/gems/ruby-2.0.0-p195/gems/serenity-odt-0.2.1/lib/serenity/generator.rb:5:in `render_odt'
/home/michael/.rvm/gems/ruby-2.0.0-p195/gems/serenity-odt-0.2.1/lib/serenity/odteruby.rb:17:in `eval'
/home/michael/.rvm/gems/ruby-2.0.0-p195/gems/serenity-odt-0.2.1/lib/serenity/odteruby.rb:17:in `evaluate'
/home/michael/.rvm/gems/ruby-2.0.0-p195/gems/serenity-odt-0.2.1/lib/serenity/template.rb:19:in `block (2 levels) in process'
/home/michael/.rvm/gems/ruby-2.0.0-p195/gems/serenity-odt-0.2.1/lib/serenity/template.rb:16:in `each'
/home/michael/.rvm/gems/ruby-2.0.0-p195/gems/serenity-odt-0.2.1/lib/serenity/template.rb:16:in `block in process'
/home/michael/.rvm/gems/ruby-2.0.0-p195/gems/rubyzip-0.9.9/lib/zip/zip_file.rb:90:in `open'
/home/michael/.rvm/gems/ruby-2.0.0-p195/gems/serenity-odt-0.2.1/lib/serenity/template.rb:15:in `process'
/home/michael/.rvm/gems/ruby-2.0.0-p195/gems/serenity-odt-0.2.1/lib/serenity/generator.rb:5:in `render_odt'

此解决方案对我没有影响:githubstackoverflow

我的环境:Ubuntu 12.04.2 LTS x86_64 3.2.0-49-generic,rvm 1.21.2。

1 个答案:

答案 0 :(得分:0)

通过在 /home/michael/.rvm/gems/ruby-2.0.0-p195/gems/serenity-odt-0.2.1/lib/serenity/template中添加字符串content = content.force_encoding('UTF-8')来修复。 RB

def process context
  tmpfiles = []
  Zip::ZipFile.open(@template) do |zipfile|
    %w(content.xml styles.xml).each do |xml_file|
      content = zipfile.read(xml_file)
      content = content.force_encoding('UTF-8')
      odteruby = OdtEruby.new(XmlReader.new(content))
      out = odteruby.evaluate(context)

      tmpfiles << (file = Tempfile.new("serenity"))
      file << out
      file.close

      zipfile.replace(xml_file, file.path)
    end
  end
end

我从here获得灵感。