たまたんのぶろぐ

たまたんが時たま言いたいことを書いてます。ジャンルはめっちゃええ加減ですwソフトやプログラムのことが多いかもしれませんが。。。。。

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 今回の「MarkDownSphinx + Pandocでマニュアル化しよう!!」は、MarkDownを使える部分を最低限生かした作りとなっていますが、別々のツールをジョイントして自動化したものですので、うまく出来ない部分などもあります。Sphinxがもう少し使いやすくなれば良いんですけどねぇー。

MarkDownをSphinx + Pandocでマニュアル化しよう(その5)

4. ドキュメントの作成

ここから、実際のドキュメントを作成していきます。 今回は、ドキュメンテーションビルダーを使用してプロジェクトを作成します。

4.1 プロジェクトを作成

ドキュメント用のプロジェクトフォルダを作成します。

mkdir sample

ドキュメンテーションビルダーを実行。

cd sample 
export LC_ALL='ja_JP.UTF-8'
sphinx-quickstart 

対話式になっています。例としていかに実際に動作させたときのものを貼り付けておきます。

sphinx-quickstart
Welcome to the Sphinx 1.6.5 quickstart utility.

Please enter values for the following settings (just press Enter to
accept a default value, if one is given in brackets).

Enter the root path for documentation.
> Root path for the documentation [.]:

You have two options for placing the build directory for Sphinx output.
Either, you use a directory "_build" within the root path, or you separate
"source" and "build" directories within the root path.
> Separate source and build directories (y/n) [n]: y

Inside the root directory, two more directories will be created; "_templates"
for custom HTML templates and "_static" for custom stylesheets and other static
files. You can enter another prefix (such as ".") to replace the underscore.
> Name prefix for templates and static dir [_]:

The project name will occur in several places in the built documentation.
> Project name: ドキュメント
> Author name(s): non

Sphinx has the notion of a "version" and a "release" for the
software. Each version can have multiple releases. For example, for
Python the version is something like 2.5 or 3.0, while the release is
something like 2.5.1 or 3.0a1.  If you don't need this dual structure,
just set both to the same value.
> Project version []: 0.1
> Project release [0.1]:

If the documents are to be written in a language other than English,
you can select a language here by its language code. Sphinx will then
translate text that it generates into that language.

For a list of supported codes, see
http://sphinx-doc.org/config.html#confval-language.
> Project language [en]: ja

The file name suffix for source files. Commonly, this is either ".txt"
or ".rst".  Only files with this suffix are considered documents.
> Source file suffix [.rst]:

One document is special in that it is considered the top node of the
"contents tree", that is, it is the root of the hierarchical structure
of the documents. Normally, this is "index", but if your "index"
document is a custom template, you can also set this to another filename.
> Name of your master document (without suffix) [index]:

Sphinx can also add configuration for epub output:
> Do you want to use the epub builder (y/n) [n]: y

Please indicate if you want to use one of the following Sphinx extensions:
> autodoc: automatically insert docstrings from modules (y/n) [n]:
> doctest: automatically test code snippets in doctest blocks (y/n) [n]:
> intersphinx: link between Sphinx documentation of different projects (y/n) [n]:
> todo: write "todo" entries that can be shown or hidden on build (y/n) [n]:
> coverage: checks for documentation coverage (y/n) [n]:
> imgmath: include math, rendered as PNG or SVG images (y/n) [n]:
> mathjax: include math, rendered in the browser by MathJax (y/n) [n]: y
> ifconfig: conditional inclusion of content based on config values (y/n) [n]:
> viewcode: include links to the source code of documented Python objects (y/n) [n]:
> githubpages: create .nojekyll file to publish the document on GitHub pages (y/n) [n]:

A Makefile and a Windows command file can be generated for you so that you
only have to run e.g. `make html' instead of invoking sphinx-build
directly.
> Create Makefile? (y/n) [y]:
> Create Windows command file? (y/n) [y]:

Creating file ./source/conf.py.
Creating file ./source/index.rst.
Creating file ./Makefile.
Creating file ./make.bat.

Finished: An initial directory structure has been created.

You should now populate your master file ./source/index.rst and create other documentation
source files. Use the Makefile to build the docs, like so:
   make builder
where "builder" is one of the supported builders, e.g. html, latex or linkcheck.

[vagrant@localhost sample]$

MarkDownをSphinx + Pandocでマニュアル化しよう(その4)

3.各種ツールのコンパイル・インストール

さてここから、本番です。

3.1. Python3のインストール

今回は、Python3をインストールします。 Python2とPython3では、基本的な文法が違うところが多いので、ご注意ください。

参考:Python 2.7.x と 3.x の決定的な違いを例とともに

3.1.1. ソース・コンパイルインストール

ソースからコンパイルインストールします。意外と時間がかかります。

cd /usr/src
wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz

tar zxvf Python-3.6.3.tgz
cd /usr/src/Python-3.6.3
./configure CFLAGS=-fPIC --enable-ipv6 --enable-shared --prefix=/usr/local/python
make clean
make
make install

3.1.2. 共有ライブラリとしてリンク

パッケージではないので、共有ライブラリにリンクが紐付いていないので紐付けします。

echo "/usr/local/python/lib" >> /etc/ld.so.conf
ldconfig #再読込

3.1.3. パスの設定

どの環境(ユーザー)からもPythonが使えるようにするため、シェルを仕込みます。

tee /etc/profile.d/python.sh <<-'EOF'
export PATH=/usr/local/python/bin:$PATH
EOF
source /etc/profile

3.1.4. バージョンの確認

起動できるかを確認します。

Pythonのバージョン確認

# python3 -V
> Python 3.6.3

pipのバージョン確認

# pip3 -V
> pip 9.0.1 from /usr/local/python/lib/python3.6/site-packages (python 3.6)

3.2. Pandoc

3.2.1. pandocのインストール

今回は、バイナリファイルを使用します。 ソースからコンパイルすると1時間以上かかるので、今回はバイナリをとってくるのを採用しました。 ただ、ダウンロードしてきたものを確認するために、srcディレクトリに データを一旦保存します。

cd /usr/src
wget https://github.com/jgm/pandoc/releases/download/2.0.1/pandoc-2.0.1-linux.tar.gz
tar xf pandoc-2.0.1-linux.tar.gz
cd /usr/src/pandoc-2.0.1/bin
cp -p pandoc /usr/local/bin/

3.2.2. pandocのバージョン確認

起動できるか確認のため、pandocのバージョンを表示します。

pandoc -v
> pandoc 2.0.1
> Compiled with pandoc-types 1.17.2, texmath 0.9.4.4, skylighting 0.4.2
> Default user data directory: /root/.pandoc
> Copyright (C) 2006-2017 John MacFarlane
> Web:  http://pandoc.org
> This is free software; see the source for copying conditions.
> There is no warranty, not even for merchantability or fitness
> for a particular purpose.

3.3. フォントインストール

Sphinxlatex経由のPDF作成)で使用するフォントをインストールします。

3.3.1. IPAフォントのインストール

このフォントの取り方は非公式です。通常は、ブラウザからダウンロードしてそれをほりこんでください。

cd /usr/src
wget -O IPAfont00303.zip http://ipafont.ipa.go.jp/old/ipafont/IPAfont00303.php
unzip IPAfont00303.zip
mkdir /usr/share/fonts/japanese
mkdir /usr/share/fonts/japanese/TrueType
cp IPAfont00303/*.ttf /usr/share/fonts/japanese/TrueType/
cd /usr/src
wget -O IPAexfont00201.zip http://ymu.dl.osdn.jp/ipafonts/57330/IPAexfont00201.zip
unzip IPAexfont00201.zip
cp IPAexfont00201/*.ttf /usr/share/fonts/japanese/TrueType/

設定を更新

fc-cache -fv

3.3.2. VLGothicフォント

yum -y install vlgothic-fonts
yum -y install vlgothic-p-fonts

3.4. TeX Live

Pdf作成用にTeX Liveを入れます。

※ 注意:ダウンロードやコンパイルを大量にするのでかなり時間がかかります。 2時間近くかかるかもしれないので、覚悟しておいてください。

3.4.1. インストール

3.4.1.1. インストーラーツールの取得

cd /usr/src/
wget http://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz

3.4.1.2. インストーラーの実行

解凍する。

tar xzvf install-tl-unx.tar.gz

解凍されたディレクトリにはいり、インストーラーを実行 解凍後のディレクトリ名は、更新がよくあるので変わります。確認してください。

cd /usr/src/install-tl-20171209/
sudo ./install-tl
Loading ftp://ftp.u-aizu.ac.jp/pub/tex/CTAN/systems/texlive/tlnet/tlpkg/texlive.tlpdb
Installing TeX Live 2017 from: ftp://ftp.u-aizu.ac.jp/pub/tex/CTAN/systems/texlive/tlnet (verified)
Platform: x86_64-linux => 'GNU/Linux on x86_64'
Distribution: net  (downloading)
Using URL: ftp://ftp.u-aizu.ac.jp/pub/tex/CTAN/systems/texlive/tlnet
Directory for temporary files: /tmp/3AV3Zold56
======================> TeX Live installation procedure <=====================

======>   Letters/digits in <angle brackets> indicate   <=======
======>   menu items for actions or customizations      <=======

 Detected platform: GNU/Linux on x86_64

 <B> set binary platforms: 1 out of 19

 <S> set installation scheme: scheme-full

 <C> set installation collections:
     40 collections out of 41, disk space required: 5149 MB

 <D> set directories:
   TEXDIR (the main TeX directory):
     /usr/local/texlive/2017
   TEXMFLOCAL (directory for site-wide local files):
     /usr/local/texlive/texmf-local
   TEXMFSYSVAR (directory for variable and automatically generated data):
     /usr/local/texlive/2017/texmf-var
   TEXMFSYSCONFIG (directory for local config):
     /usr/local/texlive/2017/texmf-config
   TEXMFVAR (personal directory for variable and automatically generated data):
     ~/.texlive2017/texmf-var
   TEXMFCONFIG (personal directory for local config):
     ~/.texlive2017/texmf-config
   TEXMFHOME (directory for user-specific files):
     ~/texmf

 <O> options:
   [ ] use letter size instead of A4 by default
   [X] allow execution of restricted list of programs via \write18
   [X] create all format files
   [X] install macro/font doc tree
   [X] install macro/font source tree
   [ ] create symlinks to standard directories

 <V> set up for portable installation

Actions:
 <I> start installation to hard disk
 <P> save installation profile to 'texlive.profile' and exit
 <H> help
 <Q> quit

Enter command: i ← iを入力
Installing to: /usr/local/texlive/2017

3.4.2. パスを全体に通す

どの環境(ユーザー)からもTeX Liveが使えるようにするため、シェルを仕込みます。

tee /etc/profile.d/texlive.sh <<-'EOF'
export PATH="/usr/local/texlive/2017/bin/x86_64-linux:$PATH"
export MANPATH="/usr/local/texlive/2017/texmf-dist/doc/man:$MANPATH"
export INFOPATH="/usr/local/texlive/2017/texmf-dist/doc/info:$INFOPATH"
EOF
source /etc/profile

更新

TeX Liveのツールを念のため更新する

tlmgr update --self
tlmgr update --all

3.5. Sphinx

3.5.1. Sphinxのインストール

ようやくSphinxのインストールにはいります。

pip3 install sphinx

3.5.1. sphinx_rtd_themeのインストール

読みやすいテーマがあるのでダウンロードしておきます。

pip3 install sphinx_rtd_theme

これで、とりあえず、Sphinxを使う環境が整いました。

MarkDownをSphinx + Pandocでマニュアル化しよう(その3)

2. CentOS6の構築

2.1. パッケージの更新

とりあえず、いろいろバグが出るのが面倒なので、モジュールのアップデート

yum -y update

2.2. パッケージのインストール

2.2.1. Developer Toolsのインストール

yum -y groupinstall "Development tools"

2.2.2. Developer Toolsのインストール

コンパイラ以外で必要なパッケージをインストール

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

2.2.3. vimのインストール

本件で使いませんが、とりあえず、入れておきます。

yum -y install vim

2.3. SELinuxの設定

今回は、テスト環境と言うことでSELinuxは、無効にしておきます。

2.3.1. 設定の無効化

setenforce 0

2.3.2. 恒久的な設定の無効化

再起動しても無効化できる様にしておきます。

mv /etc/selinux/config /etc/selinux/config.old
tee /etc/selinux/config <<-'EOF'
SELINUX=disabled
SELINUXTYPE=targeted
EOF

2.4. ロケールの変更

ロケールの変更

mv /etc/sysconfig/i18n /etc/sysconfig/i18n.old
tee /etc/sysconfig/i18n <<-'EOF'
LANG="ja_JP.UTF-8"
SYSFONT="latarcyrheb-sun16"
EOF

2.4.1. 設定の反映

設定の反映

source /etc/sysconfig/i18n

2.4.2. 確認

echo $LANG
> ja_JP.UTF-8

MarkDownをSphinx + Pandocでマニュアル化しよう(その2)

1. Vagrant環境設定

Vagrantは、あらかじめインストールしておいてください。また、仮想化ソフトウェアは、VirtualBoxをインストールするとすぐ使えます。 また、「Guest Additions」のアップデートが必要なときがありますが、「vagrant-vbguest」 というプラグインを使用してでもあげてください。

1.1. vagrantfileを作成

まずディレクトリを作成し、ディレクトリに移動

mkdir shpinx
cd shpinx 

Vagrantの作成

tee Vagrantfile <<-'EOF'
# -*- mode: ruby -*-
# vi: set ft=ruby :

ENV['VAGRANT_DEFAULT_PROVIDER'] = 'virtualbox'
# ENV['VAGRANT_DEFAULT_PROVIDER'] = 'vmware_fusion' # Vmware Fusionの場合

Vagrant.configure("2") do |config|
  config.vm.box = "centos/6"
  config.vm.network "private_network", ip:"192.168.30.10"

  config.vm.provider "virtualbox" do |vb|
    vb.memory = "512"
  end
  config.vm.provider "vmware_fusion" do |vb|
    vb.memory = "512"
  end

end
EOF

1.2. vagrant起動

一般ユーザーで通常はするのが普通ですが、いろいろするのが面倒なので、 本件は、基本的にrootユーザーで、作業をします。

1.2.1. vagrant起動

vagrant起動します。

vagrant up

1.2.2. vagrantのログイン

vagrantユーザでログイン

vagrant ssh

1.2.3. スーパーユーザーにログイン

スーパーユーザーに切り替え

sudo su -