hugo-最好用的靜態網站生成器
安裝Hugo
1. 二進制安裝(推薦:簡單、快速)
到 Hugo Releases 下載對應的操作系統版本的Hugo二進制文件(hugo或者hugo.exe)
Mac下直接使用 Homebrew 安裝:
brew install hugo
2. 源碼安裝
源碼編譯安裝,首先安裝好依賴的工具:
設置好 GOPATH 環境變量,獲取源碼并編譯:
$ export GOPATH=$HOME/go $ go get -v github.com/spf13/hugo
源碼會下載到 $GOPATH/src 目錄,二進制在 $GOPATH/bin/
如果需要更新所有Hugo的依賴庫,增加 -u 參數:
$ go get -u -v github.com/spf13/hugo
生成站點
使用Hugo快速生成站點,比如希望生成到 /path/to/site 路徑:
$ hugo new site /path/to/site
這樣就在 /path/to/site 目錄里生成了初始站點,進去目錄:
$ cd /path/to/site
站點目錄結構:
? archetypes/ ? content/ ? layouts/ ? static/ config.toml
創建文章
創建一個 about 頁面:
$ hugo new about.md
about.md 自動生成到了 content/about.md ,打開 about.md 看下:
+++ date = "2015-10-25T08:36:54-07:00" draft = true title = "about" +++ 正文內容
內容是 Markdown 格式的, +++ 之間的內容是 TOML 格式的,根據你的喜好,你可以換成 YAML 格式(使用 --- 標記)或者 JSON 格式。
創建第一篇文章,放到 post 目錄,方便之后生成聚合頁面。
$ hugo new post/first.md
打開編輯 post/first.md :
--- date: "2015-10-25T08:36:54-07:00" title: "first" --- ### Hello Hugo 1. aaa 1. bbb 1. ccc
安裝皮膚
到 皮膚列表 挑選一個心儀的皮膚,比如你覺得 Hyde 皮膚不錯,找到相關的 GitHub 地址,創建目錄 themes ,在 themes 目錄里把皮膚 git clone 下來:
# 創建 themes 目錄 $ cd themes $ git clone https://github.com/spf13/hyde.git
運行Hugo
在你的站點根目錄執行 Hugo 命令進行調試:
$ hugo server --theme=hyde --buildDrafts --watch
使用 --watch 參數可以在修改文章內容時讓瀏覽器自動刷新。
瀏覽器里打開: http://localhost:1313
部署
假設你需要部署在 GitHub Pages 上,首先在GitHub上創建一個Repository,命名為: coderzh.github.io (coderzh替換為你的github用戶名)。
在站點根目錄執行 Hugo 命令生成最終頁面:
$ hugo --theme=hyde --baseUrl="http://coderzh.github.io/"
如果一切順利,所有靜態頁面都會生成到 public 目錄,將pubilc目錄里所有文件 push 到剛創建的Repository的 master 分支。
$ cd public $ git init $ git remote add origin https://github.com/coderzh/coderzh.github.io.git $ git add -A $ git commit -m "first commit" $ git push -u origin master
瀏覽器里訪問: http://coderzh.github.io/
這個網站 java union 就是我使用hugo生成的。 這個網站模板是我自己寫的(樣式部分除外),大家如果有關于hugo的以及go 模板相關的問題可以問我。