diff --git a/.hgignore b/.hgignore
index 813a703f9..b3df24f47 100644
--- a/.hgignore
+++ b/.hgignore
@@ -1,6 +1,10 @@
.*\.pyc
.*\.egg
+.*\.so
build/
dist/
+tests/.coverage
+sphinx/pycode/Grammar.*pickle
Sphinx.egg-info/
doc/_build/
+TAGS
diff --git a/AUTHORS b/AUTHORS
index e5a737733..73ec5ed23 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -1,8 +1,31 @@
-The doctools are written and maintained by Georg Brandl .
-Substantial parts of the templates and the web application were written by
-Armin Ronacher .
+Sphinx is written and maintained by Georg Brandl .
-Other contributors are noted in the :copyright: fields within the docstrings
-of the respective files.
+Substantial parts of the templates were written by Armin Ronacher
+.
+
+Other contributors, listed alphabetically, are:
+
+* Daniel Bültmann -- todo extension
+* Michael Droettboom -- inheritance_diagram extension
+* Charles Duffy -- original graphviz extension
+* Josip Dzolonga -- coverage builder
+* Horst Gutmann -- internationalization support
+* Martin Hans -- autodoc improvements
+* Dave Kuhlman -- original LaTeX writer
+* Thomas Lamb -- linkcheck builder
+* Will Maier -- directory HTML builder
+* Benjamin Peterson -- unittests
+* Stefan Seefeld -- toctree improvements
+* Antonio Valentino -- qthelp builder
+* Pauli Virtanen -- autodoc improvements
+* Sebastian Wiesner -- image handling, distutils support
Many thanks for all contributions!
+
+There are also a few modules or functions incorporated from other
+authors and projects:
+
+* sphinx.util.jsdump uses the basestring encoding from simplejson,
+ written by Bob Ippolito, released under the MIT license
+* sphinx.util.stemmer was written by Vivake Gupta, placed in the
+ Public Domain
diff --git a/CHANGES b/CHANGES
index e1fd96a2b..cd0c82fb5 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,9 +1,292 @@
-Release 0.5 (in development)
+Release 0.6 (in development)
============================
New features added
------------------
+* Incompatible changes:
+
+ - Templating now requires the Jinja2 library, which is an enhanced
+ version of the old Jinja1 engine. Since the syntax and semantic
+ is largely the same, very few fixes should be necessary in
+ custom templates.
+
+ - The "document" div tag has been moved out of the ``layout.html``
+ template's "document" block, because the closing tag was already
+ outside. If you overwrite this block, you need to remove your
+ "document" div tag as well.
+
+ - The ``autodoc_skip_member`` event now also gets to decide
+ whether to skip members whose name starts with underscores.
+ Previously, these members were always automatically skipped.
+ Therefore, if you handle this event, add something like this
+ to your event handler to restore the old behavior::
+
+ if name.startswith('_'):
+ return True
+
+* Theming support, see the new section in the documentation.
+
+* Markup:
+
+ - Due to popular demand, added a ``:doc:`` role which directly
+ links to another document without the need of creating a
+ label to which a ``:ref:`` could link to.
+
+ - #4: Added a ``:download:`` role that marks a non-document file
+ for inclusion into the HTML output and links to it.
+
+ - Added an ``only`` directive that can selectively include text
+ based on enabled "tags". Tags can be given on the command
+ line. Also, the current builder output format (e.g. "html" or
+ "latex") is always a defined tag.
+
+ - #10: Added HTML section numbers, enabled by giving a
+ ``:numbered:`` flag to the ``toctree`` directive.
+
+ - #114: Added an ``abbr`` role to markup abbreviations and
+ acronyms.
+
+ - The ``literalinclude`` directive now supports several more
+ options, to include only parts of a file.
+
+ - The ``toctree`` directive now supports a ``:hidden:`` flag,
+ which will prevent links from being generated in place of
+ the directive -- this allows you to define your document
+ structure, but place the links yourself.
+
+ - Paths to images, literal include files and download files
+ can now be absolute (like ``/images/foo.png``). They are
+ treated as relative to the top source directory.
+
+ - #52: There is now a ``hlist`` directive, creating a compact
+ list by placing distributing items into multiple columns.
+
+ - #77: If a description environment with info field list only
+ contains one ``:param:`` entry, no bullet list is generated.
+
+ - #6: Don't generate redundant ``
`` for top-level TOC tree
+ items, which leads to a visual separation of TOC entries.
+
+ - #23: Added a ``classmethod`` directive along with ``method``
+ and ``staticmethod``.
+
+ - Scaled images now get a link to the unscaled version.
+
+ - SVG images are now supported in HTML (via ``
It was originally created to translate the
- new Python documentation, but has now been cleaned up in the hope that
- it will be useful to many other projects. (Of course, this site is also
- created from reStructuredText sources using Sphinx!)
+ new Python documentation, and it has excellent support for the documentation
+ of Python projects, but other documents can be written with it too. Of course,
+ this site is also created from reStructuredText sources using Sphinx!
- Although it is still under constant development, the following features are
+ It is still under constant development, and the following features are
already present, work fine and can be seen “in action” in the
Python docs:
@@ -76,5 +81,5 @@
The code can be found in a Mercurial repository, at
http://bitbucket.org/birkenfeld/sphinx/.
-{{ super() }}
{% endblock %}
-
-{# put the sidebar before the body #}
-{% block sidebar1 %}{{ sidebar() }}{% endblock %}
-{% block sidebar2 %}{% endblock %}
diff --git a/doc/builders.rst b/doc/builders.rst
index 21780806f..bee0094c6 100644
--- a/doc/builders.rst
+++ b/doc/builders.rst
@@ -3,7 +3,7 @@
Available builders
==================
-.. module:: sphinx.builder
+.. module:: sphinx.builders
:synopsis: Available built-in builder classes.
These are the built-in Sphinx builders. More builders can be added by
@@ -13,6 +13,7 @@ The builder's "name" must be given to the **-b** command-line option of
:program:`sphinx-build` to select a builder.
+.. module:: sphinx.builders.html
.. class:: StandaloneHTMLBuilder
This is the standard HTML builder. Its output is a directory with HTML
@@ -22,13 +23,106 @@ The builder's "name" must be given to the **-b** command-line option of
Its name is ``html``.
+.. class:: DirectoryHTMLBuilder
+
+ This is a subclass of the standard HTML builder. Its output is a directory
+ with HTML files, where each file is called ``index.html`` and placed in a
+ subdirectory named like its page name. For example, the document
+ ``markup/rest.rst`` will not result in an output file ``markup/rest.html``,
+ but ``markup/rest/index.html``. When generating links between pages, the
+ ``index.html`` is omitted, so that the URL would look like ``markup/rest/``.
+
+ Its name is ``dirhtml``.
+
+ .. versionadded:: 0.6
+
.. class:: HTMLHelpBuilder
This builder produces the same output as the standalone HTML builder, but
also generates HTML Help support files that allow the Microsoft HTML Help
Workshop to compile them into a CHM file.
- Its name is ``htmlhelp``.
+ Its name is ``htmlhelp``.
+
+.. module:: sphinx.builders.latex
+.. class:: LaTeXBuilder
+
+ This builder produces a bunch of LaTeX files in the output directory. You
+ have to specify which documents are to be included in which LaTeX files via
+ the :confval:`latex_documents` configuration value. There are a few
+ configuration values that customize the output of this builder, see the
+ chapter :ref:`latex-options` for details.
+
+ .. note::
+
+ The produced LaTeX file uses several LaTeX packages that may not be
+ present in a "minimal" TeX distribution installation. For TeXLive,
+ the following packages need to be installed:
+
+ * latex-recommended
+ * latex-extra
+ * fonts-recommended
+
+ Its name is ``latex``.
+
+.. module:: sphinx.builders.text
+.. class:: TextBuilder
+
+ This builder produces a text file for each reST file -- this is almost the
+ same as the reST source, but with much of the markup stripped for better
+ readability.
+
+ Its name is ``text``.
+
+ .. versionadded:: 0.4
+
+.. currentmodule:: sphinx.builders.html
+.. class:: SerializingHTMLBuilder
+
+ This builder uses a module that implements the Python serialization API
+ (`pickle`, `simplejson`, `phpserialize`, and others) to dump the generated
+ HTML documentation. The pickle builder is a subclass of it.
+
+ A concreate subclass of this builder serializing to the `PHP serialization`_
+ format could look like this::
+
+ import phpserialize
+
+ class PHPSerializedBuilder(SerializingHTMLBuilder):
+ name = 'phpserialized'
+ implementation = phpserialize
+ out_suffix = '.file.phpdump'
+ globalcontext_filename = 'globalcontext.phpdump'
+ searchindex_filename = 'searchindex.phpdump'
+
+ .. _PHP serialization: http://pypi.python.org/pypi/phpserialize
+
+ .. attribute:: implementation
+
+ A module that implements `dump()`, `load()`, `dumps()` and `loads()`
+ functions that conform to the functions with the same names from the
+ pickle module. Known modules implementing this interface are
+ `simplejson` (or `json` in Python 2.6), `phpserialize`, `plistlib`,
+ and others.
+
+ .. attribute:: out_suffix
+
+ The suffix for all regular files.
+
+ .. attribute:: globalcontext_filename
+
+ The filename for the file that contains the "global context". This
+ is a dict with some general configuration values such as the name
+ of the project.
+
+ .. attribute:: searchindex_filename
+
+ The filename for the search index Sphinx generates.
+
+
+ See :ref:`serialization-details` for details about the output format.
+
+ .. versionadded:: 0.5
.. class:: PickleHTMLBuilder
@@ -58,73 +152,7 @@ The builder's "name" must be given to the **-b** command-line option of
.. versionadded:: 0.5
-.. class:: LaTeXBuilder
-
- This builder produces a bunch of LaTeX files in the output directory. You
- have to specify which documents are to be included in which LaTeX files via
- the :confval:`latex_documents` configuration value. There are a few
- configuration values that customize the output of this builder, see the
- chapter :ref:`latex-options` for details.
-
- Its name is ``latex``.
-
-.. class:: TextBuilder
-
- This builder produces a text file for each reST file -- this is almost the
- same as the reST source, but with much of the markup stripped for better
- readability.
-
- Its name is ``text``.
-
- .. versionadded:: 0.4
-
-.. class:: SerializingHTMLBuilder
-
- This builder uses a module that implements the Python serialization API
- (`pickle`, `simplejson`, `phpserialize`, and others) to dump the generated
- HTML documentation. The pickle builder is a subclass of it.
-
- A concreate subclass of this builder serializing to the `PHP serialization`_
- format could look like this::
-
- import phpserialize
-
- class PHPSerializedBuilder(SerializingHTMLBuilder):
- name = 'phpserialized'
- implementation = phpserialize
- out_suffix = '.file.phpdump'
- globalcontext_filename = 'globalcontext.phpdump'
- searchindex_filename = 'searchindex.phpdump'
-
- .. _PHP serialization: http://pypi.python.org/pypi/phpserialize
-
- .. attribute:: implementation
-
- A module that implements `dump()`, `load()`, `dumps()` and `loads()`
- functions that conform to the functions with the same names from the
- pickle module. Known modules implementing this interface are
- `simplejson` (or `json` in Python 2.6), `phpserialize`, `plistlib`,
- and others.
-
- .. attribute:: out_suffix
-
- The suffix for all regular files.
-
- .. attribute:: globalcontext_filename
-
- The filename for the file that contains the "global context". This
- is a dict with some general configuration values such as the name
- of the project.
-
- .. attribute:: searchindex_filename
-
- The filename for the search index Sphinx generates.
-
-
- See :ref:`serialization-details` for details about the output format.
-
- .. versionadded:: 0.5
-
+.. module:: sphinx.builders.changes
.. class:: ChangesBuilder
This builder produces an HTML overview of all :dir:`versionadded`,
@@ -134,6 +162,7 @@ The builder's "name" must be given to the **-b** command-line option of
Its name is ``changes``.
+.. module:: sphinx.builders.linkcheck
.. class:: CheckExternalLinksBuilder
This builder scans all documents for external links, tries to open them with
diff --git a/doc/concepts.rst b/doc/concepts.rst
index ca7aaf7c4..e6d5fa026 100644
--- a/doc/concepts.rst
+++ b/doc/concepts.rst
@@ -16,6 +16,9 @@ directory`, the extension is stripped, and path separators are converted to
slashes. All values, parameters and suchlike referring to "documents" expect
such a document name.
+Examples for document names are ``index``, ``library/zipfile``, or
+``reference/datamodel/types``. Note that there is no leading slash.
+
The TOC tree
------------
@@ -55,22 +58,37 @@ tables of contents. The ``toctree`` directive is the central element.
``strings`` and so forth, and it knows that they are children of the shown
document, the library index. From this information it generates "next
chapter", "previous chapter" and "parent chapter" links.
-
+
Document titles in the :dir:`toctree` will be automatically read from the
- title of the referenced document. If that isn't what you want, you can give
- the specify an explicit title and target using a similar syntax to reST
+ title of the referenced document. If that isn't what you want, you can
+ specify an explicit title and target using a similar syntax to reST
hyperlinks (and Sphinx's :ref:`cross-referencing syntax `). This
looks like::
-
+
.. toctree::
-
+
intro
All about strings
datatypes
-
+
The second line above will link to the ``strings`` document, but will use the
title "All about strings" instead of the title of the ``strings`` document.
+ You can also add external links, by giving an HTTP URL instead of a document
+ name.
+
+ If you want to have section numbers even in HTML output, give the toctree a
+ ``numbered`` flag option. For example::
+
+ .. toctree::
+ :numbered:
+
+ foo
+ bar
+
+ Numbering then starts at the heading of ``foo``. Sub-toctrees are
+ automatically numbered (don't give the ``numbered`` flag to those).
+
You can use "globbing" in toctree directives, by giving the ``glob`` flag
option. All entries are then matched against the list of available
documents, and matches are inserted into the list alphabetically. Example::
@@ -85,7 +103,24 @@ tables of contents. The ``toctree`` directive is the central element.
This includes first all documents whose names start with ``intro``, then all
documents in the ``recipe`` folder, then all remaining documents (except the
one containing the directive, of course.) [#]_
-
+
+ The special entry name ``self`` stands for the document containing the
+ toctree directive. This is useful if you want to generate a "sitemap" from
+ the toctree.
+
+ You can also give a "hidden" option to the directive, like this::
+
+ .. toctree::
+ :hidden:
+
+ doc_1
+ doc_2
+
+ This will still notify Sphinx of the document hierarchy, but not insert links
+ into the document at the location of the directive -- this makes sense if you
+ intend to insert these links yourself, in a different style, or in the HTML
+ sidebar.
+
In the end, all documents in the :term:`source directory` (or subdirectories)
must occur in some ``toctree`` directive; Sphinx will emit a warning if it
finds a file that is not included, because that means that this file will not
@@ -100,6 +135,10 @@ tables of contents. The ``toctree`` directive is the central element.
.. versionchanged:: 0.3
Added "globbing" option.
+ .. versionchanged:: 0.6
+ Added "numbered" and "hidden" options as well as external links and
+ support for "self" references.
+
Special names
-------------
diff --git a/doc/conf.py b/doc/conf.py
index ca30ad91d..e1a48aa20 100644
--- a/doc/conf.py
+++ b/doc/conf.py
@@ -1,27 +1,12 @@
# -*- coding: utf-8 -*-
#
-# Sphinx documentation build configuration file, created by
-# sphinx-quickstart.py on Sat Mar 8 21:47:50 2008.
-#
-# This file is execfile()d with the current directory set to its containing dir.
-#
-# The contents of this file are pickled, so don't put values in the namespace
-# that aren't pickleable (module imports are okay, they're removed automatically).
-#
-# All configuration values have a default value; values that are commented out
-# serve to show the default value.
+# Sphinx documentation build configuration file
import sys, os, re
-# If your extensions are in another directory, add it here.
-#sys.path.append(os.path.dirname(__file__))
-
-# General configuration
-# ---------------------
-
# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.addons.*') or your custom ones.
-extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest']
+extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.todo']
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
@@ -34,46 +19,22 @@ master_doc = 'contents'
# General substitutions.
project = 'Sphinx'
-copyright = '2008, Georg Brandl'
+copyright = '2007-2009, Georg Brandl'
# The default replacements for |version| and |release|, also used in various
# other places throughout the built documents.
-#
-# The short X.Y version.
import sphinx
version = sphinx.__released__
-# The full version, including alpha/beta/rc tags.
release = version
-# There are two options for replacing |today|: either, you set today to some
-# non-false value, then it is used:
-#today = ''
-# Else, today_fmt is used as the format for a strftime call.
-#today_fmt = '%B %d, %Y'
-
-# List of documents that shouldn't be included in the build.
-#unused_docs = []
-
-# If true, '()' will be appended to :func: etc. cross-reference text.
-#add_function_parentheses = True
-
-# If true, the current module name will be prepended to all description
-# unit titles (such as .. function::).
-#add_module_names = True
-
+# Show author directives in the output.
show_authors = True
-# The name of the Pygments (syntax highlighting) style to use.
-pygments_style = 'friendly'
+# The HTML template theme.
+html_theme = 'sphinxdoc'
-
-# Options for HTML output
-# -----------------------
-
-# The style sheet to use for HTML and HTML Help pages. A file of that name
-# must exist either in Sphinx' static/ path, or in one of the custom paths
-# given in html_static_path.
-html_style = 'sphinxdoc.css'
+# A list of ignored prefixes names for module index sorting.
+modindex_common_prefix = ['sphinx.']
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
@@ -84,10 +45,6 @@ html_static_path = ['_static']
# using the given strftime format.
html_last_updated_fmt = '%b %d, %Y'
-# If true, SmartyPants will be used to convert quotes and dashes to
-# typographically correct entities.
-#html_use_smartypants = True
-
# Content template for the index page.
html_index = 'index.html'
@@ -98,44 +55,30 @@ html_sidebars = {'index': 'indexsidebar.html'}
# templates.
html_additional_pages = {'index': 'index.html'}
-# If true, the reST sources are included in the HTML build as _sources/.
-#html_copy_source = True
-
+# Generate an OpenSearch description with that URL as the base.
html_use_opensearch = 'http://sphinx.pocoo.org'
# Output file base name for HTML help builder.
htmlhelp_basename = 'Sphinxdoc'
-
-# Options for LaTeX output
-# ------------------------
-
-# The paper size ('letter' or 'a4').
-#latex_paper_size = 'letter'
-
-# The font size ('10pt', '11pt' or '12pt').
-#latex_font_size = '10pt'
-
# Grouping the document tree into LaTeX files. List of tuples
-# (source start file, target name, title, author, document class [howto/manual]).
+# (source start file, target name, title, author, documentclass [howto/manual]).
latex_documents = [('contents', 'sphinx.tex', 'Sphinx Documentation',
'Georg Brandl', 'manual', 1)]
+# Add our logo to the LaTeX file.
latex_logo = '_static/sphinx.png'
-#latex_use_parts = True
-
# Additional stuff for the LaTeX preamble.
latex_elements = {
'fontpkg': '\\usepackage{palatino}'
}
-# Documents to append as an appendix to all manuals.
-#latex_appendices = []
+# Put TODOs into the output.
+todo_include_todos = True
-# Extension interface
-# -------------------
+# -- Extension interface -------------------------------------------------------
from sphinx import addnodes
@@ -182,7 +125,9 @@ def parse_event(env, sig, signode):
def setup(app):
from sphinx.ext.autodoc import cut_lines
app.connect('autodoc-process-docstring', cut_lines(4, what=['module']))
- app.add_description_unit('directive', 'dir', 'pair: %s; directive', parse_directive)
+ app.add_description_unit('directive', 'dir', 'pair: %s; directive',
+ parse_directive)
app.add_description_unit('role', 'role', 'pair: %s; role', parse_role)
- app.add_description_unit('confval', 'confval', 'pair: %s; configuration value')
+ app.add_description_unit('confval', 'confval',
+ 'pair: %s; configuration value')
app.add_description_unit('event', 'event', 'pair: %s; event', parse_event)
diff --git a/doc/config.rst b/doc/config.rst
index ed5133672..54303bbb0 100644
--- a/doc/config.rst
+++ b/doc/config.rst
@@ -22,8 +22,8 @@ Important points to note:
* The term "fully-qualified name" refers to a string that names an importable
Python object inside a module; for example, the FQN
- ``"sphinx.builder.Builder"`` means the ``Builder`` class in the
- ``sphinx.builder`` module.
+ ``"sphinx.builders.Builder"`` means the ``Builder`` class in the
+ ``sphinx.builders`` module.
* Remember that document names use ``/`` as the path separator and don't contain
the file name extension.
@@ -38,6 +38,11 @@ Important points to note:
delete them from the namespace with ``del`` if appropriate. Modules are
removed automatically, so you don't need to ``del`` your imports after use.
+* There is a special object named ``tags`` available in the config file.
+ It can be used to query and change the tags (see :ref:`tags`). Use
+ ``tags.has('tag')`` to query, ``tags.add('tag')`` and ``tags.remove('tag')``
+ to change.
+
General configuration
---------------------
@@ -45,7 +50,7 @@ General configuration
.. confval:: extensions
A list of strings that are module names of Sphinx extensions. These can be
- extensions coming with Sphinx (named ``sphinx.addons.*``) or custom ones.
+ extensions coming with Sphinx (named ``sphinx.ext.*``) or custom ones.
Note that you can extend :data:`sys.path` within the conf file if your
extensions live in another directory -- but make sure you use absolute paths.
@@ -76,7 +81,7 @@ General configuration
.. versionadded:: 0.5
Previously, Sphinx accepted only UTF-8 encoded sources.
-
+
.. confval:: master_doc
The document name of the "master" document, that is, the document that
@@ -131,18 +136,35 @@ General configuration
.. confval:: templates_path
A list of paths that contain extra templates (or templates that overwrite
- builtin templates). Relative paths are taken as relative to the
- configuration directory.
+ builtin/theme-specific templates). Relative paths are taken as relative to
+ the configuration directory.
.. confval:: template_bridge
A string with the fully-qualified name of a callable (or simply a class) that
returns an instance of :class:`~sphinx.application.TemplateBridge`. This
instance is then used to render HTML documents, and possibly the output of
- other builders (currently the changes builder).
+ other builders (currently the changes builder). (Note that the template
+ bridge must be made theme-aware if HTML themes are to be used.)
+
+.. confval:: rst_epilog
+
+ .. index:: pair: global; substitutions
+
+ A string of reStructuredText that will be included at the end of every source
+ file that is read. This is the right place to add substitutions that should
+ be available in every file. An example::
+
+ rst_epilog = """
+ .. |psf| replace:: Python Software Foundation
+ """
+
+ .. versionadded:: 0.6
.. confval:: default_role
+ .. index:: default; role
+
The name of a reST role (builtin or Sphinx extension) to use as the default
role, that is, for text marked up ```like this```. This can be set to
``'obj'`` to make ```filter``` a cross-reference to the function "filter".
@@ -164,9 +186,19 @@ General configuration
.. versionadded:: 0.5
+.. confval:: modindex_common_prefix
+
+ A list of prefixes that are ignored for sorting the module index (e.g.,
+ if this is set to ``['foo.']``, then ``foo.bar`` is shown under ``B``, not
+ ``F``). This can be handy if you document a project that consists of a single
+ package. Works only for the HTML builder currently. Default is ``[]``.
+
+ .. versionadded:: 0.6
+
+
Project information
-------------------
-
+
.. confval:: project
The documented project's name.
@@ -203,8 +235,16 @@ Project information
* ``cs`` -- Czech
* ``de`` -- German
* ``en`` -- English
+ * ``es`` -- Spanish
+ * ``fi`` -- Finnish
* ``fr`` -- French
+ * ``it`` -- Italian
+ * ``nl`` -- Dutch
* ``pl`` -- Polish
+ * ``pt_BR`` -- Brazilian Portuguese
+ * ``sl`` -- Slovenian
+ * ``uk_UA`` -- Ukrainian
+ * ``zh_TW`` -- Traditional Chinese
.. confval:: today
today_fmt
@@ -227,7 +267,7 @@ Project information
:ref:`code-examples` for more details.
.. versionadded:: 0.5
-
+
.. confval:: pygments_style
The style name to use for Pygments highlighting of source code. Default is
@@ -255,6 +295,13 @@ Project information
A boolean that decides whether :dir:`moduleauthor` and :dir:`sectionauthor`
directives produce any output in the built files.
+.. confval:: trim_footnote_reference_space
+
+ Trim spaces before footnote references that are necessary for the reST parser
+ to recognize the footnote, but do not look too nice in the output.
+
+ .. versionadded:: 0.6
+
.. _html-options:
@@ -264,6 +311,37 @@ Options for HTML output
These options influence HTML as well as HTML Help output, and other builders
that use Sphinx' HTMLWriter class.
+.. confval:: html_theme
+
+ The "theme" that the HTML output should use. See the :doc:`section about
+ theming `. The default is ``'default'``.
+
+ .. versionadded:: 0.6
+
+.. confval:: html_theme_options
+
+ A dictionary of options that influence the look and feel of the selected
+ theme. These are theme-specific. For the options understood by the builtin
+ themes, see :ref:`this section `.
+
+ .. versionadded:: 0.6
+
+.. confval:: html_theme_path
+
+ A list of paths that contain custom themes, either as subdirectories or as
+ zip files. Relative paths are taken as relative to the configuration
+ directory.
+
+ .. versionadded:: 0.6
+
+.. confval:: html_style
+
+ The style sheet to use for HTML pages. A file of that name must exist either
+ in Sphinx' :file:`static/` path, or in one of the custom paths given in
+ :confval:`html_static_path`. Default is the stylesheet given by the selected
+ theme. If you only want to add or override a few things compared to the
+ theme's stylesheet, use CSS ``@import`` to import the theme's stylesheet.
+
.. confval:: html_title
The "title" for HTML documentation generated with Sphinx' own templates.
@@ -280,12 +358,6 @@ that use Sphinx' HTMLWriter class.
.. versionadded:: 0.4
-.. confval:: html_style
-
- The style sheet to use for HTML pages. A file of that name must exist either
- in Sphinx' :file:`static/` path, or in one of the custom paths given in
- :confval:`html_static_path`. Default is ``'default.css'``.
-
.. confval:: html_logo
If given, this must be the name of an image file that is the logo of the
@@ -309,8 +381,8 @@ that use Sphinx' HTMLWriter class.
A list of paths that contain custom static files (such as style sheets or
script files). Relative paths are taken as relative to the configuration
- directory. They are copied to the output directory after the builtin static
- files, so a file named :file:`default.css` will overwrite the builtin
+ directory. They are copied to the output directory after the theme's static
+ files, so a file named :file:`default.css` will overwrite the theme's
:file:`default.css`.
.. versionchanged:: 0.4
@@ -327,6 +399,15 @@ that use Sphinx' HTMLWriter class.
If true, *SmartyPants* will be used to convert quotes and dashes to
typographically correct entities. Default: ``True``.
+.. confval:: html_add_permalinks
+
+ If true, Sphinx will add "permalinks" for each heading and description
+ environment as paragraph signs that become visible when the mouse hovers over
+ them. Default: ``True``.
+
+ .. versionadded:: 0.6
+ Previously, this was always activated.
+
.. confval:: html_sidebars
Custom sidebar templates, must be a dictionary that maps document names to
@@ -388,6 +469,19 @@ that use Sphinx' HTMLWriter class.
If true, the reST sources are included in the HTML build as
:file:`_sources/{name}`. The default is ``True``.
+ .. warning::
+
+ If this config value is set to ``False``, the JavaScript search function
+ will only display the titles of matching documents, and no excerpt from
+ the matching contents.
+
+.. confval:: html_show_sourcelink
+
+ If true (and :confval:`html_copy_source` is true as well), links to the
+ reST sources will be added to the sidebar. The default is ``True``.
+
+ .. versionadded:: 0.6
+
.. confval:: html_use_opensearch
If nonempty, an `OpenSearch ` description file will be
@@ -404,10 +498,18 @@ that use Sphinx' HTMLWriter class.
.. versionadded:: 0.4
+.. confval:: html_link_suffix
+
+ Suffix for generated links to HTML files. The default is whatever
+ :confval:`html_file_suffix` is set to; it can be set differently (e.g. to
+ support different web server setups).
+
+ .. versionadded:: 0.6
+
.. confval:: html_translator_class
A string with the fully-qualified name of a HTML Translator class, that is, a
- subclass of Sphinx' :class:`~sphinx.htmlwriter.HTMLTranslator`, that is used
+ subclass of Sphinx' :class:`~sphinx.writers.html.HTMLTranslator`, that is used
to translate document trees to HTML. Default is ``None`` (use the builtin
translator).
@@ -491,7 +593,7 @@ These options influence LaTeX output.
avoid interpretation as escape sequences.
* Keys that you may want to override include:
-
+
``'papersize'``
Paper size option of the document class (``'a4paper'`` or
``'letterpaper'``), default ``'letterpaper'``.
@@ -515,9 +617,9 @@ These options influence LaTeX output.
Additional preamble content, default empty.
``'footer'```
Additional footer content (before the indices), default empty.
-
+
* Keys that don't need be overridden unless in special cases are:
-
+
``'inputenc'``
"inputenc" package inclusion, default
``'\\usepackage[utf8]{inputenc}'``.
@@ -534,9 +636,9 @@ These options influence LaTeX output.
"printindex" call, the last thing in the file, default
``'\\printindex'``. Override if you want to generate the index
differently or append some content after the index.
-
+
* Keys that are set by other options and therefore should not be overridden are:
-
+
``'docclass'``
``'classoptions'``
``'title'``
@@ -549,7 +651,20 @@ These options influence LaTeX output.
``'makemodindex'``
``'shorthandoff'``
``'printmodindex'``
-
+
+.. confval:: latex_additional_files
+
+ A list of file names, relative to the configuration directory, to copy to the
+ build directory when building LaTeX output. This is useful to copy files
+ that Sphinx doesn't copy automatically, e.g. if they are referenced in custom
+ LaTeX added in ``latex_elements``. Image files that are referenced in source
+ files (e.g. via ``.. image::``) are copied automatically.
+
+ You have to make sure yourself that the filenames don't collide with those of
+ any automatically copied files.
+
+ .. versionadded:: 0.6
+
.. confval:: latex_preamble
Additional LaTeX markup for the preamble.
diff --git a/doc/contents.rst b/doc/contents.rst
index 6ddbcbcbb..1f3860ea8 100644
--- a/doc/contents.rst
+++ b/doc/contents.rst
@@ -12,9 +12,11 @@ Sphinx documentation contents
markup/index
builders
config
+ theming
templating
extensions
-
+
+ faq
glossary
changes
examples
diff --git a/doc/ext/appapi.rst b/doc/ext/appapi.rst
index bb8ff7dd4..fba1d9457 100644
--- a/doc/ext/appapi.rst
+++ b/doc/ext/appapi.rst
@@ -13,22 +13,31 @@ the following public API:
.. method:: Sphinx.add_builder(builder)
Register a new builder. *builder* must be a class that inherits from
- :class:`~sphinx.builder.Builder`.
+ :class:`~sphinx.builders.Builder`.
-.. method:: Sphinx.add_config_value(name, default, rebuild_env)
+.. method:: Sphinx.add_config_value(name, default, rebuild)
Register a configuration value. This is necessary for Sphinx to recognize
new values and set default values accordingly. The *name* should be prefixed
with the extension name, to avoid clashes. The *default* value can be any
- Python object. The boolean value *rebuild_env* must be ``True`` if a change
- in the setting only takes effect when a document is parsed -- this means that
- the whole environment must be rebuilt.
+ Python object. The string value *rebuild* must be one of those values:
+
+ * ``'env'`` if a change in the setting only takes effect when a document is
+ parsed -- this means that the whole environment must be rebuilt.
+ * ``'html'`` if a change in the setting needs a full rebuild of HTML
+ documents.
+ * ``''`` if a change in the setting will not need any special rebuild.
.. versionchanged:: 0.4
If the *default* value is a callable, it will be called with the config
object as its argument in order to get the default value. This can be
used to implement config values whose default depends on other values.
+ .. versionchanged:: 0.6
+ Changed *rebuild* from a simple boolean (equivalent to ``''`` or
+ ``'env'``) to a string. However, booleans are still accepted and
+ converted internally.
+
.. method:: Sphinx.add_event(name)
Register an event called *name*.
@@ -45,12 +54,12 @@ the following public API:
:exc:`docutils.nodes.SkipNode`. Example::
class math(docutils.nodes.Element)
-
+
def visit_math_html(self, node):
self.body.append(self.starttag(node, 'math'))
def depart_math_html(self, node):
self.body.append('')
-
+
app.add_node(math, html=(visit_math_html, depart_math_html))
Obviously, translators for which you don't specify visitor methods will choke
@@ -59,16 +68,42 @@ the following public API:
.. versionchanged:: 0.5
Added the support for keyword arguments giving visit functions.
-.. method:: Sphinx.add_directive(name, cls, content, arguments, **options)
+.. method:: Sphinx.add_directive(name, func, content, arguments, **options)
+ Sphinx.add_directive(name, directiveclass)
Register a Docutils directive. *name* must be the prospective directive
- name, *func* the directive function for details about the signature and
- return value. *content*, *arguments* and *options* are set as attributes on
- the function and determine whether the directive has content, arguments and
- options, respectively. For their exact meaning, please consult the Docutils
- documentation.
+ name. There are two possible ways to write a directive:
- .. XXX once we target docutils 0.5, update this
+ * In the docutils 0.4 style, *func* is the directive function. *content*,
+ *arguments* and *options* are set as attributes on the function and
+ determine whether the directive has content, arguments and options,
+ respectively.
+
+ * In the docutils 0.5 style, *directiveclass* is the directive class. It
+ must already have attributes named *has_content*, *required_arguments*,
+ *optional_arguments*, *final_argument_whitespace* and *option_spec* that
+ correspond to the options for the function way. See `the Docutils docs
+ `_ for
+ details.
+
+ The directive class normally must inherit from the class
+ ``docutils.parsers.rst.Directive``. When writing a directive for usage in
+ a Sphinx extension, you inherit from ``sphinx.util.compat.Directive``
+ instead which does the right thing even on docutils 0.4 (which doesn't
+ support directive classes otherwise).
+
+ For example, the (already existing) :dir:`literalinclude` directive would be
+ added like this::
+
+ from docutils.parsers.rst import directives
+ add_directive('literalinclude', literalinclude_directive,
+ content = 0, arguments = (1, 0, 0),
+ linenos = directives.flag,
+ language = direcitves.unchanged,
+ encoding = directives.encoding)
+
+ .. versionchanged:: 0.6
+ Docutils 0.5-style directive classes are now supported.
.. method:: Sphinx.add_role(name, role)
@@ -76,6 +111,13 @@ the following public API:
source, *role* the role function (see the `Docutils documentation
`_ on details).
+.. method:: Sphinx.add_generic_role(name, nodeclass)
+
+ Register a Docutils role that does nothing but wrap its contents in the
+ node given by *nodeclass*.
+
+ .. versionadded:: 0.6
+
.. method:: Sphinx.add_description_unit(directivename, rolename, indextemplate='', parse_node=None, ref_nodeclass=None)
This method is a very convenient way to add a new type of information that
@@ -157,7 +199,32 @@ the following public API:
:confval:`the docs for the config value `.
.. versionadded:: 0.5
-
+
+.. method:: Sphinx.add_lexer(alias, lexer)
+
+ Use *lexer*, which must be an instance of a Pygments lexer class, to
+ highlight code blocks with the given language *alias*.
+
+ .. versionadded:: 0.6
+
+.. method:: Sphinx.add_autodocumenter(cls)
+
+ Add *cls* as a new documenter class for the :mod:`sphinx.ext.autodoc`
+ extension. It must be a subclass of :class:`sphinx.ext.autodoc.Documenter`.
+ This allows to auto-document new types of objects. See the source of the
+ autodoc module for examples on how to subclass :class:`Documenter`.
+
+ .. versionadded:: 0.6
+
+.. method:: Sphinx.add_autodoc_attrgetter(type, getter)
+
+ Add *getter*, which must be a function with an interface compatible to the
+ :func:`getattr` builtin, as the autodoc attribute getter for objects that are
+ instances of *type*. All cases where autodoc needs to get an attribute of a
+ type are then handled by this function instead of :func:`getattr`.
+
+ .. versionadded:: 0.6
+
.. method:: Sphinx.connect(event, callback)
Register *callback* to be called when *event* is emitted. For details on
@@ -208,14 +275,31 @@ registered event handlers.
Emitted when the builder object has been created. It is available as
``app.builder``.
+.. event:: env-purge-doc (app, env, docname)
+
+ Emitted when all traces of a source file should be cleaned from the
+ environment, that is, if the source file is removed or before it is freshly
+ read. This is for extensions that keep their own caches in attributes of the
+ environment.
+
+ For example, there is a cache of all modules on the environment. When a
+ source file has been changed, the cache's entries for the file are cleared,
+ since the module declarations could have been removed from the file.
+
+ .. versionadded:: 0.5
+
.. event:: source-read (app, docname, source)
Emitted when a source file has been read. The *source* argument is a list
whose single element is the contents of the source file. You can process the
contents and replace this item to implement source-level transformations.
+ For example, if you want to use ``$`` signs to delimit inline math, like in
+ LaTeX, you can use a regular expression to replace ``$...$`` by
+ ``:math:`...```.
+
.. versionadded:: 0.5
-
+
.. event:: doctree-read (app, doctree)
Emitted when a doctree has been parsed and read by the environment, and is
@@ -237,11 +321,15 @@ registered event handlers.
future reference and should be a child of the returned reference node.
.. versionadded:: 0.5
-
+
.. event:: doctree-resolved (app, doctree, docname)
Emitted when a doctree has been "resolved" by the environment, that is, all
- references have been resolved and TOCs have been inserted.
+ references have been resolved and TOCs have been inserted. The *doctree* can
+ be modified in place.
+
+ Here is the place to replace custom nodes that don't have visitor methods in
+ the writers, so that they don't cause errors when the writers encounter them.
.. event:: env-updated (app, env)
@@ -249,7 +337,7 @@ registered event handlers.
completed, that is, the environment and all doctrees are now up-to-date.
.. versionadded:: 0.5
-
+
.. event:: page-context (app, pagename, templatename, context, doctree)
Emitted when the HTML builder has created a context dictionary to render a
@@ -280,7 +368,7 @@ registered event handlers.
cleanup actions depending on the exception status.
.. versionadded:: 0.5
-
+
.. _template-bridge:
diff --git a/doc/ext/autodoc.rst b/doc/ext/autodoc.rst
index 993f971ae..17a2766b2 100644
--- a/doc/ext/autodoc.rst
+++ b/doc/ext/autodoc.rst
@@ -12,6 +12,13 @@
This extension can import the modules you are documenting, and pull in
documentation from docstrings in a semi-automatic way.
+.. note::
+
+ For Sphinx (actually, the Python interpreter that executes Sphinx) to find
+ your module, it must be importable. That means that the module or the
+ package must be in one of the directories on :data:`sys.path` -- adapt your
+ :data:`sys.path` in the configuration file accordingly.
+
For this to work, the docstrings must of course be written in correct
reStructuredText. You can then use all of the usual Sphinx markup in the
docstrings, and it will end up correctly in the documentation. Together with
@@ -61,7 +68,7 @@ directive.
Boil the noodle *time* minutes.
**Options and advanced usage**
-
+
* If you want to automatically document members, there's a ``members``
option::
@@ -108,9 +115,11 @@ directive.
.. versionadded:: 0.4
- * The :dir:`autoclass` and :dir:`autoexception` directives also support a
- flag option called ``show-inheritance``. When given, a list of base
- classes will be inserted just below the class signature.
+ * The :dir:`automodule`, :dir:`autoclass` and :dir:`autoexception` directives
+ also support a flag option called ``show-inheritance``. When given, a list
+ of base classes will be inserted just below the class signature (when used
+ with :dir:`automodule`, this will be inserted for every class that is
+ documented in the module).
.. versionadded:: 0.4
@@ -126,6 +135,12 @@ directive.
.. versionadded:: 0.5
+ * :dir:`automodule` and :dir:`autoclass` also has an ``member-order`` option
+ that can be used to override the global value of
+ :confval:`autodoc_member_order` for one directive.
+
+ .. versionadded:: 0.6
+
.. note::
In an :dir:`automodule` directive with the ``members`` option set, only
@@ -135,12 +150,30 @@ directive.
.. directive:: autofunction
+ autodata
automethod
autoattribute
These work exactly like :dir:`autoclass` etc., but do not offer the options
used for automatic member documentation.
+ For module data members and class attributes, documentation can either be put
+ into a special-formatted comment *before* the attribute definition, or in a
+ docstring *after* the definition. This means that in the following class
+ definition, both attributes can be autodocumented::
+
+ class Foo:
+ """Docstring for class Foo."""
+
+ #: Doc comment for attribute Foo.bar.
+ bar = 1
+
+ baz = 2
+ """Docstring for attribute Foo.baz."""
+
+ .. versionchanged:: 0.6
+ :dir:`autodata` and :dir:`autoattribute` can now extract docstrings.
+
.. note::
If you document decorated functions or methods, keep in mind that autodoc
@@ -155,19 +188,6 @@ directive.
There are also new config values that you can set:
-.. confval:: automodule_skip_lines
-
- This value (whose default is ``0``) can be used to skip an amount of lines in
- every module docstring that is processed by an :dir:`automodule` directive.
- This is provided because some projects like to put headings in the module
- docstring, which would then interfere with your sectioning, or automatic
- fields with version control tags, that you don't want to put in the generated
- documentation.
-
- .. deprecated:: 0.4
- Use the more versatile docstring processing provided by
- :event:`autodoc-process-docstring`.
-
.. confval:: autoclass_content
This value selects what content will be inserted into the main body of an
@@ -185,6 +205,14 @@ There are also new config values that you can set:
.. versionadded:: 0.3
+.. confval:: autodoc_member_order
+
+ This value selects if automatically documented members are sorted
+ alphabetical (value ``'alphabetical'``) or by member type (value
+ ``'groupwise'``). The default is alphabetical.
+
+ .. versionadded:: 0.6
+
Docstring preprocessing
-----------------------
diff --git a/doc/ext/builderapi.rst b/doc/ext/builderapi.rst
index fb2b7ea4b..bb11bfe2f 100644
--- a/doc/ext/builderapi.rst
+++ b/doc/ext/builderapi.rst
@@ -3,9 +3,9 @@
Writing new builders
====================
-XXX to be expanded.
+.. todo:: Expand this.
-.. currentmodule:: sphinx.builder
+.. currentmodule:: sphinx.builders
.. class:: Builder
@@ -20,7 +20,7 @@ XXX to be expanded.
.. automethod:: build_update
.. automethod:: build
- These methods must be overridden in concrete builder classes:
+ These methods can be overridden in concrete builder classes:
.. automethod:: init
.. automethod:: get_outdated_docs
@@ -28,3 +28,4 @@ XXX to be expanded.
.. automethod:: prepare_writing
.. automethod:: write_doc
.. automethod:: finish
+
diff --git a/doc/ext/coverage.rst b/doc/ext/coverage.rst
index 2f1e00ecc..13294f8bc 100644
--- a/doc/ext/coverage.rst
+++ b/doc/ext/coverage.rst
@@ -12,7 +12,7 @@ This extension features one additional builder, the :class:`CoverageBuilder`.
To use this builder, activate the coverage extension in your configuration
file and give ``-b coverage`` on the command line.
-XXX to be expanded.
+.. todo:: Write this section.
Several new configuration values can be used to specify what the builder
should check:
diff --git a/doc/ext/doctest.rst b/doc/ext/doctest.rst
index 9de6ba9e9..19905dc7d 100644
--- a/doc/ext/doctest.rst
+++ b/doc/ext/doctest.rst
@@ -131,7 +131,7 @@ completely equivalent. ::
Test-Output example:
- .. testcode::
+ .. testcode::
parrot.voom(3000)
@@ -149,6 +149,14 @@ There are also these config values for customizing the doctest extension:
A list of directories that will be added to :data:`sys.path` when the doctest
builder is used. (Make sure it contains absolute paths.)
+.. confval:: doctest_global_setup
+
+ Python code that is treated like it were put in a ``testsetup`` directive for
+ *every* file that is tested, and for every group. You can use this to
+ e.g. import modules you will always need in your doctests.
+
+ .. versionadded:: 0.6
+
.. confval:: doctest_test_doctest_blocks
If this is a nonempty string (the default is ``'default'``), standard reST
@@ -179,7 +187,7 @@ There are also these config values for customizing the doctest extension:
>>> print 1
1
- Some more documentation text.
+ Some more documentation text.
This feature makes it easy for you to test doctests in docstrings included
with the :mod:`~sphinx.ext.autodoc` extension without marking them up with a
diff --git a/doc/ext/graphviz.rst b/doc/ext/graphviz.rst
new file mode 100644
index 000000000..d007bf258
--- /dev/null
+++ b/doc/ext/graphviz.rst
@@ -0,0 +1,77 @@
+.. highlight:: rest
+
+:mod:`sphinx.ext.graphviz` -- Add Graphviz graphs
+=================================================
+
+.. module:: sphinx.ext.graphviz
+ :synopsis: Support for Graphviz graphs.
+
+.. versionadded:: 0.6
+
+This extension allows you to embed `Graphviz `_ graphs in
+your documents.
+
+It adds these directives:
+
+
+.. directive:: graphviz
+
+ Directive to embed graphviz code. The input code for ``dot`` is given as the
+ content. For example::
+
+ .. graphviz::
+
+ digraph foo {
+ "bar" -> "baz";
+ }
+
+ In HTML output, the code will be rendered to a PNG image. In LaTeX output,
+ the code will be rendered to an embeddable PDF file.
+
+
+.. directive:: graph
+
+ Directive for embedding a single undirected graph. The name is given as a
+ directive argument, the contents of the graph are the directive content.
+ This is a convenience directive to generate ``graph { }``.
+
+ For example::
+
+ .. graph:: foo
+
+ "bar" -- "baz";
+
+
+.. directive:: digraph
+
+ Directive for embedding a single directed graph. The name is given as a
+ directive argument, the contents of the graph are the directive content.
+ This is a convenience directive to generate ``digraph { }``.
+
+ For example::
+
+ .. digraph:: foo
+
+ "bar" -> "baz" -> "quux";
+
+
+There are also these new config values:
+
+.. confval:: graphviz_dot
+
+ The command name with which to invoke ``dot``. The default is ``'dot'``; you
+ may need to set this to a full path if ``dot`` is not in the executable
+ search path.
+
+ Since this setting is not portable from system to system, it is normally not
+ useful to set it in ``conf.py``; rather, giving it on the
+ :program:`sphinx-build` command line via the :option:`-D` option should be
+ preferable, like this::
+
+ sphinx-build -b html -D graphviz_dot=C:\graphviz\bin\dot.exe . _build/html
+
+.. confval:: graphviz_dot_args
+
+ Additional command-line arguments to give to dot, as a list. The default is
+ an empty list. This is the right place to set global graph, node or edge
+ attributes via dot's ``-G``, ``-N`` and ``-E`` options.
diff --git a/doc/ext/inheritance.rst b/doc/ext/inheritance.rst
new file mode 100644
index 000000000..fe6d636d3
--- /dev/null
+++ b/doc/ext/inheritance.rst
@@ -0,0 +1,46 @@
+.. highlight:: rest
+
+:mod:`sphinx.ext.inheritance_diagram` -- Include inheritance diagrams
+=====================================================================
+
+.. module:: sphinx.ext.inheritance_diagram
+ :synopsis: Support for displaying inheritance diagrams via graphviz.
+
+.. versionadded:: 0.6
+
+This extension allows you to include inheritance diagrams, rendered via the
+:mod:`Graphviz extension `.
+
+It adds this directive:
+
+.. directive:: inheritance-diagram
+
+ This directive has one or more arguments, each giving a module or class
+ name. Class names can be unqualified; in that case they are taken to exist
+ in the currently described module (see :dir:`module`).
+
+ For each given class, and each class in each given module, the base classes
+ are determined. Then, from all classes and their base classes, a graph is
+ generated which is then rendered via the graphviz extension to a directed
+ graph.
+
+ This directive supports an option called ``parts`` that, if given, must be an
+ integer, advising the directive to remove that many parts of module names
+ from the displayed names. (For example, if all your class names start with
+ ``lib.``, you can give ``:parts: 1`` to remove that prefix from the displayed
+ node names.)
+
+
+New config values are:
+
+.. confval:: inheritance_graph_attrs
+
+ A dictionary of graphviz graph attributes for inheritance diagrams.
+
+.. confval:: inheritance_node_attrs
+
+ A dictionary of graphviz node attributes for inheritance diagrams.
+
+.. confval:: inheritance_edge_attrs
+
+ A dictionary of graphviz edge attributes for inheritance diagrams.
diff --git a/doc/ext/intersphinx.rst b/doc/ext/intersphinx.rst
index befae2c07..302ab6a32 100644
--- a/doc/ext/intersphinx.rst
+++ b/doc/ext/intersphinx.rst
@@ -49,7 +49,7 @@ linking:
This will download the corresponding :file:`objects.inv` file from the
Internet and generate links to the pages under the given URI. The downloaded
inventory is cached in the Sphinx environment, so it must be redownloaded
- whenever you do a full rebuild.
+ whenever you do a full rebuild.
A second example, showing the meaning of a non-``None`` value::
diff --git a/doc/ext/math.rst b/doc/ext/math.rst
index 325dfd3a9..a214b41e3 100644
--- a/doc/ext/math.rst
+++ b/doc/ext/math.rst
@@ -12,8 +12,14 @@ Since mathematical notation isn't natively supported by HTML in any way, Sphinx
supports math in documentation with two extensions.
The basic math support that is common to both extensions is contained in
-:mod:`sphinx.ext.mathbase`. Other math support extensions should, if possible,
-reuse that support too.
+:mod:`sphinx.ext.mathbase`. Other math support extensions should,
+if possible, reuse that support too.
+
+.. note::
+
+ :mod:`sphinx.ext.mathbase` is not meant to be added to the
+ :confval:`extensions` config value, instead, use either
+ :mod:`sphinx.ext.pngmath` or :mod:`sphinx.ext.jsmath` as described below.
The input language for mathematics is LaTeX markup. This is the de-facto
standard for plain-text math notation and has the added advantage that no
@@ -85,7 +91,7 @@ further translation is necessary when building LaTeX output.
Euler's identity, equation :eq:`euler`, was elected one of the most
beautiful mathematical formulas.
-
+
:mod:`sphinx.ext.pngmath` -- Render math as PNG images
------------------------------------------------------
@@ -102,11 +108,8 @@ There are various config values you can set to influence how the images are buil
.. confval:: pngmath_latex
The command name with which to invoke LaTeX. The default is ``'latex'``; you
- may need to set this to a full path if ``latex`` not in the executable search
- path.
-
- This string is split into words with :func:`shlex.split`, so that you can
- include arguments as well if needed.
+ may need to set this to a full path if ``latex`` is not in the executable
+ search path.
Since this setting is not portable from system to system, it is normally not
useful to set it in ``conf.py``; rather, giving it on the
@@ -115,12 +118,23 @@ There are various config values you can set to influence how the images are buil
sphinx-build -b html -D pngmath_latex=C:\tex\latex.exe . _build/html
+ .. versionchanged:: 0.5.1
+ This value should only contain the path to the latex executable, not
+ further arguments; use :confval:`pngmath_latex_args` for that purpose.
+
.. confval:: pngmath_dvipng
The command name with which to invoke ``dvipng``. The default is
``'dvipng'``; you may need to set this to a full path if ``dvipng`` is not in
the executable search path.
+.. confval:: pngmath_latex_args
+
+ Additional arguments to give to latex, as a list. The default is an empty
+ list.
+
+ .. versionadded:: 0.5.1
+
.. confval:: pngmath_latex_preamble
Additional LaTeX code to put into the preamble of the short LaTeX files that
@@ -129,11 +143,21 @@ There are various config values you can set to influence how the images are buil
.. confval:: pngmath_dvipng_args
- Additional arguments to give to dvipng, as a list. This is empty by default.
- Arguments you might want to add here are e.g. ``['-bg', 'Transparent']``,
+ Additional arguments to give to dvipng, as a list. The default value is
+ ``['-gamma 1.5', '-D 110']`` which makes the image a bit darker and larger
+ then it is by default.
+
+ An arguments you might want to add here is e.g. ``'-bg Transparent'``,
which produces PNGs with a transparent background. This is not enabled by
default because some Internet Explorer versions don't like transparent PNGs.
+ .. note::
+
+ When you "add" an argument, you need to reproduce the default arguments if
+ you want to keep them; that is, like this::
+
+ pngmath_dvipng_args = ['-gamma 1.5', '-D 110', '-bg Transparent']
+
.. confval:: pngmath_use_preview
``dvipng`` has the ability to determine the "depth" of the rendered text: for
diff --git a/doc/ext/refcounting.rst b/doc/ext/refcounting.rst
index 225a898b9..e2f338710 100644
--- a/doc/ext/refcounting.rst
+++ b/doc/ext/refcounting.rst
@@ -4,4 +4,4 @@
.. module:: sphinx.ext.refcounting
:synopsis: Keep track of reference counting behavior.
-XXX to be written.
+.. todo:: Write this section.
diff --git a/doc/ext/todo.rst b/doc/ext/todo.rst
new file mode 100644
index 000000000..4f5a379dc
--- /dev/null
+++ b/doc/ext/todo.rst
@@ -0,0 +1,30 @@
+:mod:`sphinx.ext.todo` -- Support for todo items
+================================================
+
+.. module:: sphinx.ext.todo
+ :synopsis: Allow inserting todo items into documents.
+.. moduleauthor:: Daniel Bültmann
+
+.. versionadded:: 0.5
+
+There are two additional directives when using this extension:
+
+.. directive:: todo
+
+ Use this directive like, for example, :dir:`note`.
+
+ It will only show up in the output if :confval:`todo_include_todos` is true.
+
+
+.. directive:: todolist
+
+ This directive is replaced by a list of all todo directives in the whole
+ documentation, if :confval:`todo_include_todos` is true.
+
+
+There is also an additional config value:
+
+.. confval:: todo_include_todos
+
+ If this is ``True``, :dir:`todo` and :dir:`todolist` produce output, else
+ they produce nothing. The default is ``False``.
diff --git a/doc/ext/tutorial.rst b/doc/ext/tutorial.rst
new file mode 100644
index 000000000..c44748d23
--- /dev/null
+++ b/doc/ext/tutorial.rst
@@ -0,0 +1,343 @@
+.. _exttut:
+
+Tutorial: Writing a simple extension
+====================================
+
+This section is intended as a walkthrough for the creation of custom extensions.
+It covers the basics of writing and activating an extensions, as well as
+commonly used features of extensions.
+
+As an example, we will cover a "todo" extension that adds capabilities to
+include todo entries in the documentation, and collecting these in a central
+place. (A similar "todo" extension is distributed with Sphinx.)
+
+
+Build Phases
+------------
+
+One thing that is vital in order to understand extension mechanisms is the way
+in which a Sphinx project is built: this works in several phases.
+
+**Phase 0: Initialization**
+
+ In this phase, almost nothing interesting for us happens. The source
+ directory is searched for source files, and extensions are initialized.
+ Should a stored build environment exist, it is loaded, otherwise a new one is
+ created.
+
+**Phase 1: Reading**
+
+ In Phase 1, all source files (and on subsequent builds, those that are new or
+ changed) are read and parsed. This is the phase where directives and roles
+ are encountered by the docutils, and the corresponding functions are called.
+ The output of this phase is a *doctree* for each source files, that is a tree
+ of docutils nodes. For document elements that aren't fully known until all
+ existing files are read, temporary nodes are created.
+
+ During reading, the build environment is updated with all meta- and cross
+ reference data of the read documents, such as labels, the names of headings,
+ described Python objects and index entries. This will later be used to
+ replace the temporary nodes.
+
+ The parsed doctrees are stored on the disk, because it is not possible to
+ hold all of them in memory.
+
+**Phase 2: Consistency checks**
+
+ Some checking is done to ensure no surprises in the built documents.
+
+**Phase 3: Resolving**
+
+ Now that the metadata and cross-reference data of all existing documents is
+ known, all temporary nodes are replaced by nodes that can be converted into
+ output. For example, links are created for object references that exist, and
+ simple literal nodes are created for those that don't.
+
+**Phase 4: Writing**
+
+ This phase converts the resolved doctrees to the desired output format, such
+ as HTML or LaTeX. This happens via a so-called docutils writer that visits
+ the individual nodes of each doctree and produces some output in the process.
+
+.. note::
+
+ Some builders deviate from this general build plan, for example, the builder
+ that checks external links does not need anything more than the parsed
+ doctrees and therefore does not have phases 2--4.
+
+
+Extension Design
+----------------
+
+We want the extension to add the following to Sphinx:
+
+* A "todo" directive, containing some content that is marked with "TODO", and
+ only shown in the output if a new config value is set. (Todo entries should
+ not be in the output by default.)
+
+* A "todolist" directive that creates a list of all todo entries throughout the
+ documentation.
+
+For that, we will need to add the following elements to Sphinx:
+
+* New directives, called ``todo`` and ``todolist``.
+* New document tree nodes to represent these directives, conventionally also
+ called ``todo`` and ``todolist``. We wouldn't need new nodes if the new
+ directives only produced some content representable by existing nodes.
+* A new config value ``todo_include_todos`` (config value names should start
+ with the extension name, in order to stay unique) that controls whether todo
+ entries make it into the output.
+* New event handlers: one for the :event:`doctree-resolved` event, to replace
+ the todo and todolist nodes, and one for :event:`env-purge-doc` (the reason
+ for that will be covered later).
+
+
+The Setup Function
+------------------
+
+.. currentmodule:: sphinx.application
+
+The new elements are added in the extension's setup function. Let us create a
+new Python module called :file:`todo.py` and add the setup function::
+
+ def setup(app):
+ app.add_config_value('todo_include_todos', False, False)
+
+ app.add_node(todolist)
+ app.add_node(todo,
+ html=(visit_todo_node, depart_todo_node),
+ latex=(visit_todo_node, depart_todo_node),
+ text=(visit_todo_node, depart_todo_node))
+
+ app.add_directive('todo', TodoDirective)
+ app.add_directive('todolist', TodolistDirective)
+ app.connect('doctree-resolved', process_todo_nodes)
+ app.connect('env-purge-doc', purge_todos)
+
+The calls in this function refer to classes and functions not yet written. What
+the individual calls do is the following:
+
+* :meth:`~Sphinx.add_config_value` lets Sphinx know that it should recognize the
+ new *config value* ``todo_include_todos``, whose default value should be
+ ``False`` (this also tells Sphinx that it is a boolean value).
+
+ If the third argument was ``True``, all documents would be re-read if the
+ config value changed its value. This is needed for config values that
+ influence reading (build phase 1).
+
+* :meth:`~Sphinx.add_node` adds a new *node class* to the build system. It also
+ can specify visitor functions for each supported output format. These visitor
+ functions are needed when the new nodes stay until phase 4 -- since the
+ ``todolist`` node is always replaced in phase 3, it doesn't need any.
+
+ We need to create the two node classes ``todo`` and ``todolist`` later.
+
+* :meth:`~Sphinx.add_directive` adds a new *directive*, given by name and class.
+
+ The handler functions are created later.
+
+* Finally, :meth:`~Sphinx.connect` adds an *event handler* to the event whose
+ name is given by the first argument. The event handler function is called
+ with several arguments which are documented with the event.
+
+
+The Node Classes
+----------------
+
+Let's start with the node classes::
+
+ from docutils import nodes
+
+ class todo(nodes.Admonition, nodes.Element):
+ pass
+
+ class todolist(nodes.General, nodes.Element):
+ pass
+
+ def visit_todo_node(self, node):
+ self.visit_admonition(node)
+
+ def depart_todo_node(self, node):
+ self.depart_admonition(node)
+
+Node classes usually don't have to do anything except inherit from the standard
+docutils classes defined in :mod:`docutils.nodes`. ``todo`` inherits from
+``Admonition`` because it should be handled like a note or warning, ``todolist``
+is just a "general" node.
+
+
+The Directive Classes
+---------------------
+
+A directive class is a class deriving usually from
+``docutils.parsers.rst.Directive``. Since the class-based directive interface
+doesn't exist yet in Docutils 0.4, Sphinx has another base class called
+``sphinx.util.compat.Directive`` that you can derive your directive from, and it
+will work with both Docutils 0.4 and 0.5 upwards. The directive interface is
+covered in detail in the docutils documentation; the important thing is that the
+class has a method ``run`` that returns a list of nodes.
+
+The ``todolist`` directive is quite simple::
+
+ from sphinx.util.compat import Directive
+
+ class TodolistDirective(Directive):
+
+ def run(self):
+ return [todolist('')]
+
+An instance of our ``todolist`` node class is created and returned. The
+todolist directive has neither content nor arguments that need to be handled.
+
+The ``todo`` directive function looks like this::
+
+ from sphinx.util.compat import make_admonition
+
+ class TodoDirective(Directive):
+
+ # this enables content in the directive
+ has_content = True
+
+ def run(self):
+ env = self.state.document.settings.env
+
+ targetid = "todo-%s" % env.index_num
+ env.index_num += 1
+ targetnode = nodes.target('', '', ids=[targetid])
+
+ ad = make_admonition(todo, self.name, [_('Todo')], self.options,
+ self.content, self.lineno, self.content_offset,
+ self.block_text, self.state, self.state_machine)
+
+ if not hasattr(env, 'todo_all_todos'):
+ env.todo_all_todos = []
+ env.todo_all_todos.append({
+ 'docname': env.docname,
+ 'lineno': self.lineno,
+ 'todo': ad[0].deepcopy(),
+ 'target': targetnode,
+ })
+
+ return [targetnode] + ad
+
+Several important things are covered here. First, as you can see, you can refer
+to the build environment instance using ``self.state.document.settings.env``.
+
+Then, to act as a link target (from the todolist), the todo directive needs to
+return a target node in addition to the todo node. The target ID (in HTML, this
+will be the anchor name) is generated by using ``env.index_num`` which is
+persistent between directive calls and therefore leads to unique target names.
+The target node is instantiated without any text (the first two arguments).
+
+An admonition is created using a standard docutils function (wrapped in Sphinx
+for docutils cross-version compatibility). The first argument gives the node
+class, in our case ``todo``. The third argument gives the admonition title (use
+``arguments`` here to let the user specify the title). A list of nodes is
+returned from ``make_admonition``.
+
+Then, the todo node is added to the environment. This is needed to be able to
+create a list of all todo entries throughout the documentation, in the place
+where the author puts a ``todolist`` directive. For this case, the environment
+attribute ``todo_all_todos`` is used (again, the name should be unique, so it is
+prefixed by the extension name). It does not exist when a new environment is
+created, so the directive must check and create it if necessary. Various
+information about the todo entry's location are stored along with a copy of the
+node.
+
+In the last line, the nodes that should be put into the doctree are returned:
+the target node and the admonition node.
+
+The node structure that the directive returns looks like this::
+
+ +--------------------+
+ | target node |
+ +--------------------+
+ +--------------------+
+ | todo node |
+ +--------------------+
+ \__+--------------------+
+ | admonition title |
+ +--------------------+
+ | paragraph |
+ +--------------------+
+ | ... |
+ +--------------------+
+
+
+The Event Handlers
+------------------
+
+Finally, let's look at the event handlers. First, the one for the
+:event:`env-purge-doc` event::
+
+ def purge_todos(app, env, docname):
+ if not hasattr(env, 'todo_all_todos'):
+ return
+ env.todo_all_todos = [todo for todo in env.todo_all_todos
+ if todo['docname'] != docname]
+
+Since we store information from source files in the environment, which is
+persistent, it may become out of date when the source file changes. Therefore,
+before each source file is read, the environment's records of it are cleared,
+and the :event:`env-purge-doc` event gives extensions a chance to do the same.
+Here we clear out all todos whose docname matches the given one from the
+``todo_all_todos`` list. If there are todos left in the document, they will be
+added again during parsing.
+
+The other handler belongs to the :event:`doctree-resolved` event. This event is
+emitted at the end of phase 3 and allows custom resolving to be done::
+
+ def process_todo_nodes(app, doctree, fromdocname):
+ if not app.config.todo_include_todos:
+ for node in doctree.traverse(todo):
+ node.parent.remove(node)
+
+ # Replace all todolist nodes with a list of the collected todos.
+ # Augment each todo with a backlink to the original location.
+ env = app.builder.env
+
+ for node in doctree.traverse(todolist):
+ if not app.config.todo_include_todos:
+ node.replace_self([])
+ continue
+
+ content = []
+
+ for todo_info in env.todo_all_todos:
+ para = nodes.paragraph()
+ filename = env.doc2path(todo_info['docname'], base=None)
+ description = (
+ _('(The original entry is located in %s, line %d and can be found ') %
+ (filename, todo_info['lineno']))
+ para += nodes.Text(description, description)
+
+ # Create a reference
+ newnode = nodes.reference('', '')
+ innernode = nodes.emphasis(_('here'), _('here'))
+ newnode['refdocname'] = todo_info['docname']
+ newnode['refuri'] = app.builder.get_relative_uri(
+ fromdocname, todo_info['docname'])
+ newnode['refuri'] += '#' + todo_info['target']['refid']
+ newnode.append(innernode)
+ para += newnode
+ para += nodes.Text('.)', '.)')
+
+ # Insert into the todolist
+ content.append(todo_info['todo'])
+ content.append(para)
+
+ node.replace_self(content)
+
+It is a bit more involved. If our new "todo_include_todos" config value is
+false, all todo and todolist nodes are removed from the documents.
+
+If not, todo nodes just stay where and how they are. Todolist nodes are
+replaced by a list of todo entries, complete with backlinks to the location
+where they come from. The list items are composed of the nodes from the todo
+entry and docutils nodes created on the fly: a paragraph for each entry,
+containing text that gives the location, and a link (reference node containing
+an italic node) with the backreference. The reference URI is built by
+``app.builder.get_relative_uri`` which creates a suitable URI depending on the
+used builder, and appending the todo node's (the target's) ID as the anchor
+name.
+
diff --git a/doc/extensions.rst b/doc/extensions.rst
index 3f63c64f6..21ba0fd80 100644
--- a/doc/extensions.rst
+++ b/doc/extensions.rst
@@ -9,17 +9,25 @@ Sphinx Extensions
Since many projects will need special features in their documentation, Sphinx is
designed to be extensible on several levels.
-First, you can add new :term:`builder`\s to support new output formats or
-actions on the parsed documents. Then, it is possible to register custom
-reStructuredText roles and directives, extending the markup. And finally, there
-are so-called "hook points" at strategic places throughout the build process,
-where an extension can register a hook and run specialized code.
+This is what you can do in an extension: First, you can add new
+:term:`builder`\s to support new output formats or actions on the parsed
+documents. Then, it is possible to register custom reStructuredText roles and
+directives, extending the markup. And finally, there are so-called "hook
+points" at strategic places throughout the build process, where an extension can
+register a hook and run specialized code.
-The configuration file itself can be an extension, see the :confval:`extensions`
-configuration value docs.
+An extension is simply a Python module. When an extension is loaded, Sphinx
+imports this module and executes its ``setup()`` function, which in turn
+notifies Sphinx of everything the extension offers -- see the extension tutorial
+for examples.
+
+The configuration file itself can be treated as an extension if it contains a
+``setup()`` function. All other extensions to load must be listed in the
+:confval:`extensions` configuration value.
.. toctree::
+ ext/tutorial
ext/appapi
ext/builderapi
@@ -36,6 +44,41 @@ These extensions are built in and can be activated by respective entries in the
ext/doctest
ext/intersphinx
ext/math
+ ext/graphviz
+ ext/inheritance
ext/refcounting
ext/ifconfig
ext/coverage
+ ext/todo
+
+
+Third-party extensions
+----------------------
+
+There are several extensions that are not (yet) maintained in the Sphinx
+distribution. The `Wiki at BitBucket`_ maintains a list of those.
+
+If you write an extension that you think others will find useful, please write
+to the project mailing list (sphinx-dev@googlegroups.com) and we'll find the
+proper way of including or hosting it for the public.
+
+.. _Wiki at BitBucket: http://www.bitbucket.org/birkenfeld/sphinx/wiki/Home
+
+
+Where to put your own extensions?
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Extensions local to a project should be put within the project's directory
+structure. Set Python's module search path, ``sys.path``, accordingly so that
+Sphinx can find them.
+E.g., if your extension ``foo.py`` lies in the ``exts`` subdirectory of the
+project root, put into :file:`conf.py`::
+
+ import sys, os
+
+ sys.path.append(os.path.abspath('exts'))
+
+ extensions = ['foo']
+
+You can also install extensions anywhere else on ``sys.path``, e.g. in the
+``site-packages`` directory.
diff --git a/doc/faq.rst b/doc/faq.rst
new file mode 100644
index 000000000..ae12cdad7
--- /dev/null
+++ b/doc/faq.rst
@@ -0,0 +1,30 @@
+.. _faq:
+
+Sphinx FAQ
+==========
+
+This is a list of Frequently Asked Questions about Sphinx. Feel free to
+suggest new entries!
+
+How do I...
+-----------
+
+... get section numbers?
+ They are automatic in LaTeX output; for HTML, give a ``:numbered:`` option to
+ the :dir:`toctree` directive where you want to start numbering.
+
+... customize the look of the built HTML files?
+ Use themes, see :doc:`theming`.
+
+... add global substitutions or includes?
+ Add them in the :confval:`rst_epilog` config value.
+
+... write my own extension?
+ See the :ref:`extension tutorial `.
+
+... use Sphinx with Epydoc?
+ There's a third-party extension providing an `api role`_ which refers to
+ Epydoc's API docs for a given identifier.
+
+
+.. _api role: http://git.savannah.gnu.org/cgit/kenozooid.git/tree/doc/extapi.py
diff --git a/doc/glossary.rst b/doc/glossary.rst
index 6a80ad361..7ec787ff5 100644
--- a/doc/glossary.rst
+++ b/doc/glossary.rst
@@ -6,7 +6,7 @@ Glossary
.. glossary::
builder
- A class (inheriting from :class:`~sphinx.builder.Builder`) that takes
+ A class (inheriting from :class:`~sphinx.builders.Builder`) that takes
parsed documents and performs an action on them. Normally, builders
translate the documents to an output format, but it is also possible to
use the builder builders that e.g. check for broken links in the
diff --git a/doc/intro.rst b/doc/intro.rst
index af3cc1aaa..7b8f86511 100644
--- a/doc/intro.rst
+++ b/doc/intro.rst
@@ -89,6 +89,12 @@ The :program:`sphinx-build` script has several more options:
cross-references), but rebuild it completely. The default is to only read
and parse source files that are new or have changed since the last run.
+**-t** *tag*
+ Define the tag *tag*. This is relevant for :dir:`only` directives that only
+ include their content if this tag is set.
+
+ .. versionadded:: 0.6
+
**-d** *path*
Since Sphinx has to read and parse all source files before it can write an
output file, the parsed source files are cached as "doctree pickles".
@@ -105,9 +111,18 @@ The :program:`sphinx-build` script has several more options:
.. versionadded:: 0.3
+**-C**
+ Don't look for a configuration file; only take options via the ``-D`` option.
+
+ .. versionadded:: 0.5
+
**-D** *setting=value*
- Override a configuration value set in the :file:`conf.py` file. (The value
- must be a string value.)
+ Override a configuration value set in the :file:`conf.py` file. The value
+ must be a string or dictionary value. For the latter, supply the setting
+ name and key like this: ``-D latex_elements.docclass=scrartcl``.
+
+ .. versionchanged:: 0.6
+ The value can now be a dictionary value.
**-A** *name=value*
Make the *name* assigned to *value* in the HTML templates.
@@ -124,6 +139,13 @@ The :program:`sphinx-build` script has several more options:
Do not output anything on standard output, also suppress warnings. Only
errors are written to standard error.
+**-w** *file*
+ Write warnings (and errors) to the given file, in addition to standard error.
+
+**-W**
+ Turn warnings into errors. This means that the build stops at the first
+ warning and ``sphinx-build`` exits with exit status 1.
+
**-P**
(Useful for debugging only.) Run the Python debugger, :mod:`pdb`, if an
unhandled exception occurs while building.
diff --git a/doc/markup/code.rst b/doc/markup/code.rst
index 299ab0bc0..93cd127ba 100644
--- a/doc/markup/code.rst
+++ b/doc/markup/code.rst
@@ -30,7 +30,10 @@ installed) and handled in a smart way:
config value.
* Within Python highlighting mode, interactive sessions are recognized
- automatically and highlighted appropriately.
+ automatically and highlighted appropriately. Normal Python code is only
+ highlighted if it is parseable (so you can use Python as the default, but
+ interspersed snippets of shell commands or other code blocks will not be
+ highlighted as Python).
* The highlighting language can be changed using the ``highlight`` directive,
used as follows::
@@ -96,12 +99,14 @@ Includes
.. literalinclude:: example.py
- The file name is relative to the current file's path.
+ The file name is usually relative to the current file's path. However, if it
+ is absolute (starting with ``/``), it is relative to the top source
+ directory.
The directive also supports the ``linenos`` flag option to switch on line
numbers, and a ``language`` option to select a language different from the
current file's standard language. Example with options::
-
+
.. literalinclude:: example.rb
:language: ruby
:linenos:
@@ -113,8 +118,36 @@ Includes
.. literalinclude:: example.py
:encoding: latin-1
+ The directive also supports including only parts of the file. If it is a
+ Python module, you can select a class, function or method to include using
+ the ``pyobject`` option::
+
+ .. literalinclude:: example.py
+ :pyobject: Timer.start
+
+ This would only include the code lines belonging to the ``start()`` method in
+ the ``Timer`` class within the file.
+
+ Alternately, you can specify exactly which lines to include by giving a
+ ``lines`` option::
+
+ .. literalinclude:: example.py
+ :lines: 1,3,5-10,20-
+
+ This includes the lines 1, 3, 5 to 10 and lines 20 to the last line.
+
+ Another way to control which part of the file is included is to use the
+ ``start-after`` and ``end-before`` options (or only one of them). If
+ ``start-after`` is given as a string option, only lines that follow the first
+ line containing that string are included. If ``end-before`` is given as a
+ string option, only lines that precede the first lines containing that string
+ are included.
+
.. versionadded:: 0.4.3
The ``encoding`` option.
+ .. versionadded:: 0.6
+ The ``pyobject``, ``lines``, ``start-after`` and ``end-before`` options,
+ as well as support for absolute filenames.
.. rubric:: Footnotes
diff --git a/doc/markup/desc.rst b/doc/markup/desc.rst
index f121d23d7..ec8ede37d 100644
--- a/doc/markup/desc.rst
+++ b/doc/markup/desc.rst
@@ -17,7 +17,7 @@ typical module section might start like this::
.. moduleauthor:: John Idle
-The directives you can use for module are:
+The directives you can use for module declarations are:
.. directive:: .. module:: name
@@ -67,8 +67,8 @@ The directives you can use for module are:
.. _desc-units:
-Description units
------------------
+Object description units
+------------------------
There are a number of directives used to describe specific features provided by
modules. Each directive requires one or more signatures to provide basic
@@ -204,39 +204,12 @@ The directives are:
Like :dir:`method`, but indicates that the method is a static method.
.. versionadded:: 0.4
-
-.. directive:: .. cmdoption:: name args, name args, ...
- Describes a command line option or switch. Option argument names should be
- enclosed in angle brackets. Example::
+.. directive:: .. classmethod:: name(signature)
- .. cmdoption:: -m , --module
+ Like :dir:`method`, but indicates that the method is a class method.
- Run a module as a script.
-
- The directive will create a cross-reference target named after the *first*
- option, referencable by :role:`option` (in the example case, you'd use
- something like ``:option:`-m```).
-
-.. directive:: .. envvar:: name
-
- Describes an environment variable that the documented code uses or defines.
-
-
-There is also a generic version of these directives:
-
-.. directive:: .. describe:: text
-
- This directive produces the same formatting as the specific ones explained
- above but does not create index entries or cross-referencing targets. It is
- used, for example, to describe the directives in this document. Example::
-
- .. describe:: opcode
-
- Describes a Python bytecode instruction.
-
-Extensions may add more directives like that, using the
-:func:`~sphinx.application.Sphinx.add_description_unit` method.
+ .. versionadded:: 0.6
.. _signatures:
@@ -287,7 +260,7 @@ explained by an example::
.. function:: format_exception(etype, value, tb[, limit=None])
Format the exception with a traceback.
-
+
:param etype: exception type
:param value: exception value
:param tb: traceback object
@@ -308,3 +281,77 @@ This will render like this:
:param limit: maximum number of stack frames to show
:type limit: integer or None
:rtype: list of strings
+
+
+Command-line program markup
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+There is a set of directives allowing documenting command-line programs:
+
+.. directive:: .. cmdoption:: name args, name args, ...
+
+ Describes a command line option or switch. Option argument names should be
+ enclosed in angle brackets. Example::
+
+ .. cmdoption:: -m , --module
+
+ Run a module as a script.
+
+ The directive will create a cross-reference target named after the *first*
+ option, referencable by :role:`option` (in the example case, you'd use
+ something like ``:option:`-m```).
+
+.. directive:: .. envvar:: name
+
+ Describes an environment variable that the documented code or program uses or
+ defines.
+
+
+.. directive:: .. program:: name
+
+ Like :dir:`currentmodule`, this directive produces no output. Instead, it
+ serves to notify Sphinx that all following :dir:`cmdoption` directives
+ document options for the program called *name*.
+
+ If you use :dir:`program`, you have to qualify the references in your
+ :role:`option` roles by the program name, so if you have the following
+ situation ::
+
+ .. program:: rm
+
+ .. cmdoption:: -r
+
+ Work recursively.
+
+ .. program:: svn
+
+ .. cmdoption:: -r revision
+
+ Specify the revision to work upon.
+
+ then ``:option:`rm -r``` would refer to the first option, while
+ ``:option:`svn -r``` would refer to the second one.
+
+ The program name may contain spaces (in case you want to document subcommands
+ like ``svn add`` and ``svn commit`` separately).
+
+ .. versionadded:: 0.5
+
+
+Custom description units
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+There is also a generic version of these directives:
+
+.. directive:: .. describe:: text
+
+ This directive produces the same formatting as the specific ones explained
+ above but does not create index entries or cross-referencing targets. It is
+ used, for example, to describe the directives in this document. Example::
+
+ .. describe:: opcode
+
+ Describes a Python bytecode instruction.
+
+Extensions may add more directives like that, using the
+:func:`~sphinx.application.Sphinx.add_description_unit` method.
diff --git a/doc/markup/inline.rst b/doc/markup/inline.rst
index 7deffb435..69721b321 100644
--- a/doc/markup/inline.rst
+++ b/doc/markup/inline.rst
@@ -12,7 +12,8 @@ For all other roles, you have to write ``:rolename:`content```.
.. note::
The default role (```content```) has no special meaning by default. You are
- free to use it for anything you like.
+ free to use it for anything you like; use the :confval:`default_role` config
+ value to set it to a known role.
.. _xref-syntax:
@@ -223,7 +224,53 @@ to labels:
Using :role:`ref` is advised over standard reStructuredText links to sections
(like ```Section title`_``) because it works across files, when section headings
are changed, and for all builders that support cross-references.
-
+
+
+Cross-referencing documents
+---------------------------
+
+.. versionadded:: 0.6
+
+There is also a way to directly link to documents:
+
+.. role:: doc
+
+ Link to the specified document; the document name can be specified in
+ absolute or relative fashion. For example, if the reference
+ ``:doc:`parrot``` occurs in the document ``sketches/index``, then the link
+ refers to ``sketches/parrot``. If the reference is ``:doc:`/people``` or
+ ``:doc:`../people```, the link refers to ``people``.
+
+ If no explicit link text is given (like usual: ``:doc:`Monty Python members
+ ```), the link caption will be the title of the given document.
+
+
+Referencing downloadable files
+------------------------------
+
+.. versionadded:: 0.6
+
+.. role:: download
+
+ This role lets you link to files within your source tree that are not reST
+ documents that can be viewed, but files that can be downloaded.
+
+ When you use this role, the referenced file is automatically marked for
+ inclusion in the output when building (obviously, for HTML output only).
+ All downloadable files are put into the ``_downloads`` subdirectory of the
+ output directory; duplicate filenames are handled.
+
+ An example::
+
+ See :download:`this example script <../example.py>`.
+
+ The given filename is usually relative to the directory the current source
+ file is contained in, but if it absolute (starting with ``/``), it is taken
+ as relative to the top source directory.
+
+ The ``example.py`` file will be copied to the output directory, and a
+ suitable link generated to it.
+
Other semantic markup
---------------------
@@ -231,6 +278,16 @@ Other semantic markup
The following roles don't do anything special except formatting the text
in a different style:
+.. role:: abbr
+
+ An abbreviation. If the role content contains a parenthesized explanation,
+ it will be treated specially: it will be shown in a tool-tip in HTML, and
+ output only once in LaTeX.
+
+ Example: ``:abbr:`LIFO (last-in, first-out)```.
+
+ .. versionadded:: 0.6
+
.. role:: command
The name of an OS-level command, such as ``rm``.
@@ -329,7 +386,7 @@ in a different style:
curly braces to indicate a "variable" part, as in ``:file:``.
If you don't need the "variable part" indication, use the standard
- ````code```` instead.
+ ````code```` instead.
The following roles generate external links:
@@ -350,6 +407,7 @@ The following roles generate external links:
Note that there are no special roles for including hyperlinks as you can use
the standard reST markup for that purpose.
+
.. _default-substitutions:
Substitutions
@@ -372,6 +430,6 @@ They are set in the build configuration file.
.. describe:: |today|
- Replaced by either today's date, or the date set in the build configuration
- file. Normally has the format ``April 14, 2007``. Set by
- :confval:`today_fmt` and :confval:`today`.
+ Replaced by either today's date (the date on which the document is read), or
+ the date set in the build configuration file. Normally has the format
+ ``April 14, 2007``. Set by :confval:`today_fmt` and :confval:`today`.
diff --git a/doc/markup/misc.rst b/doc/markup/misc.rst
index 98f5485e7..01e5a3f18 100644
--- a/doc/markup/misc.rst
+++ b/doc/markup/misc.rst
@@ -3,6 +3,8 @@
Miscellaneous markup
====================
+.. _metadata:
+
File-wide metadata
------------------
@@ -46,6 +48,28 @@ Meta-information markup
output.
+.. _tags:
+
+Including content based on tags
+-------------------------------
+
+.. directive:: .. only::
+
+ Include the content of the directive only if the *expression* is true. The
+ expression should consist of tags, like this::
+
+ .. only:: html and draft
+
+ Undefined tags are false, defined tags (via the ``-t`` command-line option or
+ within :file:`conf.py`) are true. Boolean expressions, also using
+ parentheses (like ``html and (latex or draft)`` are supported.
+
+ The format of the current builder (``html``, ``latex`` or ``text``) is always
+ set as a tag.
+
+ .. versionadded:: 0.6
+
+
Tables
------
diff --git a/doc/markup/para.rst b/doc/markup/para.rst
index c60eb2587..2ea2fd2d7 100644
--- a/doc/markup/para.rst
+++ b/doc/markup/para.rst
@@ -1,4 +1,4 @@
-.. highlight:: rest
+x.. highlight:: rest
Paragraph-level markup
----------------------
@@ -85,9 +85,9 @@ units as well as normal text:
This directive creates a paragraph heading that is not used to create a
table of contents node.
-
+
.. note::
-
+
If the *title* of the rubric is "Footnotes", this rubric is ignored by
the LaTeX writer, since it is assumed to only contain footnote
definitions and therefore would create an empty heading.
@@ -100,6 +100,27 @@ units as well as normal text:
.. centered:: LICENSE AGREEMENT
+.. directive:: hlist
+
+ This directive must contain a bullet list. It will transform it into a more
+ compact list by either distributing more than one item horizontally, or
+ reducing spacing between items, depending on the builder.
+
+ For builders that support the horizontal distribution, there is a ``columns``
+ option that specifies the number of columns; it defaults to 2. Example::
+
+ .. hlist::
+ :columns: 3
+
+ * A list of
+ * short items
+ * that should be
+ * displayed
+ * horizontally
+
+ .. versionadded:: 0.6
+
+
Table-of-contents markup
------------------------
@@ -218,7 +239,7 @@ the definition of the symbol. There is this directive:
Note that no further reST parsing is done in the production, so that you
don't have to escape ``*`` or ``|`` characters.
-.. XXX describe optional first parameter
+.. XXX describe optional first parameter
The following is an example taken from the Python Reference Manual::
diff --git a/doc/rest.rst b/doc/rest.rst
index 8573430f6..e70fa105b 100644
--- a/doc/rest.rst
+++ b/doc/rest.rst
@@ -212,10 +212,19 @@ reST supports an image directive, used like so::
.. image:: gnu.png
(options)
-When used within Sphinx, the file name given (here ``gnu.png``) must be relative
-to the source file, and Sphinx will automatically copy image files over to a
-subdirectory of the output directory on building (e.g. the ``_static`` directory
-for HTML output.)
+When used within Sphinx, the file name given (here ``gnu.png``) must either be
+relative to the source file, or absolute which means that they are relative to
+the top source directory. For example, the file ``sketch/spam.rst`` could refer
+to the image ``images/spam.png`` as ``../images/spam.png`` or
+``/images/spam.png``.
+
+Sphinx will automatically copy image files over to a subdirectory of the output
+directory on building (e.g. the ``_static`` directory for HTML output.)
+
+Interpretation of image size options (``width`` and ``height``) is as follows:
+if the size has no unit or the unit is pixels, the given size will only be
+respected for output channels that support pixels (i.e. not in LaTeX output).
+Other units (like ``pt`` for points) will be used for HTML and LaTeX output.
Sphinx extends the standard docutils behavior by allowing an asterisk for the
extension::
@@ -231,6 +240,9 @@ the former, while the HTML builder would prefer the latter.
.. versionchanged:: 0.4
Added the support for file names ending in an asterisk.
+.. versionchanged:: 0.6
+ Image paths can now be absolute.
+
Footnotes
---------
@@ -277,7 +289,7 @@ markup blocks, like this::
See the `reST reference for substitutions
`_
for details.
-
+
If you want to use some substitutions for all documents, put them into a
separate file and include it into all documents you want to use them in, using
the :dir:`include` directive. Be sure to give the include file a file name
@@ -291,7 +303,17 @@ Comments
--------
Every explicit markup block which isn't a valid markup construct (like the
-footnotes above) is regarded as a comment.
+footnotes above) is regarded as a comment. For example::
+
+ .. This is a comment.
+
+You can indent text after a comment start to form multiline comments::
+
+ ..
+ This whole indented block
+ is a comment.
+
+ Still in the comment.
Source encoding
@@ -312,4 +334,7 @@ There are some problems one commonly runs into while authoring reST documents:
separated from the surrounding text by non-word characters, you have to use
a backslash-escaped space to get around that.
+* **No nested inline markup:** Something like ``*see :func:`foo`*`` is not
+ possible.
+
.. XXX more?
diff --git a/doc/sphinx-build.1 b/doc/sphinx-build.1
new file mode 100644
index 000000000..498771c92
--- /dev/null
+++ b/doc/sphinx-build.1
@@ -0,0 +1,102 @@
+.TH sphinx-build 1 "Jan 2009" "Sphinx 0.6" "User Commands"
+.SH NAME
+sphinx-build \- Sphinx documentation generator tool
+.SH SYNOPSIS
+.B sphinx-build
+[\fIoptions\fR] <\fIsourcedir\fR> <\fIoutdir\fR> [\fIfilenames\fR...]
+.SH DESCRIPTION
+sphinx-build generates documentation from the files in and places it
+in the .
+
+sphinx-build looks for /conf.py for the configuration settings.
+.B sphinx-quickstart(1)
+may be used to generate template files, including conf.py.
+
+sphinx-build can create documentation in different formats. A format is
+selected by specifying the builder name on the command line; it defaults to
+HTML. Builders can also perform other tasks related to documentation
+processing.
+
+By default, everything that is outdated is built. Output only for selected
+files can be built by specifying individual filenames.
+
+List of available builders:
+.TP
+\fBhtml\fR
+HTML files generation. This is default builder.
+.TP
+\fBhtmlhelp\fR
+Generates files for CHM generation.
+.TP
+\fBqthelp\fR
+Generates files for Qt help collection generation.
+.TP
+\fBlatex\fR
+Generates a LaTeX version of the documentation.
+.TP
+\fBtext\fR
+Generates a plain-text version of the documentation.
+.TP
+\fBchanges\fR
+Generates HTML files listing changed/added/deprecated items for the
+current version.
+.TP
+\fBlinkcheck\fR
+Checks the integrity of all external links in the documentation.
+.TP
+\fBpickle / json\fR
+Generates serialized HTML files in the selected format.
+
+.SH OPTIONS
+.TP
+\fB-b\fR
+Builder to use; defaults to html. See the full list of builders above.
+.TP
+\fB-a\fR
+Generates output for all files; without this option only output for
+new and changed files is generated.
+.TP
+\fB-E\fR
+Ignores cached files, forces to re-read all source files from disk.
+.TP
+\fB-c\fR
+Locates the conf.py file in the specified path instead of .
+.TP
+\fB-C\fR
+Specifies that no conf.py file at all is to be used. Configuration can
+only be set with the -D option.
+.TP
+\fB-D\fR =
+Overrides a setting from the configuration file.
+.TP
+\fB-d\fR
+Path to cached files; defaults to /.doctrees.
+.TP
+\fB-A\fR =
+Passes a value into the HTML templates (only for html builders).
+.TP
+\fB-N\fR
+Prevents colored output.
+.TP
+\fB-q\fR
+Quiet operation, just prints warnings and errors on stderr.
+.TP
+\fB-Q\fR
+Very quiet operation, doesn't print anything except for errors.
+.TP
+\fB-w\fR
+Write warnings and errors into the given file, in addition to stderr.
+.TP
+\fB-W\fR
+Turn warnings into errors.
+.TP
+\fB-P\fR
+Runs Pdb on exception.
+.SH "SEE ALSO"
+.BR sphinx-quickstart(1)
+.SH AUTHOR
+Georg Brandl , Armin Ronacher et
+al.
+.PP
+This manual page was initially written by Mikhail Gusarov
+, for the Debian project.
diff --git a/doc/sphinx-quickstart.1 b/doc/sphinx-quickstart.1
new file mode 100644
index 000000000..93b0a4a51
--- /dev/null
+++ b/doc/sphinx-quickstart.1
@@ -0,0 +1,17 @@
+.TH sphinx-quickstart 1 "Jan 2009" "Sphinx 0.6" "User Commands"
+.SH NAME
+sphinx-quickstart \- Sphinx documentation template generator
+.SH SYNOPSIS
+.B sphinx-quickstart
+.SH DESCRIPTION
+sphinx-quickstart is an interactive tool that asks some questions about your
+project and then generates a complete documentation directory and sample
+Makefile to be used with \fBsphinx-build(1)\fR.
+.SH "SEE ALSO"
+.BR sphinx-build(1)
+.SH AUTHOR
+Georg Brandl , Armin Ronacher et
+al.
+.PP
+This manual page was initially written by Mikhail Gusarov
+ for the Debian project.
diff --git a/doc/templating.rst b/doc/templating.rst
index 61a8a72b0..61657547d 100644
--- a/doc/templating.rst
+++ b/doc/templating.rst
@@ -1,3 +1,5 @@
+.. highlight:: html+jinja
+
.. _templating:
Templating
@@ -19,10 +21,10 @@ No. You have several other options:
configuration value accordingly.
* You can :ref:`write a custom builder ` that derives from
- :class:`~sphinx.builder.StandaloneHTMLBuilder` and calls your template engine
+ :class:`~sphinx.builders.StandaloneHTMLBuilder` and calls your template engine
of choice.
-* You can use the :class:`~sphinx.builder.PickleHTMLBuilder` that produces
+* You can use the :class:`~sphinx.builders.PickleHTMLBuilder` that produces
pickle files with the page contents, and postprocess them using a custom tool,
or use them in your Web application.
@@ -37,27 +39,26 @@ template, customizing it while also keeping the changes at a minimum.
To customize the output of your documentation you can override all the templates
(both the layout templates and the child templates) by adding files with the
-same name as the original filename into the template directory of the folder the
-Sphinx quickstart generated for you.
+same name as the original filename into the template directory of the structure
+the Sphinx quickstart generated for you.
Sphinx will look for templates in the folders of :confval:`templates_path`
first, and if it can't find the template it's looking for there, it falls back
-to the builtin templates that come with Sphinx.
+to the selected theme's templates.
A template contains **variables**, which are replaced with values when the
template is evaluated, **tags**, which control the logic of the template and
**blocks** which are used for template inheritance.
-Sphinx provides base templates with a couple of blocks it will fill with data.
-The default templates are located in the :file:`templates` folder of the Sphinx
-installation directory. Templates with the same name in the
-:confval:`templates_path` override templates located in the builtin folder.
+Sphinx' *basic* theme provides base templates with a couple of blocks it will
+fill with data. These are located in the :file:`themes/basic` subdirectory of
+the Sphinx installation directory, and used by all builtin Sphinx themes.
+Templates with the same name in the :confval:`templates_path` override templates
+supplied by the selected theme.
For example, to add a new link to the template area containing related links all
you have to do is to add a new template called ``layout.html`` with the
-following contents:
-
-.. sourcecode:: html+jinja
+following contents::
{% extends "!layout.html" %}
{% block rootrellink %}
@@ -65,16 +66,24 @@ following contents:
{{ super() }}
{% endblock %}
-By prefixing the name of the extended template with an exclamation mark, Sphinx
-will load the builtin layout template. If you override a block, you should call
-``{{ super() }}`` somewhere to render the block's content in the extended
-template -- unless you don't want that content to show up.
+By prefixing the name of the overridden template with an exclamation mark,
+Sphinx will load the layout template from the underlying HTML theme.
+**Important**: If you override a block, call ``{{ super() }}`` somewhere to
+render the block's content in the extended template -- unless you don't want
+that content to show up.
+
+
+Working the the builtin templates
+---------------------------------
+
+The builtin **basic** theme supplies the templates that all builtin Sphinx
+themes are based on. It has the following elements you can override or use:
Blocks
~~~~~~
-The following blocks exist in the ``layout`` template:
+The following blocks exist in the ``layout.html`` template:
`doctype`
The doctype of the output format. By default this is XHTML 1.0 Transitional
@@ -92,11 +101,11 @@ The following blocks exist in the ``layout`` template:
add references to JavaScript or extra CSS files.
`relbar1` / `relbar2`
- This block contains the list of related links (the parent documents on the
- left, and the links to index, modules etc. on the right). `relbar1` appears
- before the document, `relbar2` after the document. By default, both blocks
- are filled; to show the relbar only before the document, you would override
- `relbar2` like this::
+ This block contains the *relation bar*, the list of related links (the
+ parent documents on the left, and the links to index, modules etc. on the
+ right). `relbar1` appears before the document, `relbar2` after the
+ document. By default, both blocks are filled; to show the relbar only
+ before the document, you would override `relbar2` like this::
{% block relbar2 %}{% endblock %}
@@ -109,7 +118,8 @@ The following blocks exist in the ``layout`` template:
the :data:`reldelim1`.
`document`
- The contents of the document itself.
+ The contents of the document itself. It contains the block "body" where the
+ individual content is put by subtemplates like ``page.html``.
`sidebar1` / `sidebar2`
A possible location for a sidebar. `sidebar1` appears before the document
@@ -135,6 +145,10 @@ The following blocks exist in the ``layout`` template:
`sidebarrel`
The relation links (previous, next document) within the sidebar.
+`sidebarsourcelink`
+ The "Show source" link within the sidebar (normally only shown if this is
+ enabled by :confval:`html_show_sourcelink`).
+
`sidebarsearch`
The search box within the sidebar. Override this if you want to place some
content at the bottom of the sidebar.
@@ -162,13 +176,17 @@ using the ``{% set %}`` tag:
defaults to ``' |'``. Each item except of the last one in the related bar
ends with the value of this variable.
-Overriding works like this:
-
-.. sourcecode:: html+jinja
+Overriding works like this::
{% extends "!layout.html" %}
{% set reldelim1 = ' >' %}
+.. data:: script_files
+
+ Add additional script files here, like this::
+
+ {% set script_files = script_files + [pathto("_static/myscript.js", 1)] %}
+
Helper Functions
~~~~~~~~~~~~~~~~
@@ -196,7 +214,7 @@ them to generate links or output multiply used elements.
.. function:: relbar()
- Return the rendered relbar.
+ Return the rendered relation bar.
Global Variables
@@ -206,32 +224,145 @@ These global variables are available in every template and are safe to use.
There are more, but most of them are an implementation detail and might change
in the future.
+.. data:: builder
+
+ The name of the builder (e.g. ``html`` or ``htmlhelp``).
+
+.. data:: copyright
+
+ The value of :confval:`copyright`.
+
.. data:: docstitle
The title of the documentation (the value of :confval:`html_title`).
+.. data:: embedded
+
+ True if the built HTML is meant to be embedded in some viewing application
+ that handles navigation, not the web browser, such as for HTML help or Qt
+ help formats. In this case, the sidebar is not included.
+
+.. data:: favicon
+
+ The path to the HTML favicon in the static path, or ``''``.
+
+.. data:: file_suffix
+
+ The value of the builder's :attr:`out_suffix` attribute, i.e. the file name
+ extension that the output files will get. For a standard HTML builder, this
+ is usually ``.html``.
+
+.. data:: has_source
+
+ True if the reST document sources are copied (if :confval:`html_copy_source`
+ is true).
+
+.. data:: last_updated
+
+ The build date.
+
+.. data:: logo
+
+ The path to the HTML logo image in the static path, or ``''``.
+
+.. data:: master_doc
+
+ The value of :confval:`master_doc`, for usage with :func:`pathto`.
+
+.. data:: next
+
+ The next document for the navigation. This variable is either false or has
+ two attributes `link` and `title`. The title contains HTML markup. For
+ example, to generate a link to the next page, you can use this snippet::
+
+ {% if next %}
+ {{ next.title }}
+ {% endif %}
+
+.. data:: pagename
+
+ The "page name" of the current file, i.e. either the document name if the
+ file is generated from a reST source, or the equivalent hierarchical name
+ relative to the output directory (``[directory/]filename_without_extension``).
+
+.. data:: parents
+
+ A list of parent documents for navigation, structured like the :data:`next`
+ item.
+
+.. data:: prev
+
+ Like :data:`next`, but for the previous page.
+
+.. data:: project
+
+ The value of :confval:`project`.
+
+.. data:: release
+
+ The value of :confval:`release`.
+
+.. data:: rellinks
+
+ A list of links to put at the left side of the relbar, next to "next" and
+ "prev". This usually contains links to the index and the modindex. If you
+ add something yourself, it must be a tuple ``(pagename, link title,
+ accesskey, link text)``.
+
+.. data:: shorttitle
+
+ The value of :confval:`html_short_title`.
+
+.. data:: show_source
+
+ True if :confval:`html_show_sourcelink` is true.
+
+.. data:: sphinx_version
+
+ The version of Sphinx used to build.
+
+.. data:: style
+
+ The name of the main stylesheet, as given by the theme or
+ :confval:`html_style`.
+
+.. data:: title
+
+ The title of the current document, as used in the ```` tag.
+
+.. data:: use_opensearch
+
+ The value of :confval:`html_use_opensearch`.
+
+.. data:: version
+
+ The value of :confval:`version`.
+
+
+In addition to these values, there are also all **theme options** available
+(prefixed by ``theme_``), as well as the values given by the user in
+:confval:`html_context`.
+
+In documents that are created from source files (as opposed to
+automatically-generated files like the module index, or documents that already
+are in HTML form), these variables are also available:
+
+.. data:: meta
+
+ Document metadata, see :ref:`metadata`.
+
.. data:: sourcename
The name of the copied source file for the current document. This is only
nonempty if the :confval:`html_copy_source` value is true.
-.. data:: builder
+.. data:: toc
- The name of the builder (for builtin builders, ``html``, ``htmlhelp``, or
- ``web``).
+ The local table of contents for the current page, rendered as HTML bullet
+ lists.
-.. data:: next
+.. data:: toctree
- The next document for the navigation. This variable is either false or has
- two attributes `link` and `title`. The title contains HTML markup. For
- example, to generate a link to the next page, you can use this snippet:
-
- .. sourcecode:: html+jinja
-
- {% if next %}
- {{ next.title }}
- {% endif %}
-
-.. data:: prev
-
- Like :data:`next`, but for the previous page.
+ A callable yielding the global TOC tree containing the current page, rendered
+ as HTML bullet lists. If the optional keyword argument ``collapse`` is true,
+ all TOC entries that are not ancestors of the current page are collapsed.
diff --git a/doc/theming.rst b/doc/theming.rst
new file mode 100644
index 000000000..3e3a33c8d
--- /dev/null
+++ b/doc/theming.rst
@@ -0,0 +1,187 @@
+.. highlightlang:: python
+
+HTML theming support
+====================
+
+.. versionadded:: 0.6
+
+Sphinx supports changing the appearance of its HTML output via *themes*. A
+theme is a collection of HTML templates, stylesheet(s) and other static files.
+Additionally, it has a configuration file which specifies from which theme to
+inherit, which highlighting style to use, and what options exist for customizing
+the theme's look and feel.
+
+Themes are meant to be project-unaware, so they can be used for different
+projects without change.
+
+
+Using a theme
+-------------
+
+Using an existing theme is easy. If the theme is builtin to Sphinx, you only
+need to set the :confval:`html_theme` config value. With the
+:confval:`html_theme_options` config value you can set theme-specific options
+that change the look and feel. For example, you could have the following in
+your :file:`conf.py`::
+
+ html_theme = "default"
+ html_theme_options = {
+ "rightsidebar": "true",
+ "relbarbgcolor: "black"
+ }
+
+That would give you the default theme, but with a sidebar on the right side and
+a black background for the relation bar (the bar with the navigation links at
+the page's top and bottom).
+
+If the theme does not come with Sphinx, it can be in two forms: either a
+directory (containing :file:`theme.conf` and other needed files), or a zip file
+with the same contents. Either of them must be put where Sphinx can find it;
+for this there is the config value :confval:`html_theme_path`. It gives a list
+of directories, relative to the directory containing :file:`conf.py`, that can
+contain theme directories or zip files. For example, if you have a theme in the
+file :file:`blue.zip`, you can put it right in the directory containing
+:file:`conf.py` and use this configuration::
+
+ html_theme = "blue"
+ html_theme_path = ["."]
+
+
+.. _builtin-themes:
+
+Builtin themes
+--------------
+
+Sphinx comes with a selection of themes to choose from:
+
+* **basic** -- This is a basically unstyled layout used as the base for the
+ *default* and *sphinxdoc* themes, and usable as the base for custom themes as
+ well. The HTML contains all important elements like sidebar and relation bar.
+ There is one option (which is inherited by *default* and *sphinxdoc*):
+
+ - **nosidebar** (true or false): Don't include the sidebar. Defaults to
+ false.
+
+* **default** -- This is the default theme. It can be customized via these
+ options:
+
+ - **rightsidebar** (true or false): Put the sidebar on the right side.
+ Defaults to false.
+
+ - **stickysidebar** (true or false): Make the sidebar "fixed" so that it
+ doesn't scroll out of view for long body content. This may not work well
+ with all browsers. Defaults to false.
+
+ There are also various color and font options that can change the color scheme
+ without having to write a custom stylesheet:
+
+ - **footerbgcolor** (CSS color): Background color for the footer line.
+ - **footertextcolor** (CSS color): Text color for the footer line.
+ - **sidebarbgcolor** (CSS color): Background color for the sidebar.
+ - **sidebartextcolor** (CSS color): Text color for the sidebar.
+ - **sidebarlinkcolor** (CSS color): Link color for the sidebar.
+ - **relbarbgcolor** (CSS color): Background color for the relation bar.
+ - **relbartextcolor** (CSS color): Text color for the relation bar.
+ - **relbarlinkcolor** (CSS color): Link color for the relation bar.
+ - **bgcolor** (CSS color): Body background color.
+ - **textcolor** (CSS color): Body text color.
+ - **linkcolor** (CSS color): Body link color.
+ - **headbgcolor** (CSS color): Background color for headings.
+ - **headtextcolor** (CSS color): Text color for headings.
+ - **headlinkcolor** (CSS color): Link color for headings.
+ - **codebgcolor** (CSS color): Background color for code blocks.
+ - **codetextcolor** (CSS color): Default text color for code blocks, if not
+ set differently by the highlighting style.
+
+ - **bodyfont** (CSS font-family): Font for normal text.
+ - **headfont** (CSS font-family): Font for headings.
+
+* **sphinxdoc** -- The theme used for this documentation. It features a sidebar
+ on the right side. There are currently no options beyond *nosidebar*.
+
+* **traditional** -- A theme resembling the old Python documentation. There are
+ currently no options beyond *nosidebar*.
+
+
+Creating themes
+---------------
+
+As said, themes are either a directory or a zipfile (whose name is the theme
+name), containing the following:
+
+* A :file:`theme.conf` file, see below.
+* HTML templates, if needed.
+* A ``static/`` directory containing any static files that will be copied to the
+ output statid directory on build. These can be images, styles, script files.
+
+The :file:`theme.conf` file is in INI format [1]_ (readable by the standard
+Python :mod:`ConfigParser` module) and has the following structure:
+
+.. sourcecode:: ini
+
+ [theme]
+ inherit = base theme
+ stylesheet = main CSS name
+ pygments_style = stylename
+
+ [options]
+ variable = default value
+
+* The **inherit** setting gives the name of a "base theme", or ``none``. The
+ base theme will be used to locate missing templates (most themes will not have
+ to supply most templates if they use ``basic`` as the base theme), its options
+ will be inherited, and all of its static files will be used as well.
+
+* The **stylesheet** setting gives the name of a CSS file which will be
+ referenced in the HTML header. If you need more than one CSS file, either
+ include one from the other via CSS' ``@import``, or use a custom HTML template
+ that adds ```` tags as necessary. Setting the
+ :confval:`html_style` config value will override this setting.
+
+* The **pygments_style** setting gives the name of a Pygments style to use for
+ highlighting. This can be overridden by the user in the
+ :confval:`pygments_style` config value.
+
+* The **options** section contains pairs of variable names and default values.
+ These options can be overridden by the user in :confval:`html_theme_options`
+ and are accessible from all templates as ``theme_``.
+
+
+Templating
+~~~~~~~~~~
+
+The :doc:`guide to templating ` is helpful if you want to write your
+own templates. What is important to keep in mind is the order in which Sphinx
+searches for templates:
+
+* First, in the user's ``templates_path`` directories.
+* Then, in the selected theme.
+* Then, in its base theme, its base's base theme, etc.
+
+When extending a template in the base theme with the same name, use the theme
+name as an explicit directory: ``{% extends "basic/layout.html" %}``. From a
+user ``templates_path`` template, you can still use the "exclamation mark"
+syntax as described in the templating document.
+
+
+Static templates
+~~~~~~~~~~~~~~~~
+
+Since theme options are meant for the user to configure a theme more easily,
+without having to write a custom stylesheet, it is necessary to be able to
+template static files as well as HTML files. Therefore, Sphinx supports
+so-called "static templates", like this:
+
+If the name of a file in the ``static/`` directory of a theme (or in the user's
+static path, for that matter) ends with ``_t``, it will be processed by the
+template engine. The ``_t`` will be left from the final file name. For
+example, the *default* theme has a file ``static/default.css_t`` which uses
+templating to put the color options into the stylesheet. When a documentation
+is built with the default theme, the output directory will contain a
+``_static/default.css`` file where all template tags have been processed.
+
+
+.. [1] It is not an executable Python file, as opposed to :file:`conf.py`,
+ because that would pose an unnecessary security risk if themes are
+ shared.
+
diff --git a/ez_setup.py b/ez_setup.py
index 89cf056d9..d24e845e5 100644
--- a/ez_setup.py
+++ b/ez_setup.py
@@ -14,7 +14,7 @@ the appropriate options to ``use_setuptools()``.
This file can also be run as a script to install or upgrade setuptools.
"""
import sys
-DEFAULT_VERSION = "0.6c8"
+DEFAULT_VERSION = "0.6c9"
DEFAULT_URL = "http://pypi.python.org/packages/%s/s/setuptools/" % sys.version[:3]
md5_data = {
@@ -48,13 +48,18 @@ md5_data = {
'setuptools-0.6c8-py2.3.egg': '50759d29b349db8cfd807ba8303f1902',
'setuptools-0.6c8-py2.4.egg': 'cba38d74f7d483c06e9daa6070cce6de',
'setuptools-0.6c8-py2.5.egg': '1721747ee329dc150590a58b3e1ac95b',
+ 'setuptools-0.6c9-py2.3.egg': 'a83c4020414807b496e4cfbe08507c03',
+ 'setuptools-0.6c9-py2.4.egg': '260a2be2e5388d66bdaee06abec6342a',
+ 'setuptools-0.6c9-py2.5.egg': 'fe67c3e5a17b12c0e7c541b7ea43a8e6',
+ 'setuptools-0.6c9-py2.6.egg': 'ca37b1ff16fa2ede6e19383e7b59245a',
}
import sys, os
+try: from hashlib import md5
+except ImportError: from md5 import md5
def _validate_md5(egg_name, data):
if egg_name in md5_data:
- from md5 import md5
digest = md5(data).hexdigest()
if digest != md5_data[egg_name]:
print >>sys.stderr, (
@@ -64,7 +69,6 @@ def _validate_md5(egg_name, data):
sys.exit(2)
return data
-
def use_setuptools(
version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
download_delay=15
@@ -233,7 +237,6 @@ def update_md5(filenames):
"""Update our built-in md5 registry"""
import re
- from md5 import md5
for name in filenames:
base = os.path.basename(name)
@@ -270,3 +273,4 @@ if __name__=='__main__':
+
diff --git a/setup.py b/setup.py
index 13e70690e..ffecc8444 100644
--- a/setup.py
+++ b/setup.py
@@ -24,7 +24,7 @@ parsing and translating suite, the Docutils.
Although it is still under constant development, the following features
are already present, work fine and can be seen "in action" in the Python docs:
-* Output formats: HTML (including Windows HTML Help) and LaTeX,
+* Output formats: HTML (including Windows HTML Help), plain text and LaTeX,
for printable PDF versions
* Extensive cross-references: semantic markup and automatic links
for functions, classes, glossary terms and similar pieces of information
@@ -36,7 +36,7 @@ are already present, work fine and can be seen "in action" in the Python docs:
and inclusion of appropriately formatted docstrings.
'''
-requires = ['Pygments>=0.8', 'Jinja>=1.1', 'docutils>=0.4']
+requires = ['Pygments>=0.8', 'Jinja2>=2.1', 'docutils>=0.4']
if sys.version_info < (2, 4):
print 'ERROR: Sphinx requires at least Python 2.4 to run.'
@@ -98,7 +98,8 @@ else:
else:
for locale in os.listdir(self.directory):
po_file = os.path.join(self.directory, locale,
- 'LC_MESSAGES', self.domain + '.po')
+ 'LC_MESSAGES',
+ self.domain + '.po')
if os.path.exists(po_file):
po_files.append((locale, po_file))
js_files.append(os.path.join(self.directory, locale,
diff --git a/sphinx-build.py b/sphinx-build.py
index 4e2a868d9..c1998911d 100755
--- a/sphinx-build.py
+++ b/sphinx-build.py
@@ -4,8 +4,8 @@
Sphinx - Python documentation toolchain
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- :copyright: 2007-2008 by Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
import sys
diff --git a/sphinx-quickstart.py b/sphinx-quickstart.py
index 579b35587..ea55f8341 100755
--- a/sphinx-quickstart.py
+++ b/sphinx-quickstart.py
@@ -4,8 +4,8 @@
Sphinx - Python documentation toolchain
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- :copyright: 2008 by Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
import sys
diff --git a/sphinx/__init__.py b/sphinx/__init__.py
index ccd49e533..e0d8813f4 100644
--- a/sphinx/__init__.py
+++ b/sphinx/__init__.py
@@ -5,199 +5,52 @@
The Sphinx documentation toolchain.
- :copyright: 2007-2008 by Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
-import os
import sys
-import getopt
-import traceback
from os import path
-from cStringIO import StringIO
-
-from sphinx.util import format_exception_cut_frames, save_traceback
-from sphinx.util.console import darkred, nocolor, color_terminal
__revision__ = '$Revision$'
-__version__ = '0.5'
-__released__ = '0.5 (hg)'
+__version__ = '0.6'
+__released__ = '0.6 (hg)'
-
-def usage(argv, msg=None):
- if msg:
- print >>sys.stderr, msg
- print >>sys.stderr
- print >>sys.stderr, """\
-Sphinx v%s
-Usage: %s [options] sourcedir outdir [filenames...]
-Options: -b -- builder to use; default is html
- -a -- write all files; default is to only write new and changed files
- -E -- don't use a saved environment, always read all files
- -d -- path for the cached environment and doctree files
- (default: outdir/.doctrees)
- -c -- path where configuration file (conf.py) is located
- (default: same as sourcedir)
- -D -- override a setting in configuration
- -A -- pass a value into the templates, for HTML builder
- -N -- do not do colored output
- -q -- no output on stdout, just warnings on stderr
- -Q -- no output at all, not even warnings
- -P -- run Pdb on exception
- -g -- create autogenerated files
-Modi:
-* without -a and without filenames, write new and changed files.
-* with -a, write all files.
-* with filenames, write these.""" % (__version__, argv[0])
+package_dir = path.abspath(path.dirname(__file__))
def main(argv=sys.argv):
- # delay-import these to be able to get sphinx.__version__ from setup.py
- # even without docutils installed
- from sphinx.application import Sphinx, SphinxError
- from docutils.utils import SystemMessage
-
- if not sys.stdout.isatty() or not color_terminal():
- # Windows' poor cmd box doesn't understand ANSI sequences
- nocolor()
+ if sys.version_info[:3] < (2, 4, 0):
+ print >>sys.stderr, \
+ 'Error: Sphinx requires at least Python 2.4 to run.'
+ return 1
try:
- opts, args = getopt.getopt(argv[1:], 'ab:d:c:D:A:g:NEqP')
- srcdir = confdir = path.abspath(args[0])
- if not path.isdir(srcdir):
- print >>sys.stderr, 'Error: Cannot find source directory.'
- return 1
- if not path.isfile(path.join(srcdir, 'conf.py')) and \
- '-c' not in (opt[0] for opt in opts):
- print >>sys.stderr, 'Error: Source directory doesn\'t contain conf.py file.'
- return 1
- outdir = path.abspath(args[1])
- if not path.isdir(outdir):
- print >>sys.stderr, 'Making output directory...'
- os.makedirs(outdir)
- except (IndexError, getopt.error):
- usage(argv)
- return 1
-
- filenames = args[2:]
- err = 0
- for filename in filenames:
- if not path.isfile(filename):
- print >>sys.stderr, 'Cannot find file %r.' % filename
- err = 1
- if err:
- return 1
-
-
- buildername = all_files = None
- freshenv = use_pdb = False
- status = sys.stdout
- warning = sys.stderr
- confoverrides = {}
- htmlcontext = {}
- doctreedir = path.join(outdir, '.doctrees')
- for opt, val in opts:
-
- if opt == '-g':
- source_filenames =[srcdir+'/'+f for f in os.listdir(srcdir) if f.endswith('.rst')]
- if val is None:
- print >>sys.stderr, \
- 'Error: you must provide a destination directory for autodoc generation.'
- return 1
- p = path.abspath(val)
- from sphinx.ext.autosummary.generate import generate_autosummary_docs
- generate_autosummary_docs(source_filenames, p)
-
- elif opt == '-b':
- buildername = val
- elif opt == '-a':
- if filenames:
- usage(argv, 'Cannot combine -a option and filenames.')
- return 1
- all_files = True
- elif opt == '-d':
- doctreedir = path.abspath(val)
- elif opt == '-c':
- confdir = path.abspath(val)
- if not path.isfile(path.join(confdir, 'conf.py')):
- print >>sys.stderr, \
- 'Error: Configuration directory doesn\'t contain conf.py file.'
- return 1
- elif opt == '-D':
- try:
- key, val = val.split('=')
- except ValueError:
- print >>sys.stderr, \
- 'Error: -D option argument must be in the form name=value.'
- return 1
- try:
- val = int(val)
- except ValueError:
- pass
- confoverrides[key] = val
- elif opt == '-A':
- try:
- key, val = val.split('=')
- except ValueError:
- print >>sys.stderr, \
- 'Error: -A option argument must be in the form name=value.'
- return 1
- try:
- val = int(val)
- except ValueError:
- pass
- htmlcontext[key] = val
- elif opt == '-N':
- nocolor()
- elif opt == '-E':
- freshenv = True
- elif opt == '-q':
- status = StringIO()
- elif opt == '-Q':
- status = StringIO()
- warning = StringIO()
- elif opt == '-P':
- use_pdb = True
- confoverrides['html_context'] = htmlcontext
-
- try:
- app = Sphinx(srcdir, confdir, outdir, doctreedir, buildername,
- confoverrides, status, warning, freshenv)
- app.build(all_files, filenames)
- except KeyboardInterrupt:
- if use_pdb:
- import pdb
- print >>sys.stderr, darkred('Interrupted while building, starting debugger:')
- traceback.print_exc()
- pdb.post_mortem(sys.exc_info()[2])
- return 1
- except Exception, err:
- if use_pdb:
- import pdb
- print >>sys.stderr, darkred('Exception occurred while building, '
- 'starting debugger:')
- traceback.print_exc()
- pdb.post_mortem(sys.exc_info()[2])
- else:
- if isinstance(err, SystemMessage):
- print >>sys.stderr, darkred('reST markup error:')
- print >>sys.stderr, err.args[0].encode('ascii', 'backslashreplace')
- elif isinstance(err, SphinxError):
- print >>sys.stderr, darkred('%s:' % err.category)
- print >>sys.stderr, err
+ from sphinx import cmdline
+ except ImportError, err:
+ errstr = str(err)
+ if errstr.lower().startswith('no module named'):
+ whichmod = errstr[16:]
+ hint = ''
+ if whichmod.startswith('docutils'):
+ whichmod = 'Docutils library'
+ elif whichmod.startswith('jinja'):
+ whichmod = 'Jinja library'
+ elif whichmod == 'roman':
+ whichmod = 'roman module (which is distributed with Docutils)'
+ hint = ('This can happen if you upgraded docutils using\n'
+ 'easy_install without uninstalling the old version'
+ 'first.')
else:
- print >>sys.stderr, darkred('Exception occurred:')
- print >>sys.stderr, format_exception_cut_frames().rstrip()
- tbpath = save_traceback()
- print >>sys.stderr, darkred('The full traceback has been saved '
- 'in %s, if you want to report the '
- 'issue to the author.' % tbpath)
- print >>sys.stderr, ('Please also report this if it was a user '
- 'error, so that a better error message '
- 'can be provided next time.')
- print >>sys.stderr, ('Send reports to sphinx-dev@googlegroups.com. '
- 'Thanks!')
+ whichmod += ' module'
+ print >>sys.stderr, \
+ 'Error: The %s cannot be found. Did you install Sphinx '\
+ 'and its dependencies correctly?' % whichmod
+ if hint:
+ print >> sys.stderr, hint
return 1
+ raise
+ return cmdline.main(argv)
if __name__ == '__main__':
diff --git a/sphinx/_jinja.py b/sphinx/_jinja.py
deleted file mode 100644
index d6e98b214..000000000
--- a/sphinx/_jinja.py
+++ /dev/null
@@ -1,113 +0,0 @@
-# -*- coding: utf-8 -*-
-"""
- sphinx._jinja
- ~~~~~~~~~~~~~
-
- Jinja glue.
-
- :copyright: 2007-2008 by Georg Brandl, Horst Gutmann.
- :license: BSD.
-"""
-
-import codecs
-from os import path
-
-from sphinx.util import mtimes_of_files
-from sphinx.application import TemplateBridge
-
-from jinja import Environment
-from jinja.loaders import BaseLoader
-from jinja.exceptions import TemplateNotFound
-
-
-def babel_extract(fileobj, keywords, comment_tags, options):
- """
- Simple extractor to get some basic Babel support.
- """
- env = Environment()
- for lineno, sg, pl in env.get_translations_for_string(fileobj.read()):
- yield lineno, None, (sg, pl), ''
-
-
-class SphinxFileSystemLoader(BaseLoader):
- """
- A loader that loads templates either relative to one of a list of given
- paths, or from an absolute path.
- """
-
- def __init__(self, basepath, extpaths):
- self.basepath = path.abspath(basepath)
- self.extpaths = map(path.abspath, extpaths)
- self.searchpaths = self.extpaths + [self.basepath]
-
- def get_source(self, environment, name, parent):
- name = name.replace('/', path.sep)
- if name.startswith('!'):
- name = name[1:]
- if not path.exists(path.join(self.basepath, name)):
- raise TemplateNotFound(name)
- filename = path.join(self.basepath, name)
- elif path.isabs(name):
- if not path.exists(name):
- raise TemplateNotFound(name)
- filename = name
- else:
- for searchpath in self.searchpaths:
- if path.exists(path.join(searchpath, name)):
- filename = path.join(searchpath, name)
- break
- else:
- raise TemplateNotFound(name)
- f = codecs.open(filename, 'r', environment.template_charset)
- try:
- return f.read()
- finally:
- f.close()
-
-
-class TranslatorEnvironment(Environment):
- class _Translator(object):
- def __init__(self, translator):
- self.trans = translator
-
- def gettext(self, string):
- return self.trans.ugettext(string)
-
- def ngettext(self, singular, plural, n):
- return self.trans.ungettext(singular, plural, n)
-
- def __init__(self, *args, **kwargs):
- self.translator = kwargs['translator']
- del kwargs['translator']
- super(TranslatorEnvironment, self).__init__(*args, **kwargs)
-
- def get_translator(self, context):
- return TranslatorEnvironment._Translator(self.translator)
-
-
-class BuiltinTemplates(TemplateBridge):
- def init(self, builder):
- self.templates = {}
- base_templates_path = path.join(path.dirname(__file__), 'templates')
- ext_templates_path = [path.join(builder.confdir, dir)
- for dir in builder.config.templates_path]
- self.templates_path = [base_templates_path] + ext_templates_path
- loader = SphinxFileSystemLoader(base_templates_path, ext_templates_path)
- if builder.translator is not None:
- self.jinja_env = TranslatorEnvironment(loader=loader,
- friendly_traceback=False, translator=builder.translator)
- else:
- self.jinja_env = Environment(loader=loader,
- # disable traceback, more likely that something
- # in the application is broken than in the templates
- friendly_traceback=False)
-
- def newest_template_mtime(self):
- return max(mtimes_of_files(self.templates_path, '.html'))
-
- def render(self, template, context):
- if template in self.templates:
- return self.templates[template].render(context)
- templateobj = self.templates[template] = \
- self.jinja_env.get_template(template)
- return templateobj.render(context)
diff --git a/sphinx/addnodes.py b/sphinx/addnodes.py
index 8b20f7ac0..63907a0e6 100644
--- a/sphinx/addnodes.py
+++ b/sphinx/addnodes.py
@@ -5,8 +5,8 @@
Additional docutils nodes.
- :copyright: 2007-2008 by Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
from docutils import nodes
@@ -23,8 +23,12 @@ class desc(nodes.Admonition, nodes.Element): pass
class desc_addname(nodes.Part, nodes.Inline, nodes.TextElement): pass
# compatibility alias
desc_classname = desc_addname
-# return type (C); object type, e.g. -> annotation (Python)
+# return type (C); object type
class desc_type(nodes.Part, nodes.Inline, nodes.TextElement): pass
+# -> annotation (Python)
+class desc_returns(desc_type):
+ def astext(self):
+ return ' -> ' + nodes.TextElement.astext(self)
# main name of object
class desc_name(nodes.Part, nodes.Inline, nodes.TextElement): pass
# argument list
@@ -64,15 +68,25 @@ class pending_xref(nodes.Element): pass
# compact paragraph -- never makes a
class compact_paragraph(nodes.paragraph): pass
+# reference to a file to download
+class download_reference(nodes.reference): pass
+
# for the ACKS list
class acks(nodes.Element): pass
+# for horizontal lists
+class hlist(nodes.Element): pass
+class hlistcol(nodes.Element): pass
+
# sets the highlighting language for literal blocks
class highlightlang(nodes.Element): pass
# like emphasis, but doesn't apply further text processors, e.g. smartypants
class literal_emphasis(nodes.emphasis): pass
+# for abbreviations (with explanations)
+class abbreviation(nodes.Inline, nodes.TextElement): pass
+
# glossary
class glossary(nodes.Element): pass
@@ -85,13 +99,18 @@ class start_of_file(nodes.Element): pass
# tabular column specification, used for the LaTeX writer
class tabular_col_spec(nodes.Element): pass
+# only (in/exclusion based on tags)
+class only(nodes.Element): pass
+
# meta directive -- same as docutils' standard meta node, but pickleable
class meta(nodes.Special, nodes.PreBibliographic, nodes.Element): pass
# make them known to docutils. this is needed, because the HTML writer
# will choke at some point if these are not added
-nodes._add_node_class_names("""index desc desc_content desc_signature desc_type
- desc_addname desc_name desc_parameterlist desc_parameter desc_optional
+nodes._add_node_class_names("""index desc desc_content desc_signature
+ desc_type desc_returns desc_addname desc_name desc_parameterlist
+ desc_parameter desc_optional download_reference hlist hlistcol
centered versionmodified seealso productionlist production toctree
pending_xref compact_paragraph highlightlang literal_emphasis
- glossary acks module start_of_file tabular_col_spec meta""".split())
+ abbreviation glossary acks module start_of_file tabular_col_spec
+ meta""".split())
diff --git a/sphinx/application.py b/sphinx/application.py
index 4ac520fe1..9c4670660 100644
--- a/sphinx/application.py
+++ b/sphinx/application.py
@@ -7,13 +7,14 @@
Gracefully adapted from the TextPress system by Armin.
-
- :copyright: 2008 by Georg Brandl, Armin Ronacher.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
import sys
+import types
import posixpath
+from cStringIO import StringIO
from docutils import nodes
from docutils.parsers.rst import directives, roles
@@ -21,43 +22,22 @@ from docutils.parsers.rst import directives, roles
import sphinx
from sphinx.roles import xfileref_role, innernodetypes
from sphinx.config import Config
-from sphinx.builder import builtin_builders, StandaloneHTMLBuilder
-from sphinx.directives import desc_directive, target_directive, additional_xref_types
+from sphinx.errors import SphinxError, SphinxWarning, ExtensionError
+from sphinx.builders import BUILTIN_BUILDERS
+from sphinx.directives import GenericDesc, Target, additional_xref_types
from sphinx.environment import SphinxStandaloneReader
+from sphinx.util.tags import Tags
+from sphinx.util.compat import Directive, directive_dwim
from sphinx.util.console import bold
-class SphinxError(Exception):
- """
- Base class for Sphinx errors that are shown to the user in a nicer
- way than normal exceptions.
- """
- category = 'Sphinx error'
-
-class ExtensionError(SphinxError):
- """Raised if something's wrong with the configuration."""
- category = 'Extension error'
-
- def __init__(self, message, orig_exc=None):
- super(ExtensionError, self).__init__(message)
- self.orig_exc = orig_exc
-
- def __repr__(self):
- if self.orig_exc:
- return '%s(%r, %r)' % (self.__class__.__name__,
- self.message, self.orig_exc)
- return '%s(%r)' % (self.__class__.__name__, self.message)
-
- def __str__(self):
- parent_str = super(ExtensionError, self).__str__()
- if self.orig_exc:
- return '%s (exception: %s)' % (parent_str, self.orig_exc)
- return parent_str
-
+# Directive is either new-style or old-style
+clstypes = (type, types.ClassType)
# List of all known core events. Maps name to arguments description.
events = {
'builder-inited': '',
+ 'env-purge-doc': 'env, docname',
'source-read': 'docname, source text',
'doctree-read': 'the doctree before being pickled',
'missing-reference': 'env, node, contnode',
@@ -69,13 +49,16 @@ events = {
CONFIG_FILENAME = 'conf.py'
+
class Sphinx(object):
def __init__(self, srcdir, confdir, outdir, doctreedir, buildername,
- confoverrides, status, warning=sys.stderr, freshenv=False):
+ confoverrides, status, warning=sys.stderr, freshenv=False,
+ warningiserror=False, tags=None):
self.next_listener_id = 0
+ self._extensions = {}
self._listeners = {}
- self.builderclasses = builtin_builders.copy()
+ self.builderclasses = BUILTIN_BUILDERS.copy()
self.builder = None
self.srcdir = srcdir
@@ -83,14 +66,28 @@ class Sphinx(object):
self.outdir = outdir
self.doctreedir = doctreedir
- self._status = status
- self._warning = warning
+ if status is None:
+ self._status = StringIO()
+ self.quiet = True
+ else:
+ self._status = status
+ self.quiet = False
+
+ if warning is None:
+ self._warning = StringIO()
+ else:
+ self._warning = warning
self._warncount = 0
+ self.warningiserror = warningiserror
self._events = events.copy()
+ # status code for command-line application
+ self.statuscode = 0
+
# read config
- self.config = Config(confdir, CONFIG_FILENAME, confoverrides)
+ self.tags = Tags(tags)
+ self.config = Config(confdir, CONFIG_FILENAME, confoverrides, self.tags)
# load all extension modules
for extension in self.config.extensions:
@@ -108,10 +105,18 @@ class Sphinx(object):
if buildername not in self.builderclasses:
raise SphinxError('Builder name %s not registered' % buildername)
- self.info(bold('Sphinx v%s, building %s' % (sphinx.__version__, buildername)))
+ self.info(bold('Sphinx v%s, building %s' % (sphinx.__released__,
+ buildername)))
builderclass = self.builderclasses[buildername]
+ if isinstance(builderclass, tuple):
+ # builtin builder
+ mod, cls = builderclass
+ builderclass = getattr(
+ __import__('sphinx.builders.' + mod, None, None, [cls]), cls)
self.builder = builderclass(self, freshenv=freshenv)
+ self.builder.tags = self.tags
+ self.builder.tags.add(self.builder.format)
self.emit('builder-inited')
def build(self, all_files, filenames):
@@ -127,28 +132,44 @@ class Sphinx(object):
raise
else:
self.emit('build-finished', None)
+ self.builder.cleanup()
- def warn(self, message):
+ def warn(self, message, location=None, prefix='WARNING: '):
+ warntext = location and '%s: %s%s\n' % (location, prefix, message) or \
+ '%s%s\n' % (prefix, message)
+ if self.warningiserror:
+ raise SphinxWarning(warntext)
self._warncount += 1
- self._warning.write('WARNING: %s\n' % message)
+ try:
+ self._warning.write(warntext)
+ except UnicodeEncodeError:
+ encoding = getattr(self._warning, 'encoding', 'ascii')
+ self._warning.write(warntext.encode(encoding, 'replace'))
def info(self, message='', nonl=False):
- if nonl:
+ try:
self._status.write(message)
- else:
- self._status.write(message + '\n')
+ except UnicodeEncodeError:
+ encoding = getattr(self._status, 'encoding', 'ascii')
+ self._status.write(message.encode(encoding, 'replace'))
+ if not nonl:
+ self._status.write('\n')
self._status.flush()
# general extensibility interface
def setup_extension(self, extension):
- """Import and setup a Sphinx extension module."""
+ """Import and setup a Sphinx extension module. No-op if called twice."""
+ if extension in self._extensions:
+ return
try:
mod = __import__(extension, None, None, ['setup'])
except ImportError, err:
- raise ExtensionError('Could not import extension %s' % extension, err)
+ raise ExtensionError('Could not import extension %s' % extension,
+ err)
if hasattr(mod, 'setup'):
mod.setup(self)
+ self._extensions[extension] = mod
def import_object(self, objname, source=None):
"""Import an object from a 'module.name' string."""
@@ -156,15 +177,18 @@ class Sphinx(object):
module, name = objname.rsplit('.', 1)
except ValueError, err:
raise ExtensionError('Invalid full object name %s' % objname +
- (source and ' (needed for %s)' % source or ''), err)
+ (source and ' (needed for %s)' % source or ''),
+ err)
try:
return getattr(__import__(module, None, None, [name]), name)
except ImportError, err:
raise ExtensionError('Could not import %s' % module +
- (source and ' (needed for %s)' % source or ''), err)
+ (source and ' (needed for %s)' % source or ''),
+ err)
except AttributeError, err:
raise ExtensionError('Could not find %s' % objname +
- (source and ' (needed for %s)' % source or ''), err)
+ (source and ' (needed for %s)' % source or ''),
+ err)
# event interface
@@ -204,16 +228,24 @@ class Sphinx(object):
def add_builder(self, builder):
if not hasattr(builder, 'name'):
- raise ExtensionError('Builder class %s has no "name" attribute' % builder)
+ raise ExtensionError('Builder class %s has no "name" attribute'
+ % builder)
if builder.name in self.builderclasses:
- raise ExtensionError('Builder %r already exists (in module %s)' % (
- builder.name, self.builderclasses[builder.name].__module__))
+ if isinstance(self.builderclasses[builder.name], tuple):
+ raise ExtensionError('Builder %r is a builtin builder' %
+ builder.name)
+ else:
+ raise ExtensionError(
+ 'Builder %r already exists (in module %s)' % (
+ builder.name, self.builderclasses[builder.name].__module__))
self.builderclasses[builder.name] = builder
- def add_config_value(self, name, default, rebuild_env):
+ def add_config_value(self, name, default, rebuild):
if name in self.config.values:
raise ExtensionError('Config value %r already present' % name)
- self.config.values[name] = (default, rebuild_env)
+ if rebuild in (False, True):
+ rebuild = rebuild and 'env' or ''
+ self.config.values[name] = (default, rebuild)
def add_event(self, name):
if name in self._events:
@@ -226,14 +258,14 @@ class Sphinx(object):
try:
visit, depart = val
except ValueError:
- raise ExtensionError('Value for key %r must be a (visit, depart) '
- 'function tuple' % key)
+ raise ExtensionError('Value for key %r must be a '
+ '(visit, depart) function tuple' % key)
if key == 'html':
- from sphinx.htmlwriter import HTMLTranslator as translator
+ from sphinx.writers.html import HTMLTranslator as translator
elif key == 'latex':
- from sphinx.latexwriter import LaTeXTranslator as translator
+ from sphinx.writers.latex import LaTeXTranslator as translator
elif key == 'text':
- from sphinx.textwriter import TextTranslator as translator
+ from sphinx.writers.text import TextTranslator as translator
else:
# ignore invalid keys for compatibility
continue
@@ -241,28 +273,42 @@ class Sphinx(object):
if depart:
setattr(translator, 'depart_'+node.__name__, depart)
- def add_directive(self, name, func, content, arguments, **options):
- func.content = content
- func.arguments = arguments
- func.options = options
- directives.register_directive(name, func)
+ def add_directive(self, name, obj, content=None, arguments=None, **options):
+ if isinstance(obj, clstypes) and issubclass(obj, Directive):
+ if content or arguments or options:
+ raise ExtensionError('when adding directive classes, no '
+ 'additional arguments may be given')
+ directives.register_directive(name, directive_dwim(obj))
+ else:
+ obj.content = content
+ obj.arguments = arguments
+ obj.options = options
+ directives.register_directive(name, obj)
def add_role(self, name, role):
- roles.register_canonical_role(name, role)
+ roles.register_local_role(name, role)
+
+ def add_generic_role(self, name, nodeclass):
+ # don't use roles.register_generic_role because it uses
+ # register_canonical_role
+ role = roles.GenericRole(name, nodeclass)
+ roles.register_local_role(name, role)
def add_description_unit(self, directivename, rolename, indextemplate='',
parse_node=None, ref_nodeclass=None):
- additional_xref_types[directivename] = (rolename, indextemplate, parse_node)
- directives.register_directive(directivename, desc_directive)
- roles.register_canonical_role(rolename, xfileref_role)
+ additional_xref_types[directivename] = (rolename, indextemplate,
+ parse_node)
+ directives.register_directive(directivename,
+ directive_dwim(GenericDesc))
+ roles.register_local_role(rolename, xfileref_role)
if ref_nodeclass is not None:
innernodetypes[rolename] = ref_nodeclass
def add_crossref_type(self, directivename, rolename, indextemplate='',
ref_nodeclass=None):
additional_xref_types[directivename] = (rolename, indextemplate, None)
- directives.register_directive(directivename, target_directive)
- roles.register_canonical_role(rolename, xfileref_role)
+ directives.register_directive(directivename, directive_dwim(Target))
+ roles.register_local_role(rolename, xfileref_role)
if ref_nodeclass is not None:
innernodetypes[rolename] = ref_nodeclass
@@ -270,9 +316,25 @@ class Sphinx(object):
SphinxStandaloneReader.transforms.append(transform)
def add_javascript(self, filename):
+ from sphinx.builders.html import StandaloneHTMLBuilder
StandaloneHTMLBuilder.script_files.append(
posixpath.join('_static', filename))
+ def add_lexer(self, alias, lexer):
+ from sphinx.highlighting import lexers
+ if lexers is None:
+ return
+ lexers[alias] = lexer
+
+ def add_autodocumenter(self, cls):
+ from sphinx.ext import autodoc
+ autodoc.add_documenter(cls)
+ self.add_directive('auto' + cls.objtype, autodoc.AutoDirective)
+
+ def add_autodoc_attrgetter(self, type, getter):
+ from sphinx.ext import autodoc
+ autodoc.AutoDirective._special_attrgetters[type] = getter
+
class TemplateBridge(object):
"""
@@ -280,11 +342,15 @@ class TemplateBridge(object):
that renders templates given a template name and a context.
"""
- def init(self, builder):
+ def init(self, builder, theme=None, dirs=None):
"""
- Called by the builder to initialize the template system. *builder*
- is the builder object; you'll probably want to look at the value of
- ``builder.config.templates_path``.
+ Called by the builder to initialize the template system.
+
+ *builder* is the builder object; you'll probably want to look at the
+ value of ``builder.config.templates_path``.
+
+ *theme* is a :class:`sphinx.theming.Theme` object or None; in the latter
+ case, *dirs* can be list of fixed directories to look for templates.
"""
raise NotImplementedError('must be implemented in subclasses')
@@ -298,7 +364,14 @@ class TemplateBridge(object):
def render(self, template, context):
"""
- Called by the builder to render a *template* with a specified
- context (a Python dictionary).
+ Called by the builder to render a template given as a filename with a
+ specified context (a Python dictionary).
+ """
+ raise NotImplementedError('must be implemented in subclasses')
+
+ def render_string(self, template, context):
+ """
+ Called by the builder to render a template given as a string with a
+ specified context (a Python dictionary).
"""
raise NotImplementedError('must be implemented in subclasses')
diff --git a/sphinx/builder.py b/sphinx/builder.py
index 5448573a7..13c56e18f 100644
--- a/sphinx/builder.py
+++ b/sphinx/builder.py
@@ -3,1268 +3,26 @@
sphinx.builder
~~~~~~~~~~~~~~
- Builder classes for different output formats.
+ .. warning::
- :copyright: 2007-2008 by Georg Brandl, Sebastian Wiesner, Horst Gutmann.
- :license: BSD.
+ This module is only kept for API compatibility; new code should
+ import these classes directly from the sphinx.builders package.
+
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
-import os
-import time
-import codecs
-import shutil
-import gettext
-import cPickle as pickle
-from os import path
-from cgi import escape
-
-from docutils import nodes
-from docutils.io import StringOutput, FileOutput, DocTreeInput
-from docutils.core import publish_parts
-from docutils.utils import new_document
-from docutils.frontend import OptionParser
-from docutils.readers.doctree import Reader as DoctreeReader
-
-from sphinx import addnodes, locale, __version__
-from sphinx.util import ensuredir, relative_uri, SEP, os_path, texescape
-from sphinx.htmlhelp import build_hhx
-from sphinx.htmlwriter import HTMLWriter, HTMLTranslator, SmartyPantsHTMLTranslator
-from sphinx.textwriter import TextWriter
-from sphinx.latexwriter import LaTeXWriter
-from sphinx.environment import BuildEnvironment, NoUri
-from sphinx.highlighting import PygmentsBridge
-from sphinx.util.console import bold, purple, darkgreen
-from sphinx.search import js_index
-
-try:
- import json
-except ImportError:
- try:
- import ssimplejson as json
- except ImportError:
- json = None
-
-# side effect: registers roles and directives
-from sphinx import roles
-from sphinx import directives
-
-ENV_PICKLE_FILENAME = 'environment.pickle'
-LAST_BUILD_FILENAME = 'last_build'
-INVENTORY_FILENAME = 'objects.inv'
-
-
-class Builder(object):
- """
- Builds target formats from the reST sources.
- """
-
- # builder's name, for the -b command line options
- name = ''
-
- def __init__(self, app, env=None, freshenv=False):
- self.srcdir = app.srcdir
- self.confdir = app.confdir
- self.outdir = app.outdir
- self.doctreedir = app.doctreedir
- if not path.isdir(self.doctreedir):
- os.makedirs(self.doctreedir)
-
- self.app = app
- self.warn = app.warn
- self.info = app.info
- self.config = app.config
-
- self.load_i18n()
-
- # images that need to be copied over (source -> dest)
- self.images = {}
-
- # if None, this is set in load_env()
- self.env = env
- self.freshenv = freshenv
-
- self.init()
- self.load_env()
-
- # helper methods
-
- def init(self):
- """Load necessary templates and perform initialization."""
- raise NotImplementedError
-
- def init_templates(self):
- # Call this from init() if you need templates.
- if self.config.template_bridge:
- self.templates = self.app.import_object(
- self.config.template_bridge, 'template_bridge setting')()
- else:
- from sphinx._jinja import BuiltinTemplates
- self.templates = BuiltinTemplates()
- self.templates.init(self)
-
- def get_target_uri(self, docname, typ=None):
- """
- Return the target URI for a document name (typ can be used to qualify
- the link characteristic for individual builders).
- """
- raise NotImplementedError
-
- def get_relative_uri(self, from_, to, typ=None):
- """
- Return a relative URI between two source filenames. May raise environment.NoUri
- if there's no way to return a sensible URI.
- """
- return relative_uri(self.get_target_uri(from_),
- self.get_target_uri(to, typ))
-
- def get_outdated_docs(self):
- """
- Return an iterable of output files that are outdated, or a string describing
- what an update build will build.
- """
- raise NotImplementedError
-
- def status_iterator(self, iterable, summary, colorfunc=darkgreen):
- l = -1
- for item in iterable:
- if l == -1:
- self.info(bold(summary), nonl=1)
- l = 0
- self.info(colorfunc(item) + ' ', nonl=1)
- yield item
- if l == 0:
- self.info()
-
- supported_image_types = []
-
- def post_process_images(self, doctree):
- """
- Pick the best candidate for all image URIs.
- """
- for node in doctree.traverse(nodes.image):
- if '?' in node['candidates']:
- # don't rewrite nonlocal image URIs
- continue
- if '*' not in node['candidates']:
- for imgtype in self.supported_image_types:
- candidate = node['candidates'].get(imgtype, None)
- if candidate:
- break
- else:
- self.warn('%s:%s: no matching candidate for image URI %r' %
- (node.source, getattr(node, 'lineno', ''), node['uri']))
- continue
- node['uri'] = candidate
- else:
- candidate = node['uri']
- if candidate not in self.env.images:
- # non-existing URI; let it alone
- continue
- self.images[candidate] = self.env.images[candidate][1]
-
- # build methods
-
- def load_i18n(self):
- """
- Load translated strings from the configured localedirs if
- enabled in the configuration.
- """
- self.translator = None
- if self.config.language is not None:
- self.info(bold('loading translations [%s]... ' % self.config.language),
- nonl=True)
- locale_dirs = [path.join(path.dirname(__file__), 'locale')] + \
- [path.join(self.srcdir, x) for x in self.config.locale_dirs]
- for dir_ in locale_dirs:
- try:
- trans = gettext.translation('sphinx', localedir=dir_,
- languages=[self.config.language])
- if self.translator is None:
- self.translator = trans
- else:
- self.translator._catalog.update(trans.catalog)
- except Exception:
- # Language couldn't be found in the specified path
- pass
- if self.translator is not None:
- self.info('done')
- else:
- self.info('locale not available')
- if self.translator is None:
- self.translator = gettext.NullTranslations()
- self.translator.install(unicode=True)
- locale.init() # translate common labels
-
- def load_env(self):
- """Set up the build environment."""
- if self.env:
- return
- if not self.freshenv:
- try:
- self.info(bold('loading pickled environment... '), nonl=True)
- self.env = BuildEnvironment.frompickle(self.config,
- path.join(self.doctreedir, ENV_PICKLE_FILENAME))
- self.info('done')
- except Exception, err:
- if type(err) is IOError and err.errno == 2:
- self.info('not found')
- else:
- self.info('failed: %s' % err)
- self.env = BuildEnvironment(self.srcdir, self.doctreedir, self.config)
- self.env.find_files(self.config)
- else:
- self.env = BuildEnvironment(self.srcdir, self.doctreedir, self.config)
- self.env.find_files(self.config)
- self.env.set_warnfunc(self.warn)
-
- def build_all(self):
- """Build all source files."""
- self.build(None, summary='all source files', method='all')
-
- def build_specific(self, filenames):
- """Only rebuild as much as needed for changes in the source_filenames."""
- # bring the filenames to the canonical format, that is,
- # relative to the source directory and without source_suffix.
- dirlen = len(self.srcdir) + 1
- to_write = []
- suffix = self.config.source_suffix
- for filename in filenames:
- filename = path.abspath(filename)[dirlen:]
- if filename.endswith(suffix):
- filename = filename[:-len(suffix)]
- filename = filename.replace(os.path.sep, SEP)
- to_write.append(filename)
- self.build(to_write, method='specific',
- summary='%d source files given on command '
- 'line' % len(to_write))
-
- def build_update(self):
- """Only rebuild files changed or added since last build."""
- to_build = self.get_outdated_docs()
- if isinstance(to_build, str):
- self.build(['__all__'], to_build)
- else:
- to_build = list(to_build)
- self.build(to_build,
- summary='targets for %d source files that are '
- 'out of date' % len(to_build))
-
- def build(self, docnames, summary=None, method='update'):
- if summary:
- self.info(bold('building [%s]: ' % self.name), nonl=1)
- self.info(summary)
-
- updated_docnames = []
- # while reading, collect all warnings from docutils
- warnings = []
- self.env.set_warnfunc(warnings.append)
- self.info(bold('updating environment: '), nonl=1)
- iterator = self.env.update(self.config, self.srcdir, self.doctreedir, self.app)
- # the first item in the iterator is a summary message
- self.info(iterator.next())
- for docname in self.status_iterator(iterator, 'reading sources... ', purple):
- updated_docnames.append(docname)
- # nothing further to do, the environment has already done the reading
- for warning in warnings:
- if warning.strip():
- self.warn(warning)
- self.env.set_warnfunc(self.warn)
-
- if updated_docnames:
- # save the environment
- self.info(bold('pickling environment... '), nonl=True)
- self.env.topickle(path.join(self.doctreedir, ENV_PICKLE_FILENAME))
- self.info('done')
-
- # global actions
- self.info(bold('checking consistency... '), nonl=True)
- self.env.check_consistency()
- self.info('done')
- else:
- if method == 'update' and not docnames:
- self.info(bold('no targets are out of date.'))
- return
-
- # another indirection to support methods which don't build files
- # individually
- self.write(docnames, updated_docnames, method)
-
- # finish (write static files etc.)
- self.finish()
- if self.app._warncount:
- self.info(bold('build succeeded, %s warning%s.' %
- (self.app._warncount,
- self.app._warncount != 1 and 's' or '')))
- else:
- self.info(bold('build succeeded.'))
-
- def write(self, build_docnames, updated_docnames, method='update'):
- if build_docnames is None or build_docnames == ['__all__']:
- # build_all
- build_docnames = self.env.found_docs
- if method == 'update':
- # build updated ones as well
- docnames = set(build_docnames) | set(updated_docnames)
- else:
- docnames = set(build_docnames)
-
- # add all toctree-containing files that may have changed
- for docname in list(docnames):
- for tocdocname in self.env.files_to_rebuild.get(docname, []):
- docnames.add(tocdocname)
- docnames.add(self.config.master_doc)
-
- self.info(bold('preparing documents... '), nonl=True)
- self.prepare_writing(docnames)
- self.info('done')
-
- # write target files
- warnings = []
- self.env.set_warnfunc(warnings.append)
- for docname in self.status_iterator(sorted(docnames),
- 'writing output... ', darkgreen):
- doctree = self.env.get_and_resolve_doctree(docname, self)
- self.write_doc(docname, doctree)
- for warning in warnings:
- if warning.strip():
- self.warn(warning)
- self.env.set_warnfunc(self.warn)
-
- def prepare_writing(self, docnames):
- raise NotImplementedError
-
- def write_doc(self, docname, doctree):
- raise NotImplementedError
-
- def finish(self):
- raise NotImplementedError
-
-
-class StandaloneHTMLBuilder(Builder):
- """
- Builds standalone HTML docs.
- """
- name = 'html'
- copysource = True
- out_suffix = '.html'
- indexer_format = js_index
- supported_image_types = ['image/svg+xml', 'image/png', 'image/gif',
- 'image/jpeg']
- searchindex_filename = 'searchindex.js'
- add_header_links = True
- add_definition_links = True
-
- # This is a class attribute because it is mutated by Sphinx.add_javascript.
- script_files = ['_static/jquery.js', '_static/doctools.js']
-
- def init(self):
- """Load templates."""
- self.init_templates()
- self.init_translator_class()
- if self.config.html_file_suffix:
- self.out_suffix = self.config.html_file_suffix
-
- if self.config.language is not None:
- jsfile = path.join(path.dirname(__file__), 'locale', self.config.language,
- 'LC_MESSAGES', 'sphinx.js')
- if path.isfile(jsfile):
- self.script_files.append('_static/translations.js')
-
- def init_translator_class(self):
- if self.config.html_translator_class:
- self.translator_class = self.app.import_object(
- self.config.html_translator_class, 'html_translator_class setting')
- elif self.config.html_use_smartypants:
- self.translator_class = SmartyPantsHTMLTranslator
- else:
- self.translator_class = HTMLTranslator
-
- def render_partial(self, node):
- """Utility: Render a lone doctree node."""
- doc = new_document('')
- doc.append(node)
- return publish_parts(
- doc,
- source_class=DocTreeInput,
- reader=DoctreeReader(),
- writer=HTMLWriter(self),
- settings_overrides={'output_encoding': 'unicode'}
- )
-
- def prepare_writing(self, docnames):
- from sphinx.search import IndexBuilder
-
- self.indexer = IndexBuilder(self.env)
- self.load_indexer(docnames)
- self.docwriter = HTMLWriter(self)
- self.docsettings = OptionParser(
- defaults=self.env.settings,
- components=(self.docwriter,)).get_default_values()
-
- # format the "last updated on" string, only once is enough since it
- # typically doesn't include the time of day
- lufmt = self.config.html_last_updated_fmt
- if lufmt is not None:
- self.last_updated = time.strftime(lufmt or _('%b %d, %Y'))
- else:
- self.last_updated = None
-
- logo = self.config.html_logo and \
- path.basename(self.config.html_logo) or ''
-
- favicon = self.config.html_favicon and \
- path.basename(self.config.html_favicon) or ''
- if favicon and os.path.splitext(favicon)[1] != '.ico':
- self.warn('html_favicon is not an .ico file')
-
- if not isinstance(self.config.html_use_opensearch, basestring):
- self.warn('html_use_opensearch config value must now be a string')
-
- self.relations = self.env.collect_relations()
-
- rellinks = []
- if self.config.html_use_index:
- rellinks.append(('genindex', _('General Index'), 'I', _('index')))
- if self.config.html_use_modindex and self.env.modules:
- rellinks.append(('modindex', _('Global Module Index'), 'M', _('modules')))
-
- self.globalcontext = dict(
- project = self.config.project,
- release = self.config.release,
- version = self.config.version,
- last_updated = self.last_updated,
- copyright = self.config.copyright,
- master_doc = self.config.master_doc,
- style = self.config.html_style,
- use_opensearch = self.config.html_use_opensearch,
- docstitle = self.config.html_title,
- shorttitle = self.config.html_short_title,
- show_sphinx = self.config.html_show_sphinx,
- file_suffix = self.out_suffix,
- script_files = self.script_files,
- sphinx_version = __version__,
- rellinks = rellinks,
- builder = self.name,
- parents = [],
- logo = logo,
- favicon = favicon,
- )
- self.globalcontext.update(self.config.html_context)
-
- def get_doc_context(self, docname, body, metatags):
- """Collect items for the template context of a page."""
- # find out relations
- prev = next = None
- parents = []
- rellinks = self.globalcontext['rellinks'][:]
- related = self.relations.get(docname)
- titles = self.env.titles
- if related and related[2]:
- try:
- next = {'link': self.get_relative_uri(docname, related[2]),
- 'title': self.render_partial(titles[related[2]])['title']}
- rellinks.append((related[2], next['title'], 'N', _('next')))
- except KeyError:
- next = None
- if related and related[1]:
- try:
- prev = {'link': self.get_relative_uri(docname, related[1]),
- 'title': self.render_partial(titles[related[1]])['title']}
- rellinks.append((related[1], prev['title'], 'P', _('previous')))
- except KeyError:
- # the relation is (somehow) not in the TOC tree, handle that gracefully
- prev = None
- while related and related[0]:
- try:
- parents.append(
- {'link': self.get_relative_uri(docname, related[0]),
- 'title': self.render_partial(titles[related[0]])['title']})
- except KeyError:
- pass
- related = self.relations.get(related[0])
- if parents:
- parents.pop() # remove link to the master file; we have a generic
- # "back to index" link already
- parents.reverse()
-
- # title rendered as HTML
- title = titles.get(docname)
- title = title and self.render_partial(title)['title'] or ''
- # the name for the copied source
- sourcename = self.config.html_copy_source and docname + '.txt' or ''
-
- # metadata for the document
- meta = self.env.metadata.get(docname)
-
- return dict(
- parents = parents,
- prev = prev,
- next = next,
- title = title,
- meta = meta,
- body = body,
- metatags = metatags,
- rellinks = rellinks,
- sourcename = sourcename,
- toc = self.render_partial(self.env.get_toc_for(docname))['fragment'],
- # only display a TOC if there's more than one item to show
- display_toc = (self.env.toc_num_entries[docname] > 1),
- )
-
- def write_doc(self, docname, doctree):
- self.post_process_images(doctree)
- destination = StringOutput(encoding='utf-8')
- doctree.settings = self.docsettings
-
- self.imgpath = relative_uri(self.get_target_uri(docname), '_images')
- self.docwriter.write(doctree, destination)
- self.docwriter.assemble_parts()
- body = self.docwriter.parts['fragment']
- metatags = self.docwriter.clean_meta
-
- ctx = self.get_doc_context(docname, body, metatags)
- self.index_page(docname, doctree, ctx.get('title', ''))
- self.handle_page(docname, ctx, event_arg=doctree)
-
- def finish(self):
- self.info(bold('writing additional files...'), nonl=1)
-
- # the global general index
-
- if self.config.html_use_index:
- # the total count of lines for each index letter, used to distribute
- # the entries into two columns
- genindex = self.env.create_index(self)
- indexcounts = []
- for _, entries in genindex:
- indexcounts.append(sum(1 + len(subitems)
- for _, (_, subitems) in entries))
-
- genindexcontext = dict(
- genindexentries = genindex,
- genindexcounts = indexcounts,
- split_index = self.config.html_split_index,
- )
- self.info(' genindex', nonl=1)
-
- if self.config.html_split_index:
- self.handle_page('genindex', genindexcontext, 'genindex-split.html')
- self.handle_page('genindex-all', genindexcontext, 'genindex.html')
- for (key, entries), count in zip(genindex, indexcounts):
- ctx = {'key': key, 'entries': entries, 'count': count,
- 'genindexentries': genindex}
- self.handle_page('genindex-' + key, ctx, 'genindex-single.html')
- else:
- self.handle_page('genindex', genindexcontext, 'genindex.html')
-
- # the global module index
-
- if self.config.html_use_modindex and self.env.modules:
- # the sorted list of all modules, for the global module index
- modules = sorted(((mn, (self.get_relative_uri('modindex', fn) +
- '#module-' + mn, sy, pl, dep))
- for (mn, (fn, sy, pl, dep)) in
- self.env.modules.iteritems()),
- key=lambda x: x[0].lower())
- # collect all platforms
- platforms = set()
- # sort out collapsable modules
- modindexentries = []
- letters = []
- pmn = ''
- num_toplevels = 0
- num_collapsables = 0
- cg = 0 # collapse group
- fl = '' # first letter
- for mn, (fn, sy, pl, dep) in modules:
- pl = pl and pl.split(', ') or []
- platforms.update(pl)
- if fl != mn[0].lower() and mn[0] != '_':
- # heading
- modindexentries.append(['', False, 0, False,
- mn[0].upper(), '', [], False])
- letters.append(mn[0].upper())
- tn = mn.split('.')[0]
- if tn != mn:
- # submodule
- if pmn == tn:
- # first submodule - make parent collapsable
- modindexentries[-1][1] = True
- num_collapsables += 1
- elif not pmn.startswith(tn):
- # submodule without parent in list, add dummy entry
- cg += 1
- modindexentries.append([tn, True, cg, False, '', '', [], False])
- else:
- num_toplevels += 1
- cg += 1
- modindexentries.append([mn, False, cg, (tn != mn), fn, sy, pl, dep])
- pmn = mn
- fl = mn[0].lower()
- platforms = sorted(platforms)
-
- # apply heuristics when to collapse modindex at page load:
- # only collapse if number of toplevel modules is larger than
- # number of submodules
- collapse = len(modules) - num_toplevels < num_toplevels
-
- modindexcontext = dict(
- modindexentries = modindexentries,
- platforms = platforms,
- letters = letters,
- collapse_modindex = collapse,
- )
- self.info(' modindex', nonl=1)
- self.handle_page('modindex', modindexcontext, 'modindex.html')
-
- # the search page
- if self.name != 'htmlhelp':
- self.info(' search', nonl=1)
- self.handle_page('search', {}, 'search.html')
-
- # additional pages from conf.py
- for pagename, template in self.config.html_additional_pages.items():
- self.info(' '+pagename, nonl=1)
- self.handle_page(pagename, {}, template)
-
- if self.config.html_use_opensearch and self.name != 'htmlhelp':
- self.info(' opensearch', nonl=1)
- fn = path.join(self.outdir, '_static', 'opensearch.xml')
- self.handle_page('opensearch', {}, 'opensearch.xml', outfilename=fn)
-
- self.info()
-
- # copy image files
- if self.images:
- self.info(bold('copying images...'), nonl=True)
- ensuredir(path.join(self.outdir, '_images'))
- for src, dest in self.images.iteritems():
- self.info(' '+src, nonl=1)
- shutil.copyfile(path.join(self.srcdir, src),
- path.join(self.outdir, '_images', dest))
- self.info()
-
- # copy static files
- self.info(bold('copying static files... '), nonl=True)
- ensuredir(path.join(self.outdir, '_static'))
- # first, create pygments style file
- f = open(path.join(self.outdir, '_static', 'pygments.css'), 'w')
- f.write(PygmentsBridge('html', self.config.pygments_style).get_stylesheet())
- f.close()
- # then, copy translations JavaScript file
- if self.config.language is not None:
- jsfile = path.join(path.dirname(__file__), 'locale', self.config.language,
- 'LC_MESSAGES', 'sphinx.js')
- if path.isfile(jsfile):
- shutil.copyfile(jsfile, path.join(self.outdir, '_static',
- 'translations.js'))
- # then, copy over all user-supplied static files
- staticdirnames = [path.join(path.dirname(__file__), 'static')] + \
- [path.join(self.confdir, spath)
- for spath in self.config.html_static_path]
- for staticdirname in staticdirnames:
- for filename in os.listdir(staticdirname):
- if filename.startswith('.'):
- continue
- fullname = path.join(staticdirname, filename)
- targetname = path.join(self.outdir, '_static', filename)
- if path.isfile(fullname):
- shutil.copyfile(fullname, targetname)
- elif path.isdir(fullname):
- if filename in self.config.exclude_dirnames:
- continue
- if path.exists(targetname):
- shutil.rmtree(targetname)
- shutil.copytree(fullname, targetname)
- # last, copy logo file (handled differently)
- if self.config.html_logo:
- logobase = path.basename(self.config.html_logo)
- shutil.copyfile(path.join(self.confdir, self.config.html_logo),
- path.join(self.outdir, '_static', logobase))
- self.info('done')
-
- # dump the search index
- self.handle_finish()
-
- def get_outdated_docs(self):
- if self.templates:
- template_mtime = self.templates.newest_template_mtime()
- else:
- template_mtime = 0
- for docname in self.env.found_docs:
- if docname not in self.env.all_docs:
- yield docname
- continue
- targetname = self.env.doc2path(docname, self.outdir, self.out_suffix)
- try:
- targetmtime = path.getmtime(targetname)
- except Exception:
- targetmtime = 0
- try:
- srcmtime = max(path.getmtime(self.env.doc2path(docname)),
- template_mtime)
- if srcmtime > targetmtime:
- yield docname
- except EnvironmentError:
- # source doesn't exist anymore
- pass
-
- def load_indexer(self, docnames):
- keep = set(self.env.all_docs) - set(docnames)
- try:
- f = open(path.join(self.outdir, self.searchindex_filename), 'rb')
- try:
- self.indexer.load(f, self.indexer_format)
- finally:
- f.close()
- except (IOError, OSError, ValueError):
- if keep:
- self.warn("search index couldn't be loaded, but not all documents "
- "will be built: the index will be incomplete.")
- # delete all entries for files that will be rebuilt
- self.indexer.prune(keep)
-
- def index_page(self, pagename, doctree, title):
- # only index pages with title
- if self.indexer is not None and title:
- self.indexer.feed(pagename, title, doctree)
-
- # --------- these are overwritten by the serialization builder
-
- def get_target_uri(self, docname, typ=None):
- return docname + self.out_suffix
-
- def handle_page(self, pagename, addctx, templatename='page.html',
- outfilename=None, event_arg=None):
- ctx = self.globalcontext.copy()
- # current_page_name is backwards compatibility
- ctx['pagename'] = ctx['current_page_name'] = pagename
-
- def pathto(otheruri, resource=False,
- baseuri=self.get_target_uri(pagename)):
- if not resource:
- otheruri = self.get_target_uri(otheruri)
- return relative_uri(baseuri, otheruri)
- ctx['pathto'] = pathto
- ctx['hasdoc'] = lambda name: name in self.env.all_docs
- ctx['customsidebar'] = self.config.html_sidebars.get(pagename)
- ctx.update(addctx)
-
- self.app.emit('html-page-context', pagename, templatename, ctx, event_arg)
-
- output = self.templates.render(templatename, ctx)
- if not outfilename:
- outfilename = path.join(self.outdir, os_path(pagename) + self.out_suffix)
- ensuredir(path.dirname(outfilename)) # normally different from self.outdir
- try:
- f = codecs.open(outfilename, 'w', 'utf-8')
- try:
- f.write(output)
- finally:
- f.close()
- except (IOError, OSError), err:
- self.warn("Error writing file %s: %s" % (outfilename, err))
- if self.copysource and ctx.get('sourcename'):
- # copy the source file for the "show source" link
- source_name = path.join(self.outdir, '_sources', os_path(ctx['sourcename']))
- ensuredir(path.dirname(source_name))
- shutil.copyfile(self.env.doc2path(pagename), source_name)
-
- def handle_finish(self):
- self.info(bold('dumping search index... '), nonl=True)
- self.indexer.prune(self.env.all_docs)
- f = open(path.join(self.outdir, self.searchindex_filename), 'wb')
- try:
- self.indexer.dump(f, self.indexer_format)
- finally:
- f.close()
- self.info('done')
-
- self.info(bold('dumping object inventory... '), nonl=True)
- f = open(path.join(self.outdir, INVENTORY_FILENAME), 'w')
- try:
- f.write('# Sphinx inventory version 1\n')
- f.write('# Project: %s\n' % self.config.project.encode('utf-8'))
- f.write('# Version: %s\n' % self.config.version)
- for modname, info in self.env.modules.iteritems():
- f.write('%s mod %s\n' % (modname, self.get_target_uri(info[0])))
- for refname, (docname, desctype) in self.env.descrefs.iteritems():
- f.write('%s %s %s\n' % (refname, desctype, self.get_target_uri(docname)))
- finally:
- f.close()
- self.info('done')
-
-
-class SerializingHTMLBuilder(StandaloneHTMLBuilder):
- """
- An abstract builder that serializes the HTML generated.
- """
- #: the serializing implementation to use. Set this to a module that
- #: implements a `dump`, `load`, `dumps` and `loads` functions
- #: (pickle, simplejson etc.)
- implementation = None
-
- #: the filename for the global context file
- globalcontext_filename = None
-
- supported_image_types = ('image/svg+xml', 'image/png', 'image/gif',
- 'image/jpeg')
-
- def init(self):
- self.init_translator_class()
- self.templates = None # no template bridge necessary
-
- def get_target_uri(self, docname, typ=None):
- if docname == 'index':
- return ''
- if docname.endswith(SEP + 'index'):
- return docname[:-5] # up to sep
- return docname + SEP
-
- def handle_page(self, pagename, ctx, templatename='page.html',
- outfilename=None, event_arg=None):
- ctx['current_page_name'] = pagename
- sidebarfile = self.config.html_sidebars.get(pagename)
- if sidebarfile:
- ctx['customsidebar'] = sidebarfile
-
- if not outfilename:
- outfilename = path.join(self.outdir, os_path(pagename) + self.out_suffix)
-
- self.app.emit('html-page-context', pagename, templatename, ctx, event_arg)
-
- ensuredir(path.dirname(outfilename))
- f = open(outfilename, 'wb')
- try:
- self.implementation.dump(ctx, f, 2)
- finally:
- f.close()
-
- # if there is a source file, copy the source file for the
- # "show source" link
- if ctx.get('sourcename'):
- source_name = path.join(self.outdir, '_sources',
- os_path(ctx['sourcename']))
- ensuredir(path.dirname(source_name))
- shutil.copyfile(self.env.doc2path(pagename), source_name)
-
- def handle_finish(self):
- # dump the global context
- outfilename = path.join(self.outdir, self.globalcontext_filename)
- f = open(outfilename, 'wb')
- try:
- self.implementation.dump(self.globalcontext, f, 2)
- finally:
- f.close()
-
- # super here to dump the search index
- StandaloneHTMLBuilder.handle_finish(self)
-
- # copy the environment file from the doctree dir to the output dir
- # as needed by the web app
- shutil.copyfile(path.join(self.doctreedir, ENV_PICKLE_FILENAME),
- path.join(self.outdir, ENV_PICKLE_FILENAME))
-
- # touch 'last build' file, used by the web application to determine
- # when to reload its environment and clear the cache
- open(path.join(self.outdir, LAST_BUILD_FILENAME), 'w').close()
-
-
-class PickleHTMLBuilder(SerializingHTMLBuilder):
- """
- A Builder that dumps the generated HTML into pickle files.
- """
- implementation = pickle
- indexer_format = pickle
- name = 'pickle'
- out_suffix = '.fpickle'
- globalcontext_filename = 'globalcontext.pickle'
- searchindex_filename = 'searchindex.pickle'
-
-
-class JSONHTMLBuilder(SerializingHTMLBuilder):
- """
- A builder that dumps the generated HTML into JSON files.
- """
- implementation = json
- indexer_format = json
- name = 'json'
- out_suffix = '.fjson'
- globalcontext_filename = 'globalcontext.json'
- searchindex_filename = 'searchindex.json'
-
- def init(self):
- if json is None:
- from sphinx.application import SphinxError
- raise SphinxError('The module simplejson (or json in Python >= 2.6) '
- 'is not available. The JSONHTMLBuilder builder '
- 'will not work.')
- SerializingHTMLBuilder.init(self)
-
-
-class HTMLHelpBuilder(StandaloneHTMLBuilder):
- """
- Builder that also outputs Windows HTML help project, contents and index files.
- Adapted from the original Doc/tools/prechm.py.
- """
- name = 'htmlhelp'
-
- # don't copy the reST source
- copysource = False
- supported_image_types = ['image/png', 'image/gif', 'image/jpeg']
-
- # don't add links
- add_header_links = False
- add_definition_links = False
-
- def init(self):
- StandaloneHTMLBuilder.init(self)
- # the output files for HTML help must be .html only
- self.out_suffix = '.html'
-
- def handle_finish(self):
- build_hhx(self, self.outdir, self.config.htmlhelp_basename)
-
-
-class LaTeXBuilder(Builder):
- """
- Builds LaTeX output to create PDF.
- """
- name = 'latex'
- supported_image_types = ['application/pdf', 'image/png', 'image/gif',
- 'image/jpeg']
-
- def init(self):
- self.docnames = []
- self.document_data = []
- texescape.init()
-
- def get_outdated_docs(self):
- return 'all documents' # for now
-
- def get_target_uri(self, docname, typ=None):
- if typ == 'token':
- # token references are always inside production lists and must be
- # replaced by \token{} in LaTeX
- return '@token'
- if docname not in self.docnames:
- raise NoUri
- else:
- return ''
-
- def init_document_data(self):
- preliminary_document_data = map(list, self.config.latex_documents)
- if not preliminary_document_data:
- self.warn('No "latex_documents" config value found; no documents '
- 'will be written.')
- return
- # assign subdirs to titles
- self.titles = []
- for entry in preliminary_document_data:
- docname = entry[0]
- if docname not in self.env.all_docs:
- self.warn('"latex_documents" config value references unknown '
- 'document %s' % docname)
- continue
- self.document_data.append(entry)
- if docname.endswith(SEP+'index'):
- docname = docname[:-5]
- self.titles.append((docname, entry[2]))
-
- def write(self, *ignored):
- # first, assemble the "appendix" docs that are in every PDF
- appendices = []
- for fname in self.config.latex_appendices:
- appendices.append(self.env.get_doctree(fname))
-
- docwriter = LaTeXWriter(self)
- docsettings = OptionParser(
- defaults=self.env.settings,
- components=(docwriter,)).get_default_values()
-
- self.init_document_data()
-
- for entry in self.document_data:
- docname, targetname, title, author, docclass = entry[:5]
- toctree_only = False
- if len(entry) > 5:
- toctree_only = entry[5]
- destination = FileOutput(
- destination_path=path.join(self.outdir, targetname),
- encoding='utf-8')
- self.info("processing " + targetname + "... ", nonl=1)
- doctree = self.assemble_doctree(docname, toctree_only,
- appendices=(docclass == 'manual') and appendices or [])
- self.post_process_images(doctree)
- self.info("writing... ", nonl=1)
- doctree.settings = docsettings
- doctree.settings.author = author
- doctree.settings.title = title
- doctree.settings.docname = docname
- doctree.settings.docclass = docclass
- docwriter.write(doctree, destination)
- self.info("done")
-
- def assemble_doctree(self, indexfile, toctree_only, appendices):
- self.docnames = set([indexfile] + appendices)
- self.info(darkgreen(indexfile) + " ", nonl=1)
- def process_tree(docname, tree):
- tree = tree.deepcopy()
- for toctreenode in tree.traverse(addnodes.toctree):
- newnodes = []
- includefiles = map(str, toctreenode['includefiles'])
- for includefile in includefiles:
- try:
- self.info(darkgreen(includefile) + " ", nonl=1)
- subtree = process_tree(includefile,
- self.env.get_doctree(includefile))
- self.docnames.add(includefile)
- except Exception:
- self.warn('%s: toctree contains ref to nonexisting file %r' %
- (docname, includefile))
- else:
- sof = addnodes.start_of_file()
- sof.children = subtree.children
- newnodes.append(sof)
- toctreenode.parent.replace(toctreenode, newnodes)
- return tree
- tree = self.env.get_doctree(indexfile)
- if toctree_only:
- # extract toctree nodes from the tree and put them in a fresh document
- new_tree = new_document('')
- new_sect = nodes.section()
- new_sect += nodes.title('', '')
- new_tree += new_sect
- for node in tree.traverse(addnodes.toctree):
- new_sect += node
- tree = new_tree
- largetree = process_tree(indexfile, tree)
- largetree.extend(appendices)
- self.info()
- self.info("resolving references...")
- self.env.resolve_references(largetree, indexfile, self)
- # resolve :ref:s to distant tex files -- we can't add a cross-reference,
- # but append the document name
- for pendingnode in largetree.traverse(addnodes.pending_xref):
- docname = pendingnode['refdocname']
- sectname = pendingnode['refsectname']
- newnodes = [nodes.emphasis(sectname, sectname)]
- for subdir, title in self.titles:
- if docname.startswith(subdir):
- newnodes.append(nodes.Text(_(' (in '), _(' (in ')))
- newnodes.append(nodes.emphasis(title, title))
- newnodes.append(nodes.Text(')', ')'))
- break
- else:
- pass
- pendingnode.replace_self(newnodes)
- return largetree
-
- def finish(self):
- # copy image files
- if self.images:
- self.info(bold('copying images...'), nonl=1)
- for src, dest in self.images.iteritems():
- self.info(' '+src, nonl=1)
- shutil.copyfile(path.join(self.srcdir, src),
- path.join(self.outdir, dest))
- self.info()
-
- # the logo is handled differently
- if self.config.latex_logo:
- logobase = path.basename(self.config.latex_logo)
- shutil.copyfile(path.join(self.confdir, self.config.latex_logo),
- path.join(self.outdir, logobase))
-
- self.info(bold('copying TeX support files... '), nonl=True)
- staticdirname = path.join(path.dirname(__file__), 'texinputs')
- for filename in os.listdir(staticdirname):
- if not filename.startswith('.'):
- shutil.copyfile(path.join(staticdirname, filename),
- path.join(self.outdir, filename))
- self.info('done')
-
-
-class ChangesBuilder(Builder):
- """
- Write a summary with all versionadded/changed directives.
- """
- name = 'changes'
-
- def init(self):
- self.init_templates()
-
- def get_outdated_docs(self):
- return self.outdir
-
- typemap = {
- 'versionadded': 'added',
- 'versionchanged': 'changed',
- 'deprecated': 'deprecated',
- }
-
- def write(self, *ignored):
- version = self.config.version
- libchanges = {}
- apichanges = []
- otherchanges = {}
- if version not in self.env.versionchanges:
- self.info(bold('no changes in this version.'))
- return
- self.info(bold('writing summary file...'))
- for type, docname, lineno, module, descname, content in \
- self.env.versionchanges[version]:
- ttext = self.typemap[type]
- context = content.replace('\n', ' ')
- if descname and docname.startswith('c-api'):
- if not descname:
- continue
- if context:
- entry = '%s: %s: %s' % (descname, ttext, context)
- else:
- entry = '%s: %s.' % (descname, ttext)
- apichanges.append((entry, docname, lineno))
- elif descname or module:
- if not module:
- module = _('Builtins')
- if not descname:
- descname = _('Module level')
- if context:
- entry = '%s: %s: %s' % (descname, ttext, context)
- else:
- entry = '%s: %s.' % (descname, ttext)
- libchanges.setdefault(module, []).append((entry, docname, lineno))
- else:
- if not context:
- continue
- entry = '%s: %s' % (ttext.capitalize(), context)
- title = self.env.titles[docname].astext()
- otherchanges.setdefault((docname, title), []).append(
- (entry, docname, lineno))
-
- ctx = {
- 'project': self.config.project,
- 'version': version,
- 'docstitle': self.config.html_title,
- 'shorttitle': self.config.html_short_title,
- 'libchanges': sorted(libchanges.iteritems()),
- 'apichanges': sorted(apichanges),
- 'otherchanges': sorted(otherchanges.iteritems()),
- 'show_sphinx': self.config.html_show_sphinx,
- }
- f = open(path.join(self.outdir, 'index.html'), 'w')
- try:
- f.write(self.templates.render('changes/frameset.html', ctx))
- finally:
- f.close()
- f = open(path.join(self.outdir, 'changes.html'), 'w')
- try:
- f.write(self.templates.render('changes/versionchanges.html', ctx))
- finally:
- f.close()
-
- hltext = ['.. versionadded:: %s' % version,
- '.. versionchanged:: %s' % version,
- '.. deprecated:: %s' % version]
-
- def hl(no, line):
- line = '' % no + escape(line)
- for x in hltext:
- if x in line:
- line = '%s' % line
- break
- return line
-
- self.info(bold('copying source files...'))
- for docname in self.env.all_docs:
- f = open(self.env.doc2path(docname))
- lines = f.readlines()
- targetfn = path.join(self.outdir, 'rst', os_path(docname)) + '.html'
- ensuredir(path.dirname(targetfn))
- f = codecs.open(targetfn, 'w', 'utf8')
- try:
- text = ''.join(hl(i+1, line) for (i, line) in enumerate(lines))
- ctx = {'filename': self.env.doc2path(docname, None), 'text': text}
- f.write(self.templates.render('changes/rstsource.html', ctx))
- finally:
- f.close()
- shutil.copyfile(path.join(path.dirname(__file__), 'static', 'default.css'),
- path.join(self.outdir, 'default.css'))
-
- def hl(self, text, version):
- text = escape(text)
- for directive in ['versionchanged', 'versionadded', 'deprecated']:
- text = text.replace('.. %s:: %s' % (directive, version),
- '.. %s:: %s' % (directive, version))
- return text
-
- def finish(self):
- pass
-
-
-class TextBuilder(Builder):
- name = 'text'
- out_suffix = '.txt'
-
- def init(self):
- pass
-
- def get_outdated_docs(self):
- for docname in self.env.found_docs:
- if docname not in self.env.all_docs:
- yield docname
- continue
- targetname = self.env.doc2path(docname, self.outdir, self.out_suffix)
- try:
- targetmtime = path.getmtime(targetname)
- except Exception:
- targetmtime = 0
- try:
- srcmtime = path.getmtime(self.env.doc2path(docname))
- if srcmtime > targetmtime:
- yield docname
- except EnvironmentError:
- # source doesn't exist anymore
- pass
-
- def get_target_uri(self, docname, typ=None):
- return ''
-
- def prepare_writing(self, docnames):
- self.writer = TextWriter(self)
-
- def write_doc(self, docname, doctree):
- destination = StringOutput(encoding='utf-8')
- self.writer.write(doctree, destination)
- outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix)
- ensuredir(path.dirname(outfilename)) # normally different from self.outdir
- try:
- f = codecs.open(outfilename, 'w', 'utf-8')
- try:
- f.write(self.writer.output)
- finally:
- f.close()
- except (IOError, OSError), err:
- self.warn("Error writing file %s: %s" % (outfilename, err))
-
- def finish(self):
- pass
-
-
-# compatibility alias
-WebHTMLBuilder = PickleHTMLBuilder
-
-
-from sphinx.linkcheck import CheckExternalLinksBuilder
-
-builtin_builders = {
- 'html': StandaloneHTMLBuilder,
- 'pickle': PickleHTMLBuilder,
- 'json': JSONHTMLBuilder,
- 'web': PickleHTMLBuilder,
- 'htmlhelp': HTMLHelpBuilder,
- 'latex': LaTeXBuilder,
- 'text': TextBuilder,
- 'changes': ChangesBuilder,
- 'linkcheck': CheckExternalLinksBuilder,
-}
+import warnings
+
+from sphinx.builders import Builder
+from sphinx.builders.text import TextBuilder
+from sphinx.builders.html import StandaloneHTMLBuilder, WebHTMLBuilder, \
+ PickleHTMLBuilder, JSONHTMLBuilder
+from sphinx.builders.latex import LaTeXBuilder
+from sphinx.builders.changes import ChangesBuilder
+from sphinx.builders.htmlhelp import HTMLHelpBuilder
+from sphinx.builders.linkcheck import CheckExternalLinksBuilder
+
+warnings.warn('The sphinx.builder module is deprecated; please import '
+ 'builders from the respective sphinx.builders submodules.',
+ DeprecationWarning, stacklevel=2)
diff --git a/sphinx/builders/__init__.py b/sphinx/builders/__init__.py
new file mode 100644
index 000000000..41f63de4d
--- /dev/null
+++ b/sphinx/builders/__init__.py
@@ -0,0 +1,384 @@
+# -*- coding: utf-8 -*-
+"""
+ sphinx.builders
+ ~~~~~~~~~~~~~~~
+
+ Builder superclass for all builders.
+
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+import os
+import gettext
+from os import path
+
+from docutils import nodes
+
+from sphinx import package_dir, locale
+from sphinx.util import SEP, relative_uri
+from sphinx.environment import BuildEnvironment
+from sphinx.util.console import bold, purple, darkgreen, term_width_line
+
+# side effect: registers roles and directives
+from sphinx import roles
+from sphinx import directives
+
+
+ENV_PICKLE_FILENAME = 'environment.pickle'
+
+
+class Builder(object):
+ """
+ Builds target formats from the reST sources.
+ """
+
+ # builder's name, for the -b command line options
+ name = ''
+ # builder's output format, or '' if no document output is produced
+ format = ''
+
+ def __init__(self, app, env=None, freshenv=False):
+ self.srcdir = app.srcdir
+ self.confdir = app.confdir
+ self.outdir = app.outdir
+ self.doctreedir = app.doctreedir
+ if not path.isdir(self.doctreedir):
+ os.makedirs(self.doctreedir)
+
+ self.app = app
+ self.warn = app.warn
+ self.info = app.info
+ self.config = app.config
+
+ self.load_i18n()
+
+ # images that need to be copied over (source -> dest)
+ self.images = {}
+
+ # if None, this is set in load_env()
+ self.env = env
+ self.freshenv = freshenv
+
+ self.init()
+ self.load_env()
+
+ # helper methods
+
+ def init(self):
+ """
+ Load necessary templates and perform initialization. The default
+ implementation does nothing.
+ """
+ pass
+
+ def create_template_bridge(self):
+ """
+ Return the template bridge configured.
+ """
+ if self.config.template_bridge:
+ self.templates = self.app.import_object(
+ self.config.template_bridge, 'template_bridge setting')()
+ else:
+ from sphinx.jinja2glue import BuiltinTemplateLoader
+ self.templates = BuiltinTemplateLoader()
+
+ def get_target_uri(self, docname, typ=None):
+ """
+ Return the target URI for a document name (*typ* can be used to qualify
+ the link characteristic for individual builders).
+ """
+ raise NotImplementedError
+
+ def get_relative_uri(self, from_, to, typ=None):
+ """
+ Return a relative URI between two source filenames. May raise
+ environment.NoUri if there's no way to return a sensible URI.
+ """
+ return relative_uri(self.get_target_uri(from_),
+ self.get_target_uri(to, typ))
+
+ def get_outdated_docs(self):
+ """
+ Return an iterable of output files that are outdated, or a string
+ describing what an update build will build.
+
+ If the builder does not output individual files corresponding to
+ source files, return a string here. If it does, return an iterable
+ of those files that need to be written.
+ """
+ raise NotImplementedError
+
+ def old_status_iterator(self, iterable, summary, colorfunc=darkgreen):
+ l = 0
+ for item in iterable:
+ if l == 0:
+ self.info(bold(summary), nonl=1)
+ l = 1
+ self.info(colorfunc(item) + ' ', nonl=1)
+ yield item
+ if l == 1:
+ self.info()
+
+ # new version with progress info
+ def status_iterator(self, iterable, summary, colorfunc=darkgreen, length=0):
+ if length == 0:
+ for item in self.old_status_iterator(iterable, summary, colorfunc):
+ yield item
+ return
+ l = 0
+ summary = bold(summary)
+ for item in iterable:
+ l += 1
+ self.info(term_width_line('%s[%3d%%] %s' %
+ (summary, 100*l/length,
+ colorfunc(item))), nonl=1)
+ yield item
+ if l > 0:
+ self.info()
+
+ supported_image_types = []
+
+ def post_process_images(self, doctree):
+ """
+ Pick the best candidate for all image URIs.
+ """
+ for node in doctree.traverse(nodes.image):
+ if '?' in node['candidates']:
+ # don't rewrite nonlocal image URIs
+ continue
+ if '*' not in node['candidates']:
+ for imgtype in self.supported_image_types:
+ candidate = node['candidates'].get(imgtype, None)
+ if candidate:
+ break
+ else:
+ self.warn(
+ 'no matching candidate for image URI %r' % node['uri'],
+ '%s:%s' % (node.source, getattr(node, 'line', '')))
+ continue
+ node['uri'] = candidate
+ else:
+ candidate = node['uri']
+ if candidate not in self.env.images:
+ # non-existing URI; let it alone
+ continue
+ self.images[candidate] = self.env.images[candidate][1]
+
+ # build methods
+
+ def load_i18n(self):
+ """
+ Load translated strings from the configured localedirs if
+ enabled in the configuration.
+ """
+ self.translator = None
+ if self.config.language is not None:
+ self.info(bold('loading translations [%s]... ' %
+ self.config.language), nonl=True)
+ locale_dirs = [path.join(package_dir, 'locale')] + \
+ [path.join(self.srcdir, x) for x in self.config.locale_dirs]
+ for dir_ in locale_dirs:
+ try:
+ trans = gettext.translation('sphinx', localedir=dir_,
+ languages=[self.config.language])
+ if self.translator is None:
+ self.translator = trans
+ else:
+ self.translator._catalog.update(trans.catalog)
+ except Exception:
+ # Language couldn't be found in the specified path
+ pass
+ if self.translator is not None:
+ self.info('done')
+ else:
+ self.info('locale not available')
+ if self.translator is None:
+ self.translator = gettext.NullTranslations()
+ self.translator.install(unicode=True)
+ locale.init() # translate common labels
+
+ def load_env(self):
+ """Set up the build environment."""
+ if self.env:
+ return
+ if not self.freshenv:
+ try:
+ self.info(bold('loading pickled environment... '), nonl=True)
+ self.env = BuildEnvironment.frompickle(self.config,
+ path.join(self.doctreedir, ENV_PICKLE_FILENAME))
+ self.info('done')
+ except Exception, err:
+ if type(err) is IOError and err.errno == 2:
+ self.info('not found')
+ else:
+ self.info('failed: %s' % err)
+ self.env = BuildEnvironment(self.srcdir, self.doctreedir,
+ self.config)
+ self.env.find_files(self.config)
+ else:
+ self.env = BuildEnvironment(self.srcdir, self.doctreedir,
+ self.config)
+ self.env.find_files(self.config)
+ self.env.set_warnfunc(self.warn)
+
+ def build_all(self):
+ """Build all source files."""
+ self.build(None, summary='all source files', method='all')
+
+ def build_specific(self, filenames):
+ """Only rebuild as much as needed for changes in the *filenames*."""
+ # bring the filenames to the canonical format, that is,
+ # relative to the source directory and without source_suffix.
+ dirlen = len(self.srcdir) + 1
+ to_write = []
+ suffix = self.config.source_suffix
+ for filename in filenames:
+ filename = path.abspath(filename)[dirlen:]
+ if filename.endswith(suffix):
+ filename = filename[:-len(suffix)]
+ filename = filename.replace(os.path.sep, SEP)
+ to_write.append(filename)
+ self.build(to_write, method='specific',
+ summary='%d source files given on command '
+ 'line' % len(to_write))
+
+ def build_update(self):
+ """Only rebuild what was changed or added since last build."""
+ to_build = self.get_outdated_docs()
+ if isinstance(to_build, str):
+ self.build(['__all__'], to_build)
+ else:
+ to_build = list(to_build)
+ self.build(to_build,
+ summary='targets for %d source files that are '
+ 'out of date' % len(to_build))
+
+ def build(self, docnames, summary=None, method='update'):
+ """
+ Main build method. First updates the environment, and then
+ calls :meth:`write`.
+ """
+ if summary:
+ self.info(bold('building [%s]: ' % self.name), nonl=1)
+ self.info(summary)
+
+ updated_docnames = set()
+ # while reading, collect all warnings from docutils
+ warnings = []
+ self.env.set_warnfunc(lambda *args: warnings.append(args))
+ self.info(bold('updating environment: '), nonl=1)
+ msg, length, iterator = self.env.update(self.config, self.srcdir,
+ self.doctreedir, self.app)
+ self.info(msg)
+ for docname in self.status_iterator(iterator, 'reading sources... ',
+ purple, length):
+ updated_docnames.add(docname)
+ # nothing further to do, the environment has already
+ # done the reading
+ for warning in warnings:
+ self.warn(*warning)
+ self.env.set_warnfunc(self.warn)
+
+ doccount = len(updated_docnames)
+ self.info(bold('looking for now-outdated files... '), nonl=1)
+ for docname in self.env.check_dependents(updated_docnames):
+ updated_docnames.add(docname)
+ outdated = len(updated_docnames) - doccount
+ if outdated:
+ self.info('%d found' % outdated)
+ else:
+ self.info('none found')
+
+ if updated_docnames:
+ # save the environment
+ self.info(bold('pickling environment... '), nonl=True)
+ self.env.topickle(path.join(self.doctreedir, ENV_PICKLE_FILENAME))
+ self.info('done')
+
+ # global actions
+ self.info(bold('checking consistency... '), nonl=True)
+ self.env.check_consistency()
+ self.info('done')
+ else:
+ if method == 'update' and not docnames:
+ self.info(bold('no targets are out of date.'))
+ return
+
+ # another indirection to support builders that don't build
+ # files individually
+ self.write(docnames, list(updated_docnames), method)
+
+ # finish (write static files etc.)
+ self.finish()
+ status = (self.app.statuscode == 0 and 'succeeded'
+ or 'finished with problems')
+ if self.app._warncount:
+ self.info(bold('build %s, %s warning%s.' %
+ (status, self.app._warncount,
+ self.app._warncount != 1 and 's' or '')))
+ else:
+ self.info(bold('build %s.' % status))
+
+ def write(self, build_docnames, updated_docnames, method='update'):
+ if build_docnames is None or build_docnames == ['__all__']:
+ # build_all
+ build_docnames = self.env.found_docs
+ if method == 'update':
+ # build updated ones as well
+ docnames = set(build_docnames) | set(updated_docnames)
+ else:
+ docnames = set(build_docnames)
+
+ # add all toctree-containing files that may have changed
+ for docname in list(docnames):
+ for tocdocname in self.env.files_to_rebuild.get(docname, []):
+ docnames.add(tocdocname)
+ docnames.add(self.config.master_doc)
+
+ self.info(bold('preparing documents... '), nonl=True)
+ self.prepare_writing(docnames)
+ self.info('done')
+
+ # write target files
+ warnings = []
+ self.env.set_warnfunc(lambda *args: warnings.append(args))
+ for docname in self.status_iterator(
+ sorted(docnames), 'writing output... ', darkgreen, len(docnames)):
+ doctree = self.env.get_and_resolve_doctree(docname, self)
+ self.write_doc(docname, doctree)
+ for warning in warnings:
+ self.warn(*warning)
+ self.env.set_warnfunc(self.warn)
+
+ def prepare_writing(self, docnames):
+ raise NotImplementedError
+
+ def write_doc(self, docname, doctree):
+ raise NotImplementedError
+
+ def finish(self):
+ """
+ Finish the building process. The default implementation does nothing.
+ """
+ pass
+
+ def cleanup(self):
+ """
+ Cleanup any resources. The default implementation does nothing.
+ """
+
+
+BUILTIN_BUILDERS = {
+ 'html': ('html', 'StandaloneHTMLBuilder'),
+ 'dirhtml': ('html', 'DirectoryHTMLBuilder'),
+ 'pickle': ('html', 'PickleHTMLBuilder'),
+ 'json': ('html', 'JSONHTMLBuilder'),
+ 'web': ('html', 'PickleHTMLBuilder'),
+ 'htmlhelp': ('htmlhelp', 'HTMLHelpBuilder'),
+ 'qthelp': ('qthelp', 'QtHelpBuilder'),
+ 'latex': ('latex', 'LaTeXBuilder'),
+ 'text': ('text', 'TextBuilder'),
+ 'changes': ('changes', 'ChangesBuilder'),
+ 'linkcheck': ('linkcheck', 'CheckExternalLinksBuilder'),
+}
diff --git a/sphinx/builders/changes.py b/sphinx/builders/changes.py
new file mode 100644
index 000000000..e07b06d88
--- /dev/null
+++ b/sphinx/builders/changes.py
@@ -0,0 +1,156 @@
+# -*- coding: utf-8 -*-
+"""
+ sphinx.builders.changes
+ ~~~~~~~~~~~~~~~~~~~~~~~
+
+ Changelog builder.
+
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+import codecs
+import shutil
+from os import path
+from cgi import escape
+
+from sphinx import package_dir
+from sphinx.util import ensuredir, os_path, copy_static_entry
+from sphinx.theming import Theme
+from sphinx.builders import Builder
+from sphinx.util.console import bold
+
+
+class ChangesBuilder(Builder):
+ """
+ Write a summary with all versionadded/changed directives.
+ """
+ name = 'changes'
+
+ def init(self):
+ self.create_template_bridge()
+ Theme.init_themes(self)
+ self.theme = Theme('default')
+ self.templates.init(self, self.theme)
+
+ def get_outdated_docs(self):
+ return self.outdir
+
+ typemap = {
+ 'versionadded': 'added',
+ 'versionchanged': 'changed',
+ 'deprecated': 'deprecated',
+ }
+
+ def write(self, *ignored):
+ version = self.config.version
+ libchanges = {}
+ apichanges = []
+ otherchanges = {}
+ if version not in self.env.versionchanges:
+ self.info(bold('no changes in version %s.' % version))
+ return
+ self.info(bold('writing summary file...'))
+ for type, docname, lineno, module, descname, content in \
+ self.env.versionchanges[version]:
+ if isinstance(descname, tuple):
+ descname = descname[0]
+ ttext = self.typemap[type]
+ context = content.replace('\n', ' ')
+ if descname and docname.startswith('c-api'):
+ if not descname:
+ continue
+ if context:
+ entry = '%s: %s: %s' % (descname, ttext,
+ context)
+ else:
+ entry = '%s: %s.' % (descname, ttext)
+ apichanges.append((entry, docname, lineno))
+ elif descname or module:
+ if not module:
+ module = _('Builtins')
+ if not descname:
+ descname = _('Module level')
+ if context:
+ entry = '%s: %s: %s' % (descname, ttext,
+ context)
+ else:
+ entry = '%s: %s.' % (descname, ttext)
+ libchanges.setdefault(module, []).append((entry, docname,
+ lineno))
+ else:
+ if not context:
+ continue
+ entry = '%s: %s' % (ttext.capitalize(), context)
+ title = self.env.titles[docname].astext()
+ otherchanges.setdefault((docname, title), []).append(
+ (entry, docname, lineno))
+
+ ctx = {
+ 'project': self.config.project,
+ 'version': version,
+ 'docstitle': self.config.html_title,
+ 'shorttitle': self.config.html_short_title,
+ 'libchanges': sorted(libchanges.iteritems()),
+ 'apichanges': sorted(apichanges),
+ 'otherchanges': sorted(otherchanges.iteritems()),
+ 'show_sphinx': self.config.html_show_sphinx,
+ }
+ f = codecs.open(path.join(self.outdir, 'index.html'), 'w', 'utf8')
+ try:
+ f.write(self.templates.render('changes/frameset.html', ctx))
+ finally:
+ f.close()
+ f = codecs.open(path.join(self.outdir, 'changes.html'), 'w', 'utf8')
+ try:
+ f.write(self.templates.render('changes/versionchanges.html', ctx))
+ finally:
+ f.close()
+
+ hltext = ['.. versionadded:: %s' % version,
+ '.. versionchanged:: %s' % version,
+ '.. deprecated:: %s' % version]
+
+ def hl(no, line):
+ line = '' % no + escape(line)
+ for x in hltext:
+ if x in line:
+ line = '%s' % line
+ break
+ return line
+
+ self.info(bold('copying source files...'))
+ for docname in self.env.all_docs:
+ f = codecs.open(self.env.doc2path(docname), 'r', 'latin1')
+ lines = f.readlines()
+ targetfn = path.join(self.outdir, 'rst', os_path(docname)) + '.html'
+ ensuredir(path.dirname(targetfn))
+ f = codecs.open(targetfn, 'w', 'latin1')
+ try:
+ text = ''.join(hl(i+1, line) for (i, line) in enumerate(lines))
+ ctx = {
+ 'filename': self.env.doc2path(docname, None),
+ 'text': text
+ }
+ f.write(self.templates.render('changes/rstsource.html', ctx))
+ finally:
+ f.close()
+ themectx = dict(('theme_' + key, val) for (key, val) in
+ self.theme.get_options({}).iteritems())
+ copy_static_entry(path.join(package_dir, 'themes', 'default',
+ 'static', 'default.css_t'),
+ path.join(self.outdir, 'default.css_t'),
+ self, themectx)
+ copy_static_entry(path.join(package_dir, 'themes', 'basic',
+ 'static', 'basic.css'),
+ path.join(self.outdir, 'basic.css'), self)
+
+ def hl(self, text, version):
+ text = escape(text)
+ for directive in ['versionchanged', 'versionadded', 'deprecated']:
+ text = text.replace('.. %s:: %s' % (directive, version),
+ '.. %s:: %s' % (directive, version))
+ return text
+
+ def finish(self):
+ pass
diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py
new file mode 100644
index 000000000..365cf5f96
--- /dev/null
+++ b/sphinx/builders/html.py
@@ -0,0 +1,836 @@
+# -*- coding: utf-8 -*-
+"""
+ sphinx.builders.html
+ ~~~~~~~~~~~~~~~~~~~~
+
+ Several HTML builders.
+
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+import os
+import codecs
+import shutil
+import posixpath
+import cPickle as pickle
+from os import path
+try:
+ from hashlib import md5
+except ImportError:
+ # 2.4 compatibility
+ from md5 import md5
+
+from docutils import nodes
+from docutils.io import DocTreeInput, StringOutput
+from docutils.core import publish_parts
+from docutils.utils import new_document
+from docutils.frontend import OptionParser
+from docutils.readers.doctree import Reader as DoctreeReader
+
+from sphinx import package_dir, __version__
+from sphinx.util import SEP, os_path, relative_uri, ensuredir, \
+ movefile, ustrftime, copy_static_entry
+from sphinx.errors import SphinxError
+from sphinx.search import js_index
+from sphinx.theming import Theme
+from sphinx.builders import Builder, ENV_PICKLE_FILENAME
+from sphinx.highlighting import PygmentsBridge
+from sphinx.util.console import bold
+from sphinx.writers.html import HTMLWriter, HTMLTranslator, \
+ SmartyPantsHTMLTranslator
+
+try:
+ import json
+except ImportError:
+ try:
+ import simplejson as json
+ except ImportError:
+ json = None
+
+#: the filename for the inventory of objects
+INVENTORY_FILENAME = 'objects.inv'
+#: the filename for the "last build" file (for serializing builders)
+LAST_BUILD_FILENAME = 'last_build'
+
+
+class StandaloneHTMLBuilder(Builder):
+ """
+ Builds standalone HTML docs.
+ """
+ name = 'html'
+ format = 'html'
+ copysource = True
+ out_suffix = '.html'
+ link_suffix = '.html' # defaults to matching out_suffix
+ indexer_format = js_index
+ supported_image_types = ['image/svg+xml', 'image/png',
+ 'image/gif', 'image/jpeg']
+ searchindex_filename = 'searchindex.js'
+ add_permalinks = True
+ embedded = False # for things like HTML help or Qt help: suppresses sidebar
+
+ # This is a class attribute because it is mutated by Sphinx.add_javascript.
+ script_files = ['_static/jquery.js', '_static/doctools.js']
+
+ def init(self):
+ # a hash of all config values that, if changed, cause a full rebuild
+ self.config_hash = ''
+ self.tags_hash = ''
+ # section numbers for headings in the currently visited document
+ self.secnumbers = {}
+
+ self.init_templates()
+ self.init_highlighter()
+ self.init_translator_class()
+ if self.config.html_file_suffix:
+ self.out_suffix = self.config.html_file_suffix
+
+ if self.config.html_link_suffix is not None:
+ self.link_suffix = self.config.html_link_suffix
+ else:
+ self.link_suffix = self.out_suffix
+
+ if self.config.language is not None:
+ jsfile = path.join(package_dir, 'locale', self.config.language,
+ 'LC_MESSAGES', 'sphinx.js')
+ if path.isfile(jsfile):
+ self.script_files.append('_static/translations.js')
+
+ def init_templates(self):
+ Theme.init_themes(self)
+ self.theme = Theme(self.config.html_theme)
+ self.create_template_bridge()
+ self.templates.init(self, self.theme)
+
+ def init_highlighter(self):
+ # determine Pygments style and create the highlighter
+ if self.config.pygments_style is not None:
+ style = self.config.pygments_style
+ elif self.theme:
+ style = self.theme.get_confstr('theme', 'pygments_style', 'none')
+ else:
+ style = 'sphinx'
+ self.highlighter = PygmentsBridge('html', style)
+
+ def init_translator_class(self):
+ if self.config.html_translator_class:
+ self.translator_class = self.app.import_object(
+ self.config.html_translator_class,
+ 'html_translator_class setting')
+ elif self.config.html_use_smartypants:
+ self.translator_class = SmartyPantsHTMLTranslator
+ else:
+ self.translator_class = HTMLTranslator
+
+ def get_outdated_docs(self):
+ cfgdict = dict((name, self.config[name])
+ for (name, desc) in self.config.values.iteritems()
+ if desc[1] == 'html')
+ self.config_hash = md5(str(cfgdict)).hexdigest()
+ self.tags_hash = md5(str(sorted(self.tags))).hexdigest()
+ old_config_hash = old_tags_hash = ''
+ try:
+ fp = open(path.join(self.outdir, '.buildinfo'))
+ version = fp.readline()
+ if version.rstrip() != '# Sphinx build info version 1':
+ raise ValueError
+ fp.readline() # skip commentary
+ cfg, old_config_hash = fp.readline().strip().split(': ')
+ if cfg != 'config':
+ raise ValueError
+ tag, old_tags_hash = fp.readline().strip().split(': ')
+ if tag != 'tags':
+ raise ValueError
+ fp.close()
+ except ValueError:
+ self.warn('unsupported build info format in %r, building all' %
+ path.join(self.outdir, '.buildinfo'))
+ except Exception:
+ pass
+ if old_config_hash != self.config_hash or \
+ old_tags_hash != self.tags_hash:
+ for docname in self.env.found_docs:
+ yield docname
+ return
+
+ if self.templates:
+ template_mtime = self.templates.newest_template_mtime()
+ else:
+ template_mtime = 0
+ for docname in self.env.found_docs:
+ if docname not in self.env.all_docs:
+ yield docname
+ continue
+ targetname = self.env.doc2path(docname, self.outdir,
+ self.out_suffix)
+ try:
+ targetmtime = path.getmtime(targetname)
+ except Exception:
+ targetmtime = 0
+ try:
+ srcmtime = max(path.getmtime(self.env.doc2path(docname)),
+ template_mtime)
+ if srcmtime > targetmtime:
+ yield docname
+ except EnvironmentError:
+ # source doesn't exist anymore
+ pass
+
+ def render_partial(self, node):
+ """Utility: Render a lone doctree node."""
+ doc = new_document('')
+ doc.append(node)
+ return publish_parts(
+ doc,
+ source_class=DocTreeInput,
+ reader=DoctreeReader(),
+ writer=HTMLWriter(self),
+ settings_overrides={'output_encoding': 'unicode'}
+ )
+
+ def prepare_writing(self, docnames):
+ from sphinx.search import IndexBuilder
+
+ self.indexer = IndexBuilder(self.env)
+ self.load_indexer(docnames)
+ self.docwriter = HTMLWriter(self)
+ self.docsettings = OptionParser(
+ defaults=self.env.settings,
+ components=(self.docwriter,)).get_default_values()
+
+ # format the "last updated on" string, only once is enough since it
+ # typically doesn't include the time of day
+ lufmt = self.config.html_last_updated_fmt
+ if lufmt is not None:
+ self.last_updated = ustrftime(lufmt or _('%b %d, %Y'))
+ else:
+ self.last_updated = None
+
+ logo = self.config.html_logo and \
+ path.basename(self.config.html_logo) or ''
+
+ favicon = self.config.html_favicon and \
+ path.basename(self.config.html_favicon) or ''
+ if favicon and os.path.splitext(favicon)[1] != '.ico':
+ self.warn('html_favicon is not an .ico file')
+
+ if not isinstance(self.config.html_use_opensearch, basestring):
+ self.warn('html_use_opensearch config value must now be a string')
+
+ self.relations = self.env.collect_relations()
+
+ rellinks = []
+ if self.config.html_use_index:
+ rellinks.append(('genindex', _('General Index'), 'I', _('index')))
+ if self.config.html_use_modindex and self.env.modules:
+ rellinks.append(('modindex', _('Global Module Index'),
+ 'M', _('modules')))
+
+ if self.config.html_style is not None:
+ stylename = self.config.html_style
+ elif self.theme:
+ stylename = self.theme.get_confstr('theme', 'stylesheet')
+ else:
+ stylename = 'default.css'
+
+ self.globalcontext = dict(
+ embedded = self.embedded,
+ project = self.config.project,
+ release = self.config.release,
+ version = self.config.version,
+ last_updated = self.last_updated,
+ copyright = self.config.copyright,
+ master_doc = self.config.master_doc,
+ use_opensearch = self.config.html_use_opensearch,
+ docstitle = self.config.html_title,
+ shorttitle = self.config.html_short_title,
+ show_sphinx = self.config.html_show_sphinx,
+ has_source = self.config.html_copy_source,
+ show_source = self.config.html_show_sourcelink,
+ file_suffix = self.out_suffix,
+ script_files = self.script_files,
+ sphinx_version = __version__,
+ style = stylename,
+ rellinks = rellinks,
+ builder = self.name,
+ parents = [],
+ logo = logo,
+ favicon = favicon,
+ )
+ if self.theme:
+ self.globalcontext.update(
+ ('theme_' + key, val) for (key, val) in
+ self.theme.get_options(
+ self.config.html_theme_options).iteritems())
+ self.globalcontext.update(self.config.html_context)
+
+ def get_doc_context(self, docname, body, metatags):
+ """Collect items for the template context of a page."""
+ # find out relations
+ prev = next = None
+ parents = []
+ rellinks = self.globalcontext['rellinks'][:]
+ related = self.relations.get(docname)
+ titles = self.env.titles
+ if related and related[2]:
+ try:
+ next = {
+ 'link': self.get_relative_uri(docname, related[2]),
+ 'title': self.render_partial(titles[related[2]])['title']
+ }
+ rellinks.append((related[2], next['title'], 'N', _('next')))
+ except KeyError:
+ next = None
+ if related and related[1]:
+ try:
+ prev = {
+ 'link': self.get_relative_uri(docname, related[1]),
+ 'title': self.render_partial(titles[related[1]])['title']
+ }
+ rellinks.append((related[1], prev['title'], 'P', _('previous')))
+ except KeyError:
+ # the relation is (somehow) not in the TOC tree, handle
+ # that gracefully
+ prev = None
+ while related and related[0]:
+ try:
+ parents.append(
+ {'link': self.get_relative_uri(docname, related[0]),
+ 'title': self.render_partial(titles[related[0]])['title']})
+ except KeyError:
+ pass
+ related = self.relations.get(related[0])
+ if parents:
+ parents.pop() # remove link to the master file; we have a generic
+ # "back to index" link already
+ parents.reverse()
+
+ # title rendered as HTML
+ title = titles.get(docname)
+ title = title and self.render_partial(title)['title'] or ''
+ # the name for the copied source
+ sourcename = self.config.html_copy_source and docname + '.txt' or ''
+
+ # metadata for the document
+ meta = self.env.metadata.get(docname)
+
+ # local TOC and global TOC tree
+ toc = self.render_partial(self.env.get_toc_for(docname))['fragment']
+
+ return dict(
+ parents = parents,
+ prev = prev,
+ next = next,
+ title = title,
+ meta = meta,
+ body = body,
+ metatags = metatags,
+ rellinks = rellinks,
+ sourcename = sourcename,
+ toc = toc,
+ # only display a TOC if there's more than one item to show
+ display_toc = (self.env.toc_num_entries[docname] > 1),
+ )
+
+ def write_doc(self, docname, doctree):
+ destination = StringOutput(encoding='utf-8')
+ doctree.settings = self.docsettings
+
+ self.secnumbers = self.env.toc_secnumbers.get(docname, {})
+ self.imgpath = relative_uri(self.get_target_uri(docname), '_images')
+ self.post_process_images(doctree)
+ self.dlpath = relative_uri(self.get_target_uri(docname), '_downloads')
+ self.docwriter.write(doctree, destination)
+ self.docwriter.assemble_parts()
+ body = self.docwriter.parts['fragment']
+ metatags = self.docwriter.clean_meta
+
+ ctx = self.get_doc_context(docname, body, metatags)
+ self.index_page(docname, doctree, ctx.get('title', ''))
+ self.handle_page(docname, ctx, event_arg=doctree)
+
+ def finish(self):
+ self.info(bold('writing additional files...'), nonl=1)
+
+ # the global general index
+
+ if self.config.html_use_index:
+ # the total count of lines for each index letter, used to distribute
+ # the entries into two columns
+ genindex = self.env.create_index(self)
+ indexcounts = []
+ for _, entries in genindex:
+ indexcounts.append(sum(1 + len(subitems)
+ for _, (_, subitems) in entries))
+
+ genindexcontext = dict(
+ genindexentries = genindex,
+ genindexcounts = indexcounts,
+ split_index = self.config.html_split_index,
+ )
+ self.info(' genindex', nonl=1)
+
+ if self.config.html_split_index:
+ self.handle_page('genindex', genindexcontext,
+ 'genindex-split.html')
+ self.handle_page('genindex-all', genindexcontext,
+ 'genindex.html')
+ for (key, entries), count in zip(genindex, indexcounts):
+ ctx = {'key': key, 'entries': entries, 'count': count,
+ 'genindexentries': genindex}
+ self.handle_page('genindex-' + key, ctx,
+ 'genindex-single.html')
+ else:
+ self.handle_page('genindex', genindexcontext, 'genindex.html')
+
+ # the global module index
+
+ if self.config.html_use_modindex and self.env.modules:
+ # the sorted list of all modules, for the global module index
+ modules = sorted(((mn, (self.get_relative_uri('modindex', fn) +
+ '#module-' + mn, sy, pl, dep))
+ for (mn, (fn, sy, pl, dep)) in
+ self.env.modules.iteritems()),
+ key=lambda x: x[0].lower())
+ # collect all platforms
+ platforms = set()
+ # sort out collapsable modules
+ modindexentries = []
+ letters = []
+ pmn = ''
+ num_toplevels = 0
+ num_collapsables = 0
+ cg = 0 # collapse group
+ fl = '' # first letter
+ for mn, (fn, sy, pl, dep) in modules:
+ pl = pl and pl.split(', ') or []
+ platforms.update(pl)
+
+ ignore = self.env.config['modindex_common_prefix']
+ ignore = sorted(ignore, key=len, reverse=True)
+ for i in ignore:
+ if mn.startswith(i):
+ mn = mn[len(i):]
+ stripped = i
+ break
+ else:
+ stripped = ''
+
+ if fl != mn[0].lower() and mn[0] != '_':
+ # heading
+ letter = mn[0].upper()
+ if letter not in letters:
+ modindexentries.append(['', False, 0, False,
+ letter, '', [], False, ''])
+ letters.append(letter)
+ tn = mn.split('.')[0]
+ if tn != mn:
+ # submodule
+ if pmn == tn:
+ # first submodule - make parent collapsable
+ modindexentries[-1][1] = True
+ num_collapsables += 1
+ elif not pmn.startswith(tn):
+ # submodule without parent in list, add dummy entry
+ cg += 1
+ modindexentries.append([tn, True, cg, False, '', '',
+ [], False, stripped])
+ else:
+ num_toplevels += 1
+ cg += 1
+ modindexentries.append([mn, False, cg, (tn != mn), fn, sy, pl,
+ dep, stripped])
+ pmn = mn
+ fl = mn[0].lower()
+ platforms = sorted(platforms)
+
+ # apply heuristics when to collapse modindex at page load:
+ # only collapse if number of toplevel modules is larger than
+ # number of submodules
+ collapse = len(modules) - num_toplevels < num_toplevels
+
+ # As some parts of the module names may have been stripped, those
+ # names have changed, thus it is necessary to sort the entries.
+ if ignore:
+ def sorthelper(entry):
+ name = entry[0]
+ if name == '':
+ # heading
+ name = entry[4]
+ return name.lower()
+
+ modindexentries.sort(key=sorthelper)
+ letters.sort()
+
+ modindexcontext = dict(
+ modindexentries = modindexentries,
+ platforms = platforms,
+ letters = letters,
+ collapse_modindex = collapse,
+ )
+ self.info(' modindex', nonl=1)
+ self.handle_page('modindex', modindexcontext, 'modindex.html')
+
+ # the search page
+ if self.name != 'htmlhelp':
+ self.info(' search', nonl=1)
+ self.handle_page('search', {}, 'search.html')
+
+ # additional pages from conf.py
+ for pagename, template in self.config.html_additional_pages.items():
+ self.info(' '+pagename, nonl=1)
+ self.handle_page(pagename, {}, template)
+
+ if self.config.html_use_opensearch and self.name != 'htmlhelp':
+ self.info(' opensearch', nonl=1)
+ fn = path.join(self.outdir, '_static', 'opensearch.xml')
+ self.handle_page('opensearch', {}, 'opensearch.xml', outfilename=fn)
+
+ self.info()
+
+ # copy image files
+ if self.images:
+ self.info(bold('copying images...'), nonl=True)
+ ensuredir(path.join(self.outdir, '_images'))
+ for src, dest in self.images.iteritems():
+ self.info(' '+src, nonl=1)
+ shutil.copyfile(path.join(self.srcdir, src),
+ path.join(self.outdir, '_images', dest))
+ self.info()
+
+ # copy downloadable files
+ if self.env.dlfiles:
+ self.info(bold('copying downloadable files...'), nonl=True)
+ ensuredir(path.join(self.outdir, '_downloads'))
+ for src, (_, dest) in self.env.dlfiles.iteritems():
+ self.info(' '+src, nonl=1)
+ shutil.copyfile(path.join(self.srcdir, src),
+ path.join(self.outdir, '_downloads', dest))
+ self.info()
+
+ # copy static files
+ self.info(bold('copying static files... '), nonl=True)
+ ensuredir(path.join(self.outdir, '_static'))
+ # first, create pygments style file
+ f = open(path.join(self.outdir, '_static', 'pygments.css'), 'w')
+ f.write(self.highlighter.get_stylesheet())
+ f.close()
+ # then, copy translations JavaScript file
+ if self.config.language is not None:
+ jsfile = path.join(package_dir, 'locale', self.config.language,
+ 'LC_MESSAGES', 'sphinx.js')
+ if path.isfile(jsfile):
+ shutil.copyfile(jsfile, path.join(self.outdir, '_static',
+ 'translations.js'))
+ # then, copy over all user-supplied static files
+ if self.theme:
+ staticdirnames = [path.join(themepath, 'static')
+ for themepath in self.theme.get_dirchain()[::-1]]
+ else:
+ staticdirnames = []
+ staticdirnames += [path.join(self.confdir, spath)
+ for spath in self.config.html_static_path]
+ for staticdirname in staticdirnames:
+ if not path.isdir(staticdirname):
+ self.warn('static directory %r does not exist' % staticdirname)
+ continue
+ for filename in os.listdir(staticdirname):
+ if filename.startswith('.'):
+ continue
+ fullname = path.join(staticdirname, filename)
+ targetname = path.join(self.outdir, '_static', filename)
+ copy_static_entry(fullname, targetname, self,
+ self.globalcontext)
+ # last, copy logo file (handled differently)
+ if self.config.html_logo:
+ logobase = path.basename(self.config.html_logo)
+ shutil.copyfile(path.join(self.confdir, self.config.html_logo),
+ path.join(self.outdir, '_static', logobase))
+
+ # write build info file
+ fp = open(path.join(self.outdir, '.buildinfo'), 'w')
+ try:
+ fp.write('# Sphinx build info version 1\n'
+ '# This file hashes the configuration used when building'
+ ' these files. When it is not found, a full rebuild will'
+ ' be done.\nconfig: %s\ntags: %s\n' %
+ (self.config_hash, self.tags_hash))
+ finally:
+ fp.close()
+
+ self.info('done')
+
+ # dump the search index
+ self.handle_finish()
+
+ def cleanup(self):
+ # clean up theme stuff
+ if self.theme:
+ self.theme.cleanup()
+
+ def post_process_images(self, doctree):
+ """
+ Pick the best candiate for an image and link down-scaled images to
+ their high res version.
+ """
+ Builder.post_process_images(self, doctree)
+ for node in doctree.traverse(nodes.image):
+ if not node.has_key('scale') or \
+ isinstance(node.parent, nodes.reference):
+ # docutils does unfortunately not preserve the
+ # ``target`` attribute on images, so we need to check
+ # the parent node here.
+ continue
+ uri = node['uri']
+ reference = nodes.reference()
+ if uri in self.images:
+ reference['refuri'] = posixpath.join(self.imgpath,
+ self.images[uri])
+ else:
+ reference['refuri'] = uri
+ node.replace_self(reference)
+ reference.append(node)
+
+ def load_indexer(self, docnames):
+ keep = set(self.env.all_docs) - set(docnames)
+ try:
+ f = open(path.join(self.outdir, self.searchindex_filename), 'rb')
+ try:
+ self.indexer.load(f, self.indexer_format)
+ finally:
+ f.close()
+ except (IOError, OSError, ValueError):
+ if keep:
+ self.warn('search index couldn\'t be loaded, but not all '
+ 'documents will be built: the index will be '
+ 'incomplete.')
+ # delete all entries for files that will be rebuilt
+ self.indexer.prune(keep)
+
+ def index_page(self, pagename, doctree, title):
+ # only index pages with title
+ if self.indexer is not None and title:
+ self.indexer.feed(pagename, title, doctree)
+
+ def _get_local_toctree(self, docname, collapse=True):
+ return self.render_partial(self.env.get_toctree_for(
+ docname, self, collapse))['fragment']
+
+ def get_outfilename(self, pagename):
+ return path.join(self.outdir, os_path(pagename) + self.out_suffix)
+
+ # --------- these are overwritten by the serialization builder
+
+ def get_target_uri(self, docname, typ=None):
+ return docname + self.link_suffix
+
+ def handle_page(self, pagename, addctx, templatename='page.html',
+ outfilename=None, event_arg=None):
+ ctx = self.globalcontext.copy()
+ # current_page_name is backwards compatibility
+ ctx['pagename'] = ctx['current_page_name'] = pagename
+
+ def pathto(otheruri, resource=False,
+ baseuri=self.get_target_uri(pagename)):
+ if not resource:
+ otheruri = self.get_target_uri(otheruri)
+ return relative_uri(baseuri, otheruri)
+ ctx['pathto'] = pathto
+ ctx['hasdoc'] = lambda name: name in self.env.all_docs
+ ctx['customsidebar'] = self.config.html_sidebars.get(pagename)
+ ctx['toctree'] = lambda **kw: self._get_local_toctree(pagename, **kw)
+ ctx.update(addctx)
+
+ self.app.emit('html-page-context', pagename, templatename,
+ ctx, event_arg)
+
+ output = self.templates.render(templatename, ctx)
+ if not outfilename:
+ outfilename = self.get_outfilename(pagename)
+ # outfilename's path is in general different from self.outdir
+ ensuredir(path.dirname(outfilename))
+ try:
+ f = codecs.open(outfilename, 'w', 'utf-8')
+ try:
+ f.write(output)
+ finally:
+ f.close()
+ except (IOError, OSError), err:
+ self.warn("error writing file %s: %s" % (outfilename, err))
+ if self.copysource and ctx.get('sourcename'):
+ # copy the source file for the "show source" link
+ source_name = path.join(self.outdir, '_sources',
+ os_path(ctx['sourcename']))
+ ensuredir(path.dirname(source_name))
+ shutil.copyfile(self.env.doc2path(pagename), source_name)
+
+ def handle_finish(self):
+ self.info(bold('dumping search index... '), nonl=True)
+ self.indexer.prune(self.env.all_docs)
+ searchindexfn = path.join(self.outdir, self.searchindex_filename)
+ # first write to a temporary file, so that if dumping fails,
+ # the existing index won't be overwritten
+ f = open(searchindexfn + '.tmp', 'wb')
+ try:
+ self.indexer.dump(f, self.indexer_format)
+ finally:
+ f.close()
+ movefile(searchindexfn + '.tmp', searchindexfn)
+ self.info('done')
+
+ self.info(bold('dumping object inventory... '), nonl=True)
+ f = open(path.join(self.outdir, INVENTORY_FILENAME), 'w')
+ try:
+ f.write('# Sphinx inventory version 1\n')
+ f.write('# Project: %s\n' % self.config.project.encode('utf-8'))
+ f.write('# Version: %s\n' % self.config.version)
+ for modname, info in self.env.modules.iteritems():
+ f.write('%s mod %s\n' % (modname, self.get_target_uri(info[0])))
+ for refname, (docname, desctype) in self.env.descrefs.iteritems():
+ f.write('%s %s %s\n' % (refname, desctype,
+ self.get_target_uri(docname)))
+ finally:
+ f.close()
+ self.info('done')
+
+
+class DirectoryHTMLBuilder(StandaloneHTMLBuilder):
+ """
+ A StandaloneHTMLBuilder that creates all HTML pages as "index.html" in
+ a directory given by their pagename, so that generated URLs don't have
+ ``.html`` in them.
+ """
+ name = 'dirhtml'
+
+ def get_target_uri(self, docname, typ=None):
+ if docname == 'index':
+ return ''
+ if docname.endswith(SEP + 'index'):
+ return docname[:-5] # up to sep
+ return docname + SEP
+
+ def get_outfilename(self, pagename):
+ if pagename == 'index' or pagename.endswith(SEP + 'index'):
+ outfilename = path.join(self.outdir, os_path(pagename)
+ + self.out_suffix)
+ else:
+ outfilename = path.join(self.outdir, os_path(pagename),
+ 'index' + self.out_suffix)
+
+ return outfilename
+
+
+class SerializingHTMLBuilder(StandaloneHTMLBuilder):
+ """
+ An abstract builder that serializes the generated HTML.
+ """
+ #: the serializing implementation to use. Set this to a module that
+ #: implements a `dump`, `load`, `dumps` and `loads` functions
+ #: (pickle, simplejson etc.)
+ implementation = None
+
+ #: the filename for the global context file
+ globalcontext_filename = None
+
+ supported_image_types = ['image/svg+xml', 'image/png',
+ 'image/gif', 'image/jpeg']
+
+ def init(self):
+ self.config_hash = ''
+ self.tags_hash = ''
+ self.theme = None # no theme necessary
+ self.templates = None # no template bridge necessary
+ self.init_translator_class()
+ self.init_highlighter()
+
+ def get_target_uri(self, docname, typ=None):
+ if docname == 'index':
+ return ''
+ if docname.endswith(SEP + 'index'):
+ return docname[:-5] # up to sep
+ return docname + SEP
+
+ def handle_page(self, pagename, ctx, templatename='page.html',
+ outfilename=None, event_arg=None):
+ ctx['current_page_name'] = pagename
+ sidebarfile = self.config.html_sidebars.get(pagename)
+ if sidebarfile:
+ ctx['customsidebar'] = sidebarfile
+
+ if not outfilename:
+ outfilename = path.join(self.outdir,
+ os_path(pagename) + self.out_suffix)
+
+ self.app.emit('html-page-context', pagename, templatename,
+ ctx, event_arg)
+
+ ensuredir(path.dirname(outfilename))
+ f = open(outfilename, 'wb')
+ try:
+ self.implementation.dump(ctx, f, 2)
+ finally:
+ f.close()
+
+ # if there is a source file, copy the source file for the
+ # "show source" link
+ if ctx.get('sourcename'):
+ source_name = path.join(self.outdir, '_sources',
+ os_path(ctx['sourcename']))
+ ensuredir(path.dirname(source_name))
+ shutil.copyfile(self.env.doc2path(pagename), source_name)
+
+ def handle_finish(self):
+ # dump the global context
+ outfilename = path.join(self.outdir, self.globalcontext_filename)
+ f = open(outfilename, 'wb')
+ try:
+ self.implementation.dump(self.globalcontext, f, 2)
+ finally:
+ f.close()
+
+ # super here to dump the search index
+ StandaloneHTMLBuilder.handle_finish(self)
+
+ # copy the environment file from the doctree dir to the output dir
+ # as needed by the web app
+ shutil.copyfile(path.join(self.doctreedir, ENV_PICKLE_FILENAME),
+ path.join(self.outdir, ENV_PICKLE_FILENAME))
+
+ # touch 'last build' file, used by the web application to determine
+ # when to reload its environment and clear the cache
+ open(path.join(self.outdir, LAST_BUILD_FILENAME), 'w').close()
+
+
+class PickleHTMLBuilder(SerializingHTMLBuilder):
+ """
+ A Builder that dumps the generated HTML into pickle files.
+ """
+ implementation = pickle
+ indexer_format = pickle
+ name = 'pickle'
+ out_suffix = '.fpickle'
+ globalcontext_filename = 'globalcontext.pickle'
+ searchindex_filename = 'searchindex.pickle'
+
+# compatibility alias
+WebHTMLBuilder = PickleHTMLBuilder
+
+
+class JSONHTMLBuilder(SerializingHTMLBuilder):
+ """
+ A builder that dumps the generated HTML into JSON files.
+ """
+ implementation = json
+ indexer_format = json
+ name = 'json'
+ out_suffix = '.fjson'
+ globalcontext_filename = 'globalcontext.json'
+ searchindex_filename = 'searchindex.json'
+
+ def init(self):
+ if json is None:
+ raise SphinxError(
+ 'The module simplejson (or json in Python >= 2.6) '
+ 'is not available. The JSONHTMLBuilder builder will not work.')
+ SerializingHTMLBuilder.init(self)
diff --git a/sphinx/builders/htmlhelp.py b/sphinx/builders/htmlhelp.py
new file mode 100644
index 000000000..8d17f91b8
--- /dev/null
+++ b/sphinx/builders/htmlhelp.py
@@ -0,0 +1,250 @@
+# -*- coding: utf-8 -*-
+"""
+ sphinx.builders.htmlhelp
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+
+ Build HTML help support files.
+ Parts adapted from Python's Doc/tools/prechm.py.
+
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+import os
+import cgi
+from os import path
+
+from docutils import nodes
+
+from sphinx import addnodes
+from sphinx.builders.html import StandaloneHTMLBuilder
+
+
+# Project file (*.hhp) template. 'outname' is the file basename (like
+# the pythlp in pythlp.hhp); 'version' is the doc version number (like
+# the 2.2 in Python 2.2).
+# The magical numbers in the long line under [WINDOWS] set most of the
+# user-visible features (visible buttons, tabs, etc).
+# About 0x10384e: This defines the buttons in the help viewer. The
+# following defns are taken from htmlhelp.h. Not all possibilities
+# actually work, and not all those that work are available from the Help
+# Workshop GUI. In particular, the Zoom/Font button works and is not
+# available from the GUI. The ones we're using are marked with 'x':
+#
+# 0x000002 Hide/Show x
+# 0x000004 Back x
+# 0x000008 Forward x
+# 0x000010 Stop
+# 0x000020 Refresh
+# 0x000040 Home x
+# 0x000080 Forward
+# 0x000100 Back
+# 0x000200 Notes
+# 0x000400 Contents
+# 0x000800 Locate x
+# 0x001000 Options x
+# 0x002000 Print x
+# 0x004000 Index
+# 0x008000 Search
+# 0x010000 History
+# 0x020000 Favorites
+# 0x040000 Jump 1
+# 0x080000 Jump 2
+# 0x100000 Zoom/Font x
+# 0x200000 TOC Next
+# 0x400000 TOC Prev
+
+project_template = '''\
+[OPTIONS]
+Binary TOC=Yes
+Binary Index=No
+Compiled file=%(outname)s.chm
+Contents file=%(outname)s.hhc
+Default Window=%(outname)s
+Default topic=index.html
+Display compile progress=No
+Full text search stop list file=%(outname)s.stp
+Full-text search=Yes
+Index file=%(outname)s.hhk
+Language=0x409
+Title=%(title)s
+
+[WINDOWS]
+%(outname)s="%(title)s","%(outname)s.hhc","%(outname)s.hhk",\
+"index.html","index.html",,,,,0x63520,220,0x10384e,[0,0,1024,768],,,,,,,0
+
+[FILES]
+'''
+
+contents_header = '''\
+
+
+
+
+
+
+
+
+'''
+
+contents_footer = '''\
+
+'''
+
+object_sitemap = '''\
+
+'''
+
+# List of words the full text search facility shouldn't index. This
+# becomes file outname.stp. Note that this list must be pretty small!
+# Different versions of the MS docs claim the file has a maximum size of
+# 256 or 512 bytes (including \r\n at the end of each line).
+# Note that "and", "or", "not" and "near" are operators in the search
+# language, so no point indexing them even if we wanted to.
+stopwords = """
+a and are as at
+be but by
+for
+if in into is it
+near no not
+of on or
+such
+that the their then there these they this to
+was will with
+""".split()
+
+
+class HTMLHelpBuilder(StandaloneHTMLBuilder):
+ """
+ Builder that also outputs Windows HTML help project, contents and
+ index files. Adapted from the original Doc/tools/prechm.py.
+ """
+ name = 'htmlhelp'
+
+ # don't copy the reST source
+ copysource = False
+ supported_image_types = ['image/png', 'image/gif', 'image/jpeg']
+
+ # don't add links
+ add_permalinks = False
+ # don't add sidebar etc.
+ embedded = True
+
+ def init(self):
+ StandaloneHTMLBuilder.init(self)
+ # the output files for HTML help must be .html only
+ self.out_suffix = '.html'
+
+ def handle_finish(self):
+ self.build_hhx(self.outdir, self.config.htmlhelp_basename)
+
+ def build_hhx(self, outdir, outname):
+ self.info('dumping stopword list...')
+ f = open(path.join(outdir, outname+'.stp'), 'w')
+ try:
+ for word in sorted(stopwords):
+ print >>f, word
+ finally:
+ f.close()
+
+ self.info('writing project file...')
+ f = open(path.join(outdir, outname+'.hhp'), 'w')
+ try:
+ f.write(project_template % {'outname': outname,
+ 'title': self.config.html_title,
+ 'version': self.config.version,
+ 'project': self.config.project})
+ if not outdir.endswith(os.sep):
+ outdir += os.sep
+ olen = len(outdir)
+ for root, dirs, files in os.walk(outdir):
+ staticdir = (root == path.join(outdir, '_static'))
+ for fn in files:
+ if (staticdir and not fn.endswith('.js')) or \
+ fn.endswith('.html'):
+ print >>f, path.join(root, fn)[olen:].replace(os.sep,
+ '\\')
+ finally:
+ f.close()
+
+ self.info('writing TOC file...')
+ f = open(path.join(outdir, outname+'.hhc'), 'w')
+ try:
+ f.write(contents_header)
+ # special books
+ f.write('
')
+ for subitem in subitems:
+ write_index(subitem[0], subitem[1], [])
+ f.write('
')
+ for (key, group) in index:
+ for title, (refs, subitems) in group:
+ write_index(title, refs, subitems)
+ f.write('
\n')
+ finally:
+ f.close()
diff --git a/sphinx/builders/latex.py b/sphinx/builders/latex.py
new file mode 100644
index 000000000..96d70e3e7
--- /dev/null
+++ b/sphinx/builders/latex.py
@@ -0,0 +1,203 @@
+# -*- coding: utf-8 -*-
+"""
+ sphinx.builders.latex
+ ~~~~~~~~~~~~~~~~~~~~~
+
+ LaTeX builder.
+
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+import os
+import shutil
+from os import path
+
+from docutils import nodes
+from docutils.io import FileOutput
+from docutils.utils import new_document
+from docutils.frontend import OptionParser
+
+from sphinx import package_dir, addnodes
+from sphinx.util import SEP, texescape
+from sphinx.builders import Builder
+from sphinx.environment import NoUri
+from sphinx.util.console import bold, darkgreen
+from sphinx.writers.latex import LaTeXWriter
+
+
+class LaTeXBuilder(Builder):
+ """
+ Builds LaTeX output to create PDF.
+ """
+ name = 'latex'
+ format = 'latex'
+ supported_image_types = ['application/pdf', 'image/png',
+ 'image/gif', 'image/jpeg']
+
+ def init(self):
+ self.docnames = []
+ self.document_data = []
+ texescape.init()
+
+ def get_outdated_docs(self):
+ return 'all documents' # for now
+
+ def get_target_uri(self, docname, typ=None):
+ if typ == 'token':
+ # token references are always inside production lists and must be
+ # replaced by \token{} in LaTeX
+ return '@token'
+ if docname not in self.docnames:
+ raise NoUri
+ else:
+ return '%' + docname
+
+ def get_relative_uri(self, from_, to, typ=None):
+ # ignore source path
+ return self.get_target_uri(to, typ)
+
+ def init_document_data(self):
+ preliminary_document_data = map(list, self.config.latex_documents)
+ if not preliminary_document_data:
+ self.warn('no "latex_documents" config value found; no documents '
+ 'will be written')
+ return
+ # assign subdirs to titles
+ self.titles = []
+ for entry in preliminary_document_data:
+ docname = entry[0]
+ if docname not in self.env.all_docs:
+ self.warn('"latex_documents" config value references unknown '
+ 'document %s' % docname)
+ continue
+ self.document_data.append(entry)
+ if docname.endswith(SEP+'index'):
+ docname = docname[:-5]
+ self.titles.append((docname, entry[2]))
+
+ def write(self, *ignored):
+ docwriter = LaTeXWriter(self)
+ docsettings = OptionParser(
+ defaults=self.env.settings,
+ components=(docwriter,)).get_default_values()
+
+ self.init_document_data()
+
+ for entry in self.document_data:
+ docname, targetname, title, author, docclass = entry[:5]
+ toctree_only = False
+ if len(entry) > 5:
+ toctree_only = entry[5]
+ destination = FileOutput(
+ destination_path=path.join(self.outdir, targetname),
+ encoding='utf-8')
+ self.info("processing " + targetname + "... ", nonl=1)
+ doctree = self.assemble_doctree(docname, toctree_only,
+ appendices=((docclass == 'manual') and
+ self.config.latex_appendices or []))
+ self.post_process_images(doctree)
+ self.info("writing... ", nonl=1)
+ doctree.settings = docsettings
+ doctree.settings.author = author
+ doctree.settings.title = title
+ doctree.settings.docname = docname
+ doctree.settings.docclass = docclass
+ docwriter.write(doctree, destination)
+ self.info("done")
+
+ def assemble_doctree(self, indexfile, toctree_only, appendices):
+ self.docnames = set([indexfile] + appendices)
+ self.info(darkgreen(indexfile) + " ", nonl=1)
+ def process_tree(docname, tree):
+ tree = tree.deepcopy()
+ for toctreenode in tree.traverse(addnodes.toctree):
+ newnodes = []
+ includefiles = map(str, toctreenode['includefiles'])
+ for includefile in includefiles:
+ try:
+ self.info(darkgreen(includefile) + " ", nonl=1)
+ subtree = process_tree(
+ includefile, self.env.get_doctree(includefile))
+ self.docnames.add(includefile)
+ except Exception:
+ self.warn('toctree contains ref to nonexisting '
+ 'file %r' % includefile,
+ self.builder.env.doc2path(docname))
+ else:
+ sof = addnodes.start_of_file(docname=includefile)
+ sof.children = subtree.children
+ newnodes.append(sof)
+ toctreenode.parent.replace(toctreenode, newnodes)
+ return tree
+ tree = self.env.get_doctree(indexfile)
+ tree['docname'] = indexfile
+ if toctree_only:
+ # extract toctree nodes from the tree and put them in a
+ # fresh document
+ new_tree = new_document('')
+ new_sect = nodes.section()
+ new_sect += nodes.title(u'',
+ u'')
+ new_tree += new_sect
+ for node in tree.traverse(addnodes.toctree):
+ new_sect += node
+ tree = new_tree
+ largetree = process_tree(indexfile, tree)
+ largetree['docname'] = indexfile
+ for docname in appendices:
+ appendix = self.env.get_doctree(docname)
+ appendix['docname'] = docname
+ largetree.append(appendix)
+ self.info()
+ self.info("resolving references...")
+ self.env.resolve_references(largetree, indexfile, self)
+ # resolve :ref:s to distant tex files -- we can't add a cross-reference,
+ # but append the document name
+ for pendingnode in largetree.traverse(addnodes.pending_xref):
+ docname = pendingnode['refdocname']
+ sectname = pendingnode['refsectname']
+ newnodes = [nodes.emphasis(sectname, sectname)]
+ for subdir, title in self.titles:
+ if docname.startswith(subdir):
+ newnodes.append(nodes.Text(_(' (in '), _(' (in ')))
+ newnodes.append(nodes.emphasis(title, title))
+ newnodes.append(nodes.Text(')', ')'))
+ break
+ else:
+ pass
+ pendingnode.replace_self(newnodes)
+ return largetree
+
+ def finish(self):
+ # copy image files
+ if self.images:
+ self.info(bold('copying images...'), nonl=1)
+ for src, dest in self.images.iteritems():
+ self.info(' '+src, nonl=1)
+ shutil.copyfile(path.join(self.srcdir, src),
+ path.join(self.outdir, dest))
+ self.info()
+
+ # copy additional files
+ if self.config.latex_additional_files:
+ self.info(bold('copying additional files...'), nonl=1)
+ for filename in self.config.latex_additional_files:
+ self.info(' '+filename, nonl=1)
+ shutil.copyfile(path.join(self.confdir, filename),
+ path.join(self.outdir, path.basename(filename)))
+ self.info()
+
+ # the logo is handled differently
+ if self.config.latex_logo:
+ logobase = path.basename(self.config.latex_logo)
+ shutil.copyfile(path.join(self.confdir, self.config.latex_logo),
+ path.join(self.outdir, logobase))
+
+ self.info(bold('copying TeX support files... '), nonl=True)
+ staticdirname = path.join(package_dir, 'texinputs')
+ for filename in os.listdir(staticdirname):
+ if not filename.startswith('.'):
+ shutil.copyfile(path.join(staticdirname, filename),
+ path.join(self.outdir, filename))
+ self.info('done')
diff --git a/sphinx/linkcheck.py b/sphinx/builders/linkcheck.py
similarity index 80%
rename from sphinx/linkcheck.py
rename to sphinx/builders/linkcheck.py
index 457b18dcb..f3962965c 100644
--- a/sphinx/linkcheck.py
+++ b/sphinx/builders/linkcheck.py
@@ -1,12 +1,12 @@
# -*- coding: utf-8 -*-
"""
- sphinx.linkcheck
- ~~~~~~~~~~~~~~~~
+ sphinx.builders.linkcheck
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
The CheckExternalLinksBuilder class.
- :copyright: 2008 by Georg Brandl, Thomas Lamb.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
import socket
@@ -15,7 +15,7 @@ from urllib2 import build_opener, HTTPError
from docutils import nodes
-from sphinx.builder import Builder
+from sphinx.builders import Builder
from sphinx.util.console import purple, red, darkgreen
# create an opener that will simulate a browser user-agent
@@ -84,20 +84,28 @@ class CheckExternalLinksBuilder(Builder):
self.good.add(uri)
elif r == 2:
self.info(' - ' + red('broken: ') + s)
- self.broken[uri] = (r, s)
self.write_entry('broken', docname, lineno, uri + ': ' + s)
+ self.broken[uri] = (r, s)
+ if self.app.quiet:
+ self.warn('broken link: %s' % uri,
+ '%s:%s' % (self.env.doc2path(docname), lineno))
else:
self.info(' - ' + purple('redirected') + ' to ' + s)
+ self.write_entry('redirected', docname,
+ lineno, uri + ' to ' + s)
self.redirected[uri] = (r, s)
- self.write_entry('redirected', docname, lineno, uri + ' to ' + s)
-
elif len(uri) == 0 or uri[0:7] == 'mailto:' or uri[0:4] == 'ftp:':
return
else:
- self.info(uri + ' - ' + red('malformed!'))
+ self.warn(uri + ' - ' + red('malformed!'))
self.write_entry('malformed', docname, lineno, uri)
+ if self.app.quiet:
+ self.warn('malformed link: %s' % uri,
+ '%s:%s' % (self.env.doc2path(docname), lineno))
+ self.app.statuscode = 1
- return
+ if self.broken:
+ self.app.statuscode = 1
def write_entry(self, what, docname, line, uri):
output = open(path.join(self.outdir, 'output.txt'), 'a')
diff --git a/sphinx/builders/qthelp.py b/sphinx/builders/qthelp.py
new file mode 100644
index 000000000..7c2af1ef8
--- /dev/null
+++ b/sphinx/builders/qthelp.py
@@ -0,0 +1,263 @@
+# -*- coding: utf-8 -*-
+"""
+ sphinx.builders.qthelp
+ ~~~~~~~~~~~~~~~~~~~~~~
+
+ Build input files for the Qt collection generator.
+
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+import os
+import re
+import cgi
+from os import path
+
+from docutils import nodes
+
+from sphinx import addnodes
+from sphinx.builders.html import StandaloneHTMLBuilder
+
+_idpattern = re.compile(
+ r'(?P.+) (\((?P[\w\.]+)( (?P\w+))?\))$')
+
+
+# Qt Help Collection Project (.qhcp).
+# Is the input file for the help collection generator.
+# It contains references to compressed help files which should be
+# included in the collection.
+# It may contain various other information for customizing Qt Assistant.
+collection_template = '''\
+
+
+
+
+
+ %(outname)s.qhp
+
+
+
+
+ %(outname)s.qch
+
+
+
+'''
+
+# Qt Help Project (.qhp)
+# This is the input file for the help generator.
+# It contains the table of contents, indices and references to the
+# actual documentation files (*.html).
+# In addition it defines a unique namespace for the documentation.
+project_template = '''\
+
+
+ %(outname)s.org.%(outname)s.%(nversion)s
+ doc
+
+ %(outname)s
+ %(version)s
+
+
+ %(outname)s
+ %(version)s
+
+
+%(sections)s
+
+
+
+%(keywords)s
+
+
+%(files)s
+
+
+
+'''
+
+section_template = ''
+file_template = ' '*12 + '%(filename)s'
+
+
+class QtHelpBuilder(StandaloneHTMLBuilder):
+ """
+ Builder that also outputs Qt help project, contents and index files.
+ """
+ name = 'qthelp'
+
+ # don't copy the reST source
+ copysource = False
+ supported_image_types = ['image/svg+xml', 'image/png', 'image/gif',
+ 'image/jpeg']
+
+ # don't add links
+ add_permalinks = False
+ # don't add sidebar etc.
+ embedded = True
+
+ def init(self):
+ StandaloneHTMLBuilder.init(self)
+ # the output files for HTML help must be .html only
+ self.out_suffix = '.html'
+ #self.config.html_style = 'traditional.css'
+
+ def handle_finish(self):
+ self.build_qhcp(self.outdir, self.config.qthelp_basename)
+ self.build_qhp(self.outdir, self.config.qthelp_basename)
+
+ def build_qhcp(self, outdir, outname):
+ self.info('writing collection project file...')
+ f = open(path.join(outdir, outname+'.qhcp'), 'w')
+ try:
+ f.write(collection_template % {'outname': outname})
+ finally:
+ f.close()
+
+ def build_qhp(self, outdir, outname):
+ self.info('writing project file...')
+
+ # sections
+ tocdoc = self.env.get_and_resolve_doctree(self.config.master_doc, self,
+ prune_toctrees=False)
+ istoctree = lambda node: (
+ isinstance(node, addnodes.compact_paragraph)
+ and node.has_key('toctree'))
+ sections = []
+ for node in tocdoc.traverse(istoctree):
+ sections.extend(self.write_toc(node))
+
+ if self.config.html_use_modindex:
+ item = section_template % {'title': _('Global Module Index'),
+ 'ref': 'modindex.html'}
+ sections.append(' '*4*4 + item)
+ sections = '\n'.join(sections)
+
+ # keywords
+ keywords = []
+ index = self.env.create_index(self)
+ for (key, group) in index:
+ for title, (refs, subitems) in group:
+ keywords.extend(self.build_keywords(title, refs, subitems))
+ keywords = '\n'.join(keywords)
+
+ # files
+ if not outdir.endswith(os.sep):
+ outdir += os.sep
+ olen = len(outdir)
+ projectfiles = []
+ for root, dirs, files in os.walk(outdir):
+ staticdir = (root == path.join(outdir, '_static'))
+ for fn in files:
+ if (staticdir and not fn.endswith('.js')) or \
+ fn.endswith('.html'):
+ filename = path.join(root, fn)[olen:]
+ #filename = filename.replace(os.sep, '\\') # XXX
+ projectfiles.append(file_template % {'filename': filename})
+ projectfiles = '\n'.join(projectfiles)
+
+ # write the project file
+ f = open(path.join(outdir, outname+'.qhp'), 'w')
+ try:
+ nversion = self.config.version.replace('.', '_')
+ nversion = nversion.replace(' ', '_')
+ f.write(project_template % {'outname': outname,
+ 'title': self.config.html_title,
+ 'version': self.config.version,
+ 'project': self.config.project,
+ 'nversion': nversion,
+ 'masterdoc': self.config.master_doc,
+ 'sections': sections,
+ 'keywords': keywords,
+ 'files': projectfiles})
+ finally:
+ f.close()
+
+ def isdocnode(self, node):
+ if not isinstance(node, nodes.list_item):
+ return False
+ if len(node.children) != 2:
+ return False
+ if not isinstance(node.children[0], addnodes.compact_paragraph):
+ return False
+ if not isinstance(node.children[0][0], nodes.reference):
+ return False
+ if not isinstance(node.children[1], nodes.bullet_list):
+ return False
+ return True
+
+ def write_toc(self, node, indentlevel=4):
+ parts = []
+ if self.isdocnode(node):
+ refnode = node.children[0][0]
+ link = refnode['refuri']
+ title = cgi.escape(refnode.astext()).replace('"','"')
+ item = '' % {
+ 'title': title,
+ 'ref': link}
+ parts.append(' '*4*indentlevel + item)
+ for subnode in node.children[1]:
+ parts.extend(self.write_toc(subnode, indentlevel+1))
+ parts.append(' '*4*indentlevel + '')
+ elif isinstance(node, nodes.list_item):
+ for subnode in node:
+ parts.extend(self.write_toc(subnode, indentlevel))
+ elif isinstance(node, nodes.reference):
+ link = node['refuri']
+ title = cgi.escape(node.astext()).replace('"','"')
+ item = section_template % {'title': title, 'ref': link}
+ item = ' '*4*indentlevel + item.encode('ascii', 'xmlcharrefreplace')
+ parts.append(item.encode('ascii', 'xmlcharrefreplace'))
+ elif isinstance(node, nodes.bullet_list):
+ for subnode in node:
+ parts.extend(self.write_toc(subnode, indentlevel))
+ elif isinstance(node, addnodes.compact_paragraph):
+ for subnode in node:
+ parts.extend(self.write_toc(subnode, indentlevel))
+
+ return parts
+
+ def keyword_item(self, name, ref):
+ matchobj = _idpattern.match(name)
+ if matchobj:
+ groupdict = matchobj.groupdict()
+ shortname = groupdict['title']
+ id = groupdict.get('id')
+# descr = groupdict.get('descr')
+ if shortname.endswith('()'):
+ shortname = shortname[:-2]
+ id = '%s.%s' % (id, shortname)
+ else:
+ id = descr = None
+
+ if id:
+ item = ' '*12 + '' % (
+ name, id, ref)
+ else:
+ item = ' '*12 + '' % (name, ref)
+ item.encode('ascii', 'xmlcharrefreplace')
+ return item
+
+ def build_keywords(self, title, refs, subitems):
+ keywords = []
+
+ title = cgi.escape(title)
+# if len(refs) == 0: # XXX
+# write_param('See Also', title)
+ if len(refs) == 1:
+ keywords.append(self.keyword_item(title, refs[0]))
+ elif len(refs) > 1:
+ for i, ref in enumerate(refs): # XXX
+# item = (' '*12 +
+# '' % (
+# title, i, ref))
+# item.encode('ascii', 'xmlcharrefreplace')
+# keywords.append(item)
+ keywords.append(self.keyword_item(title, ref))
+
+ if subitems:
+ for subitem in subitems:
+ keywords.extend(self.build_keywords(subitem[0], subitem[1], []))
+
+ return keywords
diff --git a/sphinx/builders/text.py b/sphinx/builders/text.py
new file mode 100644
index 000000000..8651778cb
--- /dev/null
+++ b/sphinx/builders/text.py
@@ -0,0 +1,70 @@
+# -*- coding: utf-8 -*-
+"""
+ sphinx.builders.text
+ ~~~~~~~~~~~~~~~~~~~~
+
+ Plain-text Sphinx builder.
+
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+import codecs
+from os import path
+
+from docutils.io import StringOutput
+
+from sphinx.util import ensuredir, os_path
+from sphinx.builders import Builder
+from sphinx.writers.text import TextWriter
+
+
+class TextBuilder(Builder):
+ name = 'text'
+ format = 'text'
+ out_suffix = '.txt'
+
+ def init(self):
+ pass
+
+ def get_outdated_docs(self):
+ for docname in self.env.found_docs:
+ if docname not in self.env.all_docs:
+ yield docname
+ continue
+ targetname = self.env.doc2path(docname, self.outdir,
+ self.out_suffix)
+ try:
+ targetmtime = path.getmtime(targetname)
+ except Exception:
+ targetmtime = 0
+ try:
+ srcmtime = path.getmtime(self.env.doc2path(docname))
+ if srcmtime > targetmtime:
+ yield docname
+ except EnvironmentError:
+ # source doesn't exist anymore
+ pass
+
+ def get_target_uri(self, docname, typ=None):
+ return ''
+
+ def prepare_writing(self, docnames):
+ self.writer = TextWriter(self)
+
+ def write_doc(self, docname, doctree):
+ destination = StringOutput(encoding='utf-8')
+ self.writer.write(doctree, destination)
+ outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix)
+ ensuredir(path.dirname(outfilename))
+ try:
+ f = codecs.open(outfilename, 'w', 'utf-8')
+ try:
+ f.write(self.writer.output)
+ finally:
+ f.close()
+ except (IOError, OSError), err:
+ self.warn("error writing file %s: %s" % (outfilename, err))
+
+ def finish(self):
+ pass
diff --git a/sphinx/cmdline.py b/sphinx/cmdline.py
new file mode 100644
index 000000000..79bd9751c
--- /dev/null
+++ b/sphinx/cmdline.py
@@ -0,0 +1,222 @@
+# -*- coding: utf-8 -*-
+"""
+ sphinx.cmdline
+ ~~~~~~~~~~~~~~
+
+ sphinx-build command-line handling.
+
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+import os
+import sys
+import getopt
+import traceback
+from os import path
+
+from docutils.utils import SystemMessage
+
+from sphinx import __version__
+from sphinx.errors import SphinxError
+from sphinx.application import Sphinx
+from sphinx.util import Tee, format_exception_cut_frames, save_traceback
+from sphinx.util.console import darkred, nocolor, color_terminal
+
+
+def usage(argv, msg=None):
+ if msg:
+ print >>sys.stderr, msg
+ print >>sys.stderr
+ print >>sys.stderr, """\
+Sphinx v%s
+Usage: %s [options] sourcedir outdir [filenames...]
+Options: -b -- builder to use; default is html
+ -a -- write all files; default is to only write \
+new and changed files
+ -E -- don't use a saved environment, always read all files
+ -t -- include "only" blocks with
+ -d -- path for the cached environment and doctree files
+ (default: outdir/.doctrees)
+ -c -- path where configuration file (conf.py) is located
+ (default: same as sourcedir)
+ -C -- use no config file at all, only -D options
+ -D -- override a setting in configuration
+ -A -- pass a value into the templates, for HTML builder
+ -g -- auto-generate docs with sphinx.ext.autosummary
+ for autosummary directives in sources found in path
+ -N -- do not do colored output
+ -q -- no output on stdout, just warnings on stderr
+ -Q -- no output at all, not even warnings
+ -w -- write warnings (and errors) to given file
+ -W -- turn warnings into errors
+ -P -- run Pdb on exception
+Modi:
+* without -a and without filenames, write new and changed files.
+* with -a, write all files.
+* with filenames, write these.""" % (__version__, argv[0])
+
+
+def main(argv):
+ if not sys.stdout.isatty() or not color_terminal():
+ # Windows' poor cmd box doesn't understand ANSI sequences
+ nocolor()
+
+ try:
+ opts, args = getopt.getopt(argv[1:], 'ab:t:d:c:CD:A:g:NEqQWw:P')
+ allopts = set(opt[0] for opt in opts)
+ srcdir = confdir = path.abspath(args[0])
+ if not path.isdir(srcdir):
+ print >>sys.stderr, 'Error: Cannot find source directory.'
+ return 1
+ if not path.isfile(path.join(srcdir, 'conf.py')) and \
+ '-c' not in allopts and '-C' not in allopts:
+ print >>sys.stderr, ('Error: Source directory doesn\'t '
+ 'contain conf.py file.')
+ return 1
+ outdir = path.abspath(args[1])
+ if not path.isdir(outdir):
+ print >>sys.stderr, 'Making output directory...'
+ os.makedirs(outdir)
+ except (IndexError, getopt.error):
+ usage(argv)
+ return 1
+
+ filenames = args[2:]
+ err = 0
+ for filename in filenames:
+ if not path.isfile(filename):
+ print >>sys.stderr, 'Cannot find file %r.' % filename
+ err = 1
+ if err:
+ return 1
+
+ buildername = all_files = None
+ freshenv = warningiserror = use_pdb = False
+ status = sys.stdout
+ warning = sys.stderr
+ error = sys.stderr
+ warnfile = None
+ confoverrides = {}
+ htmlcontext = {}
+ tags = []
+ doctreedir = path.join(outdir, '.doctrees')
+ for opt, val in opts:
+ if opt == '-b':
+ buildername = val
+ elif opt == '-a':
+ if filenames:
+ usage(argv, 'Cannot combine -a option and filenames.')
+ return 1
+ all_files = True
+ elif opt == '-t':
+ tags.append(val)
+ elif opt == '-d':
+ doctreedir = path.abspath(val)
+ elif opt == '-c':
+ confdir = path.abspath(val)
+ if not path.isfile(path.join(confdir, 'conf.py')):
+ print >>sys.stderr, ('Error: Configuration directory '
+ 'doesn\'t contain conf.py file.')
+ return 1
+ elif opt == '-C':
+ confdir = None
+ elif opt == '-D':
+ try:
+ key, val = val.split('=')
+ except ValueError:
+ print >>sys.stderr, ('Error: -D option argument must be '
+ 'in the form name=value.')
+ return 1
+ try:
+ val = int(val)
+ except ValueError:
+ pass
+ confoverrides[key] = val
+ elif opt == '-A':
+ try:
+ key, val = val.split('=')
+ except ValueError:
+ print >>sys.stderr, ('Error: -A option argument must be '
+ 'in the form name=value.')
+ return 1
+ try:
+ val = int(val)
+ except ValueError:
+ pass
+ htmlcontext[key] = val
+ elif opt == '-g':
+ # XXX XXX XXX
+ source_filenames = [path.join(srcdir, f)
+ for f in os.listdir(srcdir) if f.endswith('.rst')]
+ if val is None:
+ print >>sys.stderr, \
+ 'Error: you must provide a destination directory ' \
+ 'for autodoc generation.'
+ return 1
+ p = path.abspath(val)
+ from sphinx.ext.autosummary.generate import generate_autosummary_docs
+ generate_autosummary_docs(source_filenames, p)
+ elif opt == '-N':
+ nocolor()
+ elif opt == '-E':
+ freshenv = True
+ elif opt == '-q':
+ status = None
+ elif opt == '-Q':
+ status = None
+ warning = None
+ elif opt == '-W':
+ warningiserror = True
+ elif opt == '-w':
+ warnfile = val
+ elif opt == '-P':
+ use_pdb = True
+ confoverrides['html_context'] = htmlcontext
+
+ if warning and warnfile:
+ warnfp = open(warnfile, 'w')
+ warning = Tee(warning, warnfp)
+ error = warning
+
+ try:
+ app = Sphinx(srcdir, confdir, outdir, doctreedir, buildername,
+ confoverrides, status, warning, freshenv,
+ warningiserror, tags)
+ app.build(all_files, filenames)
+ return app.statuscode
+ except KeyboardInterrupt:
+ if use_pdb:
+ import pdb
+ print >>error, darkred('Interrupted while building, '
+ 'starting debugger:')
+ traceback.print_exc()
+ pdb.post_mortem(sys.exc_info()[2])
+ return 1
+ except Exception, err:
+ if use_pdb:
+ import pdb
+ print >>error, darkred('Exception occurred while building, '
+ 'starting debugger:')
+ traceback.print_exc()
+ pdb.post_mortem(sys.exc_info()[2])
+ else:
+ if isinstance(err, SystemMessage):
+ print >>error, darkred('reST markup error:')
+ print >>error, err.args[0].encode('ascii', 'backslashreplace')
+ elif isinstance(err, SphinxError):
+ print >>error, darkred('%s:' % err.category)
+ print >>error, err
+ else:
+ print >>error, darkred('Exception occurred:')
+ print >>error, format_exception_cut_frames().rstrip()
+ tbpath = save_traceback()
+ print >>error, darkred('The full traceback has been saved '
+ 'in %s, if you want to report the '
+ 'issue to the author.' % tbpath)
+ print >>error, ('Please also report this if it was a user '
+ 'error, so that a better error message '
+ 'can be provided next time.')
+ print >>error, ('Send reports to sphinx-dev@googlegroups.com. '
+ 'Thanks!')
+ return 1
diff --git a/sphinx/config.py b/sphinx/config.py
index 012668e79..3e3abb0db 100644
--- a/sphinx/config.py
+++ b/sphinx/config.py
@@ -5,102 +5,121 @@
Build configuration file handling.
- :copyright: 2008 by Georg Brandl.
- :license: BSD license.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
import os
from os import path
+from sphinx.util import make_filename
+
class Config(object):
"""Configuration file abstraction."""
- # the values are: (default, needs fresh doctrees if changed)
+ # the values are: (default, what needs to be rebuilt if changed)
# If you add a value here, don't forget to include it in the
# quickstart.py file template as well as in the docs!
config_values = dict(
# general options
- project = ('Python', True),
- copyright = ('', False),
- version = ('', True),
- release = ('', True),
- today = ('', True),
- today_fmt = (None, True), # the real default is locale-dependent
+ project = ('Python', 'env'),
+ copyright = ('', 'html'),
+ version = ('', 'env'),
+ release = ('', 'env'),
+ today = ('', 'env'),
+ today_fmt = (None, 'env'), # the real default is locale-dependent
- language = (None, True),
- locale_dirs = ([], True),
+ language = (None, 'env'),
+ locale_dirs = ([], 'env'),
- master_doc = ('contents', True),
- source_suffix = ('.rst', True),
- source_encoding = ('utf-8', True),
- unused_docs = ([], True),
- exclude_dirs = ([], True),
- exclude_trees = ([], True),
- exclude_dirnames = ([], True),
- default_role = (None, True),
- add_function_parentheses = (True, True),
- add_module_names = (True, True),
- show_authors = (False, True),
- pygments_style = ('sphinx', False),
- highlight_language = ('python', False),
- templates_path = ([], False),
- template_bridge = (None, False),
- keep_warnings = (False, True),
+ master_doc = ('contents', 'env'),
+ source_suffix = ('.rst', 'env'),
+ source_encoding = ('utf-8', 'env'),
+ unused_docs = ([], 'env'),
+ exclude_dirs = ([], 'env'),
+ exclude_trees = ([], 'env'),
+ exclude_dirnames = ([], 'env'),
+ default_role = (None, 'env'),
+ add_function_parentheses = (True, 'env'),
+ add_module_names = (True, 'env'),
+ trim_footnote_reference_space = (False, 'env'),
+ show_authors = (False, 'env'),
+ pygments_style = (None, 'html'),
+ highlight_language = ('python', 'env'),
+ templates_path = ([], 'html'),
+ template_bridge = (None, 'html'),
+ keep_warnings = (False, 'env'),
+ modindex_common_prefix = ([], 'html'),
+ rst_epilog = (None, 'env'),
# HTML options
+ html_theme = ('default', 'html'),
+ html_theme_path = ([], 'html'),
+ html_theme_options = ({}, 'html'),
html_title = (lambda self: '%s v%s documentation' %
(self.project, self.release),
- False),
- html_short_title = (lambda self: self.html_title, False),
- html_style = ('default.css', False),
- html_logo = (None, False),
- html_favicon = (None, False),
- html_static_path = ([], False),
- html_last_updated_fmt = (None, False), # the real default is locale-dependent
- html_use_smartypants = (True, False),
- html_translator_class = (None, False),
- html_sidebars = ({}, False),
- html_additional_pages = ({}, False),
- html_use_modindex = (True, False),
- html_use_index = (True, False),
- html_split_index = (False, False),
- html_copy_source = (True, False),
- html_use_opensearch = ('', False),
- html_file_suffix = (None, False),
- html_show_sphinx = (True, False),
- html_context = ({}, False),
+ 'html'),
+ html_short_title = (lambda self: self.html_title, 'html'),
+ html_style = (None, 'html'),
+ html_logo = (None, 'html'),
+ html_favicon = (None, 'html'),
+ html_static_path = ([], 'html'),
+ # the real default is locale-dependent
+ html_last_updated_fmt = (None, 'html'),
+ html_use_smartypants = (True, 'html'),
+ html_translator_class = (None, 'html'),
+ html_sidebars = ({}, 'html'),
+ html_additional_pages = ({}, 'html'),
+ html_use_modindex = (True, 'html'),
+ html_add_permalinks = (True, 'html'),
+ html_use_index = (True, 'html'),
+ html_split_index = (False, 'html'),
+ html_copy_source = (True, 'html'),
+ html_show_sourcelink = (True, 'html'),
+ html_use_opensearch = ('', 'html'),
+ html_file_suffix = (None, 'html'),
+ html_link_suffix = (None, 'html'),
+ html_show_sphinx = (True, 'html'),
+ html_context = ({}, 'html'),
# HTML help only options
- htmlhelp_basename = ('pydoc', False),
+ htmlhelp_basename = (lambda self: make_filename(self.project), None),
+
+ # Qt help only options
+ qthelp_basename = (lambda self: make_filename(self.project), None),
# LaTeX options
- latex_documents = ([], False),
- latex_logo = (None, False),
- latex_appendices = ([], False),
- latex_use_parts = (False, False),
- latex_use_modindex = (True, False),
+ latex_documents = ([], None),
+ latex_logo = (None, None),
+ latex_appendices = ([], None),
+ latex_use_parts = (False, None),
+ latex_use_modindex = (True, None),
# paper_size and font_size are still separate values
# so that you can give them easily on the command line
- latex_paper_size = ('letter', False),
- latex_font_size = ('10pt', False),
- latex_elements = ({}, False),
+ latex_paper_size = ('letter', None),
+ latex_font_size = ('10pt', None),
+ latex_elements = ({}, None),
+ latex_additional_files = ([], None),
# now deprecated - use latex_elements
- latex_preamble = ('', False),
+ latex_preamble = ('', None),
)
- def __init__(self, dirname, filename, overrides):
+ def __init__(self, dirname, filename, overrides, tags):
self.overrides = overrides
self.values = Config.config_values.copy()
- config = {'__file__': path.join(dirname, filename)}
- olddir = os.getcwd()
- try:
- os.chdir(dirname)
- execfile(config['__file__'], config)
- finally:
- os.chdir(olddir)
+ config = {}
+ if dirname is not None:
+ config['__file__'] = path.join(dirname, filename)
+ config['tags'] = tags
+ olddir = os.getcwd()
+ try:
+ os.chdir(dirname)
+ execfile(config['__file__'], config)
+ finally:
+ os.chdir(olddir)
self._raw_config = config
# these two must be preinitialized because extensions can add their
# own config values
@@ -109,7 +128,12 @@ class Config(object):
def init_values(self):
config = self._raw_config
- config.update(self.overrides)
+ for valname, value in self.overrides.iteritems():
+ if '.' in valname:
+ realvalname, key = valname.split('.', 1)
+ config.setdefault(realvalname, {})[key] = value
+ else:
+ config[valname] = value
for name in config:
if name in self.values:
self.__dict__[name] = config[name]
@@ -121,7 +145,7 @@ class Config(object):
if name not in self.values:
raise AttributeError('No such config value: %s' % name)
default = self.values[name][0]
- if callable(default):
+ if hasattr(default, '__call__'):
return default(self)
return default
diff --git a/sphinx/directives/__init__.py b/sphinx/directives/__init__.py
index 462b2cffc..2de29010c 100644
--- a/sphinx/directives/__init__.py
+++ b/sphinx/directives/__init__.py
@@ -5,10 +5,23 @@
Handlers for additional ReST directives.
- :copyright: 2008 by Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
+from docutils.parsers.rst import directives
+from docutils.parsers.rst.directives import images
+
+# import and register directives
from sphinx.directives.desc import *
from sphinx.directives.code import *
from sphinx.directives.other import *
+
+
+# allow units for the figure's "figwidth"
+try:
+ images.Figure.option_spec['figwidth'] = \
+ directives.length_or_percentage_or_unitless
+except AttributeError:
+ images.figure.options['figwidth'] = \
+ directives.length_or_percentage_or_unitless
diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py
index 5ea477635..645bc7844 100644
--- a/sphinx/directives/code.py
+++ b/sphinx/directives/code.py
@@ -3,10 +3,11 @@
sphinx.directives.code
~~~~~~~~~~~~~~~~~~~~~~
- :copyright: 2007-2008 by Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
+import os
import sys
import codecs
from os import path
@@ -15,85 +16,161 @@ from docutils import nodes
from docutils.parsers.rst import directives
from sphinx import addnodes
+from sphinx.util import parselinenos
+from sphinx.util.compat import Directive, directive_dwim
-# ------ highlight directive --------------------------------------------------------
+class Highlight(Directive):
+ """
+ Directive to set the highlighting language for code blocks, as well
+ as the threshold for line numbers.
+ """
-def highlightlang_directive(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- if 'linenothreshold' in options:
+ has_content = False
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = False
+ option_spec = {
+ 'linenothreshold': directives.unchanged,
+ }
+
+ def run(self):
+ if 'linenothreshold' in self.options:
+ try:
+ linenothreshold = int(self.options['linenothreshold'])
+ except Exception:
+ linenothreshold = 10
+ else:
+ linenothreshold = sys.maxint
+ return [addnodes.highlightlang(lang=self.arguments[0].strip(),
+ linenothreshold=linenothreshold)]
+
+
+class CodeBlock(Directive):
+ """
+ Directive for a code block with special highlighting or line numbering
+ settings.
+ """
+
+ has_content = True
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = False
+ option_spec = {
+ 'linenos': directives.flag,
+ }
+
+ def run(self):
+ code = u'\n'.join(self.content)
+ literal = nodes.literal_block(code, code)
+ literal['language'] = self.arguments[0]
+ literal['linenos'] = 'linenos' in self.options
+ return [literal]
+
+
+class LiteralInclude(Directive):
+ """
+ Like ``.. include:: :literal:``, but only warns if the include file is
+ not found, and does not raise errors. Also has several options for
+ selecting what to include.
+ """
+
+ has_content = False
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = False
+ option_spec = {
+ 'linenos': directives.flag,
+ 'language': directives.unchanged_required,
+ 'encoding': directives.encoding,
+ 'pyobject': directives.unchanged_required,
+ 'lines': directives.unchanged_required,
+ 'start-after': directives.unchanged_required,
+ 'end-before': directives.unchanged_required,
+ }
+
+ def run(self):
+ document = self.state.document
+ filename = self.arguments[0]
+ if not document.settings.file_insertion_enabled:
+ return [document.reporter.warning('File insertion disabled',
+ line=self.lineno)]
+ env = document.settings.env
+ if filename.startswith('/') or filename.startswith(os.sep):
+ rel_fn = filename[1:]
+ else:
+ docdir = path.dirname(env.doc2path(env.docname, base=None))
+ rel_fn = path.normpath(path.join(docdir, filename))
+ fn = path.join(env.srcdir, rel_fn)
+
+ if 'pyobject' in self.options and 'lines' in self.options:
+ return [document.reporter.warning(
+ 'Cannot use both "pyobject" and "lines" options',
+ line=self.lineno)]
+
+ encoding = self.options.get('encoding', env.config.source_encoding)
try:
- linenothreshold = int(options['linenothreshold'])
- except Exception:
- linenothreshold = 10
- else:
- linenothreshold = sys.maxint
- return [addnodes.highlightlang(lang=arguments[0].strip(),
- linenothreshold=linenothreshold)]
+ f = codecs.open(fn, 'rU', encoding)
+ lines = f.readlines()
+ f.close()
+ except (IOError, OSError):
+ return [document.reporter.warning(
+ 'Include file %r not found or reading it failed' % filename,
+ line=self.lineno)]
+ except UnicodeError:
+ return [document.reporter.warning(
+ 'Encoding %r used for reading included file %r seems to '
+ 'be wrong, try giving an :encoding: option' %
+ (encoding, filename))]
-highlightlang_directive.content = 0
-highlightlang_directive.arguments = (1, 0, 0)
-highlightlang_directive.options = {'linenothreshold': directives.unchanged}
-directives.register_directive('highlight', highlightlang_directive)
-directives.register_directive('highlightlang', highlightlang_directive) # old name
+ objectname = self.options.get('pyobject')
+ if objectname is not None:
+ from sphinx.pycode import ModuleAnalyzer
+ analyzer = ModuleAnalyzer.for_file(fn, '')
+ tags = analyzer.find_tags()
+ if objectname not in tags:
+ return [document.reporter.warning(
+ 'Object named %r not found in include file %r' %
+ (objectname, filename), line=self.lineno)]
+ else:
+ lines = lines[tags[objectname][1]-1 : tags[objectname][2]-1]
+ linespec = self.options.get('lines')
+ if linespec is not None:
+ try:
+ linelist = parselinenos(linespec, len(lines))
+ except ValueError, err:
+ return [document.reporter.warning(str(err), line=self.lineno)]
+ lines = [lines[i] for i in linelist]
-# ------ code-block directive -------------------------------------------------------
+ startafter = self.options.get('start-after')
+ endbefore = self.options.get('end-before')
+ if startafter is not None or endbefore is not None:
+ use = not startafter
+ res = []
+ for line in lines:
+ if not use and startafter in line:
+ use = True
+ elif use and endbefore in line:
+ use = False
+ break
+ elif use:
+ res.append(line)
+ lines = res
-def codeblock_directive(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- code = u'\n'.join(content)
- literal = nodes.literal_block(code, code)
- literal['language'] = arguments[0]
- literal['linenos'] = 'linenos' in options
- return [literal]
-
-codeblock_directive.content = 1
-codeblock_directive.arguments = (1, 0, 0)
-codeblock_directive.options = {'linenos': directives.flag}
-directives.register_directive('code-block', codeblock_directive)
-directives.register_directive('sourcecode', codeblock_directive)
-
-
-# ------ literalinclude directive ---------------------------------------------------
-
-def literalinclude_directive(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- """Like .. include:: :literal:, but only warns if the include file is not found."""
- if not state.document.settings.file_insertion_enabled:
- return [state.document.reporter.warning('File insertion disabled', line=lineno)]
- env = state.document.settings.env
- rel_fn = arguments[0]
- source_dir = path.dirname(path.abspath(state_machine.input_lines.source(
- lineno - state_machine.input_offset - 1)))
- fn = path.normpath(path.join(source_dir, rel_fn))
-
- encoding = options.get('encoding', env.config.source_encoding)
- try:
- f = codecs.open(fn, 'r', encoding)
- text = f.read()
- f.close()
- except (IOError, OSError):
- retnode = state.document.reporter.warning(
- 'Include file %r not found or reading it failed' % arguments[0], line=lineno)
- except UnicodeError:
- retnode = state.document.reporter.warning(
- 'Encoding %r used for reading included file %r seems to '
- 'be wrong, try giving an :encoding: option' %
- (encoding, arguments[0]))
- else:
+ text = ''.join(lines)
retnode = nodes.literal_block(text, text, source=fn)
retnode.line = 1
- if options.get('language', ''):
- retnode['language'] = options['language']
- if 'linenos' in options:
+ if self.options.get('language', ''):
+ retnode['language'] = self.options['language']
+ if 'linenos' in self.options:
retnode['linenos'] = True
- state.document.settings.env.note_dependency(rel_fn)
- return [retnode]
+ document.settings.env.note_dependency(rel_fn)
+ return [retnode]
-literalinclude_directive.options = {'linenos': directives.flag,
- 'language': directives.unchanged,
- 'encoding': directives.encoding}
-literalinclude_directive.content = 0
-literalinclude_directive.arguments = (1, 0, 0)
-directives.register_directive('literalinclude', literalinclude_directive)
+
+directives.register_directive('highlight', directive_dwim(Highlight))
+directives.register_directive('highlightlang', directive_dwim(Highlight)) # old
+directives.register_directive('code-block', directive_dwim(CodeBlock))
+directives.register_directive('sourcecode', directive_dwim(CodeBlock))
+directives.register_directive('literalinclude', directive_dwim(LiteralInclude))
diff --git a/sphinx/directives/desc.py b/sphinx/directives/desc.py
index a73748920..60b43a5d9 100644
--- a/sphinx/directives/desc.py
+++ b/sphinx/directives/desc.py
@@ -3,8 +3,8 @@
sphinx.directives.desc
~~~~~~~~~~~~~~~~~~~~~~
- :copyright: 2007-2008 by Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
import re
@@ -14,258 +14,35 @@ from docutils import nodes
from docutils.parsers.rst import directives
from sphinx import addnodes
+from sphinx.util import ws_re
+from sphinx.util.compat import Directive, directive_dwim
-ws_re = re.compile(r'\s+')
-
-# ------ information units ---------------------------------------------------------
-
-def desc_index_text(desctype, module, name, add_modules):
- if desctype == 'function':
- if not module:
- return _('%s() (built-in function)') % name
- return _('%s() (in module %s)') % (name, module)
- elif desctype == 'data':
- if not module:
- return _('%s (built-in variable)') % name
- return _('%s (in module %s)') % (name, module)
- elif desctype == 'class':
- if not module:
- return _('%s (built-in class)') % name
- return _('%s (class in %s)') % (name, module)
- elif desctype == 'exception':
- return name
- elif desctype == 'method':
- try:
- clsname, methname = name.rsplit('.', 1)
- except ValueError:
- if module:
- return _('%s() (in module %s)') % (name, module)
- else:
- return '%s()' % name
- if module and add_modules:
- return _('%s() (%s.%s method)') % (methname, module, clsname)
- else:
- return _('%s() (%s method)') % (methname, clsname)
- elif desctype == 'staticmethod':
- try:
- clsname, methname = name.rsplit('.', 1)
- except ValueError:
- if module:
- return _('%s() (in module %s)') % (name, module)
- else:
- return '%s()' % name
- if module and add_modules:
- return _('%s() (%s.%s static method)') % (methname, module, clsname)
- else:
- return _('%s() (%s static method)') % (methname, clsname)
- elif desctype == 'attribute':
- try:
- clsname, attrname = name.rsplit('.', 1)
- except ValueError:
- if module:
- return _('%s (in module %s)') % (name, module)
- else:
- return name
- if module and add_modules:
- return _('%s (%s.%s attribute)') % (attrname, module, clsname)
- else:
- return _('%s (%s attribute)') % (attrname, clsname)
- elif desctype == 'cfunction':
- return _('%s (C function)') % name
- elif desctype == 'cmember':
- return _('%s (C member)') % name
- elif desctype == 'cmacro':
- return _('%s (C macro)') % name
- elif desctype == 'ctype':
- return _('%s (C type)') % name
- elif desctype == 'cvar':
- return _('%s (C variable)') % name
- else:
- raise ValueError('unhandled descenv: %s' % desctype)
+def _is_only_paragraph(node):
+ """True if the node only contains one paragraph (and system messages)."""
+ if len(node) == 0:
+ return False
+ elif len(node) > 1:
+ for subnode in node[1:]:
+ if not isinstance(subnode, nodes.system_message):
+ return False
+ if isinstance(node[0], nodes.paragraph):
+ return True
+ return False
-# ------ make field lists (like :param foo:) in desc bodies prettier
-
-_ = lambda x: x # make gettext extraction in constants possible
-
-doc_fields_with_arg = {
- 'param': '%param',
- 'parameter': '%param',
- 'arg': '%param',
- 'argument': '%param',
- 'keyword': '%param',
- 'kwarg': '%param',
- 'kwparam': '%param',
- 'type': '%type',
- 'raises': _('Raises'),
- 'raise': 'Raises',
- 'exception': 'Raises',
- 'except': 'Raises',
- 'var': _('Variable'),
- 'ivar': 'Variable',
- 'cvar': 'Variable',
- 'returns': _('Returns'),
- 'return': 'Returns',
-}
-
-doc_fields_without_arg = {
- 'returns': 'Returns',
- 'return': 'Returns',
- 'rtype': _('Return type'),
-}
-
-del _
-
-def handle_doc_fields(node):
- # don't traverse, only handle field lists that are immediate children
- for child in node.children:
- if not isinstance(child, nodes.field_list):
- continue
- params = None
- param_nodes = {}
- param_types = {}
- new_list = nodes.field_list()
- for field in child:
- fname, fbody = field
- try:
- typ, obj = fname.astext().split(None, 1)
- typ = _(doc_fields_with_arg[typ])
- if len(fbody.children) == 1 and \
- isinstance(fbody.children[0], nodes.paragraph):
- children = fbody.children[0].children
- else:
- children = fbody.children
- if typ == '%param':
- if not params:
- pfield = nodes.field()
- pfield += nodes.field_name('', _('Parameters'))
- pfield += nodes.field_body()
- params = nodes.bullet_list()
- pfield[1] += params
- new_list += pfield
- dlitem = nodes.list_item()
- dlpar = nodes.paragraph()
- dlpar += nodes.emphasis(obj, obj)
- dlpar += nodes.Text(' -- ', ' -- ')
- dlpar += children
- param_nodes[obj] = dlpar
- dlitem += dlpar
- params += dlitem
- elif typ == '%type':
- param_types[obj] = fbody.astext()
- else:
- fieldname = typ + ' ' + obj
- nfield = nodes.field()
- nfield += nodes.field_name(fieldname, fieldname)
- nfield += nodes.field_body()
- nfield[1] += fbody.children
- new_list += nfield
- except (KeyError, ValueError):
- fnametext = fname.astext()
- try:
- typ = _(doc_fields_without_arg[fnametext])
- except KeyError:
- # at least capitalize the field name
- typ = fnametext.capitalize()
- fname[0] = nodes.Text(typ)
- new_list += field
- for param, type in param_types.iteritems():
- if param in param_nodes:
- param_nodes[param].insert(1, nodes.Text(' (%s)' % type))
- child.replace_self(new_list)
-
-
-# ------ functions to parse a Python or C signature and create desc_* nodes.
-
+# REs for Python signatures
py_sig_re = re.compile(
r'''^ ([\w.]*\.)? # class name(s)
(\w+) \s* # thing name
- (?: \((.*)\) # optional arguments
- (\s* -> \s* .*)? )? $ # optional return annotation
+ (?: \((.*)\) # optional: arguments
+ (?:\s* -> \s* (.*))? # return annotation
+ )? $ # and nothing more
''', re.VERBOSE)
py_paramlist_re = re.compile(r'([\[\],])') # split at '[', ']' and ','
-def parse_py_signature(signode, sig, desctype, module, env):
- """
- Transform a python signature into RST nodes.
- Return (fully qualified name of the thing, classname if any).
-
- If inside a class, the current class name is handled intelligently:
- * it is stripped from the displayed name if present
- * it is added to the full name (return value) if not present
- """
- m = py_sig_re.match(sig)
- if m is None:
- raise ValueError
- classname, name, arglist, retann = m.groups()
-
- if retann:
- retann = u' \N{RIGHTWARDS ARROW} ' + retann.strip()[2:]
-
- if env.currclass:
- add_module = False
- if classname and classname.startswith(env.currclass):
- fullname = classname + name
- # class name is given again in the signature
- classname = classname[len(env.currclass):].lstrip('.')
- elif classname:
- # class name is given in the signature, but different
- # (shouldn't happen)
- fullname = env.currclass + '.' + classname + name
- else:
- # class name is not given in the signature
- fullname = env.currclass + '.' + name
- else:
- add_module = True
- fullname = classname and classname + name or name
-
- if desctype == 'staticmethod':
- signode += addnodes.desc_annotation('static ', 'static ')
-
- if classname:
- signode += addnodes.desc_addname(classname, classname)
- # exceptions are a special case, since they are documented in the
- # 'exceptions' module.
- elif add_module and env.config.add_module_names and \
- module and module != 'exceptions':
- nodetext = module + '.'
- signode += addnodes.desc_addname(nodetext, nodetext)
-
- signode += addnodes.desc_name(name, name)
- if not arglist:
- if desctype in ('function', 'method', 'staticmethod'):
- # for callables, add an empty parameter list
- signode += addnodes.desc_parameterlist()
- if retann:
- signode += addnodes.desc_type(retann, retann)
- return fullname, classname
- signode += addnodes.desc_parameterlist()
-
- stack = [signode[-1]]
- for token in py_paramlist_re.split(arglist):
- if token == '[':
- opt = addnodes.desc_optional()
- stack[-1] += opt
- stack.append(opt)
- elif token == ']':
- try:
- stack.pop()
- except IndexError:
- raise ValueError
- elif not token or token == ',' or token.isspace():
- pass
- else:
- token = token.strip()
- stack[-1] += addnodes.desc_parameter(token, token)
- if len(stack) != 1:
- raise ValueError
- if retann:
- signode += addnodes.desc_type(retann, retann)
- return fullname, classname
-
-
+# REs for C signatures
c_sig_re = re.compile(
r'''^([^(]*?) # return type
([\w:]+) \s* # thing name (colon allowed for C++ class names)
@@ -280,242 +57,689 @@ c_funcptr_sig_re = re.compile(
''', re.VERBOSE)
c_funcptr_name_re = re.compile(r'^\(\s*\*\s*(.*?)\s*\)$')
+# RE for option descriptions
+option_desc_re = re.compile(
+ r'((?:/|-|--)[-_a-zA-Z0-9]+)(\s*.*?)(?=,\s+(?:/|-|--)|$)')
+
# RE to split at word boundaries
wsplit_re = re.compile(r'(\W+)')
-# These C types aren't described in the reference, so don't try to create
-# a cross-reference to them
-stopwords = set(('const', 'void', 'char', 'int', 'long', 'FILE', 'struct'))
-
-def parse_c_type(node, ctype):
- # add cross-ref nodes for all words
- for part in filter(None, wsplit_re.split(ctype)):
- tnode = nodes.Text(part, part)
- if part[0] in string.letters+'_' and part not in stopwords:
- pnode = addnodes.pending_xref(
- '', reftype='ctype', reftarget=part, modname=None, classname=None)
- pnode += tnode
- node += pnode
- else:
- node += tnode
-
-def parse_c_signature(signode, sig, desctype):
- """Transform a C (or C++) signature into RST nodes."""
- # first try the function pointer signature regex, it's more specific
- m = c_funcptr_sig_re.match(sig)
- if m is None:
- m = c_sig_re.match(sig)
- if m is None:
- raise ValueError('no match')
- rettype, name, arglist, const = m.groups()
-
- signode += addnodes.desc_type('', '')
- parse_c_type(signode[-1], rettype)
- try:
- classname, funcname = name.split('::', 1)
- classname += '::'
- signode += addnodes.desc_addname(classname, classname)
- signode += addnodes.desc_name(funcname, funcname)
- # name (the full name) is still both parts
- except ValueError:
- signode += addnodes.desc_name(name, name)
- # clean up parentheses from canonical name
- m = c_funcptr_name_re.match(name)
- if m:
- name = m.group(1)
- if not arglist:
- if desctype == 'cfunction':
- # for functions, add an empty parameter list
- signode += addnodes.desc_parameterlist()
- return name
-
- paramlist = addnodes.desc_parameterlist()
- arglist = arglist.replace('`', '').replace('\\ ', '') # remove markup
- # this messes up function pointer types, but not too badly ;)
- args = arglist.split(',')
- for arg in args:
- arg = arg.strip()
- param = addnodes.desc_parameter('', '', noemph=True)
- try:
- ctype, argname = arg.rsplit(' ', 1)
- except ValueError:
- # no argument name given, only the type
- parse_c_type(param, arg)
- else:
- parse_c_type(param, ctype)
- param += nodes.emphasis(' '+argname, ' '+argname)
- paramlist += param
- signode += paramlist
- if const:
- signode += addnodes.desc_addname(const, const)
- return name
+# RE to strip backslash escapes
+strip_backslash_re = re.compile(r'\\(?=[^\\])')
-option_desc_re = re.compile(
- r'(/|-|--)([-_a-zA-Z0-9]+)(\s*.*?)(?=,\s+(?:/|-|--)|$)')
+class DescDirective(Directive):
+ """
+ Directive to describe a class, function or similar object. Not used
+ directly, but subclassed to add custom behavior.
+ """
-def parse_option_desc(signode, sig):
- """Transform an option description into RST nodes."""
- count = 0
- firstname = ''
- for m in option_desc_re.finditer(sig):
- prefix, optname, args = m.groups()
- if count:
- signode += addnodes.desc_addname(', ', ', ')
- signode += addnodes.desc_name(prefix+optname, prefix+optname)
- signode += addnodes.desc_addname(args, args)
- if not count:
- firstname = optname
- count += 1
- if not firstname:
- raise ValueError
- return firstname
+ has_content = True
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = True
+ option_spec = {
+ 'noindex': directives.flag,
+ 'module': directives.unchanged,
+ }
+ _ = lambda x: x # make gettext extraction in constants possible
-def desc_directive(desctype, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- env = state.document.settings.env
- inode = addnodes.index(entries=[])
- node = addnodes.desc()
- node['desctype'] = desctype
+ doc_fields_with_arg = {
+ 'param': '%param',
+ 'parameter': '%param',
+ 'arg': '%param',
+ 'argument': '%param',
+ 'keyword': '%param',
+ 'kwarg': '%param',
+ 'kwparam': '%param',
+ 'type': '%type',
+ 'raises': _('Raises'),
+ 'raise': 'Raises',
+ 'exception': 'Raises',
+ 'except': 'Raises',
+ 'var': _('Variable'),
+ 'ivar': 'Variable',
+ 'cvar': 'Variable',
+ 'returns': _('Returns'),
+ 'return': 'Returns',
+ }
- noindex = ('noindex' in options)
- node['noindex'] = noindex
- # remove backslashes to support (dummy) escapes; helps Vim's highlighting
- signatures = map(lambda s: s.strip().replace('\\', ''), arguments[0].split('\n'))
- names = []
- clsname = None
- module = options.get('module', env.currmodule)
- for i, sig in enumerate(signatures):
- # add a signature node for each signature in the current unit
- # and add a reference target for it
- sig = sig.strip()
- signode = addnodes.desc_signature(sig, '')
- signode['first'] = False
- node.append(signode)
- try:
- if desctype in ('function', 'data', 'class', 'exception',
- 'method', 'staticmethod', 'attribute'):
- name, clsname = parse_py_signature(signode, sig, desctype, module, env)
- elif desctype in ('cfunction', 'cmember', 'cmacro', 'ctype', 'cvar'):
- name = parse_c_signature(signode, sig, desctype)
- elif desctype == 'cmdoption':
- optname = parse_option_desc(signode, sig)
- if not noindex:
- targetname = 'cmdoption-' + optname
- signode['ids'].append(targetname)
- state.document.note_explicit_target(signode)
- inode['entries'].append(('pair', _('command line option; %s') % sig,
- targetname, targetname))
- env.note_reftarget('option', optname, targetname)
+ doc_fields_with_linked_arg = ('raises', 'raise', 'exception', 'except')
+
+ doc_fields_without_arg = {
+ 'returns': 'Returns',
+ 'return': 'Returns',
+ 'rtype': _('Return type'),
+ }
+
+ def handle_doc_fields(self, node):
+ """
+ Convert field lists with known keys inside the description content into
+ better-looking equivalents.
+ """
+ # don't traverse, only handle field lists that are immediate children
+ for child in node.children:
+ if not isinstance(child, nodes.field_list):
continue
- elif desctype == 'describe':
+ params = []
+ pfield = None
+ param_nodes = {}
+ param_types = {}
+ new_list = nodes.field_list()
+ for field in child:
+ fname, fbody = field
+ try:
+ typ, obj = fname.astext().split(None, 1)
+ typdesc = _(self.doc_fields_with_arg[typ])
+ if _is_only_paragraph(fbody):
+ children = fbody.children[0].children
+ else:
+ children = fbody.children
+ if typdesc == '%param':
+ if not params:
+ # add the field that later gets all the parameters
+ pfield = nodes.field()
+ new_list += pfield
+ dlitem = nodes.list_item()
+ dlpar = nodes.paragraph()
+ dlpar += nodes.emphasis(obj, obj)
+ dlpar += nodes.Text(' -- ', ' -- ')
+ dlpar += children
+ param_nodes[obj] = dlpar
+ dlitem += dlpar
+ params.append(dlitem)
+ elif typdesc == '%type':
+ typenodes = fbody.children
+ if _is_only_paragraph(fbody):
+ typenodes = ([nodes.Text(' (')] +
+ typenodes[0].children +
+ [nodes.Text(')')])
+ param_types[obj] = typenodes
+ else:
+ fieldname = typdesc + ' '
+ nfield = nodes.field()
+ nfieldname = nodes.field_name(fieldname, fieldname)
+ nfield += nfieldname
+ node = nfieldname
+ if typ in self.doc_fields_with_linked_arg:
+ node = addnodes.pending_xref(
+ obj, reftype='obj', refcaption=False,
+ reftarget=obj, modname=self.env.currmodule,
+ classname=self.env.currclass)
+ nfieldname += node
+ node += nodes.Text(obj, obj)
+ nfield += nodes.field_body()
+ nfield[1] += fbody.children
+ new_list += nfield
+ except (KeyError, ValueError):
+ fnametext = fname.astext()
+ try:
+ typ = _(self.doc_fields_without_arg[fnametext])
+ except KeyError:
+ # at least capitalize the field name
+ typ = fnametext.capitalize()
+ fname[0] = nodes.Text(typ)
+ new_list += field
+ if params:
+ if len(params) == 1:
+ pfield += nodes.field_name('', _('Parameter'))
+ pfield += nodes.field_body()
+ pfield[1] += params[0][0]
+ else:
+ pfield += nodes.field_name('', _('Parameters'))
+ pfield += nodes.field_body()
+ pfield[1] += nodes.bullet_list()
+ pfield[1][0].extend(params)
+
+ for param, type in param_types.iteritems():
+ if param in param_nodes:
+ param_nodes[param][1:1] = type
+ child.replace_self(new_list)
+
+ def get_signatures(self):
+ """
+ Retrieve the signatures to document from the directive arguments.
+ """
+ # remove backslashes to support (dummy) escapes; helps Vim highlighting
+ return [strip_backslash_re.sub('', sig.strip())
+ for sig in self.arguments[0].split('\n')]
+
+ def parse_signature(self, sig, signode):
+ """
+ Parse the signature *sig* into individual nodes and append them to
+ *signode*. If ValueError is raised, parsing is aborted and the whole
+ *sig* is put into a single desc_name node.
+ """
+ raise ValueError
+
+ def add_target_and_index(self, name, sig, signode):
+ """
+ Add cross-reference IDs and entries to self.indexnode, if applicable.
+ """
+ return # do nothing by default
+
+ def before_content(self):
+ """
+ Called before parsing content. Used to set information about the current
+ directive context on the build environment.
+ """
+ pass
+
+ def after_content(self):
+ """
+ Called after parsing content. Used to reset information about the
+ current directive context on the build environment.
+ """
+ pass
+
+ def run(self):
+ self.desctype = self.name
+ self.env = self.state.document.settings.env
+ self.indexnode = addnodes.index(entries=[])
+
+ node = addnodes.desc()
+ node.document = self.state.document
+ node['desctype'] = self.desctype
+ node['noindex'] = noindex = ('noindex' in self.options)
+
+ self.names = []
+ signatures = self.get_signatures()
+ for i, sig in enumerate(signatures):
+ # add a signature node for each signature in the current unit
+ # and add a reference target for it
+ signode = addnodes.desc_signature(sig, '')
+ signode['first'] = False
+ node.append(signode)
+ try:
+ # name can also be a tuple, e.g. (classname, objname)
+ name = self.parse_signature(sig, signode)
+ except ValueError, err:
+ # signature parsing failed
signode.clear()
signode += addnodes.desc_name(sig, sig)
- continue
+ continue # we don't want an index entry here
+ if not noindex and name not in self.names:
+ # only add target and index entry if this is the first
+ # description of the object with this name in this desc block
+ self.names.append(name)
+ self.add_target_and_index(name, sig, signode)
+
+ contentnode = addnodes.desc_content()
+ node.append(contentnode)
+ if self.names:
+ # needed for association of version{added,changed} directives
+ self.env.currdesc = self.names[0]
+ self.before_content()
+ self.state.nested_parse(self.content, self.content_offset, contentnode)
+ self.handle_doc_fields(contentnode)
+ self.env.currdesc = None
+ self.after_content()
+ return [self.indexnode, node]
+
+
+class PythonDesc(DescDirective):
+ """
+ Description of a general Python object.
+ """
+
+ def get_signature_prefix(self, sig):
+ """
+ May return a prefix to put before the object name in the signature.
+ """
+ return ''
+
+ def needs_arglist(self):
+ """
+ May return true if an empty argument list is to be generated even if
+ the document contains none.
+ """
+ return False
+
+ def parse_signature(self, sig, signode):
+ """
+ Transform a Python signature into RST nodes.
+ Returns (fully qualified name of the thing, classname if any).
+
+ If inside a class, the current class name is handled intelligently:
+ * it is stripped from the displayed name if present
+ * it is added to the full name (return value) if not present
+ """
+ m = py_sig_re.match(sig)
+ if m is None:
+ raise ValueError
+ classname, name, arglist, retann = m.groups()
+
+ if self.env.currclass:
+ add_module = False
+ if classname and classname.startswith(self.env.currclass):
+ fullname = classname + name
+ # class name is given again in the signature
+ classname = classname[len(self.env.currclass):].lstrip('.')
+ elif classname:
+ # class name is given in the signature, but different
+ # (shouldn't happen)
+ fullname = self.env.currclass + '.' + classname + name
else:
- # another registered generic x-ref directive
- rolename, indextemplate, parse_node = additional_xref_types[desctype]
- if parse_node:
- fullname = parse_node(env, sig, signode)
+ # class name is not given in the signature
+ fullname = self.env.currclass + '.' + name
+ else:
+ add_module = True
+ fullname = classname and classname + name or name
+
+ prefix = self.get_signature_prefix(sig)
+ if prefix:
+ signode += addnodes.desc_annotation(prefix, prefix)
+
+ if classname:
+ signode += addnodes.desc_addname(classname, classname)
+ # exceptions are a special case, since they are documented in the
+ # 'exceptions' module.
+ elif add_module and self.env.config.add_module_names:
+ modname = self.options.get('module', self.env.currmodule)
+ if modname and modname != 'exceptions':
+ nodetext = modname + '.'
+ signode += addnodes.desc_addname(nodetext, nodetext)
+
+ signode += addnodes.desc_name(name, name)
+ if not arglist:
+ if self.needs_arglist():
+ # for callables, add an empty parameter list
+ signode += addnodes.desc_parameterlist()
+ if retann:
+ signode += addnodes.desc_returns(retann, retann)
+ return fullname, classname
+ signode += addnodes.desc_parameterlist()
+
+ stack = [signode[-1]]
+ for token in py_paramlist_re.split(arglist):
+ if token == '[':
+ opt = addnodes.desc_optional()
+ stack[-1] += opt
+ stack.append(opt)
+ elif token == ']':
+ try:
+ stack.pop()
+ except IndexError:
+ raise ValueError
+ elif not token or token == ',' or token.isspace():
+ pass
+ else:
+ token = token.strip()
+ stack[-1] += addnodes.desc_parameter(token, token)
+ if len(stack) != 1:
+ raise ValueError
+ if retann:
+ signode += addnodes.desc_returns(retann, retann)
+ return fullname, classname
+
+ def get_index_text(self, modname, name):
+ """
+ Return the text for the index entry of the object.
+ """
+ raise NotImplementedError('must be implemented in subclasses')
+
+ def add_target_and_index(self, name_cls, sig, signode):
+ modname = self.options.get('module', self.env.currmodule)
+ fullname = (modname and modname + '.' or '') + name_cls[0]
+ # note target
+ if fullname not in self.state.document.ids:
+ signode['names'].append(fullname)
+ signode['ids'].append(fullname)
+ signode['first'] = (not self.names)
+ self.state.document.note_explicit_target(signode)
+ self.env.note_descref(fullname, self.desctype, self.lineno)
+
+ indextext = self.get_index_text(modname, name_cls)
+ if indextext:
+ self.indexnode['entries'].append(('single', indextext,
+ fullname, fullname))
+
+ def before_content(self):
+ # needed for automatic qualification of members (reset in subclasses)
+ self.clsname_set = False
+
+ def after_content(self):
+ if self.clsname_set:
+ self.env.currclass = None
+
+
+class ModulelevelDesc(PythonDesc):
+ """
+ Description of an object on module level (functions, data).
+ """
+
+ def needs_arglist(self):
+ return self.desctype == 'function'
+
+ def get_index_text(self, modname, name_cls):
+ if self.desctype == 'function':
+ if not modname:
+ return _('%s() (built-in function)') % name_cls[0]
+ return _('%s() (in module %s)') % (name_cls[0], modname)
+ elif self.desctype == 'data':
+ if not modname:
+ return _('%s (built-in variable)') % name_cls[0]
+ return _('%s (in module %s)') % (name_cls[0], modname)
+ else:
+ return ''
+
+
+class ClasslikeDesc(PythonDesc):
+ """
+ Description of a class-like object (classes, interfaces, exceptions).
+ """
+
+ def get_signature_prefix(self, sig):
+ return self.desctype + ' '
+
+ def get_index_text(self, modname, name_cls):
+ if self.desctype == 'class':
+ if not modname:
+ return _('%s (built-in class)') % name_cls[0]
+ return _('%s (class in %s)') % (name_cls[0], modname)
+ elif self.desctype == 'exception':
+ return name_cls[0]
+ else:
+ return ''
+
+ def before_content(self):
+ PythonDesc.before_content(self)
+ if self.names:
+ self.env.currclass = self.names[0][0]
+ self.clsname_set = True
+
+
+class ClassmemberDesc(PythonDesc):
+ """
+ Description of a class member (methods, attributes).
+ """
+
+ def needs_arglist(self):
+ return self.desctype.endswith('method')
+
+ def get_signature_prefix(self, sig):
+ if self.desctype == 'staticmethod':
+ return 'static '
+ elif self.desctype == 'classmethod':
+ return 'classmethod '
+ return ''
+
+ def get_index_text(self, modname, name_cls):
+ name, cls = name_cls
+ add_modules = self.env.config.add_module_names
+ if self.desctype == 'method':
+ try:
+ clsname, methname = name.rsplit('.', 1)
+ except ValueError:
+ if modname:
+ return _('%s() (in module %s)') % (name, modname)
else:
- signode.clear()
- signode += addnodes.desc_name(sig, sig)
- # normalize whitespace like xfileref_role does
- fullname = ws_re.sub('', sig)
- if not noindex:
- targetname = '%s-%s' % (rolename, fullname)
- signode['ids'].append(targetname)
- state.document.note_explicit_target(signode)
- if indextemplate:
- indexentry = _(indextemplate) % (fullname,)
- indextype = 'single'
- colon = indexentry.find(':')
- if colon != -1:
- indextype = indexentry[:colon].strip()
- indexentry = indexentry[colon+1:].strip()
- inode['entries'].append((indextype, indexentry,
- targetname, targetname))
- env.note_reftarget(rolename, fullname, targetname)
- # don't use object indexing below
- continue
- except ValueError, err:
- # signature parsing failed
+ return '%s()' % name
+ if modname and add_modules:
+ return _('%s() (%s.%s method)') % (methname, modname, clsname)
+ else:
+ return _('%s() (%s method)') % (methname, clsname)
+ elif self.desctype == 'staticmethod':
+ try:
+ clsname, methname = name.rsplit('.', 1)
+ except ValueError:
+ if modname:
+ return _('%s() (in module %s)') % (name, modname)
+ else:
+ return '%s()' % name
+ if modname and add_modules:
+ return _('%s() (%s.%s static method)') % (methname, modname,
+ clsname)
+ else:
+ return _('%s() (%s static method)') % (methname, clsname)
+ elif self.desctype == 'classmethod':
+ try:
+ clsname, methname = name.rsplit('.', 1)
+ except ValueError:
+ if modname:
+ return '%s() (in module %s)' % (name, modname)
+ else:
+ return '%s()' % name
+ if modname:
+ return '%s() (%s.%s class method)' % (methname, modname,
+ clsname)
+ else:
+ return '%s() (%s class method)' % (methname, clsname)
+ elif self.desctype == 'attribute':
+ try:
+ clsname, attrname = name.rsplit('.', 1)
+ except ValueError:
+ if modname:
+ return _('%s (in module %s)') % (name, modname)
+ else:
+ return name
+ if modname and add_modules:
+ return _('%s (%s.%s attribute)') % (attrname, modname, clsname)
+ else:
+ return _('%s (%s attribute)') % (attrname, clsname)
+ else:
+ return ''
+
+ def before_content(self):
+ PythonDesc.before_content(self)
+ if self.names and self.names[-1][1] and not self.env.currclass:
+ self.env.currclass = self.names[-1][1].strip('.')
+ self.clsname_set = True
+
+
+class CDesc(DescDirective):
+ """
+ Description of a C language object.
+ """
+
+ # These C types aren't described anywhere, so don't try to create
+ # a cross-reference to them
+ stopwords = set(('const', 'void', 'char', 'int', 'long', 'FILE', 'struct'))
+
+ def _parse_type(self, node, ctype):
+ # add cross-ref nodes for all words
+ for part in filter(None, wsplit_re.split(ctype)):
+ tnode = nodes.Text(part, part)
+ if part[0] in string.ascii_letters+'_' and \
+ part not in self.stopwords:
+ pnode = addnodes.pending_xref(
+ '', reftype='ctype', reftarget=part,
+ modname=None, classname=None)
+ pnode += tnode
+ node += pnode
+ else:
+ node += tnode
+
+ def parse_signature(self, sig, signode):
+ """Transform a C (or C++) signature into RST nodes."""
+ # first try the function pointer signature regex, it's more specific
+ m = c_funcptr_sig_re.match(sig)
+ if m is None:
+ m = c_sig_re.match(sig)
+ if m is None:
+ raise ValueError('no match')
+ rettype, name, arglist, const = m.groups()
+
+ signode += addnodes.desc_type('', '')
+ self._parse_type(signode[-1], rettype)
+ try:
+ classname, funcname = name.split('::', 1)
+ classname += '::'
+ signode += addnodes.desc_addname(classname, classname)
+ signode += addnodes.desc_name(funcname, funcname)
+ # name (the full name) is still both parts
+ except ValueError:
+ signode += addnodes.desc_name(name, name)
+ # clean up parentheses from canonical name
+ m = c_funcptr_name_re.match(name)
+ if m:
+ name = m.group(1)
+ if not arglist:
+ if self.desctype == 'cfunction':
+ # for functions, add an empty parameter list
+ signode += addnodes.desc_parameterlist()
+ return name
+
+ paramlist = addnodes.desc_parameterlist()
+ arglist = arglist.replace('`', '').replace('\\ ', '') # remove markup
+ # this messes up function pointer types, but not too badly ;)
+ args = arglist.split(',')
+ for arg in args:
+ arg = arg.strip()
+ param = addnodes.desc_parameter('', '', noemph=True)
+ try:
+ ctype, argname = arg.rsplit(' ', 1)
+ except ValueError:
+ # no argument name given, only the type
+ self._parse_type(param, arg)
+ else:
+ self._parse_type(param, ctype)
+ param += nodes.emphasis(' '+argname, ' '+argname)
+ paramlist += param
+ signode += paramlist
+ if const:
+ signode += addnodes.desc_addname(const, const)
+ return name
+
+ def get_index_text(self, name):
+ if self.desctype == 'cfunction':
+ return _('%s (C function)') % name
+ elif self.desctype == 'cmember':
+ return _('%s (C member)') % name
+ elif self.desctype == 'cmacro':
+ return _('%s (C macro)') % name
+ elif self.desctype == 'ctype':
+ return _('%s (C type)') % name
+ elif self.desctype == 'cvar':
+ return _('%s (C variable)') % name
+ else:
+ return ''
+
+ def add_target_and_index(self, name, sig, signode):
+ # note target
+ if name not in self.state.document.ids:
+ signode['names'].append(name)
+ signode['ids'].append(name)
+ signode['first'] = (not self.names)
+ self.state.document.note_explicit_target(signode)
+ self.env.note_descref(name, self.desctype, self.lineno)
+
+ indextext = self.get_index_text(name)
+ if indextext:
+ self.indexnode['entries'].append(('single', indextext, name, name))
+
+
+class CmdoptionDesc(DescDirective):
+ """
+ Description of a command-line option (.. cmdoption).
+ """
+
+ def parse_signature(self, sig, signode):
+ """Transform an option description into RST nodes."""
+ count = 0
+ firstname = ''
+ for m in option_desc_re.finditer(sig):
+ optname, args = m.groups()
+ if count:
+ signode += addnodes.desc_addname(', ', ', ')
+ signode += addnodes.desc_name(optname, optname)
+ signode += addnodes.desc_addname(args, args)
+ if not count:
+ firstname = optname
+ count += 1
+ if not firstname:
+ raise ValueError
+ return firstname
+
+ def add_target_and_index(self, name, sig, signode):
+ targetname = name.replace('/', '-')
+ if self.env.currprogram:
+ targetname = '-' + self.env.currprogram + targetname
+ targetname = 'cmdoption' + targetname
+ signode['ids'].append(targetname)
+ self.state.document.note_explicit_target(signode)
+ self.indexnode['entries'].append(
+ ('pair', _('%scommand line option; %s') %
+ ((self.env.currprogram and
+ self.env.currprogram + ' ' or ''), sig),
+ targetname, targetname))
+ self.env.note_progoption(name, targetname)
+
+
+class GenericDesc(DescDirective):
+ """
+ A generic x-ref directive registered with Sphinx.add_description_unit().
+ """
+
+ def parse_signature(self, sig, signode):
+ parse_node = additional_xref_types[self.desctype][2]
+ if parse_node:
+ name = parse_node(self.env, sig, signode)
+ else:
signode.clear()
signode += addnodes.desc_name(sig, sig)
- continue # we don't want an index entry here
- # only add target and index entry if this is the first description of the
- # function name in this desc block
- if not noindex and name not in names:
- fullname = (module and module + '.' or '') + name
- # note target
- if fullname not in state.document.ids:
- signode['names'].append(fullname)
- signode['ids'].append(fullname)
- signode['first'] = (not names)
- state.document.note_explicit_target(signode)
- env.note_descref(fullname, desctype, lineno)
- names.append(name)
+ # normalize whitespace like xfileref_role does
+ name = ws_re.sub('', sig)
+ return name
- indextext = desc_index_text(desctype, module, name,
- env.config.add_module_names)
- inode['entries'].append(('single', indextext, fullname, fullname))
+ def add_target_and_index(self, name, sig, signode):
+ rolename, indextemplate = additional_xref_types[self.desctype][:2]
+ targetname = '%s-%s' % (rolename, name)
+ signode['ids'].append(targetname)
+ self.state.document.note_explicit_target(signode)
+ if indextemplate:
+ indexentry = _(indextemplate) % (name,)
+ indextype = 'single'
+ colon = indexentry.find(':')
+ if colon != -1:
+ indextype = indexentry[:colon].strip()
+ indexentry = indexentry[colon+1:].strip()
+ self.indexnode['entries'].append((indextype, indexentry,
+ targetname, targetname))
+ self.env.note_reftarget(rolename, name, targetname)
- subnode = addnodes.desc_content()
- # needed for automatic qualification of members
- clsname_set = False
- if desctype in ('class', 'exception') and names:
- env.currclass = names[0]
- clsname_set = True
- elif desctype in ('method', 'staticmethod', 'attribute') and \
- clsname and not env.currclass:
- env.currclass = clsname.strip('.')
- clsname_set = True
- # needed for association of version{added,changed} directives
- if names:
- env.currdesc = names[0]
- state.nested_parse(content, content_offset, subnode)
- handle_doc_fields(subnode)
- if clsname_set:
- env.currclass = None
- env.currdesc = None
- node.append(subnode)
- return [inode, node]
-desc_directive.content = 1
-desc_directive.arguments = (1, 0, 1)
-desc_directive.options = {'noindex': directives.flag,
- 'module': directives.unchanged}
+class Target(Directive):
+ """
+ Generic target for user-defined cross-reference types.
+ """
-desctypes = [
- # the Python ones
- 'function',
- 'data',
- 'class',
- 'method',
- 'staticmethod',
- 'attribute',
- 'exception',
- # the C ones
- 'cfunction',
- 'cmember',
- 'cmacro',
- 'ctype',
- 'cvar',
- # for command line options
- 'cmdoption',
- # the generic one
- 'describe',
- 'envvar',
-]
+ has_content = False
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = True
+ option_spec = {}
-for _name in desctypes:
- directives.register_directive(_name, desc_directive)
+ def run(self):
+ env = self.state.document.settings.env
+ rolename, indextemplate, foo = additional_xref_types[self.name]
+ # normalize whitespace in fullname like xfileref_role does
+ fullname = ws_re.sub('', self.arguments[0].strip())
+ targetname = '%s-%s' % (rolename, fullname)
+ node = nodes.target('', '', ids=[targetname])
+ self.state.document.note_explicit_target(node)
+ ret = [node]
+ if indextemplate:
+ indexentry = indextemplate % (fullname,)
+ indextype = 'single'
+ colon = indexentry.find(':')
+ if colon != -1:
+ indextype = indexentry[:colon].strip()
+ indexentry = indexentry[colon+1:].strip()
+ inode = addnodes.index(entries=[(indextype, indexentry,
+ targetname, targetname)])
+ ret.insert(0, inode)
+ env.note_reftarget(rolename, fullname, targetname)
+ return ret
+
+# Note: the target directive is not registered here, it is used by the
+# application when registering additional xref types.
_ = lambda x: x
# Generic cross-reference types; they can be registered in the application;
-# the directives are either desc_directive or target_directive
+# the directives are either desc_directive or target_directive.
additional_xref_types = {
# directive name: (role name, index text, function to parse the desc node)
'envvar': ('envvar', _('environment variable; %s'), None),
@@ -524,33 +748,22 @@ additional_xref_types = {
del _
-# ------ target --------------------------------------------------------------------
+directives.register_directive('describe', directive_dwim(DescDirective))
-def target_directive(targettype, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- """Generic target for user-defined cross-reference types."""
- env = state.document.settings.env
- rolename, indextemplate, foo = additional_xref_types[targettype]
- # normalize whitespace in fullname like xfileref_role does
- fullname = ws_re.sub('', arguments[0].strip())
- targetname = '%s-%s' % (rolename, fullname)
- node = nodes.target('', '', ids=[targetname])
- state.document.note_explicit_target(node)
- ret = [node]
- if indextemplate:
- indexentry = indextemplate % (fullname,)
- indextype = 'single'
- colon = indexentry.find(':')
- if colon != -1:
- indextype = indexentry[:colon].strip()
- indexentry = indexentry[colon+1:].strip()
- inode = addnodes.index(entries=[(indextype, indexentry, targetname, targetname)])
- ret.insert(0, inode)
- env.note_reftarget(rolename, fullname, targetname)
- return ret
+directives.register_directive('function', directive_dwim(ModulelevelDesc))
+directives.register_directive('data', directive_dwim(ModulelevelDesc))
+directives.register_directive('class', directive_dwim(ClasslikeDesc))
+directives.register_directive('exception', directive_dwim(ClasslikeDesc))
+directives.register_directive('method', directive_dwim(ClassmemberDesc))
+directives.register_directive('classmethod', directive_dwim(ClassmemberDesc))
+directives.register_directive('staticmethod', directive_dwim(ClassmemberDesc))
+directives.register_directive('attribute', directive_dwim(ClassmemberDesc))
-target_directive.content = 0
-target_directive.arguments = (1, 0, 1)
+directives.register_directive('cfunction', directive_dwim(CDesc))
+directives.register_directive('cmember', directive_dwim(CDesc))
+directives.register_directive('cmacro', directive_dwim(CDesc))
+directives.register_directive('ctype', directive_dwim(CDesc))
+directives.register_directive('cvar', directive_dwim(CDesc))
-# note, the target directive is not registered here, it is used by the application
-# when registering additional xref types
+directives.register_directive('cmdoption', directive_dwim(CmdoptionDesc))
+directives.register_directive('envvar', directive_dwim(GenericDesc))
diff --git a/sphinx/directives/other.py b/sphinx/directives/other.py
index 2e64eb0cf..bc9a54dff 100644
--- a/sphinx/directives/other.py
+++ b/sphinx/directives/other.py
@@ -3,248 +3,329 @@
sphinx.directives.other
~~~~~~~~~~~~~~~~~~~~~~~
- :copyright: 2007-2008 by Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
import re
-import posixpath
from docutils import nodes
from docutils.parsers.rst import directives
from sphinx import addnodes
-from sphinx.util import patfilter
-from sphinx.roles import caption_ref_re
from sphinx.locale import pairindextypes
-from sphinx.util.compat import make_admonition
+from sphinx.util import patfilter, ws_re, caption_ref_re, url_re, docname_join
+from sphinx.util.compat import Directive, directive_dwim, make_admonition
-# ------ the TOC tree ---------------------------------------------------------------
+class TocTree(Directive):
+ """
+ Directive to notify Sphinx about the hierarchical structure of the docs,
+ and to include a table-of-contents like tree in the current document.
+ """
-def toctree_directive(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- env = state.document.settings.env
- suffix = env.config.source_suffix
- dirname = posixpath.dirname(env.docname)
- glob = 'glob' in options
+ has_content = True
+ required_arguments = 0
+ optional_arguments = 0
+ final_argument_whitespace = False
+ option_spec = {
+ 'maxdepth': int,
+ 'glob': directives.flag,
+ 'hidden': directives.flag,
+ 'numbered': directives.flag,
+ }
- ret = []
- subnode = addnodes.toctree()
- includefiles = []
- includetitles = {}
- all_docnames = env.found_docs.copy()
- # don't add the currently visited file in catch-all patterns
- all_docnames.remove(env.docname)
- for entry in content:
- if not entry:
- continue
- if not glob:
- # look for explicit titles and documents ("Some Title ").
- m = caption_ref_re.match(entry)
- if m:
- docname = m.group(2)
- includetitles[docname] = m.group(1)
+ def run(self):
+ env = self.state.document.settings.env
+ suffix = env.config.source_suffix
+ glob = 'glob' in self.options
+
+ ret = []
+ # (title, ref) pairs, where ref may be a document, or an external link,
+ # and title may be None if the document's title is to be used
+ entries = []
+ includefiles = []
+ includetitles = {}
+ all_docnames = env.found_docs.copy()
+ # don't add the currently visited file in catch-all patterns
+ all_docnames.remove(env.docname)
+ for entry in self.content:
+ if not entry:
+ continue
+ if not glob:
+ # look for explicit titles ("Some Title ")
+ m = caption_ref_re.match(entry)
+ if m:
+ ref = m.group(2)
+ title = m.group(1)
+ docname = ref
+ else:
+ ref = docname = entry
+ title = None
+ # remove suffixes (backwards compatibility)
+ if docname.endswith(suffix):
+ docname = docname[:-len(suffix)]
+ # absolutize filenames
+ docname = docname_join(env.docname, docname)
+ if url_re.match(ref) or ref == 'self':
+ entries.append((title, ref))
+ elif docname not in env.found_docs:
+ ret.append(self.state.document.reporter.warning(
+ 'toctree references unknown document %r' % docname,
+ line=self.lineno))
+ else:
+ entries.append((title, docname))
+ includefiles.append(docname)
else:
- docname = entry
- # remove suffixes (backwards compatibility)
- if docname.endswith(suffix):
- docname = docname[:-len(suffix)]
- # absolutize filenames
- docname = posixpath.normpath(posixpath.join(dirname, docname))
- if docname not in env.found_docs:
- ret.append(state.document.reporter.warning(
- 'toctree references unknown document %r' % docname, line=lineno))
- else:
- includefiles.append(docname)
- else:
- patname = posixpath.normpath(posixpath.join(dirname, entry))
- docnames = sorted(patfilter(all_docnames, patname))
- for docname in docnames:
- all_docnames.remove(docname) # don't include it again
- includefiles.append(docname)
- if not docnames:
- ret.append(state.document.reporter.warning(
- 'toctree glob pattern %r didn\'t match any documents' % entry,
- line=lineno))
- subnode['includefiles'] = includefiles
- subnode['includetitles'] = includetitles
- subnode['maxdepth'] = options.get('maxdepth', -1)
- subnode['glob'] = glob
- ret.append(subnode)
- return ret
-
-toctree_directive.content = 1
-toctree_directive.options = {'maxdepth': int, 'glob': directives.flag}
-directives.register_directive('toctree', toctree_directive)
+ patname = docname_join(env.docname, entry)
+ docnames = sorted(patfilter(all_docnames, patname))
+ for docname in docnames:
+ all_docnames.remove(docname) # don't include it again
+ entries.append((None, docname))
+ includefiles.append(docname)
+ if not docnames:
+ ret.append(self.state.document.reporter.warning(
+ 'toctree glob pattern %r didn\'t match any documents'
+ % entry, line=self.lineno))
+ subnode = addnodes.toctree()
+ subnode['parent'] = env.docname
+ # entries contains all entries (self references, external links etc.)
+ subnode['entries'] = entries
+ # includefiles only entries that are documents
+ subnode['includefiles'] = includefiles
+ subnode['maxdepth'] = self.options.get('maxdepth', -1)
+ subnode['glob'] = glob
+ subnode['hidden'] = 'hidden' in self.options
+ subnode['numbered'] = 'numbered' in self.options
+ ret.append(subnode)
+ return ret
-# ------ section metadata ----------------------------------------------------------
+class Module(Directive):
+ """
+ Directive to mark description of a new module.
+ """
-def module_directive(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- env = state.document.settings.env
- modname = arguments[0].strip()
- noindex = 'noindex' in options
- env.currmodule = modname
- env.note_module(modname, options.get('synopsis', ''),
- options.get('platform', ''),
- 'deprecated' in options)
- modulenode = addnodes.module()
- modulenode['modname'] = modname
- modulenode['synopsis'] = options.get('synopsis', '')
- targetnode = nodes.target('', '', ids=['module-' + modname])
- state.document.note_explicit_target(targetnode)
- ret = [modulenode, targetnode]
- if 'platform' in options:
- modulenode['platform'] = options['platform']
- node = nodes.paragraph()
- node += nodes.emphasis('', _('Platforms: '))
- node += nodes.Text(options['platform'], options['platform'])
- ret.append(node)
- # the synopsis isn't printed; in fact, it is only used in the modindex currently
- if not noindex:
- indextext = _('%s (module)') % modname
- inode = addnodes.index(entries=[('single', indextext,
- 'module-' + modname, modname)])
- ret.insert(0, inode)
- return ret
+ has_content = False
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = False
+ option_spec = {
+ 'platform': lambda x: x,
+ 'synopsis': lambda x: x,
+ 'noindex': directives.flag,
+ 'deprecated': directives.flag,
+ }
-module_directive.arguments = (1, 0, 0)
-module_directive.options = {'platform': lambda x: x,
- 'synopsis': lambda x: x,
- 'noindex': directives.flag,
- 'deprecated': directives.flag}
-directives.register_directive('module', module_directive)
-
-
-def currentmodule_directive(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- # This directive is just to tell people that we're documenting
- # stuff in module foo, but links to module foo won't lead here.
- env = state.document.settings.env
- modname = arguments[0].strip()
- if modname == 'None':
- env.currmodule = None
- else:
+ def run(self):
+ env = self.state.document.settings.env
+ modname = self.arguments[0].strip()
+ noindex = 'noindex' in self.options
env.currmodule = modname
- return []
-
-currentmodule_directive.arguments = (1, 0, 0)
-directives.register_directive('currentmodule', currentmodule_directive)
+ env.note_module(modname, self.options.get('synopsis', ''),
+ self.options.get('platform', ''),
+ 'deprecated' in self.options)
+ modulenode = addnodes.module()
+ modulenode['modname'] = modname
+ modulenode['synopsis'] = self.options.get('synopsis', '')
+ targetnode = nodes.target('', '', ids=['module-' + modname], ismod=True)
+ self.state.document.note_explicit_target(targetnode)
+ ret = [modulenode, targetnode]
+ if 'platform' in self.options:
+ platform = self.options['platform']
+ modulenode['platform'] = platform
+ node = nodes.paragraph()
+ node += nodes.emphasis('', _('Platforms: '))
+ node += nodes.Text(platform, platform)
+ ret.append(node)
+ # the synopsis isn't printed; in fact, it is only used in the
+ # modindex currently
+ if not noindex:
+ indextext = _('%s (module)') % modname
+ inode = addnodes.index(entries=[('single', indextext,
+ 'module-' + modname, modname)])
+ ret.insert(0, inode)
+ return ret
-def author_directive(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- # Show authors only if the show_authors option is on
- env = state.document.settings.env
- if not env.config.show_authors:
- return []
- para = nodes.paragraph()
- emph = nodes.emphasis()
- para += emph
- if name == 'sectionauthor':
- text = _('Section author: ')
- elif name == 'moduleauthor':
- text = _('Module author: ')
- else:
- text = _('Author: ')
- emph += nodes.Text(text, text)
- inodes, messages = state.inline_text(arguments[0], lineno)
- emph.extend(inodes)
- return [para] + messages
+class CurrentModule(Directive):
+ """
+ This directive is just to tell Sphinx that we're documenting
+ stuff in module foo, but links to module foo won't lead here.
+ """
-author_directive.arguments = (1, 0, 1)
-directives.register_directive('sectionauthor', author_directive)
-directives.register_directive('moduleauthor', author_directive)
+ has_content = False
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = False
+ option_spec = {}
-
-# ------ index markup --------------------------------------------------------------
-
-indextypes = [
- 'single', 'pair', 'triple',
-]
-
-def index_directive(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- arguments = arguments[0].split('\n')
- env = state.document.settings.env
- targetid = 'index-%s' % env.index_num
- env.index_num += 1
- targetnode = nodes.target('', '', ids=[targetid])
- state.document.note_explicit_target(targetnode)
- indexnode = addnodes.index()
- indexnode['entries'] = ne = []
- for entry in arguments:
- entry = entry.strip()
- for type in pairindextypes:
- if entry.startswith(type+':'):
- value = entry[len(type)+1:].strip()
- value = pairindextypes[type] + '; ' + value
- ne.append(('pair', value, targetid, value))
- break
+ def run(self):
+ env = self.state.document.settings.env
+ modname = self.arguments[0].strip()
+ if modname == 'None':
+ env.currmodule = None
else:
- for type in indextypes:
+ env.currmodule = modname
+ return []
+
+
+class Author(Directive):
+ """
+ Directive to give the name of the author of the current document
+ or section. Shown in the output only if the show_authors option is on.
+ """
+
+ has_content = False
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = True
+ option_spec = {}
+
+ def run(self):
+ env = self.state.document.settings.env
+ if not env.config.show_authors:
+ return []
+ para = nodes.paragraph()
+ emph = nodes.emphasis()
+ para += emph
+ if self.name == 'sectionauthor':
+ text = _('Section author: ')
+ elif self.name == 'moduleauthor':
+ text = _('Module author: ')
+ else:
+ text = _('Author: ')
+ emph += nodes.Text(text, text)
+ inodes, messages = self.state.inline_text(self.arguments[0],
+ self.lineno)
+ emph.extend(inodes)
+ return [para] + messages
+
+
+class Program(Directive):
+ """
+ Directive to name the program for which options are documented.
+ """
+
+ has_content = False
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = True
+ option_spec = {}
+
+ def run(self):
+ env = self.state.document.settings.env
+ program = ws_re.sub('-', self.arguments[0].strip())
+ if program == 'None':
+ env.currprogram = None
+ else:
+ env.currprogram = program
+ return []
+
+
+class Index(Directive):
+ """
+ Directive to add entries to the index.
+ """
+
+ has_content = False
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = True
+ option_spec = {}
+
+ indextypes = [
+ 'single', 'pair', 'triple',
+ ]
+
+ def run(self):
+ arguments = self.arguments[0].split('\n')
+ env = self.state.document.settings.env
+ targetid = 'index-%s' % env.index_num
+ env.index_num += 1
+ targetnode = nodes.target('', '', ids=[targetid])
+ self.state.document.note_explicit_target(targetnode)
+ indexnode = addnodes.index()
+ indexnode['entries'] = ne = []
+ for entry in arguments:
+ entry = entry.strip()
+ for type in pairindextypes:
if entry.startswith(type+':'):
value = entry[len(type)+1:].strip()
- ne.append((type, value, targetid, value))
+ value = pairindextypes[type] + '; ' + value
+ ne.append(('pair', value, targetid, value))
break
- # shorthand notation for single entries
else:
- for value in entry.split(','):
- ne.append(('single', value.strip(), targetid, value.strip()))
- return [indexnode, targetnode]
-
-index_directive.arguments = (1, 0, 1)
-directives.register_directive('index', index_directive)
-
-# ------ versionadded/versionchanged -----------------------------------------------
-
-def version_directive(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- node = addnodes.versionmodified()
- node['type'] = name
- node['version'] = arguments[0]
- if len(arguments) == 2:
- inodes, messages = state.inline_text(arguments[1], lineno+1)
- node.extend(inodes)
- if content:
- state.nested_parse(content, content_offset, node)
- ret = [node] + messages
- else:
- ret = [node]
- env = state.document.settings.env
- env.note_versionchange(node['type'], node['version'], node, lineno)
- return ret
-
-version_directive.arguments = (1, 1, 1)
-version_directive.content = 1
-
-directives.register_directive('deprecated', version_directive)
-directives.register_directive('versionadded', version_directive)
-directives.register_directive('versionchanged', version_directive)
+ for type in self.indextypes:
+ if entry.startswith(type+':'):
+ value = entry[len(type)+1:].strip()
+ ne.append((type, value, targetid, value))
+ break
+ # shorthand notation for single entries
+ else:
+ for value in entry.split(','):
+ value = value.strip()
+ if not value:
+ continue
+ ne.append(('single', value, targetid, value))
+ return [indexnode, targetnode]
-# ------ see also ------------------------------------------------------------------
+class VersionChange(Directive):
+ """
+ Directive to describe a change/addition/deprecation in a specific version.
+ """
-def seealso_directive(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- seealsonode = make_admonition(
- addnodes.seealso, name, [_('See also')], options, content,
- lineno, content_offset, block_text, state, state_machine)
- if arguments:
- argnodes, msgs = state.inline_text(arguments[0], lineno)
- para = nodes.paragraph()
- para += argnodes
- seealsonode[1:1] = [para] + msgs
- return [seealsonode]
+ has_content = True
+ required_arguments = 1
+ optional_arguments = 1
+ final_argument_whitespace = True
+ option_spec = {}
-seealso_directive.content = 1
-seealso_directive.arguments = (0, 1, 1)
-directives.register_directive('seealso', seealso_directive)
+ def run(self):
+ node = addnodes.versionmodified()
+ node.document = self.state.document
+ node['type'] = self.name
+ node['version'] = self.arguments[0]
+ if len(self.arguments) == 2:
+ inodes, messages = self.state.inline_text(self.arguments[1],
+ self.lineno+1)
+ node.extend(inodes)
+ if self.content:
+ self.state.nested_parse(self.content, self.content_offset, node)
+ ret = [node] + messages
+ else:
+ ret = [node]
+ env = self.state.document.settings.env
+ env.note_versionchange(node['type'], node['version'], node, self.lineno)
+ return ret
-# ------ production list (for the reference) ---------------------------------------
+class SeeAlso(Directive):
+ """
+ An admonition mentioning things to look at as reference.
+ """
+
+ has_content = True
+ required_arguments = 0
+ optional_arguments = 1
+ final_argument_whitespace = True
+ option_spec = {}
+
+ def run(self):
+ ret = make_admonition(
+ addnodes.seealso, self.name, [_('See also')], self.options,
+ self.content, self.lineno, self.content_offset, self.block_text,
+ self.state, self.state_machine)
+ if self.arguments:
+ argnodes, msgs = self.state.inline_text(self.arguments[0],
+ self.lineno)
+ para = nodes.paragraph()
+ para += argnodes
+ para += msgs
+ ret[0].insert(1, para)
+ return ret
+
token_re = re.compile('`([a-z_]+)`')
@@ -267,116 +348,223 @@ def token_xrefs(text, env):
retnodes.append(nodes.Text(text[pos:], text[pos:]))
return retnodes
-def productionlist_directive(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- env = state.document.settings.env
- node = addnodes.productionlist()
- messages = []
- i = 0
+class ProductionList(Directive):
+ """
+ Directive to list grammar productions.
+ """
- for rule in arguments[0].split('\n'):
- if i == 0 and ':' not in rule:
- # production group
- continue
- i += 1
- try:
- name, tokens = rule.split(':', 1)
- except ValueError:
- break
- subnode = addnodes.production()
- subnode['tokenname'] = name.strip()
- if subnode['tokenname']:
- idname = 'grammar-token-%s' % subnode['tokenname']
- if idname not in state.document.ids:
- subnode['ids'].append(idname)
- state.document.note_implicit_target(subnode, subnode)
- env.note_reftarget('token', subnode['tokenname'], idname)
- subnode.extend(token_xrefs(tokens, env))
- node.append(subnode)
- return [node] + messages
+ has_content = False
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = True
+ option_spec = {}
-productionlist_directive.content = 0
-productionlist_directive.arguments = (1, 0, 1)
-directives.register_directive('productionlist', productionlist_directive)
+ def run(self):
+ env = self.state.document.settings.env
+ node = addnodes.productionlist()
+ messages = []
+ i = 0
-
-# ------ glossary directive ---------------------------------------------------------
-
-def glossary_directive(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- """Glossary with cross-reference targets for :term: roles."""
- env = state.document.settings.env
- node = addnodes.glossary()
- state.nested_parse(content, content_offset, node)
-
- # the content should be definition lists
- dls = [child for child in node if isinstance(child, nodes.definition_list)]
- # now, extract definition terms to enable cross-reference creation
- for dl in dls:
- dl['classes'].append('glossary')
- for li in dl.children:
- if not li.children or not isinstance(li[0], nodes.term):
+ for rule in self.arguments[0].split('\n'):
+ if i == 0 and ':' not in rule:
+ # production group
continue
- termtext = li.children[0].astext()
- new_id = 'term-' + nodes.make_id(termtext)
- if new_id in env.gloss_entries:
- new_id = 'term-' + str(len(env.gloss_entries))
- env.gloss_entries.add(new_id)
- li[0]['names'].append(new_id)
- li[0]['ids'].append(new_id)
- state.document.settings.env.note_reftarget('term', termtext.lower(),
- new_id)
- # add an index entry too
- indexnode = addnodes.index()
- indexnode['entries'] = [('single', termtext, new_id, termtext)]
- li.insert(0, indexnode)
- return [node]
-
-glossary_directive.content = 1
-glossary_directive.arguments = (0, 0, 0)
-directives.register_directive('glossary', glossary_directive)
+ i += 1
+ try:
+ name, tokens = rule.split(':', 1)
+ except ValueError:
+ break
+ subnode = addnodes.production()
+ subnode['tokenname'] = name.strip()
+ if subnode['tokenname']:
+ idname = 'grammar-token-%s' % subnode['tokenname']
+ if idname not in self.state.document.ids:
+ subnode['ids'].append(idname)
+ self.state.document.note_implicit_target(subnode, subnode)
+ env.note_reftarget('token', subnode['tokenname'], idname)
+ subnode.extend(token_xrefs(tokens, env))
+ node.append(subnode)
+ return [node] + messages
-# ------ miscellaneous markup -------------------------------------------------------
+class TabularColumns(Directive):
+ """
+ Directive to give an explicit tabulary column definition to LaTeX.
+ """
-def centered_directive(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- if not arguments:
- return []
- subnode = addnodes.centered()
- inodes, messages = state.inline_text(arguments[0], lineno)
- subnode.extend(inodes)
- return [subnode] + messages
+ has_content = False
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = True
+ option_spec = {}
-centered_directive.arguments = (1, 0, 1)
-directives.register_directive('centered', centered_directive)
+ def run(self):
+ node = addnodes.tabular_col_spec()
+ node['spec'] = self.arguments[0]
+ return [node]
-def acks_directive(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- node = addnodes.acks()
- state.nested_parse(content, content_offset, node)
- if len(node.children) != 1 or not isinstance(node.children[0], nodes.bullet_list):
- return [state.document.reporter.warning('.. acks content is not a list',
- line=lineno)]
- return [node]
+class Glossary(Directive):
+ """
+ Directive to create a glossary with cross-reference targets
+ for :term: roles.
+ """
-acks_directive.content = 1
-acks_directive.arguments = (0, 0, 0)
-directives.register_directive('acks', acks_directive)
+ has_content = True
+ required_arguments = 0
+ optional_arguments = 0
+ final_argument_whitespace = False
+ option_spec = {}
+
+ def run(self):
+ env = self.state.document.settings.env
+ node = addnodes.glossary()
+ node.document = self.state.document
+ self.state.nested_parse(self.content, self.content_offset, node)
+
+ # the content should be definition lists
+ dls = [child for child in node
+ if isinstance(child, nodes.definition_list)]
+ # now, extract definition terms to enable cross-reference creation
+ for dl in dls:
+ dl['classes'].append('glossary')
+ for li in dl.children:
+ if not li.children or not isinstance(li[0], nodes.term):
+ continue
+ termtext = li.children[0].astext()
+ new_id = 'term-' + nodes.make_id(termtext)
+ if new_id in env.gloss_entries:
+ new_id = 'term-' + str(len(env.gloss_entries))
+ env.gloss_entries.add(new_id)
+ li[0]['names'].append(new_id)
+ li[0]['ids'].append(new_id)
+ env.note_reftarget('term', termtext.lower(), new_id)
+ # add an index entry too
+ indexnode = addnodes.index()
+ indexnode['entries'] = [('single', termtext, new_id, termtext)]
+ li.insert(0, indexnode)
+ return [node]
-def tabularcolumns_directive(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- # support giving explicit tabulary column definition to latex
- node = addnodes.tabular_col_spec()
- node['spec'] = arguments[0]
- return [node]
+class Centered(Directive):
+ """
+ Directive to create a centered line of bold text.
+ """
-tabularcolumns_directive.content = 0
-tabularcolumns_directive.arguments = (1, 0, 1)
-directives.register_directive('tabularcolumns', tabularcolumns_directive)
+ has_content = False
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = True
+ option_spec = {}
+ def run(self):
+ if not self.arguments:
+ return []
+ subnode = addnodes.centered()
+ inodes, messages = self.state.inline_text(self.arguments[0],
+ self.lineno)
+ subnode.extend(inodes)
+ return [subnode] + messages
+
+
+
+class Acks(Directive):
+ """
+ Directive for a list of names.
+ """
+
+ has_content = True
+ required_arguments = 0
+ optional_arguments = 0
+ final_argument_whitespace = False
+ option_spec = {}
+
+ def run(self):
+ node = addnodes.acks()
+ node.document = self.state.document
+ self.state.nested_parse(self.content, self.content_offset, node)
+ if len(node.children) != 1 or not isinstance(node.children[0],
+ nodes.bullet_list):
+ return [self.state.document.reporter.warning(
+ '.. acks content is not a list', line=self.lineno)]
+ return [node]
+
+
+class HList(Directive):
+ """
+ Directive for a list that gets compacted horizontally.
+ """
+
+ has_content = True
+ required_arguments = 0
+ optional_arguments = 0
+ final_argument_whitespace = False
+ option_spec = {
+ 'columns': int,
+ }
+
+ def run(self):
+ ncolumns = self.options.get('columns', 2)
+ node = nodes.paragraph()
+ node.document = self.state.document
+ self.state.nested_parse(self.content, self.content_offset, node)
+ if len(node.children) != 1 or not isinstance(node.children[0],
+ nodes.bullet_list):
+ return [self.state.document.reporter.warning(
+ '.. hlist content is not a list', line=self.lineno)]
+ fulllist = node.children[0]
+ # create a hlist node where the items are distributed
+ npercol, nmore = divmod(len(fulllist), ncolumns)
+ index = 0
+ newnode = addnodes.hlist()
+ for column in range(ncolumns):
+ endindex = index + (column < nmore and (npercol+1) or npercol)
+ col = addnodes.hlistcol()
+ col += nodes.bullet_list()
+ col[0] += fulllist.children[index:endindex]
+ index = endindex
+ newnode += col
+ return [newnode]
+
+
+class Only(Directive):
+ """
+ Directive to only include text if the given tag(s) are enabled.
+ """
+
+ has_content = True
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = True
+ option_spec = {}
+
+ def run(self):
+ node = addnodes.only()
+ node.document = self.state.document
+ node.line = self.lineno
+ node['expr'] = self.arguments[0]
+ self.state.nested_parse(self.content, self.content_offset, node)
+ return [node]
+
+
+directives.register_directive('toctree', directive_dwim(TocTree))
+directives.register_directive('module', directive_dwim(Module))
+directives.register_directive('currentmodule', directive_dwim(CurrentModule))
+directives.register_directive('sectionauthor', directive_dwim(Author))
+directives.register_directive('moduleauthor', directive_dwim(Author))
+directives.register_directive('program', directive_dwim(Program))
+directives.register_directive('index', directive_dwim(Index))
+directives.register_directive('deprecated', directive_dwim(VersionChange))
+directives.register_directive('versionadded', directive_dwim(VersionChange))
+directives.register_directive('versionchanged', directive_dwim(VersionChange))
+directives.register_directive('seealso', directive_dwim(SeeAlso))
+directives.register_directive('productionlist', directive_dwim(ProductionList))
+directives.register_directive('tabularcolumns', directive_dwim(TabularColumns))
+directives.register_directive('glossary', directive_dwim(Glossary))
+directives.register_directive('centered', directive_dwim(Centered))
+directives.register_directive('acks', directive_dwim(Acks))
+directives.register_directive('hlist', directive_dwim(HList))
+directives.register_directive('only', directive_dwim(Only))
# register the standard rst class directive under a different name
diff --git a/sphinx/environment.py b/sphinx/environment.py
index 3b7eb91e1..0884eccad 100644
--- a/sphinx/environment.py
+++ b/sphinx/environment.py
@@ -5,8 +5,8 @@
Global creation environment.
- :copyright: 2007-2008 by Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
import re
@@ -14,25 +14,25 @@ import os
import time
import heapq
import types
+import codecs
import imghdr
+import string
import difflib
import cPickle as pickle
from os import path
from glob import glob
-from string import uppercase
+from string import ascii_uppercase as uppercase
from itertools import izip, groupby
try:
- import hashlib
- md5 = hashlib.md5
+ from hashlib import md5
except ImportError:
# 2.4 compatibility
- import md5
- md5 = md5.new
+ from md5 import md5
from docutils import nodes
from docutils.io import FileInput, NullOutput
from docutils.core import Publisher
-from docutils.utils import Reporter
+from docutils.utils import Reporter, relative_path
from docutils.readers import standalone
from docutils.parsers.rst import roles
from docutils.parsers.rst.languages import en as english
@@ -42,14 +42,16 @@ from docutils.transforms import Transform
from docutils.transforms.parts import ContentsFilter
from sphinx import addnodes
-from sphinx.util import get_matching_docs, SEP
+from sphinx.util import movefile, get_matching_docs, SEP, ustrftime, \
+ docname_join, FilenameUniqDict, url_re
+from sphinx.errors import SphinxError
from sphinx.directives import additional_xref_types
default_settings = {
'embed_stylesheet': False,
'cloak_email_addresses': True,
'pep_base_url': 'http://www.python.org/dev/peps/',
- 'rfc_base_url': 'http://rfc.net/',
+ 'rfc_base_url': 'http://tools.ietf.org/html/',
'input_encoding': 'utf-8',
'doctitle_xform': False,
'sectsubtitle_xform': False,
@@ -57,7 +59,7 @@ default_settings = {
# This is increased every time an environment attribute is added
# or changed to properly invalidate pickle files.
-ENV_VERSION = 25
+ENV_VERSION = 29
default_substitutions = set([
@@ -69,12 +71,12 @@ default_substitutions = set([
dummy_reporter = Reporter('', 4, 4)
-class RedirStream(object):
- def __init__(self, writefunc):
- self.writefunc = writefunc
+class WarningStream(object):
+ def __init__(self, warnfunc):
+ self.warnfunc = warnfunc
def write(self, text):
if text.strip():
- self.writefunc(text)
+ self.warnfunc(text, None, '')
class NoUri(Exception):
@@ -99,7 +101,7 @@ class DefaultSubstitutions(Transform):
text = config[refname]
if refname == 'today' and not text:
# special handling: can also specify a strftime format
- text = time.strftime(config.today_fmt or _('%B %d, %Y'))
+ text = ustrftime(config.today_fmt or _('%B %d, %Y'))
ref.replace_self(nodes.Text(text, text))
@@ -114,7 +116,8 @@ class MoveModuleTargets(Transform):
if not node['ids']:
continue
if node['ids'][0].startswith('module-') and \
- node.parent.__class__ is nodes.section:
+ node.parent.__class__ is nodes.section and \
+ node.has_key('ismod'):
node.parent['ids'] = node['ids']
node.parent.remove(node)
@@ -131,6 +134,19 @@ class HandleCodeBlocks(Transform):
nodes.doctest_block):
node.replace_self(node.children[0])
+
+class SortIds(Transform):
+ """
+ Sort secion IDs so that the "id[0-9]+" one comes last.
+ """
+ default_priority = 261
+
+ def apply(self):
+ for node in self.document.traverse(nodes.section):
+ if len(node['ids']) > 1 and node['ids'][0].startswith('id'):
+ node['ids'] = node['ids'][1:] + [node['ids'][0]]
+
+
class CitationReferences(Transform):
"""
Handle citation references before the default docutils transform does.
@@ -151,7 +167,7 @@ class SphinxStandaloneReader(standalone.Reader):
Add our own transforms.
"""
transforms = [CitationReferences, DefaultSubstitutions, MoveModuleTargets,
- HandleCodeBlocks]
+ HandleCodeBlocks, SortIds]
def get_transforms(self):
return standalone.Reader.get_transforms(self) + self.transforms
@@ -164,14 +180,14 @@ class SphinxDummyWriter(UnfilteredWriter):
pass
-
class SphinxContentsFilter(ContentsFilter):
"""
Used with BuildEnvironment.add_toc_from() to discard cross-file links
within table-of-contents link nodes.
"""
def visit_pending_xref(self, node):
- self.parent.append(nodes.literal(node['reftarget'], node['reftarget']))
+ text = node.astext()
+ self.parent.append(nodes.literal(text, text))
raise nodes.SkipNode
@@ -202,7 +218,9 @@ class BuildEnvironment:
self.set_warnfunc(None)
values = self.config.values
del self.config.values
- picklefile = open(filename, 'wb')
+ # first write to a temporary file, so that if dumping fails,
+ # the existing environment won't be overwritten
+ picklefile = open(filename + '.tmp', 'wb')
# remove potentially pickling-problematic values from config
for key, val in vars(self.config).items():
if key.startswith('_') or \
@@ -214,6 +232,7 @@ class BuildEnvironment:
pickle.dump(self, picklefile, pickle.HIGHEST_PROTOCOL)
finally:
picklefile.close()
+ movefile(filename + '.tmp', filename)
# reset attributes
self.config.values = values
self.set_warnfunc(warnfunc)
@@ -238,13 +257,14 @@ class BuildEnvironment:
# this is to invalidate old pickles
self.version = ENV_VERSION
- # All "docnames" here are /-separated and relative and exclude the source suffix.
+ # All "docnames" here are /-separated and relative and exclude
+ # the source suffix.
self.found_docs = set() # contains all existing docnames
self.all_docs = {} # docname -> mtime at the time of build
# contains all built docnames
- self.dependencies = {} # docname -> set of dependent file names, relative to
- # documentation root
+ self.dependencies = {} # docname -> set of dependent file
+ # names, relative to documentation root
# File metadata
self.metadata = {} # docname -> dict of metadata items
@@ -253,53 +273,65 @@ class BuildEnvironment:
self.titles = {} # docname -> title node
self.tocs = {} # docname -> table of contents nodetree
self.toc_num_entries = {} # docname -> number of real entries
- # used to determine when to show the TOC in a sidebar
- # (don't show if it's only one item)
+ # used to determine when to show the TOC
+ # in a sidebar (don't show if it's only one item)
+ self.toc_secnumbers = {} # docname -> dict of sectionid -> number
+
self.toctree_includes = {} # docname -> list of toctree includefiles
- self.files_to_rebuild = {} # docname -> set of files (containing its TOCs)
- # to rebuild too
+ self.files_to_rebuild = {} # docname -> set of files
+ # (containing its TOCs) to rebuild too
self.glob_toctrees = set() # docnames that have :glob: toctrees
+ self.numbered_toctrees = set() # docnames that have :numbered: toctrees
# X-ref target inventory
self.descrefs = {} # fullname -> docname, desctype
self.filemodules = {} # docname -> [modules]
- self.modules = {} # modname -> docname, synopsis, platform, deprecated
+ self.modules = {} # modname -> docname, synopsis,
+ # platform, deprecated
self.labels = {} # labelname -> docname, labelid, sectionname
self.anonlabels = {} # labelname -> docname, labelid
+ self.progoptions = {} # (program, name) -> docname, labelid
self.reftargets = {} # (type, name) -> docname, labelid
- # where type is term, token, option, envvar, citation
+ # type: term, token, envvar, citation
# Other inventories
self.indexentries = {} # docname -> list of
# (type, string, target, aliasname)
- self.versionchanges = {} # version -> list of
- # (type, docname, lineno, module, descname, content)
- self.images = {} # absolute path -> (docnames, unique filename)
+ self.versionchanges = {} # version -> list of (type, docname,
+ # lineno, module, descname, content)
+
+ # these map absolute path -> (docnames, unique filename)
+ self.images = FilenameUniqDict()
+ self.dlfiles = FilenameUniqDict()
# These are set while parsing a file
self.docname = None # current document name
self.currmodule = None # current module name
self.currclass = None # current class name
self.currdesc = None # current descref name
+ self.currprogram = None # current program name
self.index_num = 0 # autonumber for index targets
self.gloss_entries = set() # existing definition labels
# Some magically present labels
- self.labels['genindex'] = ('genindex', '', _('Index'))
- self.labels['modindex'] = ('modindex', '', _('Module Index'))
- self.labels['search'] = ('search', '', _('Search Page'))
+ def add_magic_label(name, description):
+ self.labels[name] = (name, '', description)
+ self.anonlabels[name] = (name, '')
+ add_magic_label('genindex', _('Index'))
+ add_magic_label('modindex', _('Module Index'))
+ add_magic_label('search', _('Search Page'))
def set_warnfunc(self, func):
self._warnfunc = func
- self.settings['warning_stream'] = RedirStream(func)
+ self.settings['warning_stream'] = WarningStream(func)
def warn(self, docname, msg, lineno=None):
if docname:
if lineno is None:
lineno = ''
- self._warnfunc('%s:%s: %s' % (self.doc2path(docname), lineno, msg))
+ self._warnfunc(msg, '%s:%s' % (self.doc2path(docname), lineno))
else:
- self._warnfunc('GLOBAL:: ' + msg)
+ self._warnfunc(msg)
def clear_doc(self, docname):
"""Remove all traces of a source file in the inventory."""
@@ -309,11 +341,15 @@ class BuildEnvironment:
self.dependencies.pop(docname, None)
self.titles.pop(docname, None)
self.tocs.pop(docname, None)
+ self.toc_secnumbers.pop(docname, None)
self.toc_num_entries.pop(docname, None)
self.toctree_includes.pop(docname, None)
self.filemodules.pop(docname, None)
self.indexentries.pop(docname, None)
self.glob_toctrees.discard(docname)
+ self.numbered_toctrees.discard(docname)
+ self.images.purge_doc(docname)
+ self.dlfiles.purge_doc(docname)
for subfn, fnset in self.files_to_rebuild.items():
fnset.discard(docname)
@@ -331,13 +367,12 @@ class BuildEnvironment:
for key, (fn, _) in self.reftargets.items():
if fn == docname:
del self.reftargets[key]
+ for key, (fn, _) in self.progoptions.items():
+ if fn == docname:
+ del self.progoptions[key]
for version, changes in self.versionchanges.items():
new = [change for change in changes if change[1] != docname]
changes[:] = new
- for fullpath, (docs, _) in self.images.items():
- docs.discard(docname)
- if not docs:
- del self.images[fullpath]
def doc2path(self, docname, base=True, suffix=None):
"""
@@ -349,7 +384,8 @@ class BuildEnvironment:
"""
suffix = suffix or self.config.source_suffix
if base is True:
- return path.join(self.srcdir, docname.replace(SEP, path.sep)) + suffix
+ return path.join(self.srcdir,
+ docname.replace(SEP, path.sep)) + suffix
elif base is None:
return docname.replace(SEP, path.sep) + suffix
else:
@@ -362,8 +398,10 @@ class BuildEnvironment:
exclude_dirs = [d.replace(SEP, path.sep) for d in config.exclude_dirs]
exclude_trees = [d.replace(SEP, path.sep) for d in config.exclude_trees]
self.found_docs = set(get_matching_docs(
- self.srcdir, config.source_suffix, exclude_docs=set(config.unused_docs),
- exclude_dirs=exclude_dirs, exclude_trees=exclude_trees,
+ self.srcdir, config.source_suffix,
+ exclude_docs=set(config.unused_docs),
+ exclude_dirs=exclude_dirs,
+ exclude_trees=exclude_trees,
exclude_dirnames=['_sources'] + config.exclude_dirnames))
def get_outdated_files(self, config_changed):
@@ -415,18 +453,22 @@ class BuildEnvironment:
return added, changed, removed
def update(self, config, srcdir, doctreedir, app=None):
- """(Re-)read all files new or changed since last update. Yields a summary
- and then docnames as it processes them. Store all environment docnames
- in the canonical format (ie using SEP as a separator in place of
- os.path.sep)."""
+ """
+ (Re-)read all files new or changed since last update. Returns a
+ summary, the total count of documents to reread and an iterator that
+ yields docnames as it processes them. Store all environment docnames in
+ the canonical format (ie using SEP as a separator in place of
+ os.path.sep).
+ """
config_changed = False
if self.config is None:
msg = '[new config] '
config_changed = True
else:
- # check if a config value was changed that affects how doctrees are read
- for key, descr in config.config_values.iteritems():
- if not descr[1]:
+ # check if a config value was changed that affects how
+ # doctrees are read
+ for key, descr in config.values.iteritems():
+ if descr[1] != 'env':
continue
if self.config[key] != config[key]:
msg = '[config changed] '
@@ -443,6 +485,7 @@ class BuildEnvironment:
self.srcdir = srcdir
self.doctreedir = doctreedir
self.find_files(config)
+ self.config = config
added, changed, removed = self.get_outdated_files(config_changed)
@@ -453,43 +496,63 @@ class BuildEnvironment:
msg += '%s added, %s changed, %s removed' % (len(added), len(changed),
len(removed))
- yield msg
- self.config = config
- self.app = app
+ def update_generator():
+ self.app = app
- # clear all files no longer present
- for docname in removed:
- self.clear_doc(docname)
+ # clear all files no longer present
+ for docname in removed:
+ if app:
+ app.emit('env-purge-doc', self, docname)
+ self.clear_doc(docname)
- # read all new and changed files
- for docname in sorted(added | changed):
- yield docname
- self.read_doc(docname, app=app)
+ # read all new and changed files
+ to_read = added | changed
+ for docname in sorted(to_read):
+ yield docname
+ self.read_doc(docname, app=app)
- if config.master_doc not in self.all_docs:
- self.warn(None, 'master file %s not found' %
- self.doc2path(config.master_doc))
+ if config.master_doc not in self.all_docs:
+ self.warn(None, 'master file %s not found' %
+ self.doc2path(config.master_doc))
- self.app = None
+ self.app = None
+ if app:
+ app.emit('env-updated', self)
- # remove all non-existing images from inventory
- for imgsrc in self.images.keys():
- if not os.access(path.join(self.srcdir, imgsrc), os.R_OK):
- del self.images[imgsrc]
-
- if app:
- app.emit('env-updated', self)
+ return msg, len(added | changed), update_generator()
+ def check_dependents(self, already):
+ to_rewrite = self.assign_section_numbers()
+ for docname in to_rewrite:
+ if docname not in already:
+ yield docname
# --------- SINGLE FILE READING --------------------------------------------
+ def warn_and_replace(self, error):
+ """
+ Custom decoding error handler that warns and replaces.
+ """
+ linestart = error.object.rfind('\n', None, error.start)
+ lineend = error.object.find('\n', error.start)
+ if lineend == -1: lineend = len(error.object)
+ lineno = error.object.count('\n', 0, error.start) + 1
+ self.warn(self.docname, 'undecodable source characters, '
+ 'replacing with "?": %r' %
+ (error.object[linestart+1:error.start] + '>>>' +
+ error.object[error.start:error.end] + '<<<' +
+ error.object[error.end:lineend]), lineno)
+ return (u'?', error.end)
+
def read_doc(self, docname, src_path=None, save_parsed=True, app=None):
"""
Parse a file and add/update inventory entries for the doctree.
If srcpath is given, read from a different source file.
"""
# remove all inventory entries for that file
+ if app:
+ app.emit('env-purge-doc', self, docname)
self.clear_doc(docname)
if src_path is None:
@@ -506,15 +569,27 @@ class BuildEnvironment:
self.docname = docname
self.settings['input_encoding'] = self.config.source_encoding
+ self.settings['trim_footnote_reference_space'] = \
+ self.config.trim_footnote_reference_space
+
+ codecs.register_error('sphinx', self.warn_and_replace)
+
+ codecs.register_error('sphinx', self.warn_and_replace)
class SphinxSourceClass(FileInput):
- def read(self):
- data = FileInput.read(self)
+ def decode(self_, data):
+ return data.decode(self_.encoding, 'sphinx')
+
+ def read(self_):
+ data = FileInput.read(self_)
if app:
arg = [data]
app.emit('source-read', docname, arg)
data = arg[0]
- return data
+ if self.config.rst_epilog:
+ return data + '\n' + self.config.rst_epilog + '\n'
+ else:
+ return data
# publish manually
pub = Publisher(reader=SphinxStandaloneReader(),
@@ -529,11 +604,12 @@ class BuildEnvironment:
pub.publish()
doctree = pub.document
except UnicodeError, err:
- from sphinx.application import SphinxError
- raise SphinxError(err.message)
+ import pdb; pdb.set_trace()
+ raise SphinxError(str(err))
self.filter_messages(doctree)
self.process_dependencies(docname, doctree)
self.process_images(docname, doctree)
+ self.process_downloads(docname, doctree)
self.process_metadata(docname, doctree)
self.create_title_from(docname, doctree)
self.note_labels_from(docname, doctree)
@@ -565,7 +641,8 @@ class BuildEnvironment:
if save_parsed:
# save the parsed doctree
- doctree_filename = self.doc2path(docname, self.doctreedir, '.doctree')
+ doctree_filename = self.doc2path(docname, self.doctreedir,
+ '.doctree')
dirname = path.dirname(doctree_filename)
if not path.isdir(dirname):
os.makedirs(dirname)
@@ -590,19 +667,42 @@ class BuildEnvironment:
"""
Process docutils-generated dependency info.
"""
+ cwd = os.getcwd()
+ frompath = path.join(path.normpath(self.srcdir), 'dummy')
deps = doctree.settings.record_dependencies
if not deps:
return
- docdir = path.dirname(self.doc2path(docname, base=None))
for dep in deps.list:
- dep = path.join(docdir, dep)
- self.dependencies.setdefault(docname, set()).add(dep)
+ # the dependency path is relative to the working dir, so get
+ # one relative to the srcdir
+ relpath = relative_path(frompath,
+ path.normpath(path.join(cwd, dep)))
+ self.dependencies.setdefault(docname, set()).add(relpath)
+
+ def process_downloads(self, docname, doctree):
+ """
+ Process downloadable file paths.
+ """
+ docdir = path.dirname(self.doc2path(docname, base=None))
+ for node in doctree.traverse(addnodes.download_reference):
+ targetname = node['reftarget']
+ if targetname.startswith('/') or targetname.startswith(os.sep):
+ # absolute
+ filepath = targetname[1:]
+ else:
+ filepath = path.normpath(path.join(docdir, node['reftarget']))
+ self.dependencies.setdefault(docname, set()).add(filepath)
+ if not os.access(path.join(self.srcdir, filepath), os.R_OK):
+ self.warn(docname, 'download file not readable: %s' % filepath,
+ getattr(node, 'line', None))
+ continue
+ uniquename = self.dlfiles.add_file(docname, filepath)
+ node['filename'] = uniquename
def process_images(self, docname, doctree):
"""
Process and rewrite image URIs.
"""
- existing_names = set(v[1] for v in self.images.itervalues())
docdir = path.dirname(self.doc2path(docname, base=None))
for node in doctree.traverse(nodes.image):
# Map the mimetype to the corresponding image. The writer may
@@ -612,44 +712,48 @@ class BuildEnvironment:
node['candidates'] = candidates = {}
imguri = node['uri']
if imguri.find('://') != -1:
- self.warn(docname, 'Nonlocal image URI found: %s' % imguri, node.line)
+ self.warn(docname, 'nonlocal image URI found: %s' % imguri,
+ node.line)
candidates['?'] = imguri
continue
- imgpath = path.normpath(path.join(docdir, imguri))
+ # imgpath is the image path *from srcdir*
+ if imguri.startswith('/') or imguri.startswith(os.sep):
+ # absolute path (= relative to srcdir)
+ imgpath = path.normpath(imguri[1:])
+ else:
+ imgpath = path.normpath(path.join(docdir, imguri))
+ # set imgpath as default URI
node['uri'] = imgpath
if imgpath.endswith(os.extsep + '*'):
for filename in glob(path.join(self.srcdir, imgpath)):
- dir, base = path.split(filename)
- if base.lower().endswith('.pdf'):
- candidates['application/pdf'] = path.join(docdir, base)
- elif base.lower().endswith('.svg'):
- candidates['image/svg+xml'] = path.join(docdir, base)
+ new_imgpath = relative_path(self.srcdir, filename)
+ if filename.lower().endswith('.pdf'):
+ candidates['application/pdf'] = new_imgpath
+ elif filename.lower().endswith('.svg'):
+ candidates['image/svg+xml'] = new_imgpath
else:
- f = open(filename, 'rb')
try:
- imgtype = imghdr.what(f)
- finally:
- f.close()
+ f = open(filename, 'rb')
+ try:
+ imgtype = imghdr.what(f)
+ finally:
+ f.close()
+ except (OSError, IOError):
+ self.warn(docname,
+ 'image file %s not readable' % filename)
if imgtype:
- candidates['image/' + imgtype] = path.join(docdir, base)
+ candidates['image/' + imgtype] = new_imgpath
else:
candidates['*'] = imgpath
+ # map image paths to unique image names (so that they can be put
+ # into a single directory)
for imgpath in candidates.itervalues():
self.dependencies.setdefault(docname, set()).add(imgpath)
if not os.access(path.join(self.srcdir, imgpath), os.R_OK):
- self.warn(docname, 'Image file not readable: %s' % imgpath,
+ self.warn(docname, 'image file not readable: %s' % imgpath,
node.line)
- if imgpath in self.images:
- self.images[imgpath][0].add(docname)
continue
- uniquename = path.basename(imgpath)
- base, ext = path.splitext(uniquename)
- i = 0
- while uniquename in existing_names:
- i += 1
- uniquename = '%s%s%s' % (base, i, ext)
- self.images[imgpath] = (set([docname]), uniquename)
- existing_names.add(uniquename)
+ self.images.add_file(docname, imgpath)
def process_metadata(self, docname, doctree):
"""
@@ -701,7 +805,8 @@ class BuildEnvironment:
continue
if name in self.labels:
self.warn(docname, 'duplicate label %s, ' % name +
- 'other instance in %s' % self.doc2path(self.labels[name][0]),
+ 'other instance in ' +
+ self.doc2path(self.labels[name][0]),
node.line)
self.anonlabels[name] = docname, labelid
if node.tagname == 'section':
@@ -737,6 +842,8 @@ class BuildEnvironment:
file relations from it."""
if toctreenode['glob']:
self.glob_toctrees.add(docname)
+ if toctreenode.get('numbered'):
+ self.numbered_toctrees.add(docname)
includefiles = toctreenode['includefiles']
for includefile in includefiles:
# note that if the included file is rebuilt, this one must be
@@ -753,20 +860,32 @@ class BuildEnvironment:
except ValueError:
maxdepth = 0
+ def traverse_in_section(node, cls):
+ """Like traverse(), but stay within the same section."""
+ result = []
+ if isinstance(node, cls):
+ result.append(node)
+ for child in node.children:
+ if isinstance(child, nodes.section):
+ continue
+ result.extend(traverse_in_section(child, cls))
+ return result
+
def build_toc(node, depth=1):
entries = []
- for subnode in node:
- if isinstance(subnode, addnodes.toctree):
- # just copy the toctree node which is then resolved
- # in self.get_and_resolve_doctree
- item = subnode.copy()
- entries.append(item)
- # do the inventory stuff
- self.note_toctree(docname, subnode)
+ for sectionnode in node:
+ # find all toctree nodes in this section and add them
+ # to the toc (just copying the toctree node which is then
+ # resolved in self.get_and_resolve_doctree)
+ if not isinstance(sectionnode, nodes.section):
+ for toctreenode in traverse_in_section(sectionnode,
+ addnodes.toctree):
+ item = toctreenode.copy()
+ entries.append(item)
+ # important: do the inventory stuff
+ self.note_toctree(docname, toctreenode)
continue
- if not isinstance(subnode, nodes.section):
- continue
- title = subnode[0]
+ title = sectionnode[0]
# copy the contents of the section title, but without references
# and unnecessary stuff
visitor = SphinxContentsFilter(document)
@@ -777,7 +896,7 @@ class BuildEnvironment:
# as it is the file's title anyway
anchorname = ''
else:
- anchorname = '#' + subnode['ids'][0]
+ anchorname = '#' + sectionnode['ids'][0]
numentries[0] += 1
reference = nodes.reference('', '', refuri=docname,
anchorname=anchorname,
@@ -785,7 +904,7 @@ class BuildEnvironment:
para = addnodes.compact_paragraph('', '', reference)
item = nodes.list_item('', para)
if maxdepth == 0 or depth < maxdepth:
- item += build_toc(subnode, depth+1)
+ item += build_toc(sectionnode, depth+1)
entries.append(item)
if entries:
return nodes.bullet_list('', *entries)
@@ -804,6 +923,15 @@ class BuildEnvironment:
node['refuri'] = node['anchorname']
return toc
+ def get_toctree_for(self, docname, builder, collapse):
+ """Return the global TOC nodetree."""
+ doctree = self.get_doctree(self.config.master_doc)
+ for toctreenode in doctree.traverse(addnodes.toctree):
+ result = self.resolve_toctree(docname, builder, toctreenode,
+ prune=True, collapse=collapse)
+ if result is not None:
+ return result
+
# -------
# these are called from docutils directives and therefore use self.docname
#
@@ -811,7 +939,8 @@ class BuildEnvironment:
if fullname in self.descrefs:
self.warn(self.docname,
'duplicate canonical description name %s, ' % fullname +
- 'other instance in %s' % self.doc2path(self.descrefs[fullname][0]),
+ 'other instance in ' +
+ self.doc2path(self.descrefs[fullname][0]),
line)
self.descrefs[fullname] = (self.docname, desctype)
@@ -819,17 +948,18 @@ class BuildEnvironment:
self.modules[modname] = (self.docname, synopsis, platform, deprecated)
self.filemodules.setdefault(self.docname, []).append(modname)
+ def note_progoption(self, optname, labelid):
+ self.progoptions[self.currprogram, optname] = (self.docname, labelid)
+
def note_reftarget(self, type, name, labelid):
self.reftargets[type, name] = (self.docname, labelid)
def note_versionchange(self, type, version, node, lineno):
self.versionchanges.setdefault(version, []).append(
- (type, self.docname, lineno, self.currmodule, self.currdesc, node.astext()))
+ (type, self.docname, lineno, self.currmodule, self.currdesc,
+ node.astext()))
def note_dependency(self, filename):
- basename = path.dirname(self.doc2path(self.docname, base=None))
- # this will do the right thing when filename is absolute too
- filename = path.join(basename, filename)
self.dependencies.setdefault(self.docname, set()).add(filename)
# -------
@@ -845,7 +975,7 @@ class BuildEnvironment:
f.close()
doctree.settings.env = self
doctree.reporter = Reporter(self.doc2path(docname), 2, 4,
- stream=RedirStream(self._warnfunc))
+ stream=WarningStream(self._warnfunc))
return doctree
@@ -871,7 +1001,7 @@ class BuildEnvironment:
return doctree
def resolve_toctree(self, docname, builder, toctree, prune=True, maxdepth=0,
- titles_only=False):
+ titles_only=False, collapse=False):
"""
Resolve a *toctree* node into individual bullet lists with titles
as items, returning None (if no containing titles are found) or
@@ -881,50 +1011,102 @@ class BuildEnvironment:
to the value of the *maxdepth* option on the *toctree* node.
If *titles_only* is True, only toplevel document titles will be in the
resulting tree.
+ If *collapse* is True, all branches not containing docname will
+ be collapsed.
"""
+ if toctree.get('hidden', False):
+ return None
- def _walk_depth(node, depth, maxdepth, titleoverrides):
+ def _walk_depth(node, depth, maxdepth):
"""Utility: Cut a TOC at a specified depth."""
for subnode in node.children[:]:
- if isinstance(subnode, (addnodes.compact_paragraph, nodes.list_item)):
- _walk_depth(subnode, depth, maxdepth, titleoverrides)
+ if isinstance(subnode, (addnodes.compact_paragraph,
+ nodes.list_item)):
+ subnode['classes'].append('toctree-l%d' % (depth-1))
+ _walk_depth(subnode, depth, maxdepth)
elif isinstance(subnode, nodes.bullet_list):
- if depth > maxdepth:
+ if maxdepth > 0 and depth > maxdepth:
subnode.parent.replace(subnode, [])
else:
- _walk_depth(subnode, depth+1, maxdepth, titleoverrides)
+ _walk_depth(subnode, depth+1, maxdepth)
- def _entries_from_toctree(toctreenode, separate=False):
+ # cull sub-entries whose parents aren't 'current'
+ if (collapse and
+ depth > 1 and
+ 'current' not in subnode.parent['classes']):
+ subnode.parent.remove(subnode)
+
+ elif isinstance(subnode, nodes.reference):
+ # identify the toc entry pointing to the current document
+ if subnode['refuri'] == docname and \
+ not subnode['anchorname']:
+ # tag the whole branch as 'current'
+ p = subnode
+ while p:
+ p['classes'].append('current')
+ p = p.parent
+
+ def _entries_from_toctree(toctreenode, separate=False, subtree=False):
"""Return TOC entries for a toctree node."""
- includefiles = map(str, toctreenode['includefiles'])
-
+ refs = [(e[0], str(e[1])) for e in toctreenode['entries']]
entries = []
- for includefile in includefiles:
+ for (title, ref) in refs:
try:
- toc = self.tocs[includefile].deepcopy()
+ if url_re.match(ref):
+ reference = nodes.reference('', '',
+ refuri=ref, anchorname='',
+ *[nodes.Text(title)])
+ para = addnodes.compact_paragraph('', '', reference)
+ item = nodes.list_item('', para)
+ toc = nodes.bullet_list('', item)
+ elif ref == 'self':
+ # 'self' refers to the document from which this
+ # toctree originates
+ ref = toctreenode['parent']
+ if not title:
+ title = self.titles[ref].astext()
+ reference = nodes.reference('', '',
+ refuri=ref,
+ anchorname='',
+ *[nodes.Text(title)])
+ para = addnodes.compact_paragraph('', '', reference)
+ item = nodes.list_item('', para)
+ # don't show subitems
+ toc = nodes.bullet_list('', item)
+ else:
+ toc = self.tocs[ref].deepcopy()
+ if title and toc.children and len(toc.children) == 1:
+ child = toc.children[0]
+ for refnode in child.traverse(nodes.reference):
+ if refnode['refuri'] == ref and \
+ not refnode['anchorname']:
+ refnode.children = [nodes.Text(title)]
if not toc.children:
# empty toc means: no titles will show up in the toctree
- self.warn(docname, 'toctree contains reference to document '
- '%r that doesn\'t have a title: no link will be '
- 'generated' % includefile)
+ self.warn(docname,
+ 'toctree contains reference to document '
+ '%r that doesn\'t have a title: no link '
+ 'will be generated' % ref)
except KeyError:
# this is raised if the included file does not exist
- self.warn(docname, 'toctree contains reference to nonexisting '
- 'document %r' % includefile)
+ self.warn(docname, 'toctree contains reference to '
+ 'nonexisting document %r' % ref)
else:
# if titles_only is given, only keep the main title and
# sub-toctrees
if titles_only:
- # delete everything but the toplevel title(s) and toctrees
+ # delete everything but the toplevel title(s)
+ # and toctrees
for toplevel in toc:
# nodes with length 1 don't have any children anyway
if len(toplevel) > 1:
- subtoctrees = toplevel.traverse(addnodes.toctree)
- toplevel[1][:] = subtoctrees
+ subtrees = toplevel.traverse(addnodes.toctree)
+ toplevel[1][:] = subtrees
# resolve all sub-toctrees
for toctreenode in toc.traverse(addnodes.toctree):
i = toctreenode.parent.index(toctreenode) + 1
- for item in _entries_from_toctree(toctreenode):
+ for item in _entries_from_toctree(toctreenode,
+ subtree=True):
toctreenode.parent.insert(i, item)
i += 1
toctreenode.parent.remove(toctreenode)
@@ -932,36 +1114,41 @@ class BuildEnvironment:
entries.append(toc)
else:
entries.extend(toc.children)
+ if not subtree and not separate:
+ ret = nodes.bullet_list()
+ ret += entries
+ return [ret]
return entries
maxdepth = maxdepth or toctree.get('maxdepth', -1)
- titleoverrides = toctree.get('includetitles', {})
- tocentries = _entries_from_toctree(toctree, separate=True)
+ # NOTE: previously, this was separate=True, but that leads to artificial
+ # separation when two or more toctree entries form a logical unit, so
+ # separating mode is no longer used -- it's kept here for history's sake
+ tocentries = _entries_from_toctree(toctree, separate=False)
if not tocentries:
return None
newnode = addnodes.compact_paragraph('', '', *tocentries)
newnode['toctree'] = True
- # prune the tree to maxdepth and replace titles
- if maxdepth > 0 and prune:
- _walk_depth(newnode, 1, maxdepth, titleoverrides)
- # replace titles, if needed, and set the target paths in the
- # toctrees (they are not known at TOC generation time)
+
+ # prune the tree to maxdepth and replace titles, also set level classes
+ _walk_depth(newnode, 1, prune and maxdepth or 0)
+
+ # set the target paths in the toctrees (they are not known at TOC
+ # generation time)
for refnode in newnode.traverse(nodes.reference):
- refnode['refuri'] = builder.get_relative_uri(
- docname, refnode['refuri']) + refnode['anchorname']
- if titleoverrides and not refnode['anchorname'] \
- and refnode['refuri'] in titleoverrides:
- newtitle = titleoverrides[refnode['refuri']]
- refnode.children = [nodes.Text(newtitle)]
+ if not url_re.match(refnode['refuri']):
+ refnode['refuri'] = builder.get_relative_uri(
+ docname, refnode['refuri']) + refnode['anchorname']
return newnode
- descroles = frozenset(('data', 'exc', 'func', 'class', 'const', 'attr', 'obj',
- 'meth', 'cfunc', 'cmember', 'cdata', 'ctype', 'cmacro'))
+ descroles = frozenset(('data', 'exc', 'func', 'class', 'const',
+ 'attr', 'obj', 'meth', 'cfunc', 'cmember',
+ 'cdata', 'ctype', 'cmacro'))
def resolve_references(self, doctree, fromdocname, builder):
- reftarget_roles = set(('token', 'term', 'option', 'citation'))
+ reftarget_roles = set(('token', 'term', 'citation'))
# add all custom xref types too
reftarget_roles.update(i[0] for i in additional_xref_types.values())
@@ -975,30 +1162,33 @@ class BuildEnvironment:
try:
if typ == 'ref':
if node['refcaption']:
- # reference to anonymous label; the reference uses the supplied
- # link caption
+ # reference to anonymous label; the reference uses
+ # the supplied link caption
docname, labelid = self.anonlabels.get(target, ('',''))
sectname = node.astext()
if not docname:
- newnode = doctree.reporter.system_message(
- 2, 'undefined label: %s' % target)
+ self.warn(fromdocname, 'undefined label: %s' %
+ target, node.line)
else:
- # reference to the named label; the final node will contain the
- # section name after the label
- docname, labelid, sectname = self.labels.get(target, ('','',''))
+ # reference to the named label; the final node will
+ # contain the section name after the label
+ docname, labelid, sectname = self.labels.get(target,
+ ('','',''))
if not docname:
- newnode = doctree.reporter.system_message(
- 2, 'undefined label: %s -- if you don\'t ' % target +
- 'give a link caption the label must precede a section '
- 'header.')
+ self.warn(
+ fromdocname,
+ 'undefined label: %s' % target + ' -- if you '
+ 'don\'t give a link caption the label must '
+ 'precede a section header.', node.line)
if docname:
newnode = nodes.reference('', '')
innernode = nodes.emphasis(sectname, sectname)
if docname == fromdocname:
newnode['refid'] = labelid
else:
- # set more info in contnode in case the get_relative_uri call
- # raises NoUri, the builder will then have to resolve these
+ # set more info in contnode; in case the
+ # get_relative_uri call raises NoUri,
+ # the builder will then have to resolve these
contnode = addnodes.pending_xref('')
contnode['refdocname'] = docname
contnode['refsectname'] = sectname
@@ -1007,6 +1197,27 @@ class BuildEnvironment:
if labelid:
newnode['refuri'] += '#' + labelid
newnode.append(innernode)
+ else:
+ newnode = contnode
+ elif typ == 'doc':
+ # directly reference to document by source name;
+ # can be absolute or relative
+ docname = docname_join(fromdocname, target)
+ if docname not in self.all_docs:
+ self.warn(fromdocname, 'unknown document: %s' % docname,
+ node.line)
+ newnode = contnode
+ else:
+ if node['refcaption']:
+ # reference with explicit title
+ caption = node.astext()
+ else:
+ caption = self.titles[docname].astext()
+ innernode = nodes.emphasis(caption, caption)
+ newnode = nodes.reference('', '')
+ newnode['refuri'] = builder.get_relative_uri(
+ fromdocname, docname)
+ newnode.append(innernode)
elif typ == 'keyword':
# keywords are referenced by named labels
docname, labelid, _ = self.labels.get(target, ('','',''))
@@ -1021,14 +1232,31 @@ class BuildEnvironment:
newnode['refuri'] = builder.get_relative_uri(
fromdocname, docname) + '#' + labelid
newnode.append(contnode)
+ elif typ == 'option':
+ progname = node['refprogram']
+ docname, labelid = self.progoptions.get((progname, target),
+ ('', ''))
+ if not docname:
+ newnode = contnode
+ else:
+ newnode = nodes.reference('', '')
+ if docname == fromdocname:
+ newnode['refid'] = labelid
+ else:
+ newnode['refuri'] = builder.get_relative_uri(
+ fromdocname, docname) + '#' + labelid
+ newnode.append(contnode)
elif typ in reftarget_roles:
- docname, labelid = self.reftargets.get((typ, target), ('', ''))
+ docname, labelid = self.reftargets.get((typ, target),
+ ('', ''))
if not docname:
if typ == 'term':
- self.warn(fromdocname, 'term not in glossary: %s' % target,
+ self.warn(fromdocname,
+ 'term not in glossary: %s' % target,
node.line)
elif typ == 'citation':
- self.warn(fromdocname, 'citation not found: %s' % target,
+ self.warn(fromdocname,
+ 'citation not found: %s' % target,
node.line)
newnode = contnode
else:
@@ -1042,24 +1270,18 @@ class BuildEnvironment:
elif typ == 'mod':
docname, synopsis, platform, deprecated = \
self.modules.get(target, ('','','', ''))
- # just link to an anchor if there are multiple modules in one file
- # because the anchor is generally below the heading which is ugly
- # but can't be helped easily
- anchor = ''
if not docname:
- newnode = builder.app.emit_firstresult('missing-reference',
- self, node, contnode)
+ newnode = builder.app.emit_firstresult(
+ 'missing-reference', self, node, contnode)
if not newnode:
newnode = contnode
elif docname == fromdocname:
# don't link to self
newnode = contnode
else:
- if len(self.filemodules[docname]) > 1:
- anchor = '#' + 'module-' + target
newnode = nodes.reference('', '')
- newnode['refuri'] = (
- builder.get_relative_uri(fromdocname, docname) + anchor)
+ newnode['refuri'] = builder.get_relative_uri(
+ fromdocname, docname) + '#module-' + target
newnode['reftitle'] = '%s%s%s' % (
(platform and '(%s) ' % platform),
synopsis, (deprecated and ' (deprecated)' or ''))
@@ -1072,8 +1294,8 @@ class BuildEnvironment:
name, desc = self.find_desc(modname, clsname,
target, typ, searchorder)
if not desc:
- newnode = builder.app.emit_firstresult('missing-reference',
- self, node, contnode)
+ newnode = builder.app.emit_firstresult(
+ 'missing-reference', self, node, contnode)
if not newnode:
newnode = contnode
else:
@@ -1087,15 +1309,80 @@ class BuildEnvironment:
newnode['reftitle'] = name
newnode.append(contnode)
else:
- raise RuntimeError('unknown xfileref node encountered: %s' % node)
+ raise RuntimeError('unknown xfileref node encountered: %s'
+ % node)
except NoUri:
newnode = contnode
if newnode:
node.replace_self(newnode)
+ for node in doctree.traverse(addnodes.only):
+ try:
+ ret = builder.tags.eval_condition(node['expr'])
+ except Exception, err:
+ self.warn(fromdocname, 'exception while evaluating only '
+ 'directive expression: %s' % err, node.line)
+ node.replace_self(node.children)
+ else:
+ if ret:
+ node.replace_self(node.children)
+ else:
+ node.replace_self([])
+
# allow custom references to be resolved
builder.app.emit('doctree-resolved', doctree, fromdocname)
+ def assign_section_numbers(self):
+ """Assign a section number to each heading under a numbered toctree."""
+ # a list of all docnames whose section numbers changed
+ rewrite_needed = []
+
+ old_secnumbers = self.toc_secnumbers
+ self.toc_secnumbers = {}
+
+ def _walk_toc(node, secnums, titlenode=None):
+ # titlenode is the title of the document, it will get assigned a
+ # secnumber too, so that it shows up in next/prev/parent rellinks
+ for subnode in node.children:
+ if isinstance(subnode, nodes.bullet_list):
+ numstack.append(0)
+ _walk_toc(subnode, secnums, titlenode)
+ numstack.pop()
+ titlenode = None
+ elif isinstance(subnode, nodes.list_item):
+ _walk_toc(subnode, secnums, titlenode)
+ titlenode = None
+ elif isinstance(subnode, addnodes.compact_paragraph):
+ numstack[-1] += 1
+ secnums[subnode[0]['anchorname']] = \
+ subnode[0]['secnumber'] = tuple(numstack)
+ if titlenode:
+ titlenode['secnumber'] = tuple(numstack)
+ titlenode = None
+ elif isinstance(subnode, addnodes.toctree):
+ _walk_toctree(subnode)
+
+ def _walk_toctree(toctreenode):
+ for (title, ref) in toctreenode['entries']:
+ if url_re.match(ref) or ref == 'self':
+ # don't mess with those
+ continue
+ if ref in self.tocs:
+ secnums = self.toc_secnumbers[ref] = {}
+ _walk_toc(self.tocs[ref], secnums, self.titles.get(ref))
+ if secnums != old_secnumbers.get(ref):
+ rewrite_needed.append(ref)
+
+ for docname in self.numbered_toctrees:
+ doctree = self.get_doctree(docname)
+ for toctreenode in doctree.traverse(addnodes.toctree):
+ if toctreenode.get('numbered'):
+ # every numbered toctree gets new numbering
+ numstack = [0]
+ _walk_toctree(toctreenode)
+
+ return rewrite_needed
+
def create_index(self, builder, _fixre=re.compile(r'(.*) ([(][^()]*[)])')):
"""Create the real index from the collected index entries."""
new = {}
@@ -1114,25 +1401,42 @@ class BuildEnvironment:
pass
for fn, entries in self.indexentries.iteritems():
- # new entry types must be listed in directives.py!
+ # new entry types must be listed in directives/other.py!
for type, string, tid, alias in entries:
if type == 'single':
try:
entry, subentry = string.split(';', 1)
except ValueError:
entry, subentry = string, ''
+ if not entry:
+ self.warn(fn, 'invalid index entry %r' % string)
+ continue
add_entry(entry.strip(), subentry.strip())
elif type == 'pair':
- first, second = map(lambda x: x.strip(), string.split(';', 1))
+ try:
+ first, second = map(lambda x: x.strip(),
+ string.split(';', 1))
+ if not first or not second:
+ raise ValueError
+ except ValueError:
+ self.warn(fn, 'invalid pair index entry %r' % string)
+ continue
add_entry(first, second)
add_entry(second, first)
elif type == 'triple':
- first, second, third = map(lambda x: x.strip(), string.split(';', 2))
+ try:
+ first, second, third = map(lambda x: x.strip(),
+ string.split(';', 2))
+ if not first or not second or not third:
+ raise ValueError
+ except ValueError:
+ self.warn(fn, 'invalid triple index entry %r' % string)
+ continue
add_entry(first, second+' '+third)
add_entry(second, third+', '+first)
add_entry(third, first+' '+second)
else:
- self.warn(fn, "unknown index entry type %r" % type)
+ self.warn(fn, 'unknown index entry type %r' % type)
newlist = new.items()
newlist.sort(key=lambda t: t[0].lower())
@@ -1154,8 +1458,10 @@ class BuildEnvironment:
m = _fixre.match(key)
if m:
if oldkey == m.group(1):
- # prefixes match: add entry as subitem of the previous entry
- oldsubitems.setdefault(m.group(2), [[], {}])[0].extend(targets)
+ # prefixes match: add entry as subitem of the
+ # previous entry
+ oldsubitems.setdefault(m.group(2), [[], {}])[0].\
+ extend(targets)
del newlist[i]
continue
oldkey = m.group(1)
@@ -1175,7 +1481,8 @@ class BuildEnvironment:
else:
# get all other symbols under one heading
return 'Symbols'
- return [(key, list(group)) for (key, group) in groupby(newlist, keyfunc)]
+ return [(key, list(group))
+ for (key, group) in groupby(newlist, keyfunc)]
def collect_relations(self):
relations = {}
@@ -1214,7 +1521,8 @@ class BuildEnvironment:
# else it will stay None
# same for children
if includes:
- for subindex, args in enumerate(izip(includes, [None] + includes,
+ for subindex, args in enumerate(izip(includes,
+ [None] + includes,
includes[1:] + [None])):
collect([(docname, subindex)] + parents, *args)
relations[docname] = [parents[0][0], previous, next]
@@ -1282,14 +1590,16 @@ class BuildEnvironment:
def find_keyword(self, keyword, avoid_fuzzy=False, cutoff=0.6, n=20):
"""
- Find keyword matches for a keyword. If there's an exact match, just return
- it, else return a list of fuzzy matches if avoid_fuzzy isn't True.
+ Find keyword matches for a keyword. If there's an exact match,
+ just return it, else return a list of fuzzy matches if avoid_fuzzy
+ isn't True.
Keywords searched are: first modules, then descrefs.
Returns: None if nothing found
(type, docname, anchorname) if exact match found
- list of (quality, type, docname, anchorname, description) if fuzzy
+ list of (quality, type, docname, anchorname, description)
+ if fuzzy
"""
if keyword in self.modules:
diff --git a/sphinx/errors.py b/sphinx/errors.py
new file mode 100644
index 000000000..d9b8b6b87
--- /dev/null
+++ b/sphinx/errors.py
@@ -0,0 +1,48 @@
+# -*- coding: utf-8 -*-
+"""
+ sphinx.errors
+ ~~~~~~~~~~~~~
+
+ Contains SphinxError and a few subclasses (in an extra module to avoid
+ circular import problems).
+
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+class SphinxError(Exception):
+ """
+ Base class for Sphinx errors that are shown to the user in a nicer
+ way than normal exceptions.
+ """
+ category = 'Sphinx error'
+
+
+class SphinxWarning(SphinxError):
+ """Raised for warnings if warnings are treated as errors."""
+ category = 'Warning, treated as error'
+
+
+class ExtensionError(SphinxError):
+ """Raised if something's wrong with the configuration."""
+ category = 'Extension error'
+
+ def __init__(self, message, orig_exc=None):
+ super(ExtensionError, self).__init__(message)
+ self.orig_exc = orig_exc
+
+ def __repr__(self):
+ if self.orig_exc:
+ return '%s(%r, %r)' % (self.__class__.__name__,
+ self.message, self.orig_exc)
+ return '%s(%r)' % (self.__class__.__name__, self.message)
+
+ def __str__(self):
+ parent_str = super(ExtensionError, self).__str__()
+ if self.orig_exc:
+ return '%s (exception: %s)' % (parent_str, self.orig_exc)
+ return parent_str
+
+
+class ThemeError(SphinxError):
+ category = 'Theme error'
diff --git a/sphinx/ext/__init__.py b/sphinx/ext/__init__.py
index d111f11b9..c6e239518 100644
--- a/sphinx/ext/__init__.py
+++ b/sphinx/ext/__init__.py
@@ -5,6 +5,6 @@
Contains Sphinx features not activated by default.
- :copyright: 2008 by Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
diff --git a/sphinx/ext/autodoc.py b/sphinx/ext/autodoc.py
index f77be89c8..43c4b26a9 100644
--- a/sphinx/ext/autodoc.py
+++ b/sphinx/ext/autodoc.py
@@ -7,46 +7,82 @@
the doctree, thus avoiding duplication between docstrings and documentation
for those who like elaborate docstrings.
- :copyright: 2008 by Georg Brandl, Pauli Virtanen, Martin Hans.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
import re
import sys
-import types
import inspect
-import linecache
-from types import FunctionType, BuiltinMethodType, MethodType
+from types import ModuleType, FunctionType, BuiltinFunctionType, MethodType, \
+ ClassType
from docutils import nodes
-from docutils.parsers.rst import directives
+from docutils.utils import assemble_option_dict
from docutils.statemachine import ViewList
-from sphinx.util import rpartition
-from sphinx.directives.desc import py_sig_re
+from sphinx.util import rpartition, nested_parse_with_titles, force_decode
+from sphinx.pycode import ModuleAnalyzer, PycodeError
+from sphinx.application import ExtensionError
+from sphinx.util.compat import Directive
+from sphinx.util.docstrings import prepare_docstring
+
try:
base_exception = BaseException
except NameError:
base_exception = Exception
-_charset_re = re.compile(r'coding[:=]\s*([-\w.]+)')
-_module_charsets = {}
+
+#: extended signature RE: with explicit module name separated by ::
+py_ext_sig_re = re.compile(
+ r'''^ ([\w.]+::)? # explicit module name
+ ([\w.]+\.)? # module and/or class name(s)
+ (\w+) \s* # thing name
+ (?: \((.*)\) # optional: arguments
+ (?:\s* -> \s* (.*))? # return annotation
+ )? $ # and nothing more
+ ''', re.VERBOSE)
-class Options(object):
- pass
-
-
-def is_static_method(obj):
- """Check if the object given is a static method."""
- if isinstance(obj, (FunctionType, classmethod)):
+class DefDict(dict):
+ """A dict that returns a default on nonexisting keys."""
+ def __init__(self, default):
+ dict.__init__(self)
+ self.default = default
+ def __getitem__(self, key):
+ try:
+ return dict.__getitem__(self, key)
+ except KeyError:
+ return self.default
+ def __nonzero__(self):
+ # docutils check "if option_spec"
return True
- elif isinstance(obj, BuiltinMethodType):
- return obj.__self__ is not None
- elif isinstance(obj, MethodType):
- return obj.im_self is not None
- return False
+
+identity = lambda x: x
+
+
+class Options(dict):
+ """A dict/attribute hybrid that returns None on nonexisting keys."""
+ def __getattr__(self, name):
+ try:
+ return self[name.replace('_', '-')]
+ except KeyError:
+ return None
+
+
+ALL = object()
+
+def members_option(arg):
+ """Used to convert the :members: option to auto directives."""
+ if arg is None:
+ return ALL
+ return [x.strip() for x in arg.split(',')]
+
+def bool_option(arg):
+ """Used to convert flag options to auto directives. (Instead of
+ directives.flag(), which returns None.)"""
+ return True
class AutodocReporter(object):
@@ -155,504 +191,913 @@ def between(marker, what=None, keepempty=False):
def isdescriptor(x):
"""Check if the object is some kind of descriptor."""
for item in '__get__', '__set__', '__delete__':
- if callable(getattr(x, item, None)):
+ if hasattr(getattr(x, item, None), '__call__'):
return True
return False
-def prepare_docstring(s):
+class Documenter(object):
"""
- Convert a docstring into lines of parseable reST. Return it as a list of
- lines usable for inserting into a docutils ViewList (used as argument
- of nested_parse().) An empty line is added to act as a separator between
- this docstring and following content.
+ A Documenter knows how to autodocument a single object type. When
+ registered with the AutoDirective, it will be used to document objects
+ of that type when needed by autodoc.
+
+ Its *objtype* attribute selects what auto directive it is assigned to
+ (the directive name is 'auto' + objtype), and what directive it generates
+ by default, though that can be overridden by an attribute called
+ *directivetype*.
+
+ A Documenter has an *option_spec* that works like a docutils directive's;
+ in fact, it will be used to parse an auto directive's options that matches
+ the documenter.
"""
- lines = s.expandtabs().splitlines()
- # Find minimum indentation of any non-blank lines after first line.
- margin = sys.maxint
- for line in lines[1:]:
- content = len(line.lstrip())
- if content:
- indent = len(line) - content
- margin = min(margin, indent)
- # Remove indentation.
- if lines:
- lines[0] = lines[0].lstrip()
- if margin < sys.maxint:
- for i in range(1, len(lines)): lines[i] = lines[i][margin:]
- # Remove any leading blank lines.
- while lines and not lines[0]:
- lines.pop(0)
- # make sure there is an empty line at the end
- if lines and lines[-1]:
- lines.append('')
- return lines
+ #: name by which the directive is called (auto...) and the default
+ #: generated directive name
+ objtype = 'object'
+ #: indentation by which to indent the directive content
+ content_indent = u' '
+ #: priority if multiple documenters return True from can_document_member
+ priority = 0
+ #: order if autodoc_member_order is set to 'groupwise'
+ member_order = 0
+ option_spec = {'noindex': bool_option}
-def get_module_charset(module):
- """Return the charset of the given module (cached in _module_charsets)."""
- if module in _module_charsets:
- return _module_charsets[module]
- try:
- filename = __import__(module, None, None, ['foo']).__file__
- except (ImportError, AttributeError):
- return None
- if filename[-4:].lower() in ('.pyc', '.pyo'):
- filename = filename[:-1]
- for line in [linecache.getline(filename, x) for x in (1, 2)]:
- match = _charset_re.search(line)
- if match is not None:
- charset = match.group(1)
- break
- else:
- charset = 'ascii'
- _module_charsets[module] = charset
- return charset
+ @staticmethod
+ def get_attr(obj, name, *defargs):
+ """getattr() override for types such as Zope interfaces."""
+ for typ, func in AutoDirective._special_attrgetters.iteritems():
+ if isinstance(obj, typ):
+ return func(obj, name, *defargs)
+ return getattr(obj, name, *defargs)
+ @classmethod
+ def can_document_member(cls, member, membername, isattr, parent):
+ """Called to see if a member can be documented by this documenter."""
+ raise NotImplementedError('must be implemented in subclasses')
-class RstGenerator(object):
- def __init__(self, options, document, lineno):
- self.options = options
- self.env = document.settings.env
- self.reporter = document.reporter
- self.lineno = lineno
- self.filename_set = set()
- self.warnings = []
- self.result = ViewList()
+ def __init__(self, directive, name, indent=u''):
+ self.directive = directive
+ self.env = directive.env
+ self.options = directive.genopt
+ self.name = name
+ self.indent = indent
+ # the module and object path within the module, and the fully
+ # qualified name (all set after resolve_name succeeds)
+ self.modname = None
+ self.module = None
+ self.objpath = None
+ self.fullname = None
+ # extra signature items (arguments and return annotation,
+ # also set after resolve_name succeeds)
+ self.args = None
+ self.retann = None
+ # the object to document (set after import_object succeeds)
+ self.object = None
+ # the module analyzer to get at attribute docs, or None
+ self.analyzer = None
- def warn(self, msg):
- self.warnings.append(self.reporter.warning(msg, line=self.lineno))
+ def add_line(self, line, source, *lineno):
+ """Append one line of generated reST to the output."""
+ self.directive.result.append(self.indent + line, source, *lineno)
- def get_doc(self, what, name, obj):
- """Format and yield lines of the docstring(s) for the object."""
- docstrings = []
- if getattr(obj, '__doc__', None):
- docstrings.append(obj.__doc__)
- # skip some lines in module docstrings if configured
- if what == 'module' and self.env.config.automodule_skip_lines and docstrings:
- docstrings[0] = '\n'.join(docstrings[0].splitlines()
- [self.env.config.automodule_skip_lines:])
- # for classes, what the "docstring" is can be controlled via an option
- if what in ('class', 'exception'):
- content = self.env.config.autoclass_content
- if content in ('both', 'init'):
- initdocstring = getattr(obj, '__init__', None).__doc__
- # for new-style classes, no __init__ means default __init__
- if initdocstring == object.__init__.__doc__:
- initdocstring = None
- if initdocstring:
- if content == 'init':
- docstrings = [initdocstring]
- else:
- docstrings.append('\n\n' + initdocstring)
- # the default is only the class docstring
+ def resolve_name(self, modname, parents, path, base):
+ """
+ Resolve the module and name of the object to document given by the
+ arguments and the current module/class.
- # decode the docstrings using the module's source encoding
- charset = None
- module = getattr(obj, '__module__', None)
- if module is not None:
- charset = get_module_charset(module)
+ Must return a pair of the module name and a chain of attributes; for
+ example, it would return ``('zipfile', ['ZipFile', 'open'])`` for the
+ ``zipfile.ZipFile.open`` method.
+ """
+ raise NotImplementedError('must be implemented in subclasses')
- for docstring in docstrings:
- if isinstance(docstring, str):
- if charset:
- docstring = docstring.decode(charset)
- else:
- try:
- # try decoding with utf-8, should only work for real UTF-8
- docstring = docstring.decode('utf-8')
- except UnicodeError:
- # last resort -- can't fail
- docstring = docstring.decode('latin1')
- docstringlines = prepare_docstring(docstring)
- if self.env.app:
- # let extensions preprocess docstrings
- self.env.app.emit('autodoc-process-docstring',
- what, name, obj, self.options, docstringlines)
- for line in docstringlines:
- yield line
-
- def resolve_name(self, what, name):
+ def parse_name(self):
"""
Determine what module to import and what attribute to document.
- Returns a tuple of: the full name, the module name, a path of
- names to get via getattr, the signature and return annotation.
+ Returns True and sets *self.modname*, *self.objpath*, *self.fullname*,
+ *self.args* and *self.retann* if parsing and resolving was successful.
"""
- # first, parse the definition -- auto directives for classes and functions
- # can contain a signature which is then used instead of an autogenerated one
+ # first, parse the definition -- auto directives for classes and
+ # functions can contain a signature which is then used instead of
+ # an autogenerated one
try:
- path, base, args, retann = py_sig_re.match(name).groups()
- except:
- self.warn('invalid signature for auto%s (%r)' % (what, name))
- return
- # fullname is the fully qualified name, base the name after the last dot
- fullname = (path or '') + base
-
- if what == 'module':
- if args or retann:
- self.warn('ignoring signature arguments and return annotation '
- 'for automodule %s' % fullname)
- return fullname, fullname, [], None, None
-
- elif what in ('class', 'exception', 'function'):
- if path:
- mod = path.rstrip('.')
- else:
- mod = None
- # if documenting a toplevel object without explicit module, it can
- # be contained in another auto directive ...
- if hasattr(self.env, 'autodoc_current_module'):
- mod = self.env.autodoc_current_module
- # ... or in the scope of a module directive
- if not mod:
- mod = self.env.currmodule
- return fullname, mod, [base], args, retann
+ explicit_modname, path, base, args, retann = \
+ py_ext_sig_re.match(self.name).groups()
+ except AttributeError:
+ self.directive.warn('invalid signature for auto%s (%r)' %
+ (self.objtype, self.name))
+ return False
+ # support explicit module and class name separation via ::
+ if explicit_modname is not None:
+ modname = explicit_modname[:-2]
+ parents = path and path.rstrip('.').split('.') or []
else:
- if path:
- mod_cls = path.rstrip('.')
- else:
- mod_cls = None
- # if documenting a class-level object without path, there must be a
- # current class, either from a parent auto directive ...
- if hasattr(self.env, 'autodoc_current_class'):
- mod_cls = self.env.autodoc_current_class
- # ... or from a class directive
- if mod_cls is None:
- mod_cls = self.env.currclass
- mod, cls = rpartition(mod_cls, '.')
- # if the module name is still missing, get it like above
- if not mod and hasattr(self.env, 'autodoc_current_module'):
- mod = self.env.autodoc_current_module
- if not mod:
- mod = self.env.currmodule
- return fullname, mod, [cls, base], args, retann
+ modname = None
+ parents = []
- def format_signature(self, what, name, obj, args, retann):
- """
- Return the signature of the object, formatted for display.
- """
- if what not in ('class', 'method', 'function'):
- return ''
+ self.modname, self.objpath = \
+ self.resolve_name(modname, parents, path, base)
- err = None
- if args is not None:
+ if not self.modname:
+ return False
+
+ self.args = args
+ self.retann = retann
+ self.fullname = (self.modname or '') + \
+ (self.objpath and '.' + '.'.join(self.objpath) or '')
+ return True
+
+ def import_object(self):
+ """
+ Import the object given by *self.modname* and *self.objpath* and sets
+ it as *self.object*.
+
+ Returns True if successful, False if an error occurred.
+ """
+ try:
+ __import__(self.modname)
+ obj = self.module = sys.modules[self.modname]
+ for part in self.objpath:
+ obj = self.get_attr(obj, part)
+ self.object = obj
+ return True
+ except (ImportError, AttributeError), err:
+ self.directive.warn(
+ 'autodoc can\'t import/find %s %r, it reported error: '
+ '"%s", please check your spelling and sys.path' %
+ (self.objtype, str(self.fullname), err))
+ return False
+
+ def get_real_modname(self):
+ """
+ Get the real module name of an object to document. (It can differ
+ from the name of the module through which the object was imported.)
+ """
+ return self.get_attr(self.object, '__module__', None) or self.modname
+
+ def check_module(self):
+ """
+ Check if *self.object* is really defined in the module given by
+ *self.modname*.
+ """
+ modname = self.get_attr(self.object, '__module__', None)
+ if modname and modname != self.modname:
+ return False
+ return True
+
+ def format_args(self):
+ """
+ Format the argument signature of *self.object*. Should return None if
+ the object does not have a signature.
+ """
+ return None
+
+ def format_signature(self):
+ """
+ Format the signature (arguments and return annotation) of the object.
+ Let the user process it via the ``autodoc-process-signature`` event.
+ """
+ if self.args is not None:
# signature given explicitly
- args = "(%s)" % args
+ args = "(%s)" % self.args
else:
# try to introspect the signature
- try:
- args = None
- getargs = True
- if what == 'class':
- # for classes, the relevant signature is the __init__ method's
- obj = getattr(obj, '__init__', None)
- # classes without __init__ method, default __init__ or
- # __init__ written in C?
- if obj is None or obj is object.__init__ or not \
- (inspect.ismethod(obj) or inspect.isfunction(obj)):
- getargs = False
- elif inspect.isbuiltin(obj) or inspect.ismethoddescriptor(obj):
- # can never get arguments of a C function or method
- getargs = False
- if getargs:
- argspec = inspect.getargspec(obj)
- if what in ('class', 'method') and argspec[0] and \
- argspec[0][0] in ('cls', 'self'):
- del argspec[0][0]
- args = inspect.formatargspec(*argspec)
- except Exception, e:
- args = None
- err = e
+ args = self.format_args()
+ if args is None:
+ return ''
+ retann = self.retann
- result = self.env.app.emit_firstresult('autodoc-process-signature', what,
- name, obj, self.options, args, retann)
+ result = self.env.app.emit_firstresult(
+ 'autodoc-process-signature', self.objtype, self.fullname,
+ self.object, self.options, args, retann)
if result:
args, retann = result
if args is not None:
- return '%s%s' % (args, retann or '')
- elif err:
- # re-raise the error for perusal of the handler in generate()
- raise RuntimeError(err)
+ return args + (retann and (' -> %s' % retann) or '')
else:
return ''
- def generate(self, what, name, members, add_content, indent=u'', check_module=False):
- """
- Generate reST for the object in self.result.
- """
- fullname, mod, objpath, args, retann = self.resolve_name(what, name)
- if not mod:
- # need a module to import
- self.warn('don\'t know which module to import for autodocumenting %r '
- '(try placing a "module" or "currentmodule" directive in the '
- 'document, or giving an explicit module name)' % fullname)
- return
-
- # the name to put into the generated directive -- doesn't contain the module
- name_in_directive = '.'.join(objpath) or mod
-
- # now, import the module and get object to document
- try:
- todoc = module = __import__(mod, None, None, ['foo'])
- if hasattr(module, '__file__') and module.__file__:
- modfile = module.__file__
- if modfile[-4:].lower() in ('.pyc', '.pyo'):
- modfile = modfile[:-1]
- self.filename_set.add(modfile)
- else:
- modfile = None # e.g. for builtin and C modules
- for part in objpath:
- todoc = getattr(todoc, part)
- except (ImportError, AttributeError), err:
- self.warn('autodoc can\'t import/find %s %r, it reported error: "%s", '
- 'please check your spelling and sys.path' %
- (what, str(fullname), err))
- return
-
- # check __module__ of object if wanted (for members not given explicitly)
- if check_module:
- if hasattr(todoc, '__module__'):
- if todoc.__module__ != mod:
- return
-
- # format the object's signature, if any
- try:
- sig = self.format_signature(what, name, todoc, args, retann)
- except Exception, err:
- self.warn('error while formatting signature for %s: %s' %
- (fullname, err))
- sig = ''
-
- # make sure that the result starts with an empty line. This is
- # necessary for some situations where another directive preprocesses
- # reST and no starting newline is present
- self.result.append(u'', '')
-
- # now, create the directive header
- directive = (what == 'method' and is_static_method(todoc)) \
- and 'staticmethod' or what
- self.result.append(indent + u'.. %s:: %s%s' %
- (directive, name_in_directive, sig), '')
- if what == 'module':
- # Add some module-specific options
- if self.options.synopsis:
- self.result.append(indent + u' :synopsis: ' + self.options.synopsis,
- '')
- if self.options.platform:
- self.result.append(indent + u' :platform: ' + self.options.platform,
- '')
- if self.options.deprecated:
- self.result.append(indent + u' :deprecated:', '')
- else:
- # Be explicit about the module, this is necessary since .. class:: doesn't
- # support a prepended module name
- self.result.append(indent + u' :module: %s' % mod, '')
+ def add_directive_header(self, sig):
+ """Add the directive header and options to the generated content."""
+ directive = getattr(self, 'directivetype', self.objtype)
+ # the name to put into the generated directive -- doesn't contain
+ # the module (except for module directive of course)
+ name_in_directive = '.'.join(self.objpath) or self.modname
+ self.add_line(u'.. %s:: %s%s' % (directive, name_in_directive, sig),
+ '')
if self.options.noindex:
- self.result.append(indent + u' :noindex:', '')
- self.result.append(u'', '')
+ self.add_line(u' :noindex:', '')
+ if self.objpath:
+ # Be explicit about the module, this is necessary since .. class::
+ # etc. don't support a prepended module name
+ self.add_line(u' :module: %s' % self.modname, '')
- if self.options.show_inheritance and what in ('class', 'exception'):
- if len(todoc.__bases__):
- bases = [b.__module__ == '__builtin__' and
- u':class:`%s`' % b.__name__ or
- u':class:`%s.%s`' % (b.__module__, b.__name__)
- for b in todoc.__bases__]
- self.result.append(indent + u' Bases: %s' % ', '.join(bases),
- '')
- self.result.append(u'', '')
+ def get_doc(self, encoding=None):
+ """Decode and return lines of the docstring(s) for the object."""
+ docstring = self.get_attr(self.object, '__doc__', None)
+ if docstring:
+ # make sure we have Unicode docstrings, then sanitize and split
+ # into lines
+ return [prepare_docstring(force_decode(docstring, encoding))]
+ return []
- # the module directive doesn't have content
- if what != 'module':
- indent += u' '
+ def process_doc(self, docstrings):
+ """Let the user process the docstrings before adding them."""
+ for docstringlines in docstrings:
+ if self.env.app:
+ # let extensions preprocess docstrings
+ self.env.app.emit('autodoc-process-docstring',
+ self.objtype, self.fullname, self.object,
+ self.options, docstringlines)
+ for line in docstringlines:
+ yield line
- if modfile:
- sourcename = '%s:docstring of %s' % (modfile, fullname)
+ def add_content(self, more_content, no_docstring=False):
+ """Add content from docstrings, attribute documentation and user."""
+ # set sourcename and add content from attribute documentation
+ if self.analyzer:
+ # prevent encoding errors when the file name is non-ASCII
+ filename = unicode(self.analyzer.srcname,
+ sys.getfilesystemencoding(), 'replace')
+ sourcename = u'%s:docstring of %s' % (filename, self.fullname)
+
+ attr_docs = self.analyzer.find_attr_docs()
+ if self.objpath:
+ key = ('.'.join(self.objpath[:-1]), self.objpath[-1])
+ if key in attr_docs:
+ no_docstring = True
+ docstrings = [attr_docs[key]]
+ for i, line in enumerate(self.process_doc(docstrings)):
+ self.add_line(line, sourcename, i)
else:
- sourcename = 'docstring of %s' % fullname
+ sourcename = u'docstring of %s' % self.fullname
# add content from docstrings
- for i, line in enumerate(self.get_doc(what, fullname, todoc)):
- self.result.append(indent + line, sourcename, i)
+ if not no_docstring:
+ encoding = self.analyzer and self.analyzer.encoding
+ docstrings = self.get_doc(encoding)
+ for i, line in enumerate(self.process_doc(docstrings)):
+ self.add_line(line, sourcename, i)
- # add source content, if present
- if add_content:
- for line, src in zip(add_content.data, add_content.items):
- self.result.append(indent + line, src[0], src[1])
+ # add additional content (e.g. from document), if present
+ if more_content:
+ for line, src in zip(more_content.data, more_content.items):
+ self.add_line(line, src[0], src[1])
- # document members?
- if not members or what in ('function', 'method', 'attribute'):
- return
+ def get_object_members(self, want_all):
+ """
+ Return `(members_check_module, members)` where `members` is a
+ list of `(membername, member)` pairs of the members of *self.object*.
- # set current namespace for finding members
- self.env.autodoc_current_module = mod
- if objpath:
- self.env.autodoc_current_class = objpath[0]
-
- # add members, if possible
- _all = members == ['__all__']
- members_check_module = False
- if _all:
- # unqualified :members: given
- if what == 'module':
- if hasattr(todoc, '__all__'):
- members_check_module = False
- all_members = []
- for mname in todoc.__all__:
- try:
- all_members.append((mname, getattr(todoc, mname)))
- except AttributeError:
- self.warn('missing attribute mentioned in __all__: '
- 'module %s, attribute %s' %
- (todoc.__name__, mname))
- else:
- # for implicit module members, check __module__ to avoid
- # documenting imported objects
- members_check_module = True
- all_members = inspect.getmembers(todoc)
- else:
- if self.options.inherited_members:
- # getmembers() uses dir() which pulls in members from all
- # base classes
- all_members = inspect.getmembers(todoc)
- else:
- # __dict__ contains only the members directly defined in the class
- all_members = sorted(todoc.__dict__.iteritems())
+ If *want_all* is True, return all members. Else, only return those
+ members given by *self.options.members* (which may also be none).
+ """
+ if not want_all:
+ if not self.options.members:
+ return False, []
+ # specific members given
+ ret = []
+ for mname in self.options.members:
+ try:
+ ret.append((mname, self.get_attr(self.object, mname)))
+ except AttributeError:
+ self.directive.warn('missing attribute %s in object %s'
+ % (mname, self.fullname))
+ return False, ret
+ elif self.options.inherited_members:
+ # getmembers() uses dir() which pulls in members from all
+ # base classes
+ return False, inspect.getmembers(self.object)
else:
- all_members = [(mname, getattr(todoc, mname)) for mname in members]
- for (membername, member) in all_members:
- # ignore members whose name starts with _ by default
- if _all and membername.startswith('_'):
- continue
+ # __dict__ contains only the members directly defined in
+ # the class (but get them via getattr anyway, to e.g. get
+ # unbound method objects instead of function objects)
+ return False, sorted([
+ (mname, self.get_attr(self.object, mname))
+ for mname in self.get_attr(self.object, '__dict__')])
- # ignore undocumented members if :undoc-members: is not given
- doc = getattr(member, '__doc__', None)
- skip = not self.options.undoc_members and not doc
- # give the user a chance to decide whether this member should be skipped
+ def filter_members(self, members, want_all):
+ """
+ Filter the given member list: members are skipped if
+
+ - they are private (except if given explicitly)
+ - they are undocumented (except if undoc-members is given)
+
+ The user can override the skipping decision by connecting to the
+ ``autodoc-skip-member`` event.
+ """
+ ret = []
+
+ # search for members in source code too
+ namespace = '.'.join(self.objpath) # will be empty for modules
+
+ if self.analyzer:
+ attr_docs = self.analyzer.find_attr_docs()
+ else:
+ attr_docs = {}
+
+ # process members and determine which to skip
+ for (membername, member) in members:
+ # if isattr is True, the member is documented as an attribute
+ isattr = False
+
+ if want_all and membername.startswith('_'):
+ # ignore members whose name starts with _ by default
+ skip = True
+ elif (namespace, membername) in attr_docs:
+ # keep documented attributes
+ skip = False
+ isattr = True
+ else:
+ # ignore undocumented members if :undoc-members:
+ # is not given
+ doc = self.get_attr(member, '__doc__', None)
+ skip = not self.options.undoc_members and not doc
+
+ # give the user a chance to decide whether this member
+ # should be skipped
if self.env.app:
# let extensions preprocess docstrings
skip_user = self.env.app.emit_firstresult(
- 'autodoc-skip-member', what, membername, member, skip, self.options)
+ 'autodoc-skip-member', self.objtype, membername, member,
+ skip, self.options)
if skip_user is not None:
skip = skip_user
if skip:
continue
- if what == 'module':
- if isinstance(member, types.FunctionType):
- memberwhat = 'function'
- elif isinstance(member, types.ClassType) or \
- isinstance(member, type):
- if issubclass(member, base_exception):
- memberwhat = 'exception'
- else:
- memberwhat = 'class'
- else:
- # XXX: todo -- attribute docs
- continue
- else:
- if callable(member):
- memberwhat = 'method'
- elif isdescriptor(member):
- memberwhat = 'attribute'
- else:
- # XXX: todo -- attribute docs
- continue
- full_membername = fullname + '.' + membername
- self.generate(memberwhat, full_membername, ['__all__'], None, indent,
- check_module=members_check_module)
+ ret.append((membername, member, isattr))
+ return ret
+
+ def document_members(self, all_members=False):
+ """
+ Generate reST for member documentation. If *all_members* is True,
+ do all members, else those given by *self.options.members*.
+ """
+ # set current namespace for finding members
+ self.env.autodoc_current_module = self.modname
+ if self.objpath:
+ self.env.autodoc_current_class = self.objpath[0]
+
+ want_all = all_members or self.options.inherited_members or \
+ self.options.members is ALL
+ # find out which members are documentable
+ members_check_module, members = self.get_object_members(want_all)
+
+ # document non-skipped members
+ memberdocumenters = []
+ for (mname, member, isattr) in self.filter_members(members, want_all):
+ classes = [cls for cls in AutoDirective._registry.itervalues()
+ if cls.can_document_member(member, mname, isattr, self)]
+ if not classes:
+ # don't know how to document this member
+ continue
+ # prefer the documenter with the highest priority
+ classes.sort(key=lambda cls: cls.priority)
+ # give explicitly separated module name, so that members
+ # of inner classes can be documented
+ full_mname = self.modname + '::' + \
+ '.'.join(self.objpath + [mname])
+ memberdocumenters.append(
+ classes[-1](self.directive, full_mname, self.indent))
+
+ if (self.options.member_order or self.env.config.autodoc_member_order) \
+ == 'groupwise':
+ # sort by group; relies on stable sort to keep items in the
+ # same group sorted alphabetically
+ memberdocumenters.sort(key=lambda d: d.member_order)
+
+ for documenter in memberdocumenters:
+ documenter.generate(all_members=True,
+ real_modname=self.real_modname,
+ check_module=members_check_module)
+
+ # reset current objects
self.env.autodoc_current_module = None
self.env.autodoc_current_class = None
+ def generate(self, more_content=None, real_modname=None,
+ check_module=False, all_members=False):
+ """
+ Generate reST for the object given by *self.name*, and possibly members.
-def _auto_directive(dirname, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- what = dirname[4:] # strip "auto"
- name = arguments[0]
- genopt = Options()
- members = options.get('members', [])
- genopt.inherited_members = 'inherited-members' in options
- if genopt.inherited_members and not members:
- # :inherited-members: implies :members:
- members = ['__all__']
- genopt.undoc_members = 'undoc-members' in options
- genopt.show_inheritance = 'show-inheritance' in options
- genopt.noindex = 'noindex' in options
- genopt.synopsis = options.get('synopsis', '')
- genopt.platform = options.get('platform', '')
- genopt.deprecated = 'deprecated' in options
+ If *more_content* is given, include that content. If *real_modname* is
+ given, use that module name to find attribute docs. If *check_module* is
+ True, only generate if the object is defined in the module name it is
+ imported from. If *all_members* is True, document all members.
+ """
+ if not self.parse_name():
+ # need a module to import
+ self.directive.warn(
+ 'don\'t know which module to import for autodocumenting '
+ '%r (try placing a "module" or "currentmodule" directive '
+ 'in the document, or giving an explicit module name)'
+ % self.name)
+ return
- generator = RstGenerator(genopt, state.document, lineno)
- generator.generate(what, name, members, content)
- if not generator.result:
- return generator.warnings
+ # now, import the module and get object to document
+ if not self.import_object():
+ return
- # record all filenames as dependencies -- this will at least partially make
- # automatic invalidation possible
- for fn in generator.filename_set:
- state.document.settings.env.note_dependency(fn)
+ # If there is no real module defined, figure out which to use.
+ # The real module is used in the module analyzer to look up the module
+ # where the attribute documentation would actually be found in.
+ # This is used for situations where you have a module that collects the
+ # functions and classes of internal submodules.
+ self.real_modname = real_modname or self.get_real_modname()
- # use a custom reporter that correctly assigns lines to source and lineno
- old_reporter = state.memo.reporter
- state.memo.reporter = AutodocReporter(generator.result, state.memo.reporter)
- if dirname == 'automodule':
- node = nodes.section()
- # hack around title style bookkeeping
- surrounding_title_styles = state.memo.title_styles
- surrounding_section_level = state.memo.section_level
- state.memo.title_styles = []
- state.memo.section_level = 0
- state.nested_parse(generator.result, 0, node, match_titles=1)
- state.memo.title_styles = surrounding_title_styles
- state.memo.section_level = surrounding_section_level
- else:
- node = nodes.paragraph()
- state.nested_parse(generator.result, 0, node)
- state.memo.reporter = old_reporter
- return generator.warnings + node.children
+ # try to also get a source code analyzer for attribute docs
+ try:
+ self.analyzer = ModuleAnalyzer.for_module(self.real_modname)
+ # parse right now, to get PycodeErrors on parsing
+ self.analyzer.parse()
+ except PycodeError, err:
+ # no source file -- e.g. for builtin and C modules
+ self.analyzer = None
+ else:
+ self.directive.filename_set.add(self.analyzer.srcname)
-def auto_directive(*args, **kwds):
- return _auto_directive(*args, **kwds)
+ # check __module__ of object (for members not given explicitly)
+ if check_module:
+ if not self.check_module():
+ return
-def automodule_directive(*args, **kwds):
- return _auto_directive(*args, **kwds)
+ # make sure that the result starts with an empty line. This is
+ # necessary for some situations where another directive preprocesses
+ # reST and no starting newline is present
+ self.add_line(u'', '')
-def autoclass_directive(*args, **kwds):
- return _auto_directive(*args, **kwds)
+ # format the object's signature, if any
+ try:
+ sig = self.format_signature()
+ except Exception, err:
+ self.directive.warn('error while formatting signature for '
+ '%s: %s' % (self.fullname, err))
+ sig = ''
+
+ # generate the directive header and options, if applicable
+ self.add_directive_header(sig)
+ self.add_line(u'', '')
+
+ # e.g. the module directive doesn't have content
+ self.indent += self.content_indent
+
+ # add all content (from docstrings, attribute docs etc.)
+ self.add_content(more_content)
+
+ # document members, if possible
+ self.document_members(all_members)
-def members_option(arg):
- if arg is None:
- return ['__all__']
- return [x.strip() for x in arg.split(',')]
+class ModuleDocumenter(Documenter):
+ """
+ Specialized Documenter subclass for modules.
+ """
+ objtype = 'module'
+ content_indent = u''
+
+ option_spec = {
+ 'members': members_option, 'undoc-members': bool_option,
+ 'noindex': bool_option, 'inherited-members': bool_option,
+ 'show-inheritance': bool_option, 'synopsis': identity,
+ 'platform': identity, 'deprecated': bool_option,
+ 'member-order': identity,
+ }
+
+ @classmethod
+ def can_document_member(cls, member, membername, isattr, parent):
+ # don't document submodules automatically
+ return False
+
+ def resolve_name(self, modname, parents, path, base):
+ if modname is not None:
+ self.directive.warn('"::" in automodule name doesn\'t make sense')
+ return (path or '') + base, []
+
+ def parse_name(self):
+ ret = Documenter.parse_name(self)
+ if self.args or self.retann:
+ self.directive.warn('signature arguments or return annotation '
+ 'given for automodule %s' % self.fullname)
+ return ret
+
+ def add_directive_header(self, sig):
+ Documenter.add_directive_header(self, sig)
+
+ # add some module-specific options
+ if self.options.synopsis:
+ self.add_line(
+ u' :synopsis: ' + self.options.synopsis, '')
+ if self.options.platform:
+ self.add_line(
+ u' :platform: ' + self.options.platform, '')
+ if self.options.deprecated:
+ self.add_line(u' :deprecated:', '')
+
+ def get_object_members(self, want_all):
+ if want_all:
+ if not hasattr(self.object, '__all__'):
+ # for implicit module members, check __module__ to avoid
+ # documenting imported objects
+ return True, inspect.getmembers(self.object)
+ else:
+ memberlist = self.object.__all__
+ else:
+ memberlist = self.options.members or []
+ ret = []
+ for mname in memberlist:
+ try:
+ ret.append((mname, getattr(self.object, mname)))
+ except AttributeError:
+ self.directive.warn('missing attribute mentioned in :members: '
+ 'or __all__: module %s, attribute %s' %
+ (self.object.__name__, mname))
+ return False, ret
+
+
+class ModuleLevelDocumenter(Documenter):
+ """
+ Specialized Documenter subclass for objects on module level (functions,
+ classes, data/constants).
+ """
+ def resolve_name(self, modname, parents, path, base):
+ if modname is None:
+ if path:
+ modname = path.rstrip('.')
+ else:
+ # if documenting a toplevel object without explicit module,
+ # it can be contained in another auto directive ...
+ if hasattr(self.env, 'autodoc_current_module'):
+ modname = self.env.autodoc_current_module
+ # ... or in the scope of a module directive
+ if not modname:
+ modname = self.env.currmodule
+ # ... else, it stays None, which means invalid
+ return modname, parents + [base]
+
+
+class ClassLevelDocumenter(Documenter):
+ """
+ Specialized Documenter subclass for objects on class level (methods,
+ attributes).
+ """
+ def resolve_name(self, modname, parents, path, base):
+ if modname is None:
+ if path:
+ mod_cls = path.rstrip('.')
+ else:
+ mod_cls = None
+ # if documenting a class-level object without path,
+ # there must be a current class, either from a parent
+ # auto directive ...
+ if hasattr(self.env, 'autodoc_current_class'):
+ mod_cls = self.env.autodoc_current_class
+ # ... or from a class directive
+ if mod_cls is None:
+ mod_cls = self.env.currclass
+ # ... if still None, there's no way to know
+ if mod_cls is None:
+ return None, []
+ modname, cls = rpartition(mod_cls, '.')
+ parents = [cls]
+ # if the module name is still missing, get it like above
+ if not modname and hasattr(self.env, 'autodoc_current_module'):
+ modname = self.env.autodoc_current_module
+ if not modname:
+ modname = self.env.currmodule
+ # ... else, it stays None, which means invalid
+ return modname, parents + [base]
+
+
+class FunctionDocumenter(ModuleLevelDocumenter):
+ """
+ Specialized Documenter subclass for functions.
+ """
+ objtype = 'function'
+ member_order = 30
+
+ @classmethod
+ def can_document_member(cls, member, membername, isattr, parent):
+ return isinstance(member, (FunctionType, BuiltinFunctionType))
+
+ def format_args(self):
+ if inspect.isbuiltin(self.object) or \
+ inspect.ismethoddescriptor(self.object):
+ # can never get arguments of a C function or method
+ return None
+ try:
+ argspec = inspect.getargspec(self.object)
+ except TypeError:
+ # if a class should be documented as function (yay duck
+ # typing) we try to use the constructor signature as function
+ # signature without the first argument.
+ try:
+ argspec = inspect.getargspec(self.object.__new__)
+ except TypeError:
+ argspec = inspect.getargspec(self.object.__init__)
+ if argspec[0]:
+ del argspec[0][0]
+ return inspect.formatargspec(*argspec)
+
+ def document_members(self, all_members=False):
+ pass
+
+
+class ClassDocumenter(ModuleLevelDocumenter):
+ """
+ Specialized Documenter subclass for classes.
+ """
+ objtype = 'class'
+ member_order = 20
+ option_spec = {
+ 'members': members_option, 'undoc-members': bool_option,
+ 'noindex': bool_option, 'inherited-members': bool_option,
+ 'show-inheritance': bool_option, 'member-order': identity,
+ }
+
+ @classmethod
+ def can_document_member(cls, member, membername, isattr, parent):
+ return isinstance(member, (type, ClassType))
+
+ def import_object(self):
+ ret = ModuleLevelDocumenter.import_object(self)
+ # if the class is documented under another name, document it
+ # as data/attribute
+ if ret:
+ self.doc_as_attr = (self.objpath[-1] != self.object.__name__)
+ return ret
+
+ def format_args(self):
+ args = None
+ # for classes, the relevant signature is the __init__ method's
+ initmeth = self.get_attr(self.object, '__init__', None)
+ # classes without __init__ method, default __init__ or
+ # __init__ written in C?
+ if initmeth is None or initmeth is object.__init__ or not \
+ (inspect.ismethod(initmeth) or inspect.isfunction(initmeth)):
+ return None
+ argspec = inspect.getargspec(initmeth)
+ if argspec[0] and argspec[0][0] in ('cls', 'self'):
+ del argspec[0][0]
+ return inspect.formatargspec(*argspec)
+
+ def format_signature(self):
+ if self.doc_as_attr:
+ return ''
+ return ModuleLevelDocumenter.format_signature(self)
+
+ def add_directive_header(self, sig):
+ if self.doc_as_attr:
+ self.directivetype = 'attribute'
+ Documenter.add_directive_header(self, sig)
+
+ # add inheritance info, if wanted
+ if not self.doc_as_attr and self.options.show_inheritance:
+ self.add_line(u'', '')
+ if len(self.object.__bases__):
+ bases = [b.__module__ == '__builtin__' and
+ u':class:`%s`' % b.__name__ or
+ u':class:`%s.%s`' % (b.__module__, b.__name__)
+ for b in self.object.__bases__]
+ self.add_line(_(u' Bases: %s') % ', '.join(bases),
+ '')
+
+ def get_doc(self, encoding=None):
+ content = self.env.config.autoclass_content
+
+ docstrings = []
+ docstring = self.get_attr(self.object, '__doc__', None)
+ if docstring:
+ docstrings.append(docstring)
+
+ # for classes, what the "docstring" is can be controlled via a
+ # config value; the default is only the class docstring
+ if content in ('both', 'init'):
+ initdocstring = self.get_attr(
+ self.get_attr(self.object, '__init__', None), '__doc__')
+ # for new-style classes, no __init__ means default __init__
+ if initdocstring == object.__init__.__doc__:
+ initdocstring = None
+ if initdocstring:
+ if content == 'init':
+ docstrings = [initdocstring]
+ else:
+ docstrings.append(initdocstring)
+
+ return [prepare_docstring(force_decode(docstring, encoding))
+ for docstring in docstrings]
+
+ def add_content(self, more_content, no_docstring=False):
+ if self.doc_as_attr:
+ content = ViewList(
+ [_('alias of :class:`%s`') % self.object.__name__], source='')
+ ModuleLevelDocumenter.add_content(self, content, no_docstring=True)
+ else:
+ ModuleLevelDocumenter.add_content(self, more_content)
+
+ def document_members(self, all_members=False):
+ if self.doc_as_attr:
+ return
+ ModuleLevelDocumenter.document_members(self, all_members)
+
+
+class ExceptionDocumenter(ClassDocumenter):
+ """
+ Specialized ClassDocumenter subclass for exceptions.
+ """
+ objtype = 'exception'
+ member_order = 10
+
+ # needs a higher priority than ClassDocumenter
+ priority = 10
+
+ @classmethod
+ def can_document_member(cls, member, membername, isattr, parent):
+ return isinstance(member, (type, ClassType)) and \
+ issubclass(member, base_exception)
+
+
+class DataDocumenter(ModuleLevelDocumenter):
+ """
+ Specialized Documenter subclass for data items.
+ """
+ objtype = 'data'
+ member_order = 40
+
+ @classmethod
+ def can_document_member(cls, member, membername, isattr, parent):
+ return isinstance(parent, ModuleDocumenter) and isattr
+
+ def document_members(self, all_members=False):
+ pass
+
+
+class MethodDocumenter(ClassLevelDocumenter):
+ """
+ Specialized Documenter subclass for methods (normal, static and class).
+ """
+ objtype = 'method'
+ member_order = 50
+
+ @classmethod
+ def can_document_member(cls, member, membername, isattr, parent):
+ # other attributes are recognized via the module analyzer
+ return inspect.isroutine(member) and \
+ not isinstance(parent, ModuleDocumenter)
+
+ def import_object(self):
+ ret = ClassLevelDocumenter.import_object(self)
+ if isinstance(self.object, classmethod) or \
+ (isinstance(self.object, MethodType) and
+ self.object.im_self is not None):
+ self.directivetype = 'classmethod'
+ # document class and static members before ordinary ones
+ self.member_order = self.member_order - 1
+ elif isinstance(self.object, FunctionType) or \
+ (isinstance(self.object, BuiltinFunctionType) and
+ self.object.__self__ is not None):
+ self.directivetype = 'staticmethod'
+ # document class and static members before ordinary ones
+ self.member_order = self.member_order - 1
+ else:
+ self.directivetype = 'method'
+ return ret
+
+ def format_args(self):
+ if inspect.isbuiltin(self.object) or \
+ inspect.ismethoddescriptor(self.object):
+ # can never get arguments of a C function or method
+ return None
+ argspec = inspect.getargspec(self.object)
+ if argspec[0] and argspec[0][0] in ('cls', 'self'):
+ del argspec[0][0]
+ return inspect.formatargspec(*argspec)
+
+ def document_members(self, all_members=False):
+ pass
+
+
+class AttributeDocumenter(ClassLevelDocumenter):
+ """
+ Specialized Documenter subclass for attributes.
+ """
+ objtype = 'attribute'
+ member_order = 60
+
+ @classmethod
+ def can_document_member(cls, member, membername, isattr, parent):
+ return (isdescriptor(member) and not
+ isinstance(member, (FunctionType, BuiltinFunctionType))) \
+ or (not isinstance(parent, ModuleDocumenter) and isattr)
+
+ def document_members(self, all_members=False):
+ pass
+
+
+class AutoDirective(Directive):
+ """
+ The AutoDirective class is used for all autodoc directives. It dispatches
+ most of the work to one of the Documenters, which it selects through its
+ *_registry* dictionary.
+
+ The *_special_attrgetters* attribute is used to customize ``getattr()``
+ calls that the Documenters make; its entries are of the form ``type:
+ getattr_function``.
+
+ Note: When importing an object, all items along the import chain are
+ accessed using the descendant's *_special_attrgetters*, thus this
+ dictionary should include all necessary functions for accessing
+ attributes of the parents.
+ """
+ # a registry of objtype -> documenter class
+ _registry = {}
+
+ # a registry of type -> getattr function
+ _special_attrgetters = {}
+
+ # standard docutils directive settings
+ has_content = True
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = True
+ # allow any options to be passed; the options are parsed further
+ # by the selected Documenter
+ option_spec = DefDict(identity)
+
+ def warn(self, msg):
+ self.warnings.append(self.reporter.warning(msg, line=self.lineno))
+
+ def run(self):
+ self.filename_set = set() # a set of dependent filenames
+ self.reporter = self.state.document.reporter
+ self.env = self.state.document.settings.env
+ self.warnings = []
+ self.result = ViewList()
+
+ # find out what documenter to call
+ objtype = self.name[4:]
+ doc_class = self._registry[objtype]
+ # process the options with the selected documenter's option_spec
+ self.genopt = Options(assemble_option_dict(
+ self.options.items(), doc_class.option_spec))
+ # generate the output
+ documenter = doc_class(self, self.arguments[0])
+ documenter.generate(more_content=self.content)
+ if not self.result:
+ return self.warnings
+
+ # record all filenames as dependencies -- this will at least
+ # partially make automatic invalidation possible
+ for fn in self.filename_set:
+ self.env.note_dependency(fn)
+
+ # use a custom reporter that correctly assigns lines to source
+ # filename/description and lineno
+ old_reporter = self.state.memo.reporter
+ self.state.memo.reporter = AutodocReporter(self.result,
+ self.state.memo.reporter)
+
+ if self.name == 'automodule':
+ node = nodes.section()
+ # necessary so that the child nodes get the right source/line set
+ node.document = self.state.document
+ nested_parse_with_titles(self.state, self.result, node)
+ else:
+ node = nodes.paragraph()
+ node.document = self.state.document
+ self.state.nested_parse(self.result, 0, node)
+ self.state.memo.reporter = old_reporter
+ return self.warnings + node.children
+
+
+def add_documenter(cls):
+ """Register a new Documenter."""
+ if not issubclass(cls, Documenter):
+ raise ExtensionError('autodoc documenter %r must be a subclass '
+ 'of Documenter' % cls)
+ # actually, it should be possible to override Documenters
+ #if cls.objtype in AutoDirective._registry:
+ # raise ExtensionError('autodoc documenter for %r is already '
+ # 'registered' % cls.objtype)
+ AutoDirective._registry[cls.objtype] = cls
def setup(app):
- mod_options = {'members': members_option, 'undoc-members': directives.flag,
- 'noindex': directives.flag, 'inherited-members': directives.flag,
- 'show-inheritance': directives.flag, 'synopsis': lambda x: x,
- 'platform': lambda x: x, 'deprecated': directives.flag}
- cls_options = {'members': members_option, 'undoc-members': directives.flag,
- 'noindex': directives.flag, 'inherited-members': directives.flag,
- 'show-inheritance': directives.flag}
- app.add_directive('automodule', automodule_directive,
- 1, (1, 0, 1), **mod_options)
- app.add_directive('autoclass', autoclass_directive,
- 1, (1, 0, 1), **cls_options)
- app.add_directive('autoexception', autoclass_directive,
- 1, (1, 0, 1), **cls_options)
- app.add_directive('autofunction', auto_directive, 1, (1, 0, 1),
- noindex=directives.flag)
- app.add_directive('automethod', auto_directive, 1, (1, 0, 1),
- noindex=directives.flag)
- app.add_directive('autoattribute', auto_directive, 1, (1, 0, 1),
- noindex=directives.flag)
- # deprecated: remove in some future version.
- app.add_config_value('automodule_skip_lines', 0, True)
+ app.add_autodocumenter(ModuleDocumenter)
+ app.add_autodocumenter(ClassDocumenter)
+ app.add_autodocumenter(ExceptionDocumenter)
+ app.add_autodocumenter(DataDocumenter)
+ app.add_autodocumenter(FunctionDocumenter)
+ app.add_autodocumenter(MethodDocumenter)
+ app.add_autodocumenter(AttributeDocumenter)
+
app.add_config_value('autoclass_content', 'class', True)
+ app.add_config_value('autodoc_member_order', 'alphabetic', True)
app.add_event('autodoc-process-docstring')
app.add_event('autodoc-process-signature')
app.add_event('autodoc-skip-member')
diff --git a/sphinx/ext/coverage.py b/sphinx/ext/coverage.py
index f03dbc1e2..964e58eec 100644
--- a/sphinx/ext/coverage.py
+++ b/sphinx/ext/coverage.py
@@ -6,8 +6,8 @@
Check Python modules and C API for coverage. Mostly written by Josip
Dzolonga for the Google Highly Open Participation contest.
- :copyright: 2008 by Josip Dzolonga, Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
import re
@@ -16,7 +16,7 @@ import inspect
import cPickle as pickle
from os import path
-from sphinx.builder import Builder
+from sphinx.builders import Builder
# utility
@@ -53,17 +53,17 @@ class CoverageBuilder(Builder):
self.c_ignorexps = {}
for (name, exps) in self.config.coverage_ignore_c_items.iteritems():
- self.c_ignorexps[name] = compile_regex_list('coverage_ignore_c_items',
- exps, self.warn)
- self.mod_ignorexps = compile_regex_list('coverage_ignore_modules',
- self.config.coverage_ignore_modules,
- self.warn)
- self.cls_ignorexps = compile_regex_list('coverage_ignore_classes',
- self.config.coverage_ignore_classes,
- self.warn)
- self.fun_ignorexps = compile_regex_list('coverage_ignore_functions',
- self.config.coverage_ignore_functions,
- self.warn)
+ self.c_ignorexps[name] = compile_regex_list(
+ 'coverage_ignore_c_items', exps, self.warn)
+ self.mod_ignorexps = compile_regex_list(
+ 'coverage_ignore_modules', self.config.coverage_ignore_modules,
+ self.warn)
+ self.cls_ignorexps = compile_regex_list(
+ 'coverage_ignore_classes', self.config.coverage_ignore_classes,
+ self.warn)
+ self.fun_ignorexps = compile_regex_list(
+ 'coverage_ignore_functions', self.config.coverage_ignore_functions,
+ self.warn)
def get_outdated_docs(self):
return 'coverage overview'
@@ -128,7 +128,8 @@ class CoverageBuilder(Builder):
try:
mod = __import__(mod_name, fromlist=['foo'])
except ImportError, err:
- self.warn('module %s could not be imported: %s' % (mod_name, err))
+ self.warn('module %s could not be imported: %s' %
+ (mod_name, err))
self.py_undoc[mod_name] = {'error': err}
continue
@@ -168,7 +169,8 @@ class CoverageBuilder(Builder):
attrs = []
- for attr_name, attr in inspect.getmembers(obj, inspect.ismethod):
+ for attr_name, attr in inspect.getmembers(
+ obj, inspect.ismethod):
if attr_name[0] == '_':
# starts with an underscore, ignore it
continue
diff --git a/sphinx/ext/doctest.py b/sphinx/ext/doctest.py
index badd50ffd..51463661a 100644
--- a/sphinx/ext/doctest.py
+++ b/sphinx/ext/doctest.py
@@ -6,13 +6,14 @@
Mimic doctest by automatically executing code snippets and checking
their results.
- :copyright: 2008 by Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
import re
import sys
import time
+import codecs
import StringIO
from os import path
# circumvent relative import
@@ -21,7 +22,8 @@ doctest = __import__('doctest')
from docutils import nodes
from docutils.parsers.rst import directives
-from sphinx.builder import Builder
+from sphinx.builders import Builder
+from sphinx.util.compat import Directive
from sphinx.util.console import bold
blankline_re = re.compile(r'^\s*', re.MULTILINE)
@@ -29,63 +31,77 @@ doctestopt_re = re.compile(r'#\s*doctest:.+$', re.MULTILINE)
# set up the necessary directives
-def test_directive(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- # use ordinary docutils nodes for test code: they get special attributes
- # so that our builder recognizes them, and the other builders are happy.
- code = '\n'.join(content)
- test = None
- if name == 'doctest':
- if '' in code:
- # convert s to ordinary blank lines for presentation
- test = code
- code = blankline_re.sub('', code)
- if doctestopt_re.search(code):
- if not test:
+class TestDirective(Directive):
+ """
+ Base class for doctest-related directives.
+ """
+
+ has_content = True
+ required_arguments = 0
+ optional_arguments = 1
+ final_argument_whitespace = True
+
+ def run(self):
+ # use ordinary docutils nodes for test code: they get special attributes
+ # so that our builder recognizes them, and the other builders are happy.
+ code = '\n'.join(self.content)
+ test = None
+ if self.name == 'doctest':
+ if '' in code:
+ # convert s to ordinary blank lines for presentation
test = code
- code = doctestopt_re.sub('', code)
- nodetype = nodes.literal_block
- if name == 'testsetup' or 'hide' in options:
- nodetype = nodes.comment
- if arguments:
- groups = [x.strip() for x in arguments[0].split(',')]
- else:
- groups = ['default']
- node = nodetype(code, code, testnodetype=name, groups=groups)
- node.line = lineno
- if test is not None:
- # only save if it differs from code
- node['test'] = test
- if name == 'testoutput':
- # don't try to highlight output
- node['language'] = 'none'
- node['options'] = {}
- if name in ('doctest', 'testoutput') and 'options' in options:
- # parse doctest-like output comparison flags
- option_strings = options['options'].replace(',', ' ').split()
- for option in option_strings:
- if (option[0] not in '+-' or option[1:] not in
- doctest.OPTIONFLAGS_BY_NAME):
- # XXX warn?
- continue
- flag = doctest.OPTIONFLAGS_BY_NAME[option[1:]]
- node['options'][flag] = (option[0] == '+')
- return [node]
+ code = blankline_re.sub('', code)
+ if doctestopt_re.search(code):
+ if not test:
+ test = code
+ code = doctestopt_re.sub('', code)
+ nodetype = nodes.literal_block
+ if self.name == 'testsetup' or 'hide' in self.options:
+ nodetype = nodes.comment
+ if self.arguments:
+ groups = [x.strip() for x in self.arguments[0].split(',')]
+ else:
+ groups = ['default']
+ node = nodetype(code, code, testnodetype=self.name, groups=groups)
+ node.line = self.lineno
+ if test is not None:
+ # only save if it differs from code
+ node['test'] = test
+ if self.name == 'testoutput':
+ # don't try to highlight output
+ node['language'] = 'none'
+ node['options'] = {}
+ if self.name in ('doctest', 'testoutput') and 'options' in self.options:
+ # parse doctest-like output comparison flags
+ option_strings = self.options['options'].replace(',', ' ').split()
+ for option in option_strings:
+ if (option[0] not in '+-' or option[1:] not in
+ doctest.OPTIONFLAGS_BY_NAME):
+ # XXX warn?
+ continue
+ flag = doctest.OPTIONFLAGS_BY_NAME[option[1:]]
+ node['options'][flag] = (option[0] == '+')
+ return [node]
-# need to have individual functions for each directive due to different
-# options they accept
+class TestsetupDirective(TestDirective):
+ option_spec = {}
-def testsetup_directive(*args):
- return test_directive(*args)
+class DoctestDirective(TestDirective):
+ option_spec = {
+ 'hide': directives.flag,
+ 'options': directives.unchanged,
+ }
-def doctest_directive(*args):
- return test_directive(*args)
+class TestcodeDirective(TestDirective):
+ option_spec = {
+ 'hide': directives.flag,
+ }
-def testcode_directive(*args):
- return test_directive(*args)
-
-def testoutput_directive(*args):
- return test_directive(*args)
+class TestoutputDirective(TestDirective):
+ option_spec = {
+ 'hide': directives.flag,
+ 'options': directives.unchanged,
+ }
parser = doctest.DocTestParser()
@@ -98,9 +114,12 @@ class TestGroup(object):
self.setup = []
self.tests = []
- def add_code(self, code):
+ def add_code(self, code, prepend=False):
if code.type == 'testsetup':
- self.setup.append(code)
+ if prepend:
+ self.setup.insert(0, code)
+ else:
+ self.setup.append(code)
elif code.type == 'doctest':
self.tests.append([code])
elif code.type == 'testcode':
@@ -169,7 +188,8 @@ class DocTestBuilder(Builder):
date = time.strftime('%Y-%m-%d %H:%M:%S')
- self.outfile = file(path.join(self.outdir, 'output.txt'), 'w')
+ self.outfile = codecs.open(path.join(self.outdir, 'output.txt'),
+ 'w', encoding='utf-8')
self.outfile.write('''\
Results of doctest builder run on %s
==================================%s
@@ -179,6 +199,12 @@ Results of doctest builder run on %s
self.info(text, nonl=True)
self.outfile.write(text)
+ def _warn_out(self, text):
+ self.info(text, nonl=True)
+ if self.app.quiet:
+ self.warn(text)
+ self.outfile.write(text)
+
def get_target_uri(self, docname, typ=None):
return ''
@@ -200,6 +226,9 @@ Doctest summary
self.setup_failures, s(self.setup_failures)))
self.outfile.close()
+ if self.total_failures or self.setup_failures:
+ self.app.statuscode = 1
+
sys.path[0:0] = self.config.doctest_path
def write(self, build_docnames, updated_docnames, method='update'):
@@ -229,8 +258,12 @@ Doctest summary
return isinstance(node, (nodes.literal_block, nodes.comment)) \
and node.has_key('testnodetype')
for node in doctree.traverse(condition):
- code = TestCode(node.has_key('test') and node['test'] or node.astext(),
- type=node.get('testnodetype', 'doctest'),
+ source = node.has_key('test') and node['test'] or node.astext()
+ if not source:
+ self.warn('no code/output in %s block at %s:%s' %
+ (node.get('testnodetype', 'doctest'),
+ self.env.doc2path(docname), node.line))
+ code = TestCode(source, type=node.get('testnodetype', 'doctest'),
lineno=node.line, options=node.get('options'))
node_groups = node.get('groups', ['default'])
if '*' in node_groups:
@@ -243,10 +276,16 @@ Doctest summary
for code in add_to_all_groups:
for group in groups.itervalues():
group.add_code(code)
+ if self.config.doctest_global_setup:
+ code = TestCode(self.config.doctest_global_setup,
+ 'testsetup', lineno=0)
+ for group in groups.itervalues():
+ group.add_code(code, prepend=True)
if not groups:
return
- self._out('\nDocument: %s\n----------%s\n' % (docname, '-'*len(docname)))
+ self._out('\nDocument: %s\n----------%s\n' %
+ (docname, '-'*len(docname)))
for group in groups.itervalues():
self.test_group(group, self.env.doc2path(docname, base=None))
# Separately count results from setup code
@@ -265,7 +304,8 @@ Doctest summary
ns = {}
examples = []
for setup in group.setup:
- examples.append(doctest.Example(setup.code, '', lineno=setup.lineno))
+ examples.append(doctest.Example(setup.code, '',
+ lineno=setup.lineno))
if examples:
# simulate a doctest with the setup code
setup_doctest = doctest.DocTest(examples, {},
@@ -274,7 +314,7 @@ Doctest summary
setup_doctest.globs = ns
old_f = self.setup_runner.failures
self.type = 'exec' # the snippet may contain multiple statements
- self.setup_runner.run(setup_doctest, out=self._out,
+ self.setup_runner.run(setup_doctest, out=self._warn_out,
clear_globs=False)
if self.setup_runner.failures > old_f:
# don't run the group
@@ -284,8 +324,6 @@ Doctest summary
test = parser.get_doctest(code[0].code, {},
group.name, filename, code[0].lineno)
if not test.examples:
- self._out('WARNING: no examples in doctest block at '
- + filename + ', line %s\n' % code[0].lineno)
continue
for example in test.examples:
# apply directive's comparison options
@@ -307,18 +345,16 @@ Doctest summary
# DocTest.__init__ copies the globs namespace, which we don't want
test.globs = ns
# also don't clear the globs namespace after running the doctest
- self.test_runner.run(test, out=self._out, clear_globs=False)
+ self.test_runner.run(test, out=self._warn_out, clear_globs=False)
def setup(app):
- app.add_directive('testsetup', testsetup_directive, 1, (0, 1, 1))
- app.add_directive('doctest', doctest_directive, 1, (0, 1, 1),
- hide=directives.flag, options=directives.unchanged)
- app.add_directive('testcode', testcode_directive, 1, (0, 1, 1),
- hide=directives.flag)
- app.add_directive('testoutput', testoutput_directive, 1, (0, 1, 1),
- hide=directives.flag, options=directives.unchanged)
+ app.add_directive('testsetup', TestsetupDirective)
+ app.add_directive('doctest', DoctestDirective)
+ app.add_directive('testcode', TestcodeDirective)
+ app.add_directive('testoutput', TestoutputDirective)
app.add_builder(DocTestBuilder)
# this config value adds to sys.path
app.add_config_value('doctest_path', [], False)
app.add_config_value('doctest_test_doctest_blocks', 'default', False)
+ app.add_config_value('doctest_global_setup', '', False)
diff --git a/sphinx/ext/graphviz.py b/sphinx/ext/graphviz.py
new file mode 100644
index 000000000..d51e2ed5f
--- /dev/null
+++ b/sphinx/ext/graphviz.py
@@ -0,0 +1,185 @@
+# -*- coding: utf-8 -*-
+"""
+ sphinx.ext.graphviz
+ ~~~~~~~~~~~~~~~~~~~
+
+ Allow graphviz-formatted graphs to be included in Sphinx-generated
+ documents inline.
+
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+import os
+import re
+import sys
+import posixpath
+from os import path
+from subprocess import Popen, PIPE
+try:
+ from hashlib import sha1 as sha
+except ImportError:
+ from sha import sha
+
+from docutils import nodes
+
+from sphinx.errors import SphinxError
+from sphinx.util import ensuredir
+from sphinx.util.compat import Directive
+
+
+mapname_re = re.compile(r')
+ self.body.append('\n' %
+ (fname, self.encode(code).strip(), imgcss))
+ else:
+ # has a map: get the name of the map and connect the parts
+ mapname = mapname_re.match(imgmap[0]).group(1)
+ self.body.append('\n' %
+ (fname, self.encode(code).strip(),
+ mapname, imgcss))
+ self.body.extend(imgmap)
+ self.body.append('
\n')
+ raise nodes.SkipNode
+
+
+def html_visit_graphviz(self, node):
+ render_dot_html(self, node, node['code'], node['options'])
+
+
+def render_dot_latex(self, node, code, options, prefix='graphviz'):
+ try:
+ fname = render_dot(self, code, options, 'pdf', prefix)
+ except GraphvizError, exc:
+ self.builder.warn('dot code %r: ' % code + str(exc))
+ raise nodes.SkipNode
+
+ if fname is not None:
+ self.body.append('\\includegraphics{%s}' % fname)
+ raise nodes.SkipNode
+
+
+def latex_visit_graphviz(self, node):
+ render_dot_latex(self, node, node['code'], node['options'])
+
+def setup(app):
+ app.add_node(graphviz,
+ html=(html_visit_graphviz, None),
+ latex=(latex_visit_graphviz, None))
+ app.add_directive('graphviz', Graphviz)
+ app.add_directive('graph', GraphvizSimple)
+ app.add_directive('digraph', GraphvizSimple)
+ app.add_config_value('graphviz_dot', 'dot', 'html')
+ app.add_config_value('graphviz_dot_args', [], 'html')
diff --git a/sphinx/ext/ifconfig.py b/sphinx/ext/ifconfig.py
index 204178f2f..90cd2b2c4 100644
--- a/sphinx/ext/ifconfig.py
+++ b/sphinx/ext/ifconfig.py
@@ -13,26 +13,36 @@
This stuff is only included in the built docs for unstable versions.
The argument for ``ifconfig`` is a plain Python expression, evaluated in the
- namespace of the project configuration (that is, all variables from ``conf.py``
- are available.)
+ namespace of the project configuration (that is, all variables from
+ ``conf.py`` are available.)
- :copyright: 2008 by Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
from docutils import nodes
+from sphinx.util.compat import Directive
+
class ifconfig(nodes.Element): pass
-def ifconfig_directive(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- node = ifconfig()
- node.line = lineno
- node['expr'] = arguments[0]
- state.nested_parse(content, content_offset, node)
- return [node]
+class IfConfig(Directive):
+
+ has_content = True
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = True
+ option_spec = {}
+
+ def run(self):
+ node = ifconfig()
+ node.document = self.state.document
+ node.line = self.lineno
+ node['expr'] = self.arguments[0]
+ self.state.nested_parse(self.content, self.content_offset, node)
+ return [node]
def process_ifconfig_nodes(app, doctree, docname):
@@ -58,5 +68,5 @@ def process_ifconfig_nodes(app, doctree, docname):
def setup(app):
app.add_node(ifconfig)
- app.add_directive('ifconfig', ifconfig_directive, 1, (1, 0, 1))
+ app.add_directive('ifconfig', IfConfig)
app.connect('doctree-resolved', process_ifconfig_nodes)
diff --git a/sphinx/ext/inheritance_diagram.py b/sphinx/ext/inheritance_diagram.py
new file mode 100644
index 000000000..8183359d4
--- /dev/null
+++ b/sphinx/ext/inheritance_diagram.py
@@ -0,0 +1,367 @@
+"""
+ sphinx.ext.inheritance_diagram
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+ Defines a docutils directive for inserting inheritance diagrams.
+
+ Provide the directive with one or more classes or modules (separated
+ by whitespace). For modules, all of the classes in that module will
+ be used.
+
+ Example::
+
+ Given the following classes:
+
+ class A: pass
+ class B(A): pass
+ class C(A): pass
+ class D(B, C): pass
+ class E(B): pass
+
+ .. inheritance-diagram: D E
+
+ Produces a graph like the following:
+
+ A
+ / \
+ B C
+ / \ /
+ E D
+
+ The graph is inserted as a PNG+image map into HTML and a PDF in
+ LaTeX.
+
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+import os
+import re
+import sys
+import inspect
+import subprocess
+try:
+ from hashlib import md5
+except ImportError:
+ from md5 import md5
+
+from docutils import nodes
+from docutils.parsers.rst import directives
+
+from sphinx.roles import xfileref_role
+from sphinx.ext.graphviz import render_dot_html, render_dot_latex
+from sphinx.util.compat import Directive
+
+
+class_sig_re = re.compile(r'''^([\w.]*\.)? # module names
+ (\w+) \s* $ # class/final module name
+ ''', re.VERBOSE)
+
+
+class InheritanceException(Exception):
+ pass
+
+
+class InheritanceGraph(object):
+ """
+ Given a list of classes, determines the set of classes that they inherit
+ from all the way to the root "object", and then is able to generate a
+ graphviz dot graph from them.
+ """
+ def __init__(self, class_names, currmodule, show_builtins=False):
+ """
+ *class_names* is a list of child classes to show bases from.
+
+ If *show_builtins* is True, then Python builtins will be shown
+ in the graph.
+ """
+ self.class_names = class_names
+ self.classes = self._import_classes(class_names, currmodule)
+ self.all_classes = self._all_classes(self.classes)
+ if len(self.all_classes) == 0:
+ raise InheritanceException('No classes found for '
+ 'inheritance diagram')
+ self.show_builtins = show_builtins
+
+ def _import_class_or_module(self, name, currmodule):
+ """
+ Import a class using its fully-qualified *name*.
+ """
+ try:
+ path, base = class_sig_re.match(name).groups()
+ except ValueError:
+ raise InheritanceException('Invalid class or module %r specified '
+ 'for inheritance diagram' % name)
+
+ fullname = (path or '') + base
+ path = (path and path.rstrip('.') or '')
+
+ # two possibilities: either it is a module, then import it
+ try:
+ module = __import__(fullname)
+ todoc = sys.modules[fullname]
+ except ImportError:
+ # else it is a class, then import the module
+ if not path:
+ if currmodule:
+ # try the current module
+ path = currmodule
+ else:
+ raise InheritanceException(
+ 'Could not import class %r specified for '
+ 'inheritance diagram' % base)
+ try:
+ module = __import__(path)
+ todoc = getattr(sys.modules[path], base)
+ except (ImportError, AttributeError):
+ raise InheritanceException(
+ 'Could not import class or module %r specified for '
+ 'inheritance diagram' % (path + '.' + base))
+
+ # If a class, just return it
+ if inspect.isclass(todoc):
+ return [todoc]
+ elif inspect.ismodule(todoc):
+ classes = []
+ for cls in todoc.__dict__.values():
+ if inspect.isclass(cls) and cls.__module__ == todoc.__name__:
+ classes.append(cls)
+ return classes
+ raise InheritanceException('%r specified for inheritance diagram is '
+ 'not a class or module' % name)
+
+ def _import_classes(self, class_names, currmodule):
+ """
+ Import a list of classes.
+ """
+ classes = []
+ for name in class_names:
+ classes.extend(self._import_class_or_module(name, currmodule))
+ return classes
+
+ def _all_classes(self, classes):
+ """
+ Return a list of all classes that are ancestors of *classes*.
+ """
+ all_classes = {}
+
+ def recurse(cls):
+ all_classes[cls] = None
+ for c in cls.__bases__:
+ if c not in all_classes:
+ recurse(c)
+
+ for cls in classes:
+ recurse(cls)
+
+ return all_classes.keys()
+
+ def class_name(self, cls, parts=0):
+ """
+ Given a class object, return a fully-qualified name. This
+ works for things I've tested in matplotlib so far, but may not
+ be completely general.
+ """
+ module = cls.__module__
+ if module == '__builtin__':
+ fullname = cls.__name__
+ else:
+ fullname = '%s.%s' % (module, cls.__name__)
+ if parts == 0:
+ return fullname
+ name_parts = fullname.split('.')
+ return '.'.join(name_parts[-parts:])
+
+ def get_all_class_names(self):
+ """
+ Get all of the class names involved in the graph.
+ """
+ return [self.class_name(x) for x in self.all_classes]
+
+ # These are the default attrs for graphviz
+ default_graph_attrs = {
+ 'rankdir': 'LR',
+ 'size': '"8.0, 12.0"',
+ }
+ default_node_attrs = {
+ 'shape': 'box',
+ 'fontsize': 10,
+ 'height': 0.25,
+ 'fontname': 'Vera Sans, DejaVu Sans, Liberation Sans, '
+ 'Arial, Helvetica, sans',
+ 'style': '"setlinewidth(0.5)"',
+ }
+ default_edge_attrs = {
+ 'arrowsize': 0.5,
+ 'style': '"setlinewidth(0.5)"',
+ }
+
+ def _format_node_attrs(self, attrs):
+ return ','.join(['%s=%s' % x for x in attrs.items()])
+
+ def _format_graph_attrs(self, attrs):
+ return ''.join(['%s=%s;\n' % x for x in attrs.items()])
+
+ def generate_dot(self, name, parts=0, urls={}, env=None,
+ graph_attrs={}, node_attrs={}, edge_attrs={}):
+ """
+ Generate a graphviz dot graph from the classes that
+ were passed in to __init__.
+
+ *name* is the name of the graph.
+
+ *urls* is a dictionary mapping class names to HTTP URLs.
+
+ *graph_attrs*, *node_attrs*, *edge_attrs* are dictionaries containing
+ key/value pairs to pass on as graphviz properties.
+ """
+ g_attrs = self.default_graph_attrs.copy()
+ n_attrs = self.default_node_attrs.copy()
+ e_attrs = self.default_edge_attrs.copy()
+ g_attrs.update(graph_attrs)
+ n_attrs.update(node_attrs)
+ e_attrs.update(edge_attrs)
+ if env:
+ g_attrs.update(env.config.inheritance_graph_attrs)
+ n_attrs.update(env.config.inheritance_node_attrs)
+ e_attrs.update(env.config.inheritance_edge_attrs)
+
+ res = []
+ res.append('digraph %s {\n' % name)
+ res.append(self._format_graph_attrs(g_attrs))
+
+ for cls in self.all_classes:
+ if not self.show_builtins and cls in __builtins__.values():
+ continue
+
+ name = self.class_name(cls, parts)
+
+ # Write the node
+ this_node_attrs = n_attrs.copy()
+ url = urls.get(self.class_name(cls))
+ if url is not None:
+ this_node_attrs['URL'] = '"%s"' % url
+ res.append(' "%s" [%s];\n' %
+ (name, self._format_node_attrs(this_node_attrs)))
+
+ # Write the edges
+ for base in cls.__bases__:
+ if not self.show_builtins and base in __builtins__.values():
+ continue
+
+ base_name = self.class_name(base, parts)
+ res.append(' "%s" -> "%s" [%s];\n' %
+ (base_name, name,
+ self._format_node_attrs(e_attrs)))
+ res.append('}\n')
+ return ''.join(res)
+
+
+class inheritance_diagram(nodes.General, nodes.Element):
+ """
+ A docutils node to use as a placeholder for the inheritance diagram.
+ """
+ pass
+
+
+class InheritanceDiagram(Directive):
+ """
+ Run when the inheritance_diagram directive is first encountered.
+ """
+ has_content = False
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = True
+ option_spec = {
+ 'parts': directives.nonnegative_int,
+ }
+
+ def run(self):
+ node = inheritance_diagram()
+ node.document = self.state.document
+ env = self.state.document.settings.env
+ class_names = self.arguments[0].split()
+
+ # Create a graph starting with the list of classes
+ try:
+ graph = InheritanceGraph(class_names, env.currmodule)
+ except InheritanceException, err:
+ return [node.document.reporter.warning(err.args[0],
+ line=self.lineno)]
+
+ # Create xref nodes for each target of the graph's image map and
+ # add them to the doc tree so that Sphinx can resolve the
+ # references to real URLs later. These nodes will eventually be
+ # removed from the doctree after we're done with them.
+ for name in graph.get_all_class_names():
+ refnodes, x = xfileref_role(
+ 'class', ':class:`%s`' % name, name, 0, self.state)
+ node.extend(refnodes)
+ # Store the graph object so we can use it to generate the
+ # dot file later
+ node['graph'] = graph
+ # Store the original content for use as a hash
+ node['parts'] = self.options.get('parts', 0)
+ node['content'] = ' '.join(class_names)
+ return [node]
+
+
+def get_graph_hash(node):
+ return md5(node['content'] + str(node['parts'])).hexdigest()[-10:]
+
+
+def html_visit_inheritance_diagram(self, node):
+ """
+ Output the graph for HTML. This will insert a PNG with clickable
+ image map.
+ """
+ graph = node['graph']
+ parts = node['parts']
+
+ graph_hash = get_graph_hash(node)
+ name = 'inheritance%s' % graph_hash
+
+ # Create a mapping from fully-qualified class names to URLs.
+ urls = {}
+ for child in node:
+ if child.get('refuri') is not None:
+ urls[child['reftitle']] = child.get('refuri')
+ elif child.get('refid') is not None:
+ urls[child['reftitle']] = '#' + child.get('refid')
+
+ dotcode = graph.generate_dot(name, parts, urls, env=self.builder.env)
+ render_dot_html(self, node, dotcode, [], 'inheritance', 'inheritance')
+ raise nodes.SkipNode
+
+
+def latex_visit_inheritance_diagram(self, node):
+ """
+ Output the graph for LaTeX. This will insert a PDF.
+ """
+ graph = node['graph']
+ parts = node['parts']
+
+ graph_hash = get_graph_hash(node)
+ name = 'inheritance%s' % graph_hash
+
+ dotcode = graph.generate_dot(name, parts, urls, env=self.builder.env,
+ graph_attrs={'size': '"6.0,6.0"'})
+ render_dot_latex(self, node, dotcode, [], 'inheritance')
+ raise nodes.SkipNode
+
+
+def skip(self, node):
+ raise nodes.SkipNode
+
+
+def setup(app):
+ app.setup_extension('sphinx.ext.graphviz')
+ app.add_node(
+ inheritance_diagram,
+ latex=(latex_visit_inheritance_diagram, None),
+ html=(html_visit_inheritance_diagram, None),
+ text=(skip, None))
+ app.add_directive('inheritance-diagram', InheritanceDiagram)
+ app.add_config_value('inheritance_graph_attrs', {}, False),
+ app.add_config_value('inheritance_node_attrs', {}, False),
+ app.add_config_value('inheritance_edge_attrs', {}, False),
diff --git a/sphinx/ext/intersphinx.py b/sphinx/ext/intersphinx.py
index 0c034e0d5..5340a55ec 100644
--- a/sphinx/ext/intersphinx.py
+++ b/sphinx/ext/intersphinx.py
@@ -20,8 +20,8 @@
also be specified individually, e.g. if the docs should be buildable
without Internet access.
- :copyright: 2008 by Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
import time
@@ -31,7 +31,7 @@ from os import path
from docutils import nodes
-from sphinx.builder import INVENTORY_FILENAME
+from sphinx.builders.html import INVENTORY_FILENAME
def fetch_inventory(app, uri, inv):
@@ -125,7 +125,7 @@ def missing_reference(app, env, node, contnode):
if target not in env.intersphinx_inventory:
return None
type, proj, version, uri = env.intersphinx_inventory[target]
- print "Intersphinx hit:", target, uri
+ # print "Intersphinx hit:", target, uri
newnode = nodes.reference('', '')
newnode['refuri'] = uri + '#' + target
newnode['reftitle'] = '(in %s v%s)' % (proj, version)
diff --git a/sphinx/ext/jsmath.py b/sphinx/ext/jsmath.py
index bd2579de5..e51af4550 100644
--- a/sphinx/ext/jsmath.py
+++ b/sphinx/ext/jsmath.py
@@ -6,8 +6,8 @@
Set up everything for use of JSMath to display math in HTML
via JavaScript.
- :copyright: 2008 by Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
from docutils import nodes
@@ -32,7 +32,8 @@ def html_visit_displaymath(self, node):
if i == 0:
# necessary to e.g. set the id property correctly
if node['number']:
- self.body.append('(%s)' % node['number'])
+ self.body.append('(%s)' %
+ node['number'])
self.body.append(self.starttag(node, 'div', CLASS='math'))
else:
# but only once!
diff --git a/sphinx/ext/mathbase.py b/sphinx/ext/mathbase.py
index fc002c604..fea786e3e 100644
--- a/sphinx/ext/mathbase.py
+++ b/sphinx/ext/mathbase.py
@@ -5,13 +5,15 @@
Set up math support in source files and LaTeX/text output.
- :copyright: 2008 by Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
from docutils import nodes, utils
from docutils.parsers.rst import directives
+from sphinx.util.compat import Directive
+
class math(nodes.Inline, nodes.TextElement):
pass
@@ -45,22 +47,33 @@ def eq_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
node['docname'] = inliner.document.settings.env.docname
return [node], []
-def math_directive(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- latex = '\n'.join(content)
- if arguments and arguments[0]:
- latex = arguments[0] + '\n\n' + latex
- node = displaymath()
- node['latex'] = latex
- node['label'] = options.get('label', None)
- node['nowrap'] = 'nowrap' in options
- node['docname'] = state.document.settings.env.docname
- ret = [node]
- if node['label']:
- tnode = nodes.target('', '', ids=['equation-' + node['label']])
- state.document.note_explicit_target(tnode)
- ret.insert(0, tnode)
- return ret
+
+class MathDirective(Directive):
+
+ has_content = True
+ required_arguments = 0
+ optional_arguments = 1
+ final_argument_whitespace = True
+ option_spec = {
+ 'label': directives.unchanged,
+ 'nowrap': directives.flag,
+ }
+
+ def run(self):
+ latex = '\n'.join(self.content)
+ if self.arguments and self.arguments[0]:
+ latex = self.arguments[0] + '\n\n' + latex
+ node = displaymath()
+ node['latex'] = latex
+ node['label'] = self.options.get('label', None)
+ node['nowrap'] = 'nowrap' in self.options
+ node['docname'] = self.state.document.settings.env.docname
+ ret = [node]
+ if node['label']:
+ tnode = nodes.target('', '', ids=['equation-' + node['label']])
+ self.state.document.note_explicit_target(tnode)
+ ret.insert(0, tnode)
+ return ret
def latex_visit_math(self, node):
@@ -134,6 +147,5 @@ def setup(app, htmlinlinevisitors, htmldisplayvisitors):
html=(html_visit_eqref, html_depart_eqref))
app.add_role('math', math_role)
app.add_role('eq', eq_role)
- app.add_directive('math', math_directive, 1, (0, 1, 1),
- label=directives.unchanged, nowrap=directives.flag)
+ app.add_directive('math', MathDirective)
app.connect('doctree-resolved', number_equations)
diff --git a/sphinx/ext/pngmath.py b/sphinx/ext/pngmath.py
index 9f628e110..745bdba41 100644
--- a/sphinx/ext/pngmath.py
+++ b/sphinx/ext/pngmath.py
@@ -5,16 +5,15 @@
Render math in HTML via dvipng.
- :copyright: 2008 by Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
import re
-import shlex
import shutil
import tempfile
import posixpath
-from os import path
+from os import path, getcwd, chdir
from subprocess import Popen, PIPE
try:
from hashlib import sha1 as sha
@@ -23,8 +22,9 @@ except ImportError:
from docutils import nodes
+from sphinx.errors import SphinxError
from sphinx.util import ensuredir
-from sphinx.application import SphinxError
+from sphinx.util.png import read_png_depth, write_png_depth
from sphinx.ext.mathbase import setup as mathbase_setup, wrap_displaymath
class MathExtError(SphinxError):
@@ -75,9 +75,17 @@ def render_math(self, math):
"""
use_preview = self.builder.config.pngmath_use_preview
- shasum = "%s.png" % sha(math).hexdigest()
+ shasum = "%s.png" % sha(math.encode('utf-8')).hexdigest()
relfn = posixpath.join(self.builder.imgpath, 'math', shasum)
outfn = path.join(self.builder.outdir, '_images', 'math', shasum)
+ if path.isfile(outfn):
+ depth = read_png_depth(outfn)
+ return relfn, depth
+
+ # if latex or dvipng has failed once, don't bother to try again
+ if hasattr(self.builder, '_mathpng_warned_latex') or \
+ hasattr(self.builder, '_mathpng_warned_dvipng'):
+ return None, None
latex = DOC_HEAD + self.builder.config.pngmath_latex_preamble
latex += (use_preview and DOC_BODY_PREVIEW or DOC_BODY) % math
@@ -96,28 +104,39 @@ def render_math(self, math):
tf.write(latex)
tf.close()
- ltx_args = shlex.split(self.builder.config.pngmath_latex)
- ltx_args += ['--interaction=nonstopmode', '--output-directory=' + tempdir,
- 'math.tex']
+ # build latex command; old versions of latex don't have the
+ # --output-directory option, so we have to manually chdir to the
+ # temp dir to run it.
+ ltx_args = [self.builder.config.pngmath_latex, '--interaction=nonstopmode']
+ # add custom args from the config file
+ ltx_args.extend(self.builder.config.pngmath_latex_args)
+ ltx_args.append('math.tex')
+
+ curdir = getcwd()
+ chdir(tempdir)
+
try:
- p = Popen(ltx_args, stdout=PIPE, stderr=PIPE)
- except OSError, err:
- if err.errno != 2: # No such file or directory
- raise
- if not hasattr(self.builder, '_mathpng_warned_latex'):
+ try:
+ p = Popen(ltx_args, stdout=PIPE, stderr=PIPE)
+ except OSError, err:
+ if err.errno != 2: # No such file or directory
+ raise
self.builder.warn('LaTeX command %r cannot be run (needed for math '
'display), check the pngmath_latex setting' %
self.builder.config.pngmath_latex)
self.builder._mathpng_warned_latex = True
- return relfn, None
+ return None, None
+ finally:
+ chdir(curdir)
+
stdout, stderr = p.communicate()
if p.returncode != 0:
- raise MathExtError('latex exited with error:\n[stderr]\n%s\n[stdout]\n%s'
- % (stderr, stdout))
+ raise MathExtError('latex exited with error:\n[stderr]\n%s\n'
+ '[stdout]\n%s' % (stderr, stdout))
ensuredir(path.dirname(outfn))
# use some standard dvipng arguments
- dvipng_args = shlex.split(self.builder.config.pngmath_dvipng)
+ dvipng_args = [self.builder.config.pngmath_dvipng]
dvipng_args += ['-o', outfn, '-T', 'tight', '-z9']
# add custom ones from config value
dvipng_args.extend(self.builder.config.pngmath_dvipng_args)
@@ -130,22 +149,22 @@ def render_math(self, math):
except OSError, err:
if err.errno != 2: # No such file or directory
raise
- if not hasattr(self.builder, '_mathpng_warned_dvipng'):
- self.builder.warn('dvipng command %r cannot be run (needed for math '
- 'display), check the pngmath_dvipng setting' %
- self.builder.config.pngmath_dvipng)
- self.builder._mathpng_warned_dvipng = True
- return relfn, None
+ self.builder.warn('dvipng command %r cannot be run (needed for math '
+ 'display), check the pngmath_dvipng setting' %
+ self.builder.config.pngmath_dvipng)
+ self.builder._mathpng_warned_dvipng = True
+ return None, None
stdout, stderr = p.communicate()
if p.returncode != 0:
- raise MathExtError('dvipng exited with error:\n[stderr]\n%s\n[stdout]\n%s'
- % (stderr, stdout))
+ raise MathExtError('dvipng exited with error:\n[stderr]\n%s\n'
+ '[stdout]\n%s' % (stderr, stdout))
depth = None
if use_preview:
for line in stdout.splitlines():
m = depth_re.match(line)
if m:
depth = int(m.group(1))
+ write_png_depth(outfn, depth)
break
return relfn, depth
@@ -161,10 +180,28 @@ def cleanup_tempdir(app, exc):
pass
def html_visit_math(self, node):
- fname, depth = render_math(self, '$'+node['latex']+'$')
- self.body.append('' %
- (fname, self.encode(node['latex']),
- depth and 'style="vertical-align: %dpx" ' % (-depth) or ''))
+ try:
+ fname, depth = render_math(self, '$'+node['latex']+'$')
+ except MathExtError, exc:
+ sm = nodes.system_message(str(exc), type='WARNING', level=2,
+ backrefs=[], source=node['latex'])
+ sm.walkabout(self)
+ self.builder.warn('display latex %r: ' % node['latex'] + str(exc))
+ raise nodes.SkipNode
+ if fname is None:
+ # something failed -- use text-only as a bad substitute
+ self.body.append('%s' %
+ self.encode(node['latex']).strip())
+ else:
+ if depth is None:
+ self.body.append(
+ '' %
+ (fname, self.encode(node['latex']).strip()))
+ else:
+ self.body.append(
+ '' %
+ (fname, self.encode(node['latex']).strip(), -depth))
raise nodes.SkipNode
def html_visit_displaymath(self, node):
@@ -172,13 +209,25 @@ def html_visit_displaymath(self, node):
latex = node['latex']
else:
latex = wrap_displaymath(node['latex'], None)
- fname, depth = render_math(self, latex)
+ try:
+ fname, depth = render_math(self, latex)
+ except MathExtError, exc:
+ sm = nodes.system_message(str(exc), type='WARNING', level=2,
+ backrefs=[], source=node['latex'])
+ sm.walkabout(self)
+ self.builder.warn('inline latex %r: ' % node['latex'] + str(exc))
+ raise nodes.SkipNode
self.body.append(self.starttag(node, 'div', CLASS='math'))
self.body.append('
')
if node['number']:
self.body.append('(%s)' % node['number'])
- self.body.append('\n' %
- (fname, self.encode(node['latex'])))
+ if fname is None:
+ # something failed -- use text-only as a bad substitute
+ self.body.append('%s' %
+ self.encode(node['latex']).strip())
+ else:
+ self.body.append('\n' %
+ (fname, self.encode(node['latex']).strip()))
self.body.append('
')
raise nodes.SkipNode
@@ -188,6 +237,7 @@ def setup(app):
app.add_config_value('pngmath_dvipng', 'dvipng', False)
app.add_config_value('pngmath_latex', 'latex', False)
app.add_config_value('pngmath_use_preview', False, False)
- app.add_config_value('pngmath_dvipng_args', [], False)
+ app.add_config_value('pngmath_dvipng_args', ['-gamma 1.5', '-D 110'], False)
+ app.add_config_value('pngmath_latex_args', [], False)
app.add_config_value('pngmath_latex_preamble', '', False)
app.connect('build-finished', cleanup_tempdir)
diff --git a/sphinx/ext/refcounting.py b/sphinx/ext/refcounting.py
index c6e5a76fc..cad9d7f1a 100644
--- a/sphinx/ext/refcounting.py
+++ b/sphinx/ext/refcounting.py
@@ -9,8 +9,8 @@
Usage: Set the `refcount_file` config value to the path to the reference
count data file.
- :copyright: 2008 by Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
from os import path
@@ -55,7 +55,8 @@ class Refcounts(dict):
refcount = None
else:
refcount = int(refcount)
- # Update the entry with the new parameter or the result information.
+ # Update the entry with the new parameter or the result
+ # information.
if arg:
entry.args.append((arg, type, refcount))
else:
@@ -81,7 +82,8 @@ class Refcounts(dict):
if entry.result_refs is None:
rc += "Always NULL."
else:
- rc += (entry.result_refs and "New" or "Borrowed") + " reference."
+ rc += (entry.result_refs and "New" or "Borrowed") + \
+ " reference."
node.insert(0, refcount(rc, rc))
diff --git a/sphinx/ext/todo.py b/sphinx/ext/todo.py
new file mode 100644
index 000000000..61dd4d673
--- /dev/null
+++ b/sphinx/ext/todo.py
@@ -0,0 +1,147 @@
+# -*- coding: utf-8 -*-
+"""
+ sphinx.ext.todo
+ ~~~~~~~~~~~~~~~
+
+ Allow todos to be inserted into your documentation. Inclusion of todos can
+ be switched of by a configuration variable. The todolist directive collects
+ all todos of your project and lists them along with a backlink to the
+ original location.
+
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+from docutils import nodes
+
+from sphinx.util.compat import Directive, make_admonition
+
+class todo_node(nodes.Admonition, nodes.Element): pass
+class todolist(nodes.General, nodes.Element): pass
+
+
+class Todo(Directive):
+ """
+ A todo entry, displayed (if configured) in the form of an admonition.
+ """
+
+ has_content = True
+ required_arguments = 0
+ optional_arguments = 0
+ final_argument_whitespace = False
+ option_spec = {}
+
+ def run(self):
+ env = self.state.document.settings.env
+
+ targetid = "todo-%s" % env.index_num
+ env.index_num += 1
+ targetnode = nodes.target('', '', ids=[targetid])
+
+ ad = make_admonition(todo_node, self.name, [_('Todo')], self.options,
+ self.content, self.lineno, self.content_offset,
+ self.block_text, self.state, self.state_machine)
+
+ # Attach a list of all todos to the environment,
+ # the todolist works with the collected todo nodes
+ if not hasattr(env, 'todo_all_todos'):
+ env.todo_all_todos = []
+ env.todo_all_todos.append({
+ 'docname': env.docname,
+ 'lineno': self.lineno,
+ 'todo': ad[0].deepcopy(),
+ 'target': targetnode,
+ })
+
+ return [targetnode] + ad
+
+
+class TodoList(Directive):
+ """
+ A list of all todo entries.
+ """
+
+ has_content = False
+ required_arguments = 0
+ optional_arguments = 0
+ final_argument_whitespace = False
+ option_spec = {}
+
+ def run(self):
+ # Simply insert an empty todolist node which will be replaced later
+ # when process_todo_nodes is called
+ return [todolist('')]
+
+
+def process_todo_nodes(app, doctree, fromdocname):
+ if not app.config['todo_include_todos']:
+ for node in doctree.traverse(todo_node):
+ node.parent.remove(node)
+
+ # Replace all todolist nodes with a list of the collected todos.
+ # Augment each todo with a backlink to the original location.
+ env = app.builder.env
+
+ if not hasattr(env, 'todo_all_todos'):
+ env.todo_all_todos = []
+
+ for node in doctree.traverse(todolist):
+ if not app.config['todo_include_todos']:
+ node.replace_self([])
+ continue
+
+ content = []
+
+ for todo_info in env.todo_all_todos:
+ para = nodes.paragraph()
+ filename = env.doc2path(todo_info['docname'], base=None)
+ description = (
+ _('(The original entry is located in %s, line %d and '
+ 'can be found ') % (filename, todo_info['lineno']))
+ para += nodes.Text(description, description)
+
+ # Create a reference
+ newnode = nodes.reference('', '')
+ innernode = nodes.emphasis(_('here'), _('here'))
+ newnode['refdocname'] = todo_info['docname']
+ newnode['refuri'] = app.builder.get_relative_uri(
+ fromdocname, todo_info['docname'])
+ newnode['refuri'] += '#' + todo_info['target']['refid']
+ newnode.append(innernode)
+ para += newnode
+ para += nodes.Text('.)', '.)')
+
+ # Insert into the todolist
+ content.append(todo_info['todo'])
+ content.append(para)
+
+ node.replace_self(content)
+
+
+def purge_todos(app, env, docname):
+ if not hasattr(env, 'todo_all_todos'):
+ return
+ env.todo_all_todos = [todo for todo in env.todo_all_todos
+ if todo['docname'] != docname]
+
+
+def visit_todo_node(self, node):
+ self.visit_admonition(node)
+
+def depart_todo_node(self, node):
+ self.depart_admonition(node)
+
+def setup(app):
+ app.add_config_value('todo_include_todos', False, False)
+
+ app.add_node(todolist)
+ app.add_node(todo_node,
+ html=(visit_todo_node, depart_todo_node),
+ latex=(visit_todo_node, depart_todo_node),
+ text=(visit_todo_node, depart_todo_node))
+
+ app.add_directive('todo', Todo)
+ app.add_directive('todolist', TodoList)
+ app.connect('doctree-resolved', process_todo_nodes)
+ app.connect('env-purge-doc', purge_todos)
+
diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py
index 1637b2c3f..61c413d7a 100644
--- a/sphinx/highlighting.py
+++ b/sphinx/highlighting.py
@@ -5,8 +5,8 @@
Highlight code blocks using Pygments.
- :copyright: 2007-2008 by Georg Brandl.
- :license: BSD.
+ :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
"""
import sys
@@ -30,6 +30,8 @@ try:
from pygments.token import Generic, Comment, Number
except ImportError:
pygments = None
+ lexers = None
+ HtmlFormatter = LatexFormatter = None
else:
class SphinxStyle(Style):
"""
@@ -47,12 +49,15 @@ else:
Number: '#208050',
})
+ class NoneStyle(Style):
+ """Style without any styling."""
+
lexers = dict(
none = TextLexer(),
python = PythonLexer(),
pycon = PythonConsoleLexer(),
- # the python3 option exists as of Pygments 0.12, but it doesn't
- # do any harm in previous versions
+ # the python3 option exists as of Pygments 1.0,
+ # but it doesn't do any harm in previous versions
pycon3 = PythonConsoleLexer(python3=True),
rest = RstLexer(),
c = CLexer(),
@@ -81,22 +86,33 @@ if sys.version_info < (2, 5):
class PygmentsBridge(object):
+ # Set these attributes if you want to have different Pygments formatters
+ # than the default ones.
+ html_formatter = HtmlFormatter
+ latex_formatter = LatexFormatter
+
def __init__(self, dest='html', stylename='sphinx'):
self.dest = dest
if not pygments:
return
- if stylename == 'sphinx':
+ if stylename is None or stylename == 'sphinx':
style = SphinxStyle
+ elif stylename == 'none':
+ style = NoneStyle
elif '.' in stylename:
module, stylename = stylename.rsplit('.', 1)
- style = getattr(__import__(module, None, None, ['']), stylename)
+ style = getattr(__import__(module, None, None, ['__name__']),
+ stylename)
else:
style = get_style_by_name(stylename)
- self.hfmter = {False: HtmlFormatter(style=style),
- True: HtmlFormatter(style=style, linenos=True)}
- self.lfmter = {False: LatexFormatter(style=style, commandprefix='PYG'),
- True: LatexFormatter(style=style, linenos=True,
- commandprefix='PYG')}
+ if dest == 'html':
+ self.fmter = {False: self.html_formatter(style=style),
+ True: self.html_formatter(style=style, linenos=True)}
+ else:
+ self.fmter = {False: self.latex_formatter(style=style,
+ commandprefix='PYG'),
+ True: self.latex_formatter(style=style, linenos=True,
+ commandprefix='PYG')}
def unhighlighted(self, source):
if self.dest == 'html':
@@ -170,9 +186,9 @@ class PygmentsBridge(object):
lexer.add_filter('raiseonerror')
try:
if self.dest == 'html':
- return highlight(source, lexer, self.hfmter[bool(linenos)])
+ return highlight(source, lexer, self.fmter[bool(linenos)])
else:
- hlsource = highlight(source, lexer, self.lfmter[bool(linenos)])
+ hlsource = highlight(source, lexer, self.fmter[bool(linenos)])
return hlsource.translate(tex_hl_escape_map)
except ErrorToken:
# this is most probably not the selected language,
@@ -186,9 +202,9 @@ class PygmentsBridge(object):
# no HTML styles needed
return ''
if self.dest == 'html':
- return self.hfmter[0].get_style_defs()
+ return self.fmter[0].get_style_defs()
else:
- styledefs = self.lfmter[0].get_style_defs()
+ styledefs = self.fmter[0].get_style_defs()
# workaround for Pygments < 0.12
if styledefs.startswith('\\newcommand\\at{@}'):
styledefs += _LATEX_STYLES
diff --git a/sphinx/htmlhelp.py b/sphinx/htmlhelp.py
deleted file mode 100644
index 33dbd9ff5..000000000
--- a/sphinx/htmlhelp.py
+++ /dev/null
@@ -1,210 +0,0 @@
-# -*- coding: utf-8 -*-
-"""
- sphinx.htmlhelp
- ~~~~~~~~~~~~~~~
-
- Build HTML help support files.
- Adapted from the original Doc/tools/prechm.py.
-
- :copyright: 2007-2008 by Georg Brandl.
- :license: BSD.
-"""
-
-import os
-import cgi
-from os import path
-
-from docutils import nodes
-
-from sphinx import addnodes
-
-# Project file (*.hhp) template. 'outname' is the file basename (like
-# the pythlp in pythlp.hhp); 'version' is the doc version number (like
-# the 2.2 in Python 2.2).
-# The magical numbers in the long line under [WINDOWS] set most of the
-# user-visible features (visible buttons, tabs, etc).
-# About 0x10384e: This defines the buttons in the help viewer. The
-# following defns are taken from htmlhelp.h. Not all possibilities
-# actually work, and not all those that work are available from the Help
-# Workshop GUI. In particular, the Zoom/Font button works and is not
-# available from the GUI. The ones we're using are marked with 'x':
-#
-# 0x000002 Hide/Show x
-# 0x000004 Back x
-# 0x000008 Forward x
-# 0x000010 Stop
-# 0x000020 Refresh
-# 0x000040 Home x
-# 0x000080 Forward
-# 0x000100 Back
-# 0x000200 Notes
-# 0x000400 Contents
-# 0x000800 Locate x
-# 0x001000 Options x
-# 0x002000 Print x
-# 0x004000 Index
-# 0x008000 Search
-# 0x010000 History
-# 0x020000 Favorites
-# 0x040000 Jump 1
-# 0x080000 Jump 2
-# 0x100000 Zoom/Font x
-# 0x200000 TOC Next
-# 0x400000 TOC Prev
-
-project_template = '''\
-[OPTIONS]
-Binary TOC=Yes
-Compiled file=%(outname)s.chm
-Contents file=%(outname)s.hhc
-Default Window=%(outname)s
-Default topic=index.html
-Display compile progress=No
-Full text search stop list file=%(outname)s.stp
-Full-text search=Yes
-Index file=%(outname)s.hhk
-Language=0x409
-Title=%(title)s
-
-[WINDOWS]
-%(outname)s="%(title)s","%(outname)s.hhc","%(outname)s.hhk",\
-"index.html","index.html",,,,,0x63520,220,0x10384e,[0,0,1024,768],,,,,,,0
-
-[FILES]
-'''
-
-contents_header = '''\
-
-
-
-
-
-
-
-
-'''
-
-contents_footer = '''\
-
-'''
-
-object_sitemap = '''\
-
-'''
-
-# List of words the full text search facility shouldn't index. This
-# becomes file outname.stp. Note that this list must be pretty small!
-# Different versions of the MS docs claim the file has a maximum size of
-# 256 or 512 bytes (including \r\n at the end of each line).
-# Note that "and", "or", "not" and "near" are operators in the search
-# language, so no point indexing them even if we wanted to.
-stopwords = """
-a and are as at
-be but by
-for
-if in into is it
-near no not
-of on or
-such
-that the their then there these they this to
-was will with
-""".split()
-
-
-def build_hhx(builder, outdir, outname):
- builder.info('dumping stopword list...')
- f = open(path.join(outdir, outname+'.stp'), 'w')
- try:
- for word in sorted(stopwords):
- print >>f, word
- finally:
- f.close()
-
- builder.info('writing project file...')
- f = open(path.join(outdir, outname+'.hhp'), 'w')
- try:
- f.write(project_template % {'outname': outname,
- 'title': builder.config.html_title,
- 'version': builder.config.version,
- 'project': builder.config.project})
- if not outdir.endswith(os.sep):
- outdir += os.sep
- olen = len(outdir)
- for root, dirs, files in os.walk(outdir):
- staticdir = (root == path.join(outdir, '_static'))
- for fn in files:
- if (staticdir and not fn.endswith('.js')) or fn.endswith('.html'):
- print >>f, path.join(root, fn)[olen:].replace(os.sep, '\\')
- finally:
- f.close()
-
- builder.info('writing TOC file...')
- f = open(path.join(outdir, outname+'.hhc'), 'w')
- try:
- f.write(contents_header)
- # special books
- f.write('
- {% trans %}Note: You requested an out-of-date URL from this server. We've tried to redirect you to the new location of this page, but it may not be the right one.{% endtrans %}
-
- {% endif %}
- {{ body }}
-{% endblock %}
diff --git a/sphinx/texinputs/sphinx.sty b/sphinx/texinputs/sphinx.sty
index 209c35f9d..0ce6b8637 100644
--- a/sphinx/texinputs/sphinx.sty
+++ b/sphinx/texinputs/sphinx.sty
@@ -17,7 +17,15 @@
\RequirePackage{makeidx}
\RequirePackage{framed}
\RequirePackage{color}
+% For highlighted code.
\RequirePackage{fancyvrb}
+% For table captions.
+\RequirePackage{threeparttable}
+% Handle footnotes in tables.
+\RequirePackage{footnote}
+\makesavenoteenv{tabulary}
+% For floating figures in the text.
+\RequirePackage{wrapfig}
% Redefine these colors to your liking in the preamble.
\definecolor{TitleColor}{rgb}{0.126,0.263,0.361}
@@ -55,11 +63,10 @@
\fi\fi
% XeLaTeX can do colors, too
-\IfFileExists{ifxetex.sty}{\RequirePackage{ifxetex}}{}
-\ifx\ifxetex\undefined\else\ifxetex
+\ifx\XeTeXrevision\undefined\else
\def\py@NormalColor{\color[rgb]{0.0,0.0,0.0}}
\def\py@TitleColor{\color{TitleColor}}
-\fi\fi
+\fi
% Increase printable page size (copied from fullpage.sty)
\topmargin 0pt
@@ -451,6 +458,20 @@
\fi
}{\end{fulllineitems}}
+% class method ----------------------------------------------------------
+% \begin{classmethoddesc}[classname]{methodname}{args}
+\newcommand{\classmethodline}[3][\@undefined]{
+ \py@sigline{class \bfcode{#2}}{#3}}
+\newenvironment{classmethoddesc}[3][\@undefined]{
+ \begin{fulllineitems}
+ \ifx\@undefined#1\relax
+ \classmethodline{#2}{#3}
+ \else
+ \def\py@thisclass{#1}
+ \classmethodline{#2}{#3}
+ \fi
+}{\end{fulllineitems}}
+
% object data attribute --------------------------------------------------
% \begin{memberdesc}[classname]{membername}
\newcommand{\memberline}[2][\py@classbadkey]{%
diff --git a/sphinx/texinputs/tabulary.sty b/sphinx/texinputs/tabulary.sty
index 2a96de971..ba83c0afb 100644
--- a/sphinx/texinputs/tabulary.sty
+++ b/sphinx/texinputs/tabulary.sty
@@ -109,6 +109,8 @@ Z \string\tymax: \the\tymax^^J}%
\global\advance\TY@linewidth-#1\relax}
\def\endtabulary{%
\gdef\@halignto{}%
+ \let\TY@footnote\footnote%
+ \def\footnote{}% prevent footnotes from doing anything
\expandafter\TY@tab\the\toks@
\crcr\omit
{\xdef\TY@save@row{}%
@@ -172,6 +174,7 @@ Z \message{> tymin}%
\TY@checkmin
\TY@count\z@
\let\TY@box\TY@box@v
+ \let\footnote\TY@footnote % restore footnotes
{\expandafter\TY@final\the\toks@\endTY@final}%
\count@\z@
\@tempswatrue
diff --git a/sphinx/templates/changes/frameset.html b/sphinx/themes/basic/changes/frameset.html
similarity index 64%
rename from sphinx/templates/changes/frameset.html
rename to sphinx/themes/basic/changes/frameset.html
index 6d0535334..9d9af9eb5 100644
--- a/sphinx/templates/changes/frameset.html
+++ b/sphinx/themes/basic/changes/frameset.html
@@ -2,7 +2,7 @@
"http://www.w3.org/TR/html4/frameset.dtd">
- {% trans version=version, docstitle=docstitle %}Changes in Version {{ version }} — {{ docstitle }}{% endtrans %}
+ {% trans version=version|e, docstitle=docstitle|e %}Changes in Version {{ version }} — {{ docstitle }}{% endtrans %}