Today I Learnedです。
Sphinx拡張1開発をしていて、ユーザがconf.py中の変数で設定できるようにしたいと思いました。
目次
設定できるSphinx拡張を観察:sphinx-revealjs
やり方をパクるために、お世話になっている2sphinx-revealjsをのぞきます。
https://sphinx-revealjs.readthedocs.io/en/latest/configurations/
revealjs_static_pathなどがあります。
conf.pyにて3
revealjs_static_path = ["_static"]
conf.pyで変数に代入することで設定します。
実現方法は
https://github.com/attakei/sphinx-revealjs/blob/v3.0.2/sphinx_revealjs/__init__.py#L124
app.add_config_value("revealjs_static_path", [], True)
Sphinxの設定値(現時点の理解)
設定値の宣言
Sphinxアプリケーションのadd_config_value()メソッド
https://www.sphinx-doc.org/ja/master/extdev/appapi.html#sphinx.application.Sphinx.add_config_value
sphinx-revealjsの例だと
revealjs_static_pathという名前の設定値- デフォルト値は
[](空のリスト) rebuild引数Trueは"env"の意味と思われます設定を変更してからビルドをかけると、環境全体が再ビルドされます。
設定値へのアクセス
sphinx-revealjsを見たところ、Builderの持つconfig属性でアクセスしていました。
- Builderは
self.config.revealjs_static_path builder.config.revealjs_static_pathで参照
Builderは出力をビルドしますが、出力フォーマット変換はTranslatorに委譲します4。
TranslatorはBuilder(への参照)を持つので、Translatorからはself.builder.config.XXXのようになりました。