20 November 2015

首先在Github上建一个repository,名为 username.github.com(注意是.com 不是.io)。

然后在Github上找一个现成的博客模板,clone到本地,再同步到你的Github上username.github.com项目中。 比如我找的是 jekyll-bootstrap

只需过一会儿,就可以通过 https://username.github.io 访问到你的博客了。

接下来可以定制美化你的博客了(懒的话也可以不做),美化完成后就可以写文章了,所有文章都放到/_posts目录里面, 支持markdown(.md)格式和html(.html)格式,你只需关注内容本身,其他(头、尾、样式、评论功能等)都不用管,文件同步到github后, github会自动使用jekyll(一个静态网站生成器)解析你的代码,生成一个静态页面。 另外,jeklly 的模板页使用的是 Liquid 语言(一种开源的基于Ruby的模板语言),你可能需要熟悉一下它的语法(不是很难)。

然后你会发现每次都要把内容提交到github上才能看到效果很不方便,所以需要在本地也搭建一个jekyll环境,方便在本地显示无误再同步。 在wndows上搭建jekyll环境请看这里:http://jekyll-windows.juthilo.com

你主要需要安装以下软件(括号中是我安装的版本)

  • Ruby (2.2.3)
  • Jekyll (3.0.0)
  • pygments.rb (0.6.3)
  • Python (2.7.10)
  • Pygments (2.0.2)

其中Pygments是实现代码高亮用的,注意pygments.rb和Pygments是两个东西,Pygments是python写的,pygments.rb是ruby写的, pygments.rb相当于一个wrapper,可以让Jekyll调用Pygments.

有时为了安装某一个程序,你还不得不去装一些依赖程序,这种事情经常发生在Linux系统上。

都安装好以后,编辑一下 _config.yml 文件,加入下面这行,启用代码高亮:

    highlighter: pygments
    

PS:pygments的配置文件使用YAML语言,规则还是很简单的,可以自行了解一下。

最后,在windows控制台或linux bash环境下启动服务:

    C:\Users\鸡\Documents\GitHub\chaojidan.github.io>jekyll serve
    Configuration file: C:/Users/鸡蛋/Documents/GitHub/chaojidan.github.io/_config.yml
    Source: C:/Users/鸡蛋/Documents/GitHub/chaojidan.github.io
    Destination: C:/Users/鸡蛋/Documents/GitHub/chaojidan.github.io/_site
    Incremental build: disabled. Enable with --incremental
    Generating...
    done in 3.028 seconds.
    Please add the following to your Gemfile to avoid polling for changes:
    gem 'wdm', '>= 0.1.0' if Gem.win_platform?
    Auto-regeneration: enabled for 'C:/Users/鸡蛋/Documents/GitHub/chaojidan.github.io'
    Configuration file: C:/Users/鸡蛋/Documents/GitHub/chaojidan.github.io/_config.yml
    Server address: http://127.0.0.1:4000/
    Server running... press ctrl-c to stop.
    

在浏览器上访问 http://localhost:4000/ 就能看到效果了