Total Pageviews

Saturday, 23 July 2016

安装静态博客程序hakyll


         1.)  参见https://docs.haskellstack.org/en/stable/README/
(https://docs.haskellstack.org/en/stable/install_and_upgrade/)
        curl -sSL https://get.haskellstack.org/ | sh
        (这一步为安装stack。会显示:
        ...
        Stack has been installed to: /usr/local/bin/stack)
       
        2.)
        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
root@AR:~#
这样ghc就安装好了。

(最近,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
/root/.stack/programs/x86_64-linux/ghc-7.10.3/lib/ghc-7.10.3/bin/ghc
/root/.stack/programs/x86_64-linux/ghc-7.10.3/share/doc/ghc
/root/.stack/programs/x86_64-linux/ghc-7.10.3/bin/ghc
/root/.stack/programs/x86_64-linux/ghc-8.0.1/lib/ghc-8.0.1/bin/ghc

/root/.stack/programs/x86_64-linux/ghc-8.0.1/bin/ghc

(如果运行 stack setup,提示no space left on device.那么这样做:
root@ary:~# which stack
/usr/bin/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
x86_64-linux
root@ary:~/.stack/programs# cd x86_64-linux
root@ary:~/.stack/programs/x86_64-linux# ls
ghc-7.10.3.tar.xz
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   bindist.mk    configure  ghc.mk      libffi      rts
LICENSE   compiler    docs       includes    libraries  rules
Makefile  config.guess    driver       inplace     mk      settings.in
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
这样,ghc就装好了.
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: http://www.haskell.org/ghc/docs/latest/html/users_guide/
If you *really* want to see every option, then you can pass
'--show-options' to the compiler. )

        3.)
        stack install hakyll
        (此步骤耗时50分钟)
(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 
意味着指定解析器ghc的版本为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就安装完成了。
       
        hakyll-init hakyll-site
(此命令会在当前目录下,生成hakyll-site目录)
        cd hakyll-site
        stack init  # To create stack.yaml
        stack build
        (会生成~/hakyll-site/.stack-work/install/i386-linux/lts-6.8/7.10.3/bin/site可执行文件)
        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
        (此命令就是生成静态网站的命令,它会在当前目录下生成_site目录,_site目录就是静态博客的根目录)
        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
---

这是测试1.看看如何呢?

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

注意:源帖在服务器里的posts目录里是按字母顺序(从a到z)和数字顺序(从小到大)从上到下排列的,排在最下面的帖子就显示在网页的最上面。所以我新建了2016-07-24-test-2.markdown后,接着想新建2016-07-24-chinese-economy.markdown,但为了让“稀奇古怪的中国经济”帖子显示在“测试2”帖子的上方,所以需要把2016-07-24-chinese-economy.markdown重命名为2016-07-24-uchinese-economy.markdown(字母u排在字母t之后)。这跟http://briteming.blogspot.com/2016/07/python-icehoney-blog.html里所说的做法一样:
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
root@AR:/usr/local/hakyll-site/posts#


演示网站:http://hk.bright.biz.st/
项目地址:
https://github.com/jaspervdj/hakyll    
https://jaspervdj.be/hakyll/tutorials/01-installation.html
https://github.com/jaspervdj/jaspervdj
(https://github.com/ghc)

一个类似的程序https://github.com/alexanderlobov/homepage
搭建如下:
cd /usr/local
git clone https://github.com/alexanderlobov/homepage 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#
(可见/usr/local/alexanderlobov-homepage/_site就是静态网站的根目录)

新建源帖:
root@AR:/usr/local/alexanderlobov-homepage/_site# cd ..
root@AR:/usr/local/alexanderlobov-homepage# cd posts
root@AR:/usr/local/alexanderlobov-homepage/posts# nano 2016-11-19-test-1.md
root@AR:/usr/local/alexanderlobov-homepage/posts# cat 2016-11-19-test-1.md
---
title: 测试1
tags: misc1, misc2, misc3
language: english
---

这是测试1.

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

演示网站:http://alh.bright.biz.st/
项目地址:https://github.com/alexanderlobov/homepage
----------------------------------

使用 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 的话,你的站点文件结构就会类似于
  .
  ├── README.md
  ├── about.rst
  ├── contact.markdown
  ├── css
  │   └── default.scss
  ├── images
  │   └── haskell-logo.png
  ├── index.html
  ├── posts
  │   └── use-hakyll-to-build-static-website.org
  ├── 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`
                  defaultContext
          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 对理解函数式编程是很有帮助的.


文/编程青年(简书作者)
原文链接:http://www.jianshu.com/p/5374aa58d3ca
-----------------------

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

 from https://jaspervdj.be/hakyll/examples.html

----------
相关帖子:http://briteming.blogspot.com/2016/07/haskellghc.html