Total Pageviews

Monday 21 November 2016

一个用rust写的静态网站生成器cobalt.rs

(先运行命令:curl -sSf https://static.rust-lang.org/rustup.sh | sh -s -- --channel=nightly 来安装rust环境。参见http://briteming.blogspot.com/2015/11/rust.html)


    
A static site generator written in Rust.

Content

Installation

  $ cargo install cobalt-bin

Examples

There are a few people already using cobalt. Here is a list of projects and their source code that use cobalt.

Usage

  $ cobalt new myBlog
  $ cobalt build -s myBlog -d path/to/your/destination
See more options with
  $ cobalt -h

Layouts

You can have custom layouts in the _layouts directory.
Layouts will be compiled as liquid templates.

Posts

Posts live in posts by default, but you can use a different directory for posts with the -p flag or by setting the posts variable in your .cobalt.yml.
Example:
extends: posts.liquid

title:   My first Blogpost
date:    01 Jan 2016 21:00:00 +0100
---
Hey there this is my first blogpost and this is super awesome.

My Blog is lorem ipsum like, yes it is..
The content before --- are meta attributes ("front matter") made accessible to the template via their key (see below).
The extends attribute specifies which layout will be used.
The date attribute will be used to sort blog posts (from last to first). date must have the format %dd %Mon %YYYY %HH:%MM:%SS %zzzz, so for example 27 May 2016 21:00:30 +0100.

Drafts

Cobalt supports leaving posts in "draft" state. Drafts will not be rendered unless Cobalt is run with the --drafts flag.
To mark a post as draft you can either set draft: true in your front matter or add it to the drafts folder (_drafts by default). The draft folder location can be specified using the draft key in your .cobalt.yml.

Other files

Any file with the .md or .liquid file extension is considered a liquid template and will be parsed for metadata and compiled using liquid, like a post.
Unlike posts, files outside the _posts directory will not be indexed as blog posts and not passed to the index file in the list of contents.
All other files and directories in the source folder will be recursively added to your destination folder.
You can specify different template extensions by setting the template_extensions field in your config file:
template_extensions: ['txt', 'lqd']

Custom paths

Custom paths are much like permalinks in Jekyll, but with a bit more flexibility. You can specify a path attribute in the front matter of any document to give it a custom path. The path is always relative to the document root, independent of where the file is located.
Example:
extends: posts.liquid

title:   My first Blogpost
path: /some/other/path/
would result in a file with the url your-website.com/some/other/path/index.html.
You can also set a global attribute post_path in your .cobalt.yml that will be used for all posts.
Any attribute in the front matter can be interpolated into the path. If you set a date attribute you have access to several other custom attributes. See the Jekyll documentation.
More examples:
date: 01 Jan 2016 21:00:00 +0100
path: /:year/:month/:day/thing.html
-> /2016/01/01/thing.html
date: 01 Jan 2016 21:00:00 +0100
author: johann
path: /:author/:year/:month/:day/title
-> /johann/2016/01/01/title/index.html

Attributes

All template files have access to a set of attributes.
In example above title is accessible via {{ title }} and date via {{ date }}, for the layout template as well as the post template.

Special Attributes

content

{{ content }} is accessible only to layouts and contains the compiled text below the --- block of the post.

posts

{{ posts }} is a list of the attributes of all templates in the _posts directory. Example usage on a page listing all blog posts:
{% for post in posts %}
 <a href="{{post.path}}">{{ post.title }}</a>
{% endfor %}

RSS

To generate an RSS file from the metadata of your _posts, you need to provide the following data in your config.file:
# path where the RSS file should be generated
rss: rss.xml
name: My blog!
description: Blog description
link: http://example.com
None of these fields are optional, as by the RSS 2.0 spec.
Make sure to also provide the fields titledate and description in the front matter of your posts.

Import

To import your site to your gh-pages branch you can either pass a build --import flag when you build the site or after you have build the site with build you can run import. There are also some flags that can be found via import --help.

Deployment

With Travis CI

You can easily deploy a cobalt site to gh-pages! To do this with travis is also very easy. You will need to have rust available on travis. In your travis.yml you will need to have something similar to this:
sudo: false
language: rust

before_script:
  - cargo install cobalt-bin
  - export PATH="$PATH:/home/travis/.cargo/bin"

script:
  - cobalt build

after_success: |
  [ $TRAVIS_BRANCH = master ] &&  
  [ $TRAVIS_PULL_REQUEST = false ] &&  
  cobalt import &&
  git config user.name "Cobalt Site Deployer" &&
  git config user.email "name@example.com" &&
  git push -fq https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git gh-pages
For the GH_TOKEN you will need to create a personal access token, which can be found here, then you will need to use the travis cli tool to encrypt your personal access token. You can do this like so travis encrypt GH_TOKEN=... --add env.global

With GitLab CI

You can also deploy a cobalt site to GitLab Pages using GitLab CI. GitLab CI uses Docker, you can use nott/cobalt or any other image with cobalt in PATH.
An example of .gitlab-ci.yml:

image: nott/cobalt:latest

pages:
  script:
  - mkdir -p public
  - cobalt build -d public
  artifacts:
    paths:
    - public/
  only:
  - master
from https://github.com/cobalt-org/cobalt.rs
----------------------
我的补充说明:
  curl -sSf https://static.rust-lang.org/rustup.sh | sh -s -- --channel=nightly
cargo install cobalt-bin (此步耗时40分钟)
会显示:
...
 Installing /root/.cargo/bin/cobalt
warning: be sure to add `/root/.cargo/bin` to your PATH to be able to run the installed binaries.
于是,编辑/etc/profile文件,在其最下面加上:
export PATH=$PATH:/root/.cargo/bin
然后source /etc/profile
至此,cobalt安装完毕。
cd /usr/local
cobalt new cobalt-site (此命令会在当前目录下,生成cobalt-site目录)
cd cobalt-site

root@AR:/usr/local/cobalt-site# ls
index.liquid  _layouts posts
root@AR:/usr/local/cobalt-site# cobalt build -s . -d output 
(此命令的意思是源目录为当前目录,输出目录为output目录。此命令会在当前目录下,生成output目录。此命令就是生成/更新静态网站的根目录的命令)
root@AR:/usr/local/cobalt-site# ls
index.liquid  _layouts output posts
(生成了output 目录)
root@AR:/usr/local/cobalt-site# cd output
root@AR:/usr/local/cobalt-site/output# ls
index.html  posts
root@AR:/usr/local/cobalt-site/output#
(可见/usr/local/cobalt-site/output就是静态网站的根目录)

新建源帖:
root@AR:/usr/local/cobalt-site/output# cd ../posts
root@AR:/usr/local/cobalt-site/posts# nano test-1.md 
root@AR:/usr/local/cobalt-site/posts# cat test-1.md 
extends: default.liquid

title: 测试1
date: 21 November 2016 21:25:30 -0500
---

这是测试1.
看看如何?
root@AR:/usr/local/cobalt-site/posts# cd ..
root@AR:/usr/local/cobalt-site# cobalt build -s . -d output

演示网站:http://cr.bright.biz.st (不过,这样建立的网站很简陋,连css文件都没有。而且显示的中文是乱码,须设置浏览器的编码为utf-8,才能正常浏览。)

下面克隆别人的第一个修改版:
root@AR:/usr/local/cobalt-site# cd ..
root@AR:/usr/local# git clone https://github.com/johannhof/johannhof.github.io
root@AR:/usr/local# cd johannhof.github.io
root@AR:/usr/local/johannhof.github.io# cobalt build -s . -d output
root@AR:/usr/local/johannhof.github.io# cd output
root@AR:/usr/local/johannhof.github.io/output# 
(/usr/local/johannhof.github.io/output就是静态网站的根目录)

新建源帖:
root@AR:/usr/local/johannhof.github.io/output# cd ..
root@AR:/usr/local/johannhof.github.io# cd blog
root@AR:/usr/local/johannhof.github.io/blog# nano test-1.md
root@AR:/usr/local/johannhof.github.io/blog# cat test-1.md
extends: post.liquid

title: 测试1
date: 22 November 2016 13:17:00 +0100
route: blog
---

这是测试1.

看看如何?
root@AR:/usr/local/johannhof.github.io/blog#
root@AR:/usr/local/johannhof.github.io/blog# cd ..
root@AR:/usr/local/johannhof.github.io# cobalt build -s . -d output

演示网站:http://jh.bright.biz.st
项目地址:https://github.com/johannhof/johannhof.github.io
第二个修改版
cd /usr/local/
git clone https://github.com/amethyst/website amethyst-website
cd amethyst-website

root@AR:/usr/local/amethyst-website# ls
generate.sh  LICENSE.md  README.md  src  TWIA_TEMPLATE.md
root@AR:/usr/local/amethyst-website# cd src
root@AR:/usr/local/amethyst-website/src# ls
assets css  doc  index.liquid _layouts  posts
root@AR:/usr/local/amethyst-website/src# cobalt build -s . -d output
root@AR:/usr/local/amethyst-website/src# ls
assets css  doc  index.liquid _layouts  output  posts
root@AR:/usr/local/amethyst-website/src# cd output
root@AR:/usr/local/amethyst-website/src/output# 
(/usr/local/amethyst-website/src/output就是静态网站的根目录)
新建源帖:
root@AR:/usr/local/amethyst-website/src/output# cd ..
root@AR:/usr/local/amethyst-website/src# cd posts
root@AR:/usr/local/amethyst-website/src/posts# nano test-1.md
root@AR:/usr/local/amethyst-website/src/posts# cat test-1.md
extends: default.liquid

title: 测试1
date: 22 November 2016 15:18:00 -0600
---
这是测试1.

看看如何?
root@AR:/usr/local/amethyst-website/src/posts# cd ..
root@AR:/usr/local/amethyst-website/src# cobalt build -s . -d output

演示网站:http://amt.bright.biz.st
项目地址:https://github.com/amethyst/website

第三个修改版
cd /usr/local/
root@AR:/usr/local# git clone https://github.com/kstep/kstep.github.com
root@AR:/usr/local# cd kstep.github.com
root@AR:/usr/local/kstep.github.com# 
root@AR:/usr/local/kstep.github.com# ls
CNAME     feed.xml  posts       rust _src
_deploy      googlebc2573d126de6c25.html  README.md   scalalab3  static
favicon.ico  index.html  robots.txt  showcase
(/usr/local/kstep.github.com就是静态网站的根目录)
root@AR:/usr/local/kstep.github.com# ls _src
index.liquid  _layouts posts
root@AR:/usr/local/kstep.github.com# cd _src
root@AR:/usr/local/kstep.github.com/_src# ls
index.liquid  _layouts posts
root@AR:/usr/local/kstep.github.com/_src# cobalt build -s . -d .. 
(此即生成/更新静态网站的根目录的命令)

新建源帖:
root@AR:/usr/local/kstep.github.com/_src# cd posts
root@AR:/usr/local/kstep.github.com/_src/posts# nano 2016-11-22-test-1.md
root@AR:/usr/local/kstep.github.com/_src/posts# cat 2016-11-22-test-1.md
extends: default.liquid

lang: en
date: 22 November 2016 17:25:00 +0300
title: 测试1
---

这是测试1,

看看如何?
root@AR:/usr/local/kstep.github.com/_src/posts# cd ..
root@AR:/usr/local/kstep.github.com/_src# cobalt build -s . -d .. 

演示网站:http://ks.bright.biz.st
项目地址:https://github.com/kstep/kstep.github.com