開発の中でテンプレートを使うことは当たり前になっています。そうすることでデザインとコードとを分離したり、繰り返しの処理などを簡略化して書けるようになります。そんな便利なテンプレートをプログラミングでしか使えないのは勿体ないでしょう。 そこで使ってみたいのがpangaeaです。Go製のテンプレートエンジンで、普段のテキスト文書を対象として処理してくれるソフトウェアです。

pangaeaの使い方

pangaeaはJavaScriptが埋め込めるのが特徴となっています。以下は例文です。

This is just a normal text file that has pangaea scripts embedded, e.g. JavaScript:

<pangaea type="text/javascript">
  function name(){
    return "Pangaea";
  }
</pangaea>

We can call the scripts to get the name, which is < %= name() %>.

Other JavaScript also works, so we know that 5 + 10 is < %= 5 + 10 %>.

Ruby風に < %= %> という記号を使って関数を呼び出したり、計算処理ができるようになっています。後はpangaeaコマンドに文字列を渡すと、変換された結果を返してくれます。

$ cat test.txt | ./pangaea 
This is just a normal text file that has pangaea scripts embedded, e.g. JavaScript:


We can call the scripts to get the name, which is Pangaea.

Other JavaScript also works, so we know that 5 + 10 is 15.

他にも -params という引数を使ってコマンドを呼び出す際に変数を渡すこともできるようになっています。同じようなテキストで名前だけを変えたり、プログラマブルにドキュメントを作るのに使えそうです。

pangaeaはGo製のオープンソース・ソフトウェア(MIT License)です。

matryer/pangaea - Go