LoginSignup
0
0

More than 3 years have passed since last update.

ドキュメント生成の前処理でテキストを任意変換するSphinx拡張

Posted at

前提

最近、Sphinxで文書作成しています。

ということで一旦解決したのだが、PlantUMLを使う際にVSCode上でプレビューできないのがストレスになってきた。

やりたいこと

make htmlする時に、

```plantuml

の囲みを

.. uml::

に自動変換したい。

方針

公式ドキュメント内に、イベント実装方法あり。

拡張プラグイン実装

こんな感じで書いてみた。

uml_change.py
def uml_change(app, docname, source):  
    umlFlg = False
    lines = []
    for line in source[0].split("\n"):
        if umlFlg:
            if line.strip() == "```":
                # 囲みの終端が来たら削除する
                line = ""
                umlFlg = False
            else:
                # 囲みの中はインデントする
                line = "    " + line

        ### markdown表記をreST表記に変換
        if line.strip() == "```plantuml":
            line = ".. uml::"
            umlFlg = True

        lines.append(line)

    source[0] = "\n".join(lines)

def setup(app):  
    app.connect("source-read", uml_change)  

conf.py設定

conf.py に以下の記載を追加する。

conf.py
import sys
import os
sys.path.append(os.path.abspath("./bin")) #フォルダ名は一例です

上記スクリプトを指定したフォルダに格納する(上記例で言えばbin配下)。

extensionsに作成したプラグイン名を追加。

conf.py
extensions = ['uml_change']

make html やってみて、uml画像が表示されていたら成功。

補足

この方法の応用で色々任意の前処理が書けるはず。markdownのリンク形式をrestructuredTextの書式に変換する、等。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0