Total Pageviews

Friday 26 August 2016

搭建基于hakyll的静态博客程序xcaptain.github.io

请先按此文http://briteming.blogspot.com/2016/07/hakyll.html安装hakyll.

cd /usr/local
git clone --branch source https://github.com/xcaptain/xcaptain.github.io xcaptain.github.io
cd xcaptain.github.io


root@AR:/usr/local/xcaptain.github.io# ls
about.rst   contact.markdown  images   site.hs
blog.cabal  css       index.html  templates   
_cache     fonts       posts   

root@AR:/usr/local/xcaptain.github.io# ghc --make site.hs
root@AR:/usr/local/xcaptain.github.io# ls
about.rst   contact.markdown  images   site   site.hs
blog.cabal  css       index.html  site.o   templates
_cache     fonts       posts   site.hi  
root@AR:/usr/local/xcaptain.github.io# ./site build
root@AR:/usr/local/xcaptain.github.io# ls
about.rst   contact.markdown  images   site   site.hs
blog.cabal  css       index.html  _site    site.o
_cache     fonts       posts   site.hi  templates
root@AR:/usr/local/xcaptain.github.io# cd _site
root@AR:/usr/local/xcaptain.github.io/_site# ls
about.html    css     index.html  posts.html  tags
contact.html  images  posts   rss.xml     tags.html
root@AR:/usr/local/xcaptain.github.io/_site#
(可见/usr/local/xcaptain.github.io/_site/就是静态网站的根目录)

新建源贴:
root@AR:/usr/local/xcaptain.github.io/_site# cd ../posts
root@AR:/usr/local/xcaptain.github.io/posts# nano 2016-08-26-1428-test-1.md
root@AR:/usr/local/xcaptain.github.io/posts# cat 2016-08-26-1428-test-1.md
---
title: 测试1
tags: misc1, misc2, misc3
---

这是测试1.
root@AR:/usr/local/xcaptain.github.io/posts# cd ..
root@AR:/usr/local/xcaptain.github.io# ./site build

演示网站:http://surmount.biz.st:3426,http://xct.smt.biz.st
项目地址:https://github.com/xcaptain/xcaptain.github.io/tree/source
---------------


hakyll算是非常极客的一个工具了,haskell写的,使用pandoc处理文本,而且配置文件完全是haskell代码,想着能用这么geek的语言,玩这么geek的软件,就 非常激动。
使用hakyll也非常简单,步骤是:
  1. cabal install hakyll 安装hakyll,这一步非常耗时,因为自己的笔记本CPU落伍了。
  2. hakyll-init blog 会在当前目录下创建一个叫作blog的目录。
  3. cd blog 进入这个目录,会发现有一系列文件,这是默认的博客站点。
  4. cabal build 默认会build这个haskell站点,生成站点可执行文件。
  5. dist/build/site/site build 会编译posts/下面的文件,生成html文件到_site/posts/下面。
  6. dist/build/site/site watch 会开启一个web服务器,默认访问地址是http://127.0.0.1:4000/ 点击访问,然后就你发现默认的首页了。
  7. 默认站点好了接下来就是迁移原博客的文章,从原来的git仓库中把所有的markdown文件复制到posts/下面。
  8. 再次试着build整个站点,这时候可能会有点问题,我这里因为有的文章元数据中categories:熟悉为空报了几个错,改完就好了。
  9. 配置博客站点,修改站点外观,添加rss和tag页,我用的是variadic的配置,很简单,配置文件改动很小但是 却把我要的功能都实现了,真的很厉害。
  10. 再次build,并且watch新的项目,顺利跑通了之后,但是看不到标签,很奇怪,原来老的文章是使用categories作为标签名的,现在使用tags了,自然要改。 sed -i 's/categories\:/tags\:/g' 一行sed命令,批量替换,真不错。
  11. 博客在本地搭好了,并且预览过了,接下来就是部署到线上,还是用github-pages。那么我就需要2个分支,一个source分支用来存放静态站点的代码,包括站点 配置,原始markdown格式的博文,这样将来要换到别的机器上,直接clone source分支的代码下来build就行了。还有一个分支是master分支,这个分支存放build之后 的文件,也就是所有的静态文件,html, css, js, 图片等。因为我线上已经有一个博客站点了,所以只需要添加远程github仓库就行。 cd blog进入我的博客根目录,执行git init . 添加版本控制, git add ., git commit -m 'first commit', git checkout -b source切换到 source分支,然后删掉master分支git branch -d master,因为代码只要保存在source,master只能放生成的静态文件。git remote add origin git@github.com:xcaptain/xcaptain.github.io添加我的远程仓库, 要注意编辑.gitignore文件,把_site, _tmp, _cache都忽略掉。然后是git submodule add git@github.com:xcaptain/xcaptain.github.io.git _site/把 线上的master分支作为我的一个模块,克隆到_site目录下。再执行一下dist/build/site/site build会生成新的静态文件写入_site/目录,当然那些老的文件不会被 删掉,比如说我之前的CNAME文件,favicon文件都还留着,这时候cd _site/然后再提交一下。本地所有的代码都干净之后就是推送部署了。在blog/目录下执行 git push origin source把当前分支推送到origin的source分支,然后再切换到_site/目录,执行git push origin master把生成的静态文件推送到线上的 master分支。访问http://blog.iyue.club开测,真不错.
from http://blog.iyue.club/posts/2015-12-10-switch-to-hakyll.html