rst2pdfでblockdiagを有効にする

rst2pdfはreStructuredTextからPDFを生成してくれるツールです。
blockdiagディレクティブを入力ファイルに書いても、そのままでは動作しません。
rst2pdfの拡張の仕組みを使うことでblockdiagを有効にできます。
試したバージョンは、Python 2.7、rst2pdf 0.93、blockdiag 1.3.3です。

rst2pdfの拡張モジュールを用意する

rst2pdfの拡張モジュールは、install関数を持ってれば呼んでくれます。拡張モジュールはPythonのパスが通ってる場所に置きます(rst2pdfを実行するカレントディレクトリでok)

blockdiag_r2p.py
from blockdiag.utils.rst.directives import setup


def install(createpdf, options):
    setup(format='PDF')

install関数内でblockdiagディレクティブをdocutilsに登録するように書いています。
formatはPNGでも動作しますが、リサイズされないので画像が小さくなってしまいました。PDFを指定すると自動でリサイズされました。

動作確認

ja.json

rst2pdfは日本語フォントの指定が必要だったのでja.jsonを用意。

{
  "fontsAlias": {
    "stdFont": "VL-PGothic-Regular",
    "stdBold": "VL-PGothic-Regular",
    "stdItalic": "VL-PGothic-Regular",
    "stdMono": "VL-Gothic-Regular"
  }
}
test.rst
テスト
======

rst2pdfでblockdiagを使用しています。

.. blockdiag::

   {
     入力 -> 処理1 -> 出力;
     入力 -> 処理2 -> 出力;
   }
実行結果

"-e"オプションで拡張モジュールの名前を指定してrst2pdfを実行します。

$ rst2pdf -s ja.json -e blockdiag_r2p test.rst
[WARNING] image.py:468 image /home/tokibito/sandbox/rst2pdf-test/blockdiag-2ef52a9db2824b0d580c5f4f482c95849fe39267.pdf is too wide for the frame, rescaling

WARNINGログが出ますが、PDFは出力されます。