http://about.me/shimizukawa, @shimizukawa
[speech]
Hi everyone. Today I’ll talk about Sphinx documentation generator. Thank you for opportunity of this presentation. Before that, let me introduce myself.
My name is Takayuki Shimizukawa, I came from Japan. I joined the PyConTW last year, and it is my second time to come to Taiwan.
My activity in Sphinx, I am a Sphinx co-maintainer and a Sphinx-uses.jp chariman. And Python, I was a vice-chairman PyCon JP in last 2 years.
Expert Python Programming (translation into Japanese)
Python Professional Programming (2 chapters)
[speech]
Books.
[speech]
The Sphinx. Today, I’ll talk about documentation generator that is called “Sphinx”.
[speech]
I will tell about Sphinx by three parts. Introduction, Demonstration and Case studies.
[speech]
First, I’ll introduce “what is Sphinx” and “what is reStructuredText”. By the way, how many people already using Sphinx?
OK, please raise your hands for about “Do you know Sphinx?” Thank you.
And next, “Did you use Sphinx already?” Thanks.
[speech]
What is Sphinx?
[speech]
Sphinx is a documentation generator. Sphinx generate documentation from reStructuredText markup.
In other words, Sphinx reads reST text file and outputs html, epub, pdf, or other several formats.
Sphinx is extendable, so you can also output in a different format, or read the file in a different format.
There is markup sample:
reStructuredText_ (*reST*) is **markup** language.
:term:`role` and :term:`directive` is extendable.
.. _reStructuredText: http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html
reStructuredText (reST) is markup language. role and directive is extendable.
[speech]
And reStructuredText reST is markup language.
This is a markup sample that contains LINK, EMPHASIS, STRONG and TERM role.
Sphinx provides many useful reST directive and role.
[speech]
Sphinx provides many useful reST directive and role.
Simple and beautiful html themes. Detail information is here: Builtin themes.
[speech]
Sphinx includes simple and beautiful html themes. Each themes are called: default, sphinxdoc, scrolls, agogo, traditional, nature, haiku and pyramid.
Detail information is written at builtin themes page.
You can find 3rd-party Sphinx themes at PyPI or other sites. For example,
And presentation themes:
[speech]
There are 3rd party themes. You can find 3rd-party Sphinx themes at PyPI or other sites. For example, bizstyle, dotted, basicstrap and sphinxjp.
And few presentation themes are also exist: s6, htmlslide, impressjs.
BTW, this presentation slide was also built by Sphinx with s6 theme.
[speech]
So, I’ll also introduce Sphinx is not possible.
[speech]
Sphinx did not have GUI frontend as like as Word. But you can choose any GUI editor to edit reST plain text.
There is a way to import/export Word?
NO..!
[speech]
Sphinx did not have a way to import Word file. However, there is a experimental implementation to build Word docx file, but it is not stable.
There is a way to export to PowerPoint?
NO.....!!!
[speech]
3rd-party theme will support to make presentation slide by using Sphinx. But it is controlled by HTML, css and JavaScript. It is not easy to convert PowerPoint file format.
Are there a way to export to Excel?
What are you saying???
[speech]
In Japan, many many documentations has been made with Excel. It is not feasible I think because it is not easy to maintenance and not easy to recognize difference by changing.
I think Sphinx will never support to convert to Excel.
[speech]
(read slide.)
[speech]
Several official extensions are included in the Sphinx that are maintained by sphinx comitters.
autodoc, math, intersphinx, graphviz, todo, ... For about some extensions, I’ll do demonstration later.
Contributed extensions:
And other extensions:
[speech]
You can find several extensions contributed by users in the Sphinx Contrib repository like these:
And you can find some extensions in PyPI. For example, Some type of html themes like bizstyle or s6. and some type of builders like textile or docx.
VS | Sphinx | Office |
---|---|---|
Files | Many files/dirs | 1 monolithic file |
Format | Plain text | Binary file |
GUI | No GUI frontend | Must use GUI |
Begin | Need learning to write | Easy to use |
Restructuring | Easy | Difficult |
Versioning | With hg/git/.. | By self |
Match to | Work with program source | Others |
[speech]
Honestly, Word have powerful features as versioning or parallel editiong or some other features. But I think these features are hid to casual users.
VS | Sphinx | Wiki |
---|---|---|
Structure | 1 tree | Semi-lattice |
Root and path | 1 root and 1 path from start to end | No root and no order to read |
Cross reference | exists | exists |
Glossary | exists | exists |
Index | exists | exists |
Match to | Books, order to read is important | Dictionary |
[speech]
Sphinx is suitable for the text like a book or a paper.
The particle size of Wiki pages are small, it is suitable for the dictionary that does not depend on the order in which they read.
Install from PyPI:
$ easy_install Sphinx
Searching for Sphinx
Reading http://pypi.python.org/simple/Sphinx/
Best match: Sphinx 1.2b1
...
Finished processing dependencies for Sphinx
Sphinx and other dependency packages are installed. Sphinx 1.2b1 is current newest version.
[speech]
Install from PyPI by using easy_install.
easy_install is defacto standard package installer. There are other installation methods: pip, buildout or invoke setup.py. In this case I used easy_install.
This command installed Sphinx and other dependency packages. Sphinx 1.2b1 is current newest version.
Generate a scaffold by using sphinx-quickstart:
$ sphinx-quickstart sample
(many interactive questions)
Generated files are:
$ cd sample
$ ls -a
./ Makefile _static/ conf.py* make.bat*
../ _build/ _templates/ index.rst
Run make html to build html output:
$ make html
[speech]
sphinx-quickstart command generate Sphinx documentation scaffold. And then, you can use make html command to build html output soon.
Generated html files are in _build/html directory.
Some text line.
Second line will joined to 1st line.
* item 1
* item 2
* item 2-1
* item 2-2
* item 3
Some text line. Second line will joined to 1st line.
Tip
You need blank line before and after nested items. And nested items need 2 spaces before *.
1. item 1
2. item 2
#. item 2-1
#. item 2-2
3. item 3
Tip
#. rendering auto numbered list. but it is not human readable.
[speech]
“number plus dot” or “sharp plus dot” render numbered list.
Use code-block directive to rendering code with highlighting.
.. code-block:: ruby
class Foo
def initialize(value)
puts "value = #{value}"
end
end
class Foo
def initialize(value)
puts "value = #{value}"
end
end
Note
This directive was provided by sphinx. The same feature is provided by docutils-0.9 as code directive.
Use toctree directive to build a tree structure.
.. toctree::
:numbered:
:maxdepth: 2
spam
egg
Note
This directive was provided by sphinx.
Link between pages by using glossary directive and term role:
.. glossary::
Sphinx
Sphinx is a docmentation generator.
reStructuredText
reST is markup language to make structured document.
These glossary terms are linked from other pages
by using `term` role like :term:`Sphinx`.
These glossary terms are linked from other pages by using term role like Sphinx.
Link between pages by using doc role:
Link to another reST page with :doc:`subdir/index`.
`subdir/index` will be replaced with title of
that's document file.
Link to another reST page with Subdirectory Root Page. subdir/index will be replaced with title of that’s document file.
Use math directive to rendering numerical formula.
Pythagoras theorem is :math:`a^2 + b^2 = c^2`.
.. math:: (a + b)^2 = a^2 + 2ab + b^2
.. math::
:nowrap:
\begin{eqnarray}
y & = & ax^2 + bx + c \\
f(x) & = & x^2 + 2xy + y^2
\end{eqnarray}
Pythagoras theorem is .
Note
This directive was provided by sphinx. Same name directive was provided by docutils-0.8 or later, but it is bit different.
Add sphinx.ext.todo extension in conf.py:
extensions = [
'sphinx.ext.todo',
]
todo_include_todos = True
Then you can use todo directive:
.. todo:: write test for this function.
Todo
write test for this function.
... and todolist directive:
.. todolist::
Todo
write test for this function.
(The original entry is located in c:\project\sphinx-doc\pycontw2013-sphinx-introduction\index.rst, line 1095.)
Add sphinx.ext.autodoc extension in conf.py:
extensions = [
'sphinx.ext.autodoc',
]
Then you can use automodule directive:
Add sphinx.ext.intersphinx extension and intersphinx setting in conf.py:
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.intersphinx', #<- added
]
intersphinx_mapping = {
'py': ('http://docs.python.jp/2/', None),
}
Then your linking markups link remote pages:
This markup link to Python's official reference page:
:py:func:`urllib.urlopen` on the :mod:`urllib` page.
This markup link to Python’s official reference page: urllib.urlopen() on the urllib page.
[speech]
Linking to other published Sphinx document by using same syntax to link to internal pages.
A domain is a collection of markup (directives and roles) to describe and link to objects belonging together.
.. py:function:: event.register(event_id, user_name)
(description for this function here).
You can use :py:func:`event.register` to register.
(description for this function here).
You can use event.register() to register.
[speech]
What is domain? A domain is a collection of markup (reStructuredText directives and roles) to describe and link to objects belonging together.
For example. In this case, :py:func:`event.register` is py domain’s
func role that make lnik to the declaration line.
.. py:function:: event.register(event_id, user_name) is py domain’s function directive that make declaration of the function and create Sphinx’s index page entry.
Sphinx contains several domains and sphinx-contrib contains more useful domains.
Sphinx generate po files for internationalization(i18n). sphinx-intl also support your work.
$ easy_install sphinx-intl
Generate pot files and create specified po files.
$ make gettext
$ sphinx-intl update -p _build/locale -l zh_TW
And next, you need translate po files in locale/ sub dirs. When you did that, set language=’zh_TW’ in the conf.py to use translated po files.
$ sphinx-intl build
$ make html
Ok, you can get the translated HTML.
[speech]
Sphinx generate pot files for internationalization(i18n). sphinx-intl also support your work that is 3rd-party tool then you need install this by using easy_install.
Generate po files can be done with make getext command. And next, you need translate po files in locale/ sub dirs. When you did that, set language=’zh_TW’ in the conf.py to use translated po files.
sphinx-intl build compile po files into mo files. Finally you can get the translated html by make html.
Blockdiag extensions is 3rd party extension for sphinx. Install sphinxcontrib-blockdiag extension:
$ easy_install Pillow
$ easy_install sphinxcontrib-blockdiag
$ easy_install sphinxcontrib-seqdiag
$ easy_install sphinxcontrib-actdiag
$ easy_install sphinxcontrib-nwdiag
Note
Pillow is successor of PIL (Python Imaging Library) that support Python3 and 64bit binary distributions.
Add sphinxcontrib.blockdiag extension in conf.py:
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.intersphinx',
'sphinxcontrib.blockdiag', #<- added
]
Then you can use blockdiag directive:
.. blockdiag::
{
A [label="ME"];
A -> B [label="Open"];
A -> C;
O -> P -> C;
}
Add sphinxcontrib.seqdiag extension in conf.py:
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.intersphinx',
'sphinxcontrib.blockdiag',
'sphinxcontrib.seqdiag', #<- added
]
Then you can use seqdiag directive:
.. seqdiag::
{
A => B;
A -> B;
A <-- B;
A => C => D;
}
[speech]
At last, I’ll introduce few case studies.
[speech]
Of course, Sphinx official reference is created by using Sphinx.
Using sphinx.ext.autodoc.
[speech]
For instance, Sphinx API part for developers is describing reference of functions and classes that was generated by autodoc.
Using Internationalization and Transifex.
sphinx-intl also support your work.
[speech]
And internationalization. This Japanese translated reference page in the sphinx-users.jp was generated by Sphinx with Japanese translation catalog that was imported from Transifex.
Transifex will help you to translate in a team. And sphinx-intl also support your work.
[speech]
Tinkerer is a blog tool made by using the Sphinx. You can be written your blog in reStructuredText format. In addition, you can use the rich Sphinx extensions.
Create a new blog:
$ mkdir blog
$ cd blog
$ tinker -s
And setup blog title or else:
$ vi conf.py
And create a blog entry file and write in reST:
$ tinker -p "first post"
$ vi 2013/05/25/first_post.rst
Finally, build command generates HTML and RSS:
$ tinker -b
[speech]
Tinkerer provides original commands to manage your blog; create a new blog, create a blog entry, build html.
(read slide)
Tinkerer make it easier to write a blog.
[speech]
I’d like to introduce PyCon APAC 2013 in this autumn and Sphinx users community in Japan.
[speech]
We will hold 3-days Python event at September 14, 15, 16 in Tokyo Japan.
Registration of this event will start at middle of June.
[speech]
Sphinx users community group in Japan that was called “spinx users jp”.
The group manage original site that contains full-translated reference, original tutorials and reverse dictionary.
Also we holding monthly event “Sphinx & translation hack-a-thon” and annual event named “SphinxCon JP”
The first Sphinx event in the world!
[speech]
SphinxCon JP at last year was very exciting. I think it is the first Sphinx event in the world! About 70 people were gathered on this event. We would like to do something in PyCon APAC of this year too. Please join to PyCon APAC!