MarkDownをSphinx + Pandocでマニュアル化しよう(その6)
スクリプトの作成
スクリプトの内容は以下の通りです。 このスクリプトは、「source」ディレクトリ内の「.md」拡張子のついているMarkDownをすべて「reStructuredText(rst)」に置き換えます。そして、Sphinxの目次のに相当する「index.rst」を作成上書き作成し、「epub3」「html」「pdf」を生成します。 最後に、「source」ディレクトリ内の「reStructuredText(rst)」を全て削除します。
vim make.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
index_rst = """ドキュメント
========================================
.. toctree::
:maxdepth: 2
:caption: Contents:
"""
sourcePath='./source'
makeEpub='make epub'
makeHtml='make html'
makePdf='make latexpdf'
for searchpath, dirs, files in os.walk(sourcePath):
for file in files:
fullPath = os.path.join(searchpath, file)
rootPath, ext = os.path.splitext(fullPath)
if ext == '.md':
os.system('pandoc ' + rootPath + '.md -f markdown -t rst -o ' + rootPath + '.rst')
filePath = rootPath.replace("./source/", "")
index_rst = index_rst + "\n" + ' ' + filePath
f = open('./source/index.rst', 'w')
f.write(index_rst)
f.close()
os.system(makeEpub)
os.system(makeHtml)
os.system(makePdf)
for searchpath, dirs, files in os.walk(sourcePath):
for file in files:
fullPath = os.path.join(searchpath, file)
rootPath, ext = os.path.splitext(fullPath)
if ext == '.rst':
os.remove(rootPath + '.rst')
※ teeでしようとしたのですが、うまくはっついてくれなかったので、vimで作成したほうが良いです。 タブは、絶対に使用しないでください。
テーマの変更
デフォルトのテーマは、綺麗なのですが、読みにくいので、テーマを変更しています。
vim ./source/conf.py
86行目当たりを変更
html_theme = 'alabaster'
↓ 以下に変更
html_theme = 'sphinx_rtd_theme'
データの作成
tee source/1.test.md <<-'EOF'
# 1. テスト
## 1.1 テスト1
### 1.1.1 サンプル1
## 1.2 テスト2
### 1.2.1 サンプル2
EOF
tee source/2.test.md <<-'EOF'
# 2. テスト
## 2.1 テスト1
### 2.1.1 サンプル1
## 2.2 テスト2
### 2.2.1 サンプル2
EOF
ドキュメントを生成
ビルドします。 ドキュメントは、「epub3」「html」「pdf」を全て作成します。
python3 make.py
buildディレクトリ内に、html、epub、pdfが出来ていればOK!!
こんな感じです。
インデックスは、ファイル名順に生成されます。
おわりに
めっちゃ長い文章になりましたが、これである程度は、ハッピーになれると思いますw 今回の「MarkDownをSphinx + Pandocでマニュアル化しよう!!」は、MarkDownを使える部分を最低限生かした作りとなっていますが、別々のツールをジョイントして自動化したものですので、うまく出来ない部分などもあります。Sphinxがもう少し使いやすくなれば良いんですけどねぇー。