完善本地搭建的jekyll环境(Windows)

序:上篇文章虽然在本地搭建好了jekyll环境,但是却存在一些问题,如通过jekyll new创建的站点无法正常跑起来、中文编码有问题。这说明之前搭建的环境有不周之处。

PS:因之前自己搭建环境时并未做过多记录,现在写起文章来需要在模拟几次安装,所以本文写的有点慢,毕竟不像当初只是为了安装,现在是为了弄清楚安装。立足点不同,需要做的事情也不同,这次需要做许多确认工作。

一、语法高亮插件问题

通过jekyll new命令创建的站点中使用了语法高亮插件pygments(该语法插件支持超多语言,估计也是因此jekyll中便使用了吧),但pygments需要单独另外安装,所以导致部署站点时出现如下错误,虽然网站启动了,但是并没有成功生产静态页面,导致浏览页面时都是空的。

C:	est>jekyll serve
Configuration file: C:/test/_config.yml
            Source: C:/test
       Destination: C:/test/_site
      Generating... C:/Ruby193/lib/ruby/gems/1.9.1/gems/posix-spawn-0.3.6/lib/po
six/spawn.rb:162: warning: cannot close fd before spawn
  Liquid Exception: No such file or directory - /bin/sh in _posts/2013-09-0
9-welcome-to-jekyll.markdown
done.
[2013-09-09 20:57:52] INFO  WEBrick 1.3.1
[2013-09-09 20:57:52] INFO  ruby 1.9.3 (2013-05-15) [i386-mingw32]
[2013-09-09 20:57:53] INFO  WEBrick::HTTPServer#start: pid=344 port=4000

在自动生成的站点的_post下,有一后缀为markdown的文件,其内容中包含如下代码,即以ruby语法高亮显示中间的代码,正是这个代码引起了上面的错误:

{% highlight ruby %}
def print_hi(name)
  puts "Hi, #{name}"
end
print_hi('Tom')
#=> prints 'Hi, Tom' to STDOUT.
{% endhighlight %}

对于上面的错误提示信息,我当初看得也是一头雾水,网上搜了下才发现的,至于错误信息具体啥意思,没有深究,毕竟这是ruby语言的,而我还没也没功夫去学~

下面开始解决此问题:

安装Python:好了下面开始解决问题,先去pythonPortablePython下载一个v2.7版本的Python(我下的是PortablePython_2.7.2.1.exe,v3版本似乎不行,网上有一些文章给了个V3版本的下载地址,我试了多次都无法解决此问题,不知道是不是因为这些文章过于久远导致方法过时了,最后幸得有人提过此问题,才得以解决。),这个似乎是另一门编程语言了啊,这个语法高亮的插件便是利用该语言编写的,因此要使用该插件就要安装它了。这里稍微提下python与Portablepython不同吧,后者继承了许多东东,比如这里如果安装了它,那么pygments便已经继承了在里面(我下的这个版本是如此),但后来发现pygments并不是最新版-.-!,虽然仍然可以继续下面的pygments安装,但是是否替换了老版本就无法确认了,也许版本之间的差异感觉不出来,但既然有这样的问题,还是选择用python好了,不过下面也会提及Portablepython的,因为我最初就是用它的。

如果安装PortablePython:下载安装版安装好后(选择安装全部即可~),需要自己配系统环境变量,对此表示无解(配置的路径为C:Portable Python 2.7.2.1App,具体看你的安装路径,然后指定到App文件夹即可)。

如果安装Python:下载安装好后系统环境变量已经自动配置了。

安装pygments:这里需要借用easy install来安装pygments(pygments官网介绍的),所谓easy install就是一个工具方便我们在python中安装类似pygments这类的东东。在pygments官方有介绍安装的:http://pygments.org/download/,有兴趣的可以瞧瞧。

如果是Python:因为要借用easy_install来安装pygments,所以需要先安装easy_install,根据http://peak.telecommunity.com/DevCenter/EasyInstall介绍,需要借用setuptools来安装easy_install,到此处https://pypi.python.org/pypi/setuptools下载最新版的setuptools,不过根据https://pypi.python.org/pypi/setuptools/1.1.6里面的说明,在Windows下建议的安装方式是直接下载安装脚本,该脚本会联网下载setuptools并进行安装(其实下载下来的setuptools里面就包含了该安装脚本-_-,如果把下载下来的安装脚本和setuptools放在一块,脚本会直接读取本地已有的setuptools进行安装。)。

下载安装脚本后,通过cmd执行python ez_setup.py命令即可,之后会刷屏哦,刷完基本就好了,当然前提是你网络信号要好啊^_^。之后再python目录下会多出一个Script文件夹,easy_install就在里面,在cmd中切换到Script文件夹,然后执行easy_install Pygments命令来安装Pygments,该操作也是联网下载安装(当然也可以离线,需要到此处下载相应版本的egg文件放于Script目录下),安装的日志信息同PortablePython使用easy_install Pygments命令应该是一样的。

如果是PortablePython:我在安装PortablePython后,easy install已经附带在里面的AppScript目录下(可能有些版本不会附带进去的,比如V3版本,该文章中便有介绍如何安装,但是V3版无法解决此处的问题,所以这里就不提及如何安装easy install了),在配置好系统环境变量后,打开cmd切换到AppScript下,执行如下命令即可,之后便会联网下载所需的文件进行安装,如下所示提示信息,注意大小写!!

C:Portable Python 2.7.2.1AppScripts>easy_install Pygments
Searching for Pygments
Reading http://pypi.python.org/simple/Pygments/
Best match: Pygments 1.6
Downloading https://pypi.python.org/packages/2.7/P/Pygments/Pygments-1.6-py2.7.e
gg#md5=1e1e52b1e434502682aab08938163034
Processing Pygments-1.6-py2.7.egg
creating c:portable python 2.7.2.1applibsite-packagesPygments-1.6-py2.7.egg

Extracting Pygments-1.6-py2.7.egg to c:portable python 2.7.2.1applibsite-pac
kages
Adding Pygments 1.6 to easy-install.pth file
Installing pygmentize-script.py script to C:Portable Python 2.7.2.1AppScripts

Installing pygmentize.exe script to C:Portable Python 2.7.2.1AppScripts
Installing pygmentize.exe.manifest script to C:Portable Python 2.7.2.1AppScri
pts

Installed c:portable python 2.7.2.1applibsite-packagespygments-1.6-py2.7.eg
g
Processing dependencies for Pygments
Finished processing dependencies for Pygments

在安装好pygments,我们比如会兴奋的开始在自己的站点中执行jekyll serve,但发现问题还存在着(如果你的没问题,先请看下文,如果还是没问题,我真好奇了,请赐教~)

最终的问题着实让我纠结了一阵子,幸好有网友于网上提过次问题,幸得解决,该问题最终归结到了jekyll这边,确切的说是ruby这边,具体是pygments.rb上,瞧这名字估计是ruby调用pygments的一个工具包之类的,通过在cmd执行gem list可以看到pygments.rb的版本:

C:	est>gem list

*** LOCAL GEMS ***

bigdecimal (1.1.0)
classifier (1.3.3)
colorator (0.1)
commander (4.1.5)
directory_watcher (1.4.1)
fast-stemmer (1.0.2)
highline (1.6.19)
io-console (0.3)
jekyll (1.2.1)
json (1.5.5)
liquid (2.5.2)
maruku (0.6.1)
minitest (2.5.1)
posix-spawn (0.3.6)
pygments.rb (0.5.2)
rake (0.9.2.2)
rdoc (3.9.5)
redcarpet (2.3.0)
safe_yaml (0.7.1)
syntax (1.0.0)
yajl-ruby (1.1.0 x86-mingw32)

根据Stack Overflow上此贴的回答,pygments.rb的0.5.1、0.5.2版本有问题,导致无法正常执行(至于具体原因还没有深究过,不过觉得奇怪,为啥官方不修复呢?),所以需要把版本降至0.5.0才能正常运行,那么先安装0.5.0版然后卸载高版本就行了,如下命令,主要联网哦:

C:>gem install pygments.rb --version "=0.5.0"
Fetching: pygments.rb-0.5.0.gem (100%)
Successfully installed pygments.rb-0.5.0
1 gem installed
Installing ri documentation for pygments.rb-0.5.0...
Installing RDoc documentation for pygments.rb-0.5.0...

C:>gem uninstall pygments.rb --version "=0.5.2"
Successfully uninstalled pygments.rb-0.5.2

之后执行jekyll serve就没问题了:

C:	est>jekyll serve
Configuration file: C:/test/_config.yml
            Source: C:/test
       Destination: C:/test/_site
      Generating... done.
  Server running... press ctrl-c to stop.

事后(2013-09-20),在看Ruby193lib ubygems1.9.1gemsjekyll-1.2.1目录下的相关文件时,发现jekyll.gemspec中包含如下信息:

  s.add_runtime_dependency('liquid', "~> 2.5.2")
  s.add_runtime_dependency('classifier', "~> 1.3")
  s.add_runtime_dependency('directory_watcher', "~> 1.4.1")
  s.add_runtime_dependency('maruku', "~> 0.5")
  s.add_runtime_dependency('pygments.rb', "~> 0.5.0")
  s.add_runtime_dependency('commander', "~> 4.1.3")

上面红色字样可见依赖的是0.5.0版本,从而可以推测高版本真的不行吧。

然后我在rubygems网站上的jekyll下载页也看到了类似信息:

Runtime Dependencies
classifier ~> 1.3
colorator ~> 0.1
commander ~> 4.1.3
directory_watcher ~> 1.4.1
liquid ~> 2.5.2
maruku ~> 0.5
pygments.rb ~> 0.5.0
redcarpet ~> 2.3.0
safe_yaml ~> 0.7.0

 二、中文乱码问题(Add@2013-11-23:新版本已不再是问题,参见Windows下运行jekyll,编码已不再是问题)

由于这些都是外国进来的玩意,所以由于语言的不同,乱码也就产生了。一般咱们编写都是采用utf-8的吧,但是在windows下安装的jekyll,默认是以GBK编码的方式去读取咱们编写的文件,如此便乱码了。

要解决此问题,总不至于要写GBK编码的文件吧,毕竟这个编码不怎么通用,所以需要修改jekyll中的代码,让其以utf-8编码来读取我们的文件,已有网友找到解决方法!需要修改jekyll中读取的代码,指定为utf-8编码即可。具体如下:

//文件:C:Ruby193lib
ubygems1.9.1gemsjekyll-1.2.1libjekyllconvertible.rb
//具体路径视版本号及安装路径有所不同
//找到如下代码:
self.content = File.read(File.join(base, name))
//修改为:
self.content = File.read(File.join(base, name), :encoding => "utf-8")

之后在文件中写入中文时,记得保存为utf-8的编码,否则运行时会报错的哦!

事后(2013-09-20),在Ruby193lib ubygems1.9.1gemsjekyll-1.2.1site中发现疑似jekyll官网源码,且是以jekyll来运行的,于是好奇考出执行jekyll serve命令,谁知报如下错误:

Generating... [31m  Liquid Exception: invalid byte sequence in GBK in _posts/2013-05-06-jekyll-1-0-0-released.markdown

初步推测是编码问题,此时前面提到的编码问题已经修复,定是其它地方出现此类问题导致。通过jekyll serve --trace命令打印出详细错误信息:

 from C://Ruby193/lib/ruby/gems/1.9.1/gems/liquid-2.5.2/lib/liquid/template.rb:169:in `tokenize'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/liquid-2.5.2/lib/liquid/template.rb:75:in `parse'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/liquid-2.5.2/lib/liquid/template.rb:62:in `parse'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/jekyll-1.2.1/lib/jekyll/tags/include.rb:59:in `render'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/liquid-2.5.2/lib/liquid/block.rb:137:in `block in render_all'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/liquid-2.5.2/lib/liquid/block.rb:124:in `each'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/liquid-2.5.2/lib/liquid/block.rb:124:in `render_all'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/liquid-2.5.2/lib/liquid/block.rb:110:in `render'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/liquid-2.5.2/lib/liquid/template.rb:150:in `render'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/liquid-2.5.2/lib/liquid/template.rb:160:in `render!'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/jekyll-1.2.1/lib/jekyll/convertible.rb:82:in `render_liquid'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/jekyll-1.2.1/lib/jekyll/convertible.rb:113:in `render_all_layouts'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/jekyll-1.2.1/lib/jekyll/convertible.rb:148:in `do_layout'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/jekyll-1.2.1/lib/jekyll/post.rb:259:in `render'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/jekyll-1.2.1/lib/jekyll/site.rb:205:in `block in render'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/jekyll-1.2.1/lib/jekyll/site.rb:204:in `each'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/jekyll-1.2.1/lib/jekyll/site.rb:204:in `render'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/jekyll-1.2.1/lib/jekyll/site.rb:36:in `process'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/jekyll-1.2.1/lib/jekyll/command.rb:18:in `process_site'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/jekyll-1.2.1/lib/jekyll/commands/build.rb:23:in `build'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/jekyll-1.2.1/lib/jekyll/commands/build.rb:7:in `process'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/jekyll-1.2.1/bin/jekyll:99:in `block (2 levels) in <top (required)>'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/commander-4.1.5/lib/commander/command.rb:180:in `call'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/commander-4.1.5/lib/commander/command.rb:180:in `call'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/commander-4.1.5/lib/commander/command.rb:155:in `run'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/commander-4.1.5/lib/commander/runner.rb:402:in `run_active_command'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/commander-4.1.5/lib/commander/runner.rb:78:in `run!'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/commander-4.1.5/lib/commander/delegates.rb:11:in `run!'
 from C://Ruby193/lib/ruby/gems/1.9.1/gems/commander-4.1.5/lib/commander/import.rb:10:in `block in <top (required)>'

之后一个个文件查看了下,发现Ruby193lib ubygems1.9.1gemsjekyll-1.0.3libjekyll agsinclude.rb中有有文件读取代码,经如下修改能正常运行:

#原来的代码
source = File.read(File.join(includes_dir, @file))
    
#修改为以utf-8编码读取    
source = File.read(File.join(includes_dir, @file),:encoding=>"utf-8")

可见在解决了一个编码问题后,同类的问题也就能够自己解决了,以后再遇到此类编码问题,便可以自己查看错误信息尝试解决了。从这个问题可以看出,在windows下运行编码问题着实让人郁闷啊……

 PS:以上是我目前在Windows上使用所遇到的问题(事后遇到问题我会不断补充),在某些参考文章中还提到其他的一些问题,但我却没碰到,可能是版本差异的原因吧,如果有朋友遇到问题,我很乐意交流。

参考资料:

http://stackoverflow.com/questions/17364028/jekyll-on-windows-pygments-not-working

http://www.madhur.co.in/blog/2011/09/01/runningjekyllwindows.html

http://stackoverflow.com/questions/14253116/run-jekyll-server-failed-in-win7

http://liufeiyu.cn/github/2012/12/04/how-to-use-git-to-post-articles.html

原文地址:https://www.cnblogs.com/yevon/p/3310857.html