Skip to content

Commit

Permalink
Better docs (#71)
Browse files Browse the repository at this point in the history
* Reformat existing docs for the m.css documentation theme.

* Indent reST files with four spaces.

Two spaces are just too little for prose.

* Added m.css submodule and a helper script for building docs.

* Showcase a bunch of features for connecting code and docs better.

* bindings: Simulator class definition is needed sooner.

Co-authored-by: Erik Wijmans <erik.wijmans@oculus.com>
  • Loading branch information
2 people authored and mosra committed Sep 4, 2019
1 parent b18fe76 commit d8edf92
Show file tree
Hide file tree
Showing 30 changed files with 600 additions and 603 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true

# isort can't parse [*.{py,rst}], so specifying it separately
# https://github.com/timothycrosley/isort/issues/830
[*.py]
indent_size = 4
[*.rst]
indent_size = 4
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@
[submodule "src/deps/magnum-bindings"]
path = src/deps/magnum-bindings
url = https://github.com/mosra/magnum-bindings.git
[submodule "docs/m.css"]
path = docs/m.css
url = https://github.com/mosra/m.css
6 changes: 6 additions & 0 deletions build_docs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

# Propagate failures properly
set -e

./docs/m.css/documentation/python.py docs/conf.py
1 change: 1 addition & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
m.math.cache
20 changes: 0 additions & 20 deletions docs/Makefile

This file was deleted.

324 changes: 90 additions & 234 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,256 +1,112 @@
#!/usr/bin/env python3

# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
# This file does only contain a selection of the most common options. For a
# full list see the documentation:
# http://www.sphinx-doc.org/en/master/config

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import re
import sys

import habitat_sim
from docutils import nodes
from sphinx import addnodes
from sphinx.util.docfields import TypedField

sys.path.insert(0, os.path.abspath(os.path.join("..")))

# -- Project information -----------------------------------------------------

project = u"Habitat Sim"
copyright = u"2018, Habitat Contributors"
author = u"Habitat Contributors"

# The short X.Y version
version = u"master (" + habitat_sim.__version__ + " )"
# The full version, including alpha/beta/rc tags.
release = u"master"


# -- General configuration ---------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.autosummary",
"sphinx.ext.doctest",
"sphinx.ext.intersphinx",
"sphinx.ext.todo",
"sphinx.ext.coverage",
"sphinx.ext.mathjax",
"sphinx.ext.napoleon",
"sphinx.ext.viewcode",
"sphinx_autodoc_typehints", # needs to load after napoleon
# TODO make this less brittle
sys.path = [
os.path.join(os.path.dirname(__file__), "../"),
# os.path.join(os.path.dirname(__file__), '../build-bundledmagnum/src/deps/magnum-bindings/src/python/')
] + sys.path

import habitat_sim # NOQA

# TODO: remove once m.css handles class hierarchies better
habitat_sim.logging.GlogFormatter.formatStack.__doc__ = ""

PROJECT_TITLE = "Habitat"
PROJECT_SUBTITLE = "Sim Python docs"
MAIN_PROJECT_URL = "https://aihabitat.org"
INPUT_MODULES = [habitat_sim]
INPUT_DOCS = ["docs.rst"]
INPUT_PAGES = [
"pages/index.rst",
"pages/new-actions.rst",
"pages/stereo-agent.rst",
"pages/notebooks.rst",
]

napoleon_use_ivar = True
napoleon_use_param = True

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = ".rst"

# The master toctree document.
master_doc = "index"

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path .
exclude_patterns = [u"_build", "Thumbs.db", ".DS_Store"]

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = "sphinx"

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = True

# Disable docstring inheritance
autodoc_inherit_docstrings = False


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = "sphinx_rtd_theme"

# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#
# html_theme_options = {}

# 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,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]

# Custom sidebar templates, must be a dictionary that maps document names
# to template names.
#
# The default sidebars (for documents that don't match any pattern) are
# defined by theme itself. Builtin themes are using these templates by
# default: ``['localtoc.html', 'relations.html', 'sourcelink.html',
# 'searchbox.html']``.
#
# html_sidebars = {}


# -- Options for HTMLHelp output ---------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = "HabitatSimDoc"

PLUGINS = [
"m.abbr",
"m.code",
"m.components",
"m.dox",
"m.gh",
"m.htmlsanity",
"m.images",
"m.link",
"m.math",
"m.sphinx",
]

# -- Options for LaTeX output ------------------------------------------------
CLASS_INDEX_EXPAND_LEVELS = 2

latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',
# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
#
# 'preamble': '',
# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
NAME_MAPPING = {
# TODO: remove once the inventory file contains this info
"_magnum": "magnum"
}
PYBIND11_COMPATIBILITY = True
ATTRS_COMPATIBILITY = True

OUTPUT = "../build/docs/habitat-sim/"

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
LINKS_NAVBAR1 = [
(
master_doc,
"habitat_sim.tex",
u"Habitat Sim Documentation",
u"Manolis Savva",
"manual",
)
"Pages",
"pages",
[
("Add new actions", "new-actions"),
("Stereo agent", "stereo-agent"),
("Notebooks", "notebooks"),
],
),
("Classes", "classes", []),
]
LINKS_NAVBAR2 = [
("C++ Docs", "../habitat-cpp/index.html", []),
("Habitat API", "https://aihabitat.org/habitat-api/", []),
]


# -- Options for manual page output ------------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [(master_doc, "habitat_sim", u"Habitat Sim Documentation", [author], 1)]


# -- Options for Texinfo output ----------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
M_SPHINX_INVENTORIES = [
# TODO: clean up once paths are relative to this file implicitly
(
master_doc,
"habitat_sim",
u"Habitat Sim Documentation",
author,
"habitat_sim",
"One line description of project.",
"Miscellaneous",
)
os.path.join(os.path.realpath(os.path.dirname(__file__)), "python.inv"),
"https://docs.python.org/3/",
[],
["m-doc-external"],
),
(
os.path.join(os.path.realpath(os.path.dirname(__file__)), "numpy.inv"),
"https://docs.scipy.org/doc/numpy/",
[],
["m-doc-external"],
),
(
os.path.join(
os.path.realpath(os.path.dirname(__file__)), "magnum-bindings.inv"
),
"https://doc.magnum.graphics/python/",
[],
["m-doc-external"],
),
]
M_SPHINX_INVENTORY_OUTPUT = "objects.inv"
M_SPHINX_PARSE_DOCSTRINGS = True

M_HTMLSANITY_SMART_QUOTES = True
# Will people hate me if I enable this?
# M_HTMLSANITY_HYPHENATION = True

# -- Extension configuration -------------------------------------------------
_hex_colors_src = re.compile(
r"""<span class="s2">&quot;0x(?P<hex>[0-9a-f]{6})&quot;</span>"""
)
_hex_colors_dst = r"""<span class="s2">&quot;0x\g<hex>&quot;</span><span class="m-code-color" style="background-color: #\g<hex>;"></span>"""

# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {
"python": ("https://docs.python.org/", None),
"numpy": ("http://docs.scipy.org/doc/numpy/", None),
M_CODE_FILTERS_POST = {
("Python", "string_hex_colors"): lambda code: _hex_colors_src.sub(
_hex_colors_dst, code
)
}


# -- A patch that prevents Sphinx from cross-referencing ivar tags -------
# See http://stackoverflow.com/a/41184353/3343043


def patched_make_field(self, types, domain, items, **kw):
# `kw` catches `env=None` needed for newer sphinx while maintaining
# backwards compatibility when passed along further down!

# type: (List, unicode, Tuple) -> nodes.field
def handle_item(fieldarg, content):
par = nodes.paragraph()
par += addnodes.literal_strong("", fieldarg) # Patch: this line added
# par.extend(self.make_xrefs(self.rolename, domain, fieldarg,
# addnodes.literal_strong))
if fieldarg in types:
par += nodes.Text(" (")
# NOTE: using .pop() here to prevent a single type node to be
# inserted twice into the doctree, which leads to
# inconsistencies later when references are resolved
fieldtype = types.pop(fieldarg)
if len(fieldtype) == 1 and isinstance(fieldtype[0], nodes.Text):
typename = u"".join(n.astext() for n in fieldtype)
typename = typename.replace("int", "python:int")
typename = typename.replace("long", "python:long")
typename = typename.replace("float", "python:float")
typename = typename.replace("type", "python:type")
par.extend(
self.make_xrefs(
self.typerolename,
domain,
typename,
addnodes.literal_emphasis,
**kw
)
)
else:
par += fieldtype
par += nodes.Text(")")
par += nodes.Text(" -- ")
par += content
return par

fieldname = nodes.field_name("", self.label)
if len(items) == 1 and self.can_collapse:
fieldarg, content = items[0]
bodynode = handle_item(fieldarg, content)
else:
bodynode = self.list_type()
for fieldarg, content in items:
bodynode += nodes.list_item("", handle_item(fieldarg, content))
fieldbody = nodes.field_body("", bodynode)
return nodes.field("", fieldname, fieldbody)


TypedField.make_field = patched_make_field
Loading

0 comments on commit d8edf92

Please sign in to comment.