Github-Page + Hakyll(MacOS)

Github+Hakyll效果见此。利用Hakyll做模版来做github-page花了挺久的时间,主要是不熟悉Hakyll,找到的教程没有特别好的,因此作此章,记录一下。重要参考tutorial。

在github建repo

首先在github新建一个repository名字叫username.github.io。(github-page要求一定要是这个名字,做成之后访问https://username.github.io/即可看到自己的静态网站。此处的username是你自己github的用户名)

安装Hakyll

Hakyll需要借助Stack来使用。使用下面的命令分别安装 stack, ghc, hakyll(简单参考此处,但并未完全按这个步骤来)

$ brew install haskell-stack

$ stack setup

$ stack install hakyll

用Hakyll在本地创建静态网站的模版

假设我本地的github-page打算放的路径是 ~/workspace/github_page。运行下列命令。

$ stack new myblog hakyll-template

Github-Page + Hakyll(MacOS)_第1张图片
setup-hakyll-template.png

看到all done表示成功。目录下会出现 myblog/文件夹。接着运行以下命令。

git init 

# create new branch called develop and switch to it. 
git checkout -b develop 
    
# track all the source files for our blog. 
git add . 
    
# make our first commit 
git commit -m "initial commit." 
    
# and add the GitHub repository as a remote. 
git remote add origin 
Github-Page + Hakyll(MacOS)_第2张图片
image.png

部署

这里解释一下,我们在本地develop分支上修改posts文件夹中的文件,build完之后搞出release版放在_site文件夹下。然后切换到master分支,从_site中把新release version的东西拿出来,即cp _site/. .命令。然后提交commit,push到远端,这些新版本的文件是新改过之后build出来的发布版,不需要md文件之类的,已经被转成html文件等用于发布。push完切回本地develop分支,删掉master分支。(参考官网tutorial)

# Temporarily save any uncommitted changes that may exist in the current branch.
git stash

# Ensure we are in the correct branch.
git checkout develop

# build(tutorial中这个地方没写好,少了stack build这一步骤)
stack build

# Get a clean build of our site.
stack exec myblog clean
stack exec myblog build

# Update the local list of remote branches to ensure we’re able to
# checkout the branch  we want in the next step.
git fetch --all

# Switch to the master branch.
# 这个地方和github的交互会有问题,见下文
git checkout -b master --track origin/master

在最后一步中 git checkout -b master --track origin/master时候会出现问题:

fatal: 'origin/master' is not a commit and a branch 'master' cannot be created from it

原因是github那边的repo还没有提交过commit,是一个崭新的repo。(google无果之后我采取了比较不优雅的方案,有好的方案欢迎留言。)

# 首先,创建一个新的master分支
git checkout -b master

# 把本地master推到远端,并设定了master去track远端分支origin/master
git push -u origin master

# 操作完可以看一眼当前分支状况,可以看到本地的master分支跟踪了origin/master
git branch -vv
image.png

在develop分支上写好blog的新改动(_site内的内容)之后再切换到master分支,把 _site中的东西拿到目录中。通过master分支推到远端。

# Next, copy the freshly made contents of ’_site’ over the old ones.
cp -a _site/. .

# Commit our changes.
git add -A

git commit -m "Publish."
# And send them to GitHub.
git push origin master:master

# Final clean up and return to the original state.
git checkout develop
git branch -D master

这样就把本地的修改内容推到远端了。访问https://username.github.io/查看自己的个人静态网站。暂时看到的效果是这样:
[图片上传中...(github-page页面.png-98b598-1533453016208-0)]

你可能感兴趣的:(Github-Page + Hakyll(MacOS))