Skip to content

Instantly share code, notes, and snippets.

@hekyou
Created September 16, 2012 01:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hekyou/3730720 to your computer and use it in GitHub Desktop.
Save hekyou/3730720 to your computer and use it in GitHub Desktop.

Docutilsについて

DocutilsでHTMLを生成してみよう

  • 2012.09.16
    • PyCon JP 2012
    • SphinxCon JP 2012

おまえ誰よ

name

Keita Uchiyama (内山 景太)

twitter

@hekyou

blog

http://d.hatena.ne.jp/hekyou/

join

#bookathon #expertpython

work

組込 -> Web/アプリ -> インフラ

  • Products
    • blockdiag pukiwiki plugin
    • Interactive shell live!! for blockdiag
    • docutils check tool

Docutilsって何?

  • Python製のテキスト処理ツール群

何ができるの?

  • reStructuredTextから他の形式に変換

image

今日扱う範囲

  • reStructuredText -> HTML

image

試してみる -インストール--------------------------

$ pip install docutils
$ pip install Pygments

or

$ pip install Sphinx
  • Sphinxインストール時にDocutilsもインストールされる

試してみる -reST作成----------------------

$ vim foo.rst
Example
-------

* List1

  * SubList1
  * SubList2

* List2

試してみる -実行------------------

$ rst2html.py foo.rst > foo.html

試してみる -確認------------------

image

ここまでのまとめ

DocutilsでreStructuredTextからHTMLを生成するには

rst2html.py

を使うことで簡単にできる

第二部

Docutilsの中を見てみよう

第一部のおさらい

image

Docutilsの構造

image

Input

image

  • 入力処理を請け負う
  • 標準ではFileInputが使用される

Reader

image

  • 読み込み処理を請け負う
  • 読み込みたいデータと不要なデータを区別するように作ることが可能

Parser

image

  • 指定フォーマットの解析処理を請け負う
  • docutils独自のXMLに変換している
  • 標準ではreSTの解析しか出来ないが他のフォーマットも追加することが可能

Transform

image

  • 変換処理を請け負う
  • 独自XMLに変換処理を加えたい場合に使用する

Writer

image

  • 指定フォーマットへの変換処理を請け負う
  • 標準で用意されていないフォーマットはWriterを追加することで可能になる

Output

image

  • 書き込み処理を請け負う

Publisher

image

  • それぞれの処理を実行する
  • プログラム中からdocutilsを使うための関数がいくつか用意されている

publish関数

  • プログラム中からDocutilsの機能を使う場合
    • publish系の関数を使用する

publish type

  • publish_cmdline
  • publish_file
  • publish_string
  • publish_parts
  • publish_doctree
  • publish_from_doctree
  • ...

デモ

docutils check tool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment