Hugo によるはじめてのサイトジェネレート!
Jekyll での記事生成速度が遅くなってきたので、速いと評判の Hugo を使ってみることにしました。遅いといっても 146 記事で 5 秒ちょいなので、待てなくはありません。Jekyll は 2013 年 3 月から使いはじめたので、36 posts/year という増加量です。1000 記事に行く前に死んでしまうでしょうから、たいした問題ではないのかもしれません。しかし変更したものについては、ブラウザですぐに確認したいので、やっぱりたいした問題なのでした。
インストール
go get -v github.com/spf13/hugo
これでインストールすることができます。アップデートをする時は下記のように -u
オプションを付ければ良いみたい。
go get -u -v github.com/spf13/hugo
ダウンロードには Hugo 本体やその他のライブラリ等のインストールを含めると、トータルで 11 分くらいかかりました。その時のログは下記なのですが、依存しているものがかなりたくさんあるのですね。
> go get -v github.com/spf13/hugo github.com/spf13/hugo (download) github.com/fsnotify/fsnotify (download) ... golang.org/x/sys (download) github.com/kardianos/osext (download) github.com/spf13/afero (download) ... golang.org/x/text (download) ... github.com/spf13/cast (download) github.com/spf13/cobra (download) github.com/spf13/pflag (download) github.com/cpuguy83/go-md2man (download) github.com/spf13/fsync (download) github.com/PuerkitoBio/purell (download) github.com/PuerkitoBio/urlesc (download) ... golang.org/x/net (download) github.com/kyokomi/emoji (download) github.com/miekg/mmark (download) github.com/BurntSushi/toml (download) github.com/mitchellh/mapstructure (download) github.com/russross/blackfriday (download) github.com/shurcooL/sanitized_anchor_name (download) github.com/spf13/viper (download) github.com/hashicorp/hcl (download) github.com/magiconair/properties (download) github.com/pelletier/go-toml (download) github.com/pelletier/go-buffruneio (download) github.com/spf13/jwalterweatherman (download) github.com/bep/gitmap (download) github.com/bep/inflect (download) github.com/dchest/cssmin (download) github.com/nicksnyder/go-i18n (download) github.com/eknkc/amber (download) github.com/yosssi/ace (download) github.com/spf13/nitro (download) github.com/gorilla/websocket (download) golang.org/x/sys/unix github.com/kardianos/osext golang.org/x/text/transform github.com/spf13/afero/mem github.com/spf13/cast github.com/spf13/pflag golang.org/x/text/unicode/norm github.com/cpuguy83/go-md2man/vendor/github.com/shurcooL/sanitized_anchor_name github.com/cpuguy83/go-md2man/vendor/github.com/russross/blackfriday github.com/fsnotify/fsnotify github.com/PuerkitoBio/urlesc golang.org/x/text/unicode/bidi github.com/spf13/cobra github.com/cpuguy83/go-md2man/md2man golang.org/x/text/secure/bidirule github.com/spf13/afero golang.org/x/text/width golang.org/x/net/idna github.com/kyokomi/emoji github.com/BurntSushi/toml github.com/spf13/cobra/doc github.com/PuerkitoBio/purell github.com/mitchellh/mapstructure github.com/shurcooL/sanitized_anchor_name github.com/russross/blackfriday github.com/spf13/fsync github.com/spf13/hugo/bufferpool github.com/hashicorp/hcl/hcl/strconv github.com/hashicorp/hcl/hcl/token github.com/magiconair/properties github.com/hashicorp/hcl/hcl/ast github.com/miekg/mmark github.com/hashicorp/hcl/hcl/scanner github.com/hashicorp/hcl/hcl/parser github.com/hashicorp/hcl/json/token github.com/hashicorp/hcl/json/scanner github.com/hashicorp/hcl/json/parser github.com/pelletier/go-buffruneio github.com/spf13/jwalterweatherman github.com/pelletier/go-toml github.com/hashicorp/hcl gopkg.in/yaml.v2 github.com/bep/gitmap github.com/bep/inflect github.com/dchest/cssmin github.com/nicksnyder/go-i18n/i18n/language github.com/nicksnyder/go-i18n/i18n/translation github.com/eknkc/amber/parser github.com/eknkc/amber github.com/yosssi/ace github.com/spf13/viper github.com/nicksnyder/go-i18n/i18n/bundle github.com/spf13/hugo/parser github.com/spf13/nitro github.com/gorilla/websocket github.com/spf13/hugo/utils github.com/spf13/hugo/watcher github.com/spf13/hugo/hugofs github.com/spf13/hugo/helpers github.com/spf13/hugo/livereload github.com/spf13/hugo/target github.com/spf13/hugo/source github.com/spf13/hugo/tpl github.com/spf13/hugo/transform github.com/spf13/hugo/hugolib github.com/spf13/hugo/create github.com/spf13/hugo/commands github.com/spf13/hugo
これで hugo
コマンドはすでに使えるようになっています。GOPATH
は設定してありますからね。
記事を作成する
hugo new site hogehugo
とすると、カレントディレクトリに hogehugo
というディレクトリと共に必要なものが生成されます。このディレクトリに移動して、とりあえず記事を作成してみましょう。
cd hogehugo hugo new post/hugohugo.md
記事を編集・保存してから次のコマンドを入力するとサーバが起動されて、localhost:1313
にアクセスすればページが見える、
hugo server
ハズだったのですが、見えません。サーバは起動しているのですが、何もない白いページが表示されるのでした。
先のコマンドで生成された記事のデフォルトでは、front matter の部分に draft = ture
と記述されていました。「これがアレなんかなぁ」と思ったので、下記のオプションを付けてみました。
> hugo server --buildDrafts Started building sites ... ERROR 2017/01/09 23:12:48 Failed to normalize URL string. Returning in = "/" ERROR 2017/01/09 23:12:48 Failed to normalize URL string. Returning in = "/" ============================================================= Your rendered home page is blank: /index.html is zero-length * Did you specify a theme on the command-line or in your "config.toml" file? (Current theme: "") * For more debugging information, run "hugo -v" ============================================================= ERROR 2017/01/09 23:12:48 Failed to normalize URL string. Returning in = "/" ERROR 2017/01/09 23:12:48 Failed to normalize URL string. Returning in = "404.html" ERROR 2017/01/09 23:12:48 Failed to normalize URL string. Returning in = "404.html" Built site for language en: 0 of 2 drafts rendered 0 future content 0 expired content 0 regular pages created 1 other pages created 0 non-page files copied 0 paginator pages created 0 tags created 0 categories created total in 5 ms Watching for changes in /Users/hogehugo/{data,content,layouts,static} Serving pages from memory Web Server is available at http://localhost:1313/ (bind address 127.0.0.1) Press Ctrl+C to stop
こんなエラーが出ています。調べてみると「stable の 0.18.1 を使えや」とあります。でも、go ってバージョン(git の tag)を指定してビルドできるんでしたっけ?
ちょっと古いけれども「Goパッケージ依存管理最新事情」によるとバージョン指定はできないようですね。
結局、バイナリの v0.18.1 をダウンロードすることにした(また無駄なデータ通信量が……)。unzip
して bin ディレクトリに放り込みました。.app(bunlde 形式)のような「バイナリ、これひとつあれば OK なのよ」というのは素敵ですね。
さて、エラーは出なくなったものの、あいかわらず白い画面のままです。「index.html
が無いやん?」と言われていますが、そんなの Quickstart には書いてないやん。
日本語の情報が欲しくて調べてみると、下記の記事が引っ掛ってきました。ゴリラのページは画像が大きすぎて読み込み中止に。
どうやら layouts
に single.html
というファイルが必要のようです。(またしても) Quickstart に書いてないやんけ!どうやら現在の Hugo は、方針として theme を利用させるようになっているみたいですね。
最低限必要なものとしては下記のようになります。各ファイルの内容や書き方については上記のサイトを参考にします。
content/post/hogehugo.md layouts/_default/single.html layouts/index.html (なくても何とかなる)
そして
hugo server -w --buildDrafts
やっと確認することができました。Hugo のバージョンはあまり関係なかったみたいですね。Hugo v0.19-DEV
でもエラーは出ていますけれども、ちゃんと記事は生成されていますから良しとしましょう。
次にしたこと
Jekyll で構築していた記事を Hugo にコピーしてビルドしてみました(hugo import jekyll scihate hugoblo
での移行は微妙でした)。メッセージによるとプロセス速度は 145ms らしいです。記事のインデックスやもろもろの作成はまだですけれども、とてつもなく速いですわ。
Jekyll に似ているようで似ていないというか、使っているうちにわかるようになってくるとは思いますけれども、こんがらがります。例えば、各記事ごとにディレクトリが作成されて、その中に index.html
が作られるので、今までとは URI が変わってしまいます。
あ!markdown を解釈するエンジンが違うのか、引用の中でコードを書いているような場合でうまくいってない?GFM エンジンとか選択できるのかしら?今まで作成してきた記事を移行するかどうか微妙なところです……。
Jekyll の動向を調べてみたところ、--incremental
というオプションを付けるとそれなりになるそうです(日本語で言及しているサイトがほとんどない)。試してみると、446ms となりました。これなら Jekyll のままでいいかもしれません。最終的にビルドする時に時間がかかるのは気になりませんから。
とりあえずはチュートリアルを読み続けてみます。