昨日の日記の続きです。
Sphinxで、PDFを生成(rst2pdf)するためには、ドキュメント・プロジェクトをquickstartで作成する毎に、conf.pyの修正、make.batの修正、ja.jsonのコピーをする必要があります。ちょっと面倒ですね。
そこで、quickstartでプロジェクトを作成するときに、デフォルトでPDFの設定を入れられるよういじってみました。
sphinx-quickstartで実行される処理はquickstart.py
Sphinxをインストールすると、以下のディレクトリにSphinxのスクリプトが置かれます。
そこで、このquickstart.pyを修正して、PDF関係の設定を入れてみました。diff -u の結果を以下に示します。
quickstart.pyの修正
$ diff -u quickstart.py.orig quickstart.py --- quickstart.py.orig 2011-05-11 21:52:18.408185800 +0900 +++ quickstart.py 2011-05-21 13:04:08.397668600 +0900 @@ -80,7 +80,7 @@ # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. -#language = None +language = 'ja' # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used:
これはPDFとは関係なく、Sphinxが生成するテキスト(「目次」とか)を日本語にしています。
@@ -284,6 +284,41 @@
#epub_tocdup = True
'''
+PDF_CONFIG = '''
+
+# -- Options for PDF output --------------------------------------------
+
+# Grouping the document tree into PDF files. List of tuples
+# (source start file, target name, title, author, options).
+#
+# If there is more than one author, separate them with \\.
+# For example: r'Guido van Rossum\\Fred L. Drake, Jr., editor'
+#
+# The options element is a dictionary that lets you override
+# this config per-document.
+# For example,
+# ('index', u'MyProject', u'My Project', u'Author Name',
+# dict(pdf_compressed = True))
+# would mean that specific document would be compressed
+# regardless of the global pdf_compressed setting.
+pdf_documents = [
+('index', u'%(project_str)s', u'My Project', u'%(author_str)s'),
+]
+
+# A comma-separated list of custom stylesheets. Example:
+pdf_stylesheets = ['sphinx','kerning','a4','ja']
+# Create a compressed PDF
+# Use True/False or 1/0
+# Example: compressed=True
+#pdf_compressed = False
+# A colon-separated list of folders to search for fonts. Example:
+pdf_font_path = ['C:\Windows\Fonts']
+# Language to be used for hyphenation support
+pdf_language = "ja"
+# Mode for literal blocks wider than the frame. Can be
+
+'''
+
INTERSPHINX_CONFIG = '''
# Example configuration for intersphinx: refer to the Python standard library.ここでは、PDF関係の設定を記述しています。日本語決め打ちです。
プロジェクト名と著者名はsphinx-quickstartで入力したテキストを引けたので、それを変数で参照して埋めています。
フォントのパスは、Windows OSの標準パスで、かつC:ドライブ決め打ちです。
@@ -347,6 +382,7 @@ \t@echo " latexpdf to make LaTeX files and run them through pdflatex" \t@echo " text to make text files" \t@echo " man to make manual pages" +\t@echo " pdf to make PDF file" \t@echo " changes to make an overview of all changed/added/deprecated items" \t@echo " linkcheck to check all external links for integrity" \t@echo " doctest to run all doctests embedded in the documentation \ @@ -433,6 +469,11 @@ \t@echo \t@echo "Build finished. The manual pages are in $(BUILDDIR)/man." +pdf: +\t$(SPHINXBUILD) -b pdf $(ALLSPHINXOPTS) $(BUILDDIR)/pdf +\t@echo +\t@echo "Build ninished. The PDF file is in $(BUILDDIR)/pdf." + changes: \t$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes \t@echo @@ -481,6 +522,7 @@ \techo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter \techo. text to make text files \techo. man to make manual pages +\techo. pdf to make PDF file \techo. changes to make an overview over all changed/added/deprecated items \techo. linkcheck to check all external links for integrity \techo. doctest to run all doctests embedded in the documentation if enabled @@ -594,6 +636,14 @@ \tgoto end ) +if "%%1" == "pdf" ( +\t%%SPHINXBUILD%% -b pdf %%ALLSPHINXOPTS%% %%BUILDDIR%%/pdf +\tif errorLevel 1 exit /b 1 +\techo. +\techo.Build finished. The PDF file is in %%BUILDDIR%%/pdf. +\tgoto end +) + if "%%1" == "changes" ( \t%%SPHINXBUILD%% -b changes %%ALLSPHINXOPTS%% %%BUILDDIR%%/changes \tif errorlevel 1 exit /b 1
これは、make.batに吐き出す内容です。
@@ -623,6 +673,28 @@
:end
'''
+JAJSONFILE = '''\
+{
+ "fontsAlias" : {
+ "stdFont": "meiryo",
+ "stdBold": "meiryob",
+ "stdItalic": "meiryo",
+ "stdBoldItalic": "meiryo",
+ "stdMono": "MS Gothic",
+ "stdMonoBold": "MS Gothic",
+ "stdMonoItalic": "MS Gothic",
+ "stdMonoBoldItalic": "MS Gothic"
+},
+"styles" : [
+ ["base", {
+ "wordWrap": "CJK"
+ }],
+ ["literal", {
+ "wordWrap": "None"
+ }]
+ ]
+}
+'''
def mkdir_p(dir):
if path.isdir(dir):これは、ja.jsonファイルの内容を定義したもので、後程これをファイルに出力するスクリプトが登場します。
@@ -775,9 +847,14 @@
'n', boolean)
print '''
+Sphinx extension for PDF output via rst2pdf:'''
+ do_prompt(d, 'pdf', 'Do you want to use the PDF builder (y/N)',
+ 'n', boolean)
+
+ print '''
Please indicate if you want to use one of the following Sphinx extensions:'''
do_prompt(d, 'ext_autodoc', 'autodoc: automatically insert docstrings '
- 'from modules (y/N)', 'n', boolean)
+ 'from modules (y/N)', d['pdf'] and 'y' or 'n', boolean)
do_prompt(d, 'ext_doctest', 'doctest: automatically test code snippets '
'in doctest blocks (y/N)', 'n', boolean)
do_prompt(d, 'ext_intersphinx', 'intersphinx: link between Sphinx '
@@ -814,6 +891,8 @@
for name in ('autodoc', 'doctest', 'intersphinx', 'todo', 'coverage',
'pngmath', 'jsmath', 'ifconfig', 'viewcode')
if d['ext_' + name])
+ if d['pdf']:
+ d['extensions'] += ", 'rst2pdf.pdfbuilder'"
d['copyright'] = time.strftime('%Y') + ', ' + d['author']
d['author_texescaped'] = unicode(d['author']).\
translate(texescape.tex_escape_map)sphinx-quickstartコマンドで、ユーザーにPDF出力を使うかどうか問い合わせる処理を追加しています。
PDF出力を使う場合、autodoc拡張を使うかどうかの問い合わせのデフォルトを'n'ではなく'y'にしています。
@@ -848,6 +927,8 @@
conf_text += EPUB_CONFIG % d
if d['ext_intersphinx']:
conf_text += INTERSPHINX_CONFIG
+ if d['pdf']:
+ conf_text += PDF_CONFIG % d
f = open(path.join(srcdir, 'conf.py'), 'w')
f.write(conf_text.encode('utf-8'))PDF出力をする場合、PDF設定文字列をconf.pyに追記するようにしています。
@@ -873,6 +954,11 @@
f.write((BATCHFILE % d).encode('utf-8'))
f.close()
+ if d['pdf']:
+ f = open(path.join(d['path'], 'ja.json'), 'w')
+ f.write((JAJSONFILE % d).encode('utf-8'))
+ f.close()
+
print
print bold('Finished: An initial directory structure has been created.')
print '''PDF出力をする場合、ja.jsonを生成するようにしています。
quickstart.pycの削除
quickstart.pyを変更したら、拡張子.pyc のファイルは削除しておきます。もしかするとpythonがタイムスタンプで自動的に削除してくれるかもしれませんが、念のため。