Jekyll中文
前几天在使用Jekyll进行博客开发的时候,一直遇到一个问题,就是使用中文会显示不出来,或者显示为乱码。
主要有以下几个问题吧:
-
首先就是ruby自身的问题了,这里需要更改ruby中安装的Jekyll目录下的convertible.rb文件,将文件中的:
self.content = File.read(File.join(base, name))
-
这点改完之后在post里的中文已经可以正常显示,但是在将title设置为中文或者在include文件中使用中文时仍然会有问题,首先说title中的中文,这点和我所使用的
Rakefile
文件有关,该文件中对title中的特殊字符进行了过滤,使用了如下的规则:slug = name.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
这里将所有的非英文字符或数字的字符都替换为了空,这样可以避免在文件名中加入特殊符号,为了在保持这一特性的情况下正常使用中文,这里重新定义了变量
name
,在自动生成文件时需要输入name属性来对其进行初始化,而这样一来生成post的指令就从:rake post title="hello"
变成了
rake post name="hello" title="中文"
这里,title若不设置则将其设置为与name一样。
-
解决了文件名中文字符的显示后又带来了一个新的问题,title属性若为中文时在sublime就显示为乱码,但是在notepad++中显示正常,查看了一下是rake生成的文件格式的问题,title中包含中文是rake自动生成的文件格式就变成了ANSI格式的,这个可能是Windows操作系统有关,由于Jekyll生成网页时需要UTF-8格式的文件,所以可以直接使用notepad++ 进行文件格式的转换,而这里为了方便使用在sublime中添加了convertToUTF8插件,具体添加方法可以参考官方文档。添加完之后重启sublime,这样在读入其他格式的文件时会自动将其转化为UTF-8格式的内容进行显示,但是在保存默认会将其按照原来的格式进行保存,所以需要设置convertToUTF8的一个属性:
convert_on_save:never
这样在文件关闭时就不会被保存为原来的格式了。
-
这样解决了title在sublime中的显示和文件格式的问题后,仍然就一个比较严峻的问题,就是在include文件中出现的中文会影响本地服务器的启动,会报错:
incompatible character encodings: UTF-8 and GBK.
这个根据网上查找的结果,应该是Windows系统控制台的原因,网上给出的方法中测试了几个,最后只有
chcp 65001
使得能够本地服务器能够正常启动,但是每次启动本地服务器前都输入这条指令就显得有些麻烦了,所以将这条指令配置到了Rakefile
中,这样就可以在启动本地服务器的时候自动切换到65001字符集的控制台。
在最后,上述中有些配置文件的更改是在本地的,在上传到github后要经过github 的服务器编译运行,但是幸好的是本地所遇到的问题都没有在github的博客网站上碰到。
最后附上Rakefile的源码吧。
require "rubygems"
require 'rake'
require 'yaml'
require 'time'
SOURCE = "."
CONFIG = {
'version' => "0.3.0",
'themes' => File.join(SOURCE, "_includes", "themes"),
'layouts' => File.join(SOURCE, "_layouts"),
'posts' => File.join(SOURCE, "_posts"),
'post_ext' => "md",
}
# Usage: rake post title="A Title" [date="2012-02-09"] [tags=[tag1,tag2]] [category="category"]
desc "Begin a new post in #{CONFIG['posts']}"
task :post do
abort("rake aborted: '#{CONFIG['posts']}' directory not found.") unless FileTest.directory?(CONFIG['posts'])
name = ENV["name"] || "new-post"
title = ENV["title"] || name
puts title
tags = ENV["tags"] || "[]"
category = ENV["category"] || ""
# slug = title.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
slug = name.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
begin
date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now).strftime('%Y-%m-%d')
rescue => e
puts "Error - date format must be YYYY-MM-DD, please check you typed it correctly!"
exit -1
end
filename = File.join(CONFIG['posts'], "#{date}-#{slug}.#{CONFIG['post_ext']}")
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title}\""
post.puts 'description: ""'
post.puts "category: \"#{category.gsub(/-/,' ')}\""
post.puts "tags: #{tags}"
post.puts "date: #{Time.now}"
post.puts "---"
end
end # task :post
# Usage: rake page name="about.html"
# You can also specify a sub-directory path.
# If you don't specify a file extention we create an index.html at the path specified
desc "Create a new page."
task :page do
name = ENV["name"] || "new-page.md"
filename = File.join(SOURCE, "#{name}")
filename = File.join(filename, "index.html") if File.extname(filename) == ""
title = File.basename(filename, File.extname(filename)).gsub(/[\W\_]/, " ").gsub(/\b\w/){$&.upcase}
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
mkdir_p File.dirname(filename)
puts "Creating new page: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: page"
post.puts "title: \"#{title}\""
post.puts 'description: ""'
post.puts "---"
end
end # task :page
desc "Launch preview environment"
task :preview do
system "chcp 65001"
system "jekyll serve -w"
end # task :preview
def verify_manifest(theme_path)
manifest_path = File.join(theme_path, "manifest.yml")
manifest_file = File.open( manifest_path )
abort("rake aborted: repo must contain valid manifest.yml") unless File.exist? manifest_file
manifest = YAML.load( manifest_file )
manifest_file.close
manifest
end
def ask(message, valid_options)
if valid_options
answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer)
else
answer = get_stdin(message)
end
answer
end
def get_stdin(message)
print message
STDIN.gets.chomp
end
#Load custom rake scripts
Dir['_rake/*.rake'].each { |r| load r }