Total Pageviews

Saturday, 23 July 2016


         1.)  参见
        curl -sSL | sh
        Stack has been installed to: /usr/local/bin/stack)
        stack setup (这一步为安装ghc)
        显示:Run from outside a project, using implicit global project config
Using resolver: lts-6.8 from implicit global project's config file: /root/.stack/global-project/stack.yaml
Preparing to install GHC to an isolated location.
This will not interfere with any system-level installation.
Downloaded ghc-7.10.3.                                    
Installed GHC.                             
stack will use a locally installed GHC
For more information on paths, see 'stack path' and 'stack exec env'
To use this GHC and packages outside of a project, consider using:
stack ghc, stack ghci, stack runghc, or stack exec

(最近,2016-09-30,运行stack setup,显示:
Writing implicit global project config file to: /root/.stack/global-project/stack.yaml
Note: You can change the snapshot via the resolver field there.
Downloaded lts-7.1 build plan.    
Fetched package index.                                                          
Populated index cache.    
Preparing to install GHC to an isolated location.
This will not interfere with any system-level installation.
Downloaded ghc-8.0.1.                                      
Installing GHC ...                                                       

Installed GHC. 

root@mn:~# find / -name ghc
echo "export PATH=$PATH:/root/.stack/programs/x86_64-linux/ghc-8.0.1/bin" >> /etc/profile && . /etc/profile)

(如果运行 stack setup,提示no space left on device.那么这样做:
root@ary:~# which stack
root@ary:~# cd .stack
root@ary:~/.stack# ls
build-plan  build-plan-cache  config.yaml  global-project  indices  programs
root@ary:~/.stack# cd programs
root@ary:~/.stack/programs# ls
root@ary:~/.stack/programs# cd x86_64-linux
root@ary:~/.stack/programs/x86_64-linux# ls
root@ary:~/.stack/programs/x86_64-linux# tar Jxvf ghc-7.10.3.tar.xz
root@ary:~/.stack/programs/x86_64-linux# cd ghc-7.10.3
root@ary:~/.stack/programs/x86_64-linux/ghc-7.10.3# ls
INSTALL    configure      libffi      rts
LICENSE   compiler    docs       includes    libraries  rules
Makefile  config.guess    driver       inplace     mk
README      config.sub    ghc       install-sh  packages   utils
root@ary:~/.stack/programs/x86_64-linux/ghc-7.10.3# ./configure
root@ary:~/.stack/programs/x86_64-linux/ghc-7.10.3# make install
root@ary:~/.stack/programs/x86_64-linux/ghc-7.10.3# echo "export PATH=$PATH:~/.stack/programs/x86_64-linux/ghc-7.10.3/bin" >> /etc/profile && source /etc/profile
root@ary:~/.stack/programs/x86_64-linux/ghc-7.10.3# ghc --help

The User's Guide has more information about GHC's *many* options.  An
online copy can be found here:
If you *really* want to see every option, then you can pass
'--show-options' to the compiler. )

        stack install hakyll
(2016-09-30,运行stack install hakyll ,显示:
Run from outside a project, using implicit global project config
Using resolver: lts-6.8 from implicit global project's config file: /root/.stack/global-project/stack.yaml
Downloaded lts-6.8 build plan.    
No compiler found, expected minor version match with ghc-7.10.3 (x86_64) (based on resolver setting in /root/.stack/global-project/stack.yaml).
Try running "stack setup" to install the correct GHC into /root/.stack/programs/x86_64-linux/ 

解决办法:stack setup --resolver ghc-7.10.3 
Run from outside a project, using implicit global project config
Warning: /root/.stack/global-project/stack.yaml: Unrecognized field in ProjectAndConfigMonoid: #resolver
Using resolver: ghc-7.10.3 specified on command line。
Preparing to install GHC to an isolated location.
This will not interfere with any system-level installation.
Downloaded ghc-7.10.3.                                     
Installed GHC.

注意:lts的版本须为6.8,这可以在 /root/.stack/global-project/stack.yaml里面修改resolver的值为lts-6.8来做到。

然后再次运行stack install hakyll ,就不会遇错了。最后,显示:
Copying from /root/.stack/snapshots/i386-linux/lts-6.8/7.10.3/bin/hakyll-init to /root/.local/bin/hakyll-init
        echo 'export PATH=$PATH:/root/.local/bin/' >> /etc/profile && source /etc/profile
        hakyll-init hakyll-site
        cd hakyll-site
        stack init  # To create stack.yaml
        stack build
        echo "export  PATH=$PATH:/root/hakyll-site/.stack-work/install/i386-linux/lts-6.8/7.10.3/bin" >> /etc/profile && source /etc/profile
        site build
        mv hakyll-site /usr/local/
        cd /usr/local/hakyll-site

root@AR:/usr/local/hakyll-site# ls
about.rst  contact.markdown  hakyll-site.cabal    index.html  _site    stack.yaml
_cache       css             images        posts        site.hs  templates
root@AR:/usr/local/hakyll-site# cd _site
root@AR:/usr/local/hakyll-site/_site# ls
about.html  archive.html  contact.html    css  images  index.html  posts
( /usr/local/hakyll-site/_site就是静态博客的根目录)
root@AR:/usr/local/hakyll-site/_site# nano /etc/nginx/sites-available/default
root@AR:/usr/local/hakyll-site/_site# killall nginx
root@AR:/usr/local/hakyll-site/_site# nginx

root@AR:/usr/local/hakyll-site/_site# cd ..
root@AR:/usr/local/hakyll-site# cd posts
root@AR:/usr/local/hakyll-site/posts# nano 2016-07-24-test-1.markdown
root@AR:/usr/local/hakyll-site/posts# cat 2016-07-24-test-1.markdown
title: 测试1
author: brite


root@AR:/usr/local/hakyll-site/posts# cd ..
root@AR:/usr/local/hakyll-site# site build

root@AR:/usr/local/hakyll-site/posts# ls
2015-08-12-spqr.markdown         2016-07-24-test-1.markdown
2015-10-07-rosa-rosa-rosam.markdown  2016-07-24-test-2.markdown
2015-11-28-carpe-diem.markdown         2016-07-24-uchinese-economy.markdown
2015-12-07-tu-quoque.markdown         2016-07-24-umagic-thief.markdown


cd /usr/local
git clone alexanderlobov-homepage
cd alexanderlobov-homepage
ghc --make site.hs (此命令会在当前目录下,生成可执行文件site)
./site build (此命令会在当前目录下,生成_site目录)

root@AR:/usr/local/alexanderlobov-homepage# cd _site
root@AR:/usr/local/alexanderlobov-homepage/_site# ls
archive.html  css  index.html  posts


root@AR:/usr/local/alexanderlobov-homepage/_site# cd ..
root@AR:/usr/local/alexanderlobov-homepage# cd posts
root@AR:/usr/local/alexanderlobov-homepage/posts# nano
root@AR:/usr/local/alexanderlobov-homepage/posts# cat
title: 测试1
tags: misc1, misc2, misc3
language: english


root@AR:/usr/local/alexanderlobov-homepage/posts# cd ..
root@AR:/usr/local/alexanderlobov-homepage# ./site build


使用 hakyll 构建静态站点

hakyll 就拥有更大的灵活性,hakyll 的哲学在于,用户书写内容,hakyll 负责把这些内容转化成不同的展现形式。
这样生成的站点形式就不局限于博客,也可以包含 slides 或者 pdf 之类的内容。

hakyll 的安装与基本使用

hakyll 使用非常高大上的 haskell 语言编写,所以要使用 hakyll 就需要
haskell 环境。在 mac 下的安装非常简单:
  brew install ghc cabal-install
安装完成后记得把 $HOME/.cabal/bin 加入到 $PATH。
  $ hakyll-init my-site
  $ cd my-site
  $ ghc --make -threaded site.hs
  $ ./site build
  $ ./site watch
打开 http://localhost:8000/ 就能看到默认生成的站点了,如果你只想像使用 jekyll 一样使用 hakyll 的话,到这里就够了,同样在 posts 文件夹下书写你的博文就可以了。

hakyll 的高级功能

如果你想领略 hakyll 的强大而灵活的配置,那么请往下看。

hakyll 的工作原理

如果你照着上面的方式安装并初始化了 hakyll 的话,你的站点文件结构就会类似于
  ├── about.rst
  ├── contact.markdown
  ├── css
  │   └── default.scss
  ├── images
  │   └── haskell-logo.png
  ├── index.html
  ├── posts
  │   └──
  ├── site
  ├── site.hi
  ├── site.hs
  ├── site.o
  └── templates
      ├── archive.html
      ├── default.html
      ├── post-list.html
      └── post.html
这样, 其实除了 site 开头的几个文件以外 (site 是编译好的二进制文件,
有100mb 左右大小, 记得不要纳入版本控制!),其他都是静态文件,且结构是可以随便定义的。其中 site.hs 可以认为是 hakyll 的配置文件,打开它就会看到一大堆看(高)不(大)懂(上)的 haskell 代码。
稍微观察一下就会发现, 其中有三个关键词
  • match
  • route
  • compile
match 就是匹配, 后面的文件名是你站点目录下的源文件, 比如 'css/*' 就指代
css 文件夹下的所有文件
route 和我们通常程序中所有的路由有点不同, 指的是 match
到的文件会被放置在编译好的站点的位置(默认是 site 文件夹)
compile 就像是一个管道。一个资源,从 match (头) 到 route(尾)的过程中需要做的转换工作,比如 markdown -> html, 或者压缩混淆等。
这样一来 hakyll 的结构就很清楚了,用户提供原始的数据,然后 hakyll 就去编译他们并且放置到正确的位置。
下面举几个实际的例子,来表现一下 hakyll 的强大。

sass 支持

  -- 使用 compass 来处理 scss 样式
  match (fromList ["sass/main.scss", "sass/blog.scss"]) $ do
      route   $ setExtension "css"
      compile $ getResourceString >>=
          withItemBody (unixFilter "sass" ["-s", "--scss", "-I", "sass/"]) >>=
          return . fmap compressCss


  create ["archive.html"] $ do
      route idRoute
      compile $ do
          posts <- recentFirst =<< loadAll "posts/*"
          let archiveCtx =
                  listField "posts" postCtx (return posts) `mappend`
                  constField "title" "Archives"            `mappend`
          makeItem ""
              >>= loadAndApplyTemplate "templates/archive.html" archiveCtx
              >>= loadAndApplyTemplate "templates/default.html" archiveCtx
              >>= relativizeUrls
这个例子中 archive.html 这个页面原本是不存在的,里面的内容都是动态获取的,它读取了 posts 下的所有文件,然后读取他们的 title 属性, 然后列出来,这和 jekyll 的读取一个对象的属性并循环出来(命令式编程风格)不同,毕竟 haskell 是函数式语言。


  match "slides/*" $ do
      route   $ setExtension "html"
      compile $ getResourceString >>=
          withItemBody (unixFilter "pandoc" ["-s", "--mathml", "-i", "-t", "dzslides"])
使用强大的 pandoc,直接就把 markdown 文件变成 slides 了。


这里 列出了很多使用 hakyll 的网站,都有源码在 github 上,可供参考。


hakyll 将 Haskell 的函数式编程思想带到了静态网站的构建中,学习使用 hakyll 对理解函数式编程是很有帮助的.


People using Hakyll

A lot of sites running Hakyll also publish the source code. This is a very interesting resource to learn from as well. If you’re using Hakyll for your site, and the source code is available, please notify me so I can add you to this list. This list has no particular ordering.
Note that there is also a Hakyll CSS garden which has some themes you can use directly with the default Hakyll site.

Hakyll 4

Hakyll 3.X