Octopress 上的 utf-8和ASCII-8BIT 冲突的解决方案

昨天在写上一篇文章的时候,突然发现rake generate 不在工作了,提示的错误如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
xiaoxuantekimacbook-pro:octopress txx$ rake generate
## Generating Site with Jekyll
identical source/stylesheets/screen.css 
Configuration from /Users/txx/Desktop/workspace/octopress/_config.yml
Building site: source -> public
Liquid Exception: incompatible character encodings: UTF-8 and ASCII-8BIT in atom.xml
/Users/txx/.rvm/gems/ruby-1.9.3-p392/gems/liquid-2.3.0/lib/liquid/block.rb:92:in `join'
/Users/txx/.rvm/gems/ruby-1.9.3-p392/gems/liquid-2.3.0/lib/liquid/block.rb:92:in `render_all'
/Users/txx/.rvm/gems/ruby-1.9.3-p392/gems/liquid-2.3.0/lib/liquid/block.rb:82:in `render'
/Users/txx/.rvm/gems/ruby-1.9.3-p392/gems/liquid-2.3.0/lib/liquid/template.rb:124:in `render'
/Users/txx/.rvm/gems/ruby-1.9.3-p392/gems/liquid-2.3.0/lib/liquid/template.rb:132:in `render!'
/Users/txx/.rvm/gems/ruby-1.9.3-p392/gems/jekyll-0.12.0/lib/jekyll/convertible.rb:79:in `do_layout'
/Users/txx/Desktop/workspace/octopress/plugins/post_filters.rb:167:in `do_layout'
/Users/txx/.rvm/gems/ruby-1.9.3-p392/gems/jekyll-0.12.0/lib/jekyll/page.rb:100:in `render'
/Users/txx/Desktop/workspace/octopress/plugins/category_generator.rb:100:in `write_category_index'
/Users/txx/Desktop/workspace/octopress/plugins/category_generator.rb:111:in `block in write_category_indexes'
/Users/txx/Desktop/workspace/octopress/plugins/category_generator.rb:110:in `each'
/Users/txx/Desktop/workspace/octopress/plugins/category_generator.rb:110:in `write_category_indexes'
/Users/txx/Desktop/workspace/octopress/plugins/category_generator.rb:139:in `generate'
/Users/txx/.rvm/gems/ruby-1.9.3-p392/gems/jekyll-0.12.0/lib/jekyll/site.rb:190:in `block in generate'
/Users/txx/.rvm/gems/ruby-1.9.3-p392/gems/jekyll-0.12.0/lib/jekyll/site.rb:189:in `each'
/Users/txx/.rvm/gems/ruby-1.9.3-p392/gems/jekyll-0.12.0/lib/jekyll/site.rb:189:in `generate'
/Users/txx/.rvm/gems/ruby-1.9.3-p392/gems/jekyll-0.12.0/lib/jekyll/site.rb:40:in `process'
/Users/txx/.rvm/gems/ruby-1.9.3-p392/gems/jekyll-0.12.0/bin/jekyll:264:in `<top (required)>'
/Users/txx/.rvm/gems/ruby-1.9.3-p392/bin/jekyll:19:in `load'
/Users/txx/.rvm/gems/ruby-1.9.3-p392/bin/jekyll:19:in `<main>'
/Users/txx/.rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `eval'
/Users/txx/.rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `<main>'
Build Failed

我们注意到Error 是出现在

1
Liquid Exception: incompatible character encodings: UTF-8 and ASCII-8BIT in atom.xml

直接把这句话扔给google,结果得到了一大堆日本人同样遇到此问题的解决方案。 例如这个

他们的做法是 在 jekyll 的安装目录下 修改 lib/jekyll/converters/markdown.rb

1
2
3
4
5
6
7
8
       rd = RDiscount.new(content, *@rdiscount_extensions)
       html = rd.to_html
       if rd.generate_toc and html.include?(@config['rdiscount']['toc_token'])
-            html.gsub!(@config['rdiscount']['toc_token'], rd.toc_content)
+            html.gsub!(@config['rdiscount']['toc_token'], rd.toc_content.force_encoding('utf-8'))
       end
       html
     when 'maruku'

结果我怎么用都无效果…..

在google上搜来搜去也没有其他的解决方案了。 就用了一个最暴力的做法,屏蔽markdown 的每个模块找bug,结果发现bug 就出在这里。

那么简单了 把最后的 html 也 force encoding 一下就好了~

1
html = html.force_encoding('utf-8')

再次编译 Accepted!

Comments