Skip to content

Commit

Permalink
Merge pull request #1794 from alicevision/dev/lv/sphinxDoc
Browse files Browse the repository at this point in the history
[docs] Python documentation generation using Sphinx
  • Loading branch information
fabiencastan committed Oct 21, 2022
2 parents ad69c70 + f3bed08 commit a0068c3
Show file tree
Hide file tree
Showing 21 changed files with 474 additions and 74 deletions.
18 changes: 18 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# .readthedocs.yaml
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Build HTML documentation with Sphinx
sphinx:
builder: html
configuration: docs/source/conf.py

# Python requirements
python:
install:
- requirements: requirements.txt
- requirements: dev_requirements.txt
- requirements: docs/requirements.txt
3 changes: 3 additions & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Sphinx
build/
source/generated/
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = source
BUILDDIR = build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
24 changes: 24 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Documentation

We use [Sphinx](https://www.sphinx-doc.org) to generate Meshroom's documentation.

## Requirements

To install all the requirements for building the documentation, simply run:
```bash
pip install sphinx sphinx-rtd-theme myst-parser
```

You also need to have [Graphviz](https://graphviz.org/) installed.

> Note: since Sphinx will import the entire `meshroom` package, all requirements for Meshroom must also be installed
## Build

To generate the documentation, go to the `docs` folder and run the Sphinx makefile:
```bash
cd meshroom/docs
make html
```

To access the documentation, simply go to `meshroom/docs/build/html` and open `index.html` in a browser.
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=source
set BUILDDIR=build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
myst-parser
Empty file added docs/source/_ext/__init__.py
Empty file.
70 changes: 70 additions & 0 deletions docs/source/_ext/fetch_md.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Sphinx extension defining the fetch_md directive
#
# Goal:
# include the content of a given markdown file
#
# Usage:
# .. fetch_md:: path/to/file.md
# the filepath is relative to the project base directory
#
# Note:
# some markdown files contain links to other files that belong to the project
# since those links are relative to the file location, they are no longer valid in the new context
# therefore we try to update these links but it is not always possible

import os
from docutils.nodes import SparseNodeVisitor
from docutils.parsers.rst import Directive
from utils import md_to_docutils, get_link_key

# Python2 compatibility
try:
FileNotFoundError
except NameError:
FileNotFoundError = IOError


class Relinker(SparseNodeVisitor):

def relink(self, node, base_dir):
key = get_link_key(node)
if key is None:
return
link = node.attributes[key]
if link.startswith('http') or link.startswith('mailto'):
return
if link.startswith('/'):
link = link[1:]
node.attributes[key] = base_dir+'/'+link

def visit_image(self, node):
self.relink(node, os.getenv('PROJECT_DIR'))


class FetchMd(Directive):

required_arguments = 1

def run(self):
path = os.path.abspath(os.getenv('PROJECT_DIR')+'/'+self.arguments[0])
result = []
try:
with open(path) as file:
text = file.read()
doc = md_to_docutils(text)
relinker = Relinker(doc)
doc.walk(relinker)
result.append(doc[0])
except FileNotFoundError:
pass
return result


def setup(app):
app.add_directive('fetch_md', FetchMd)

return {
'version': '0.1',
'parallel_read_safe': True,
'parallel_write_safe': True
}
67 changes: 67 additions & 0 deletions docs/source/_ext/meshroom_doc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Sphinx extension defining the meshroom_doc directive
#
# Goal:
# create specific documentation content for meshroom objects
#
# Usage:
# .. meshroom_doc::
# :module: module_name
# :class: class_name
#
# Note:
# for now this tool focuses only on meshroom nodes

from docutils import nodes
from docutils.parsers.rst import Directive
from utils import md_to_docutils

import importlib
from meshroom.core import desc


class MeshroomDoc(Directive):

required_arguments = 4

def parse_args(self):
module_name = self.arguments[self.arguments.index(':module:')+1]
class_name = self.arguments[self.arguments.index(':class:')+1]
return (module_name, class_name)

def run(self):
result = []
# Import module and class
module_name, class_name = self.parse_args()
module = importlib.import_module(module_name)
node_class = getattr(module, class_name)
# Class inherits desc.Node
if issubclass(node_class, desc.Node):
node = node_class()
# Category
doc = md_to_docutils('**Category**: {}'.format(node.category))
result.extend(doc.children)
# Documentation
doc = md_to_docutils(node.documentation)
result.extend(doc.children)
# Inputs
text_inputs = '**Inputs**: \n'
for attr in node.inputs:
text_inputs += '- {} ({})\n'.format(attr._name, attr.__class__.__name__)
doc = md_to_docutils(text_inputs)
result.extend(doc.children)
# Outputs
text_outputs = '**Outputs**: \n'
for attr in node.outputs:
text_outputs += '- {} ({})\n'.format(attr._name, attr.__class__.__name__)
doc = md_to_docutils(text_outputs)
result.extend(doc.children)
return result


def setup(app):
app.add_directive("meshroom_doc", MeshroomDoc)
return {
'version': '0.1',
'parallel_read_safe': True,
'parallel_write_safe': True,
}
25 changes: 25 additions & 0 deletions docs/source/_ext/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Utility functions for custom Sphinx extensions

from myst_parser.docutils_ import Parser
from myst_parser.mdit_to_docutils.base import make_document


# Given a string written in markdown
# parse its content
# and return the corresponding docutils document
def md_to_docutils(text):
parser = Parser()
doc = make_document(parser_cls=Parser)
parser.parse(text, doc)
return doc


# Given a docutils node
# find an attribute that corresponds to a link (if it exists)
# and return its key
def get_link_key(node):
link_keys = ['uri', 'refuri', 'refname']
for key in link_keys:
if key in node.attributes.keys():
return key
return None
38 changes: 38 additions & 0 deletions docs/source/_templates/autosummary/class.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{{ fullname | escape | underline}}


.. inheritance-diagram:: {{ fullname }}


.. meshroom_doc::
:module: {{ module }}
:class: {{ objname }}


.. currentmodule:: {{ module }}

.. autoclass:: {{ objname }}

{% block methods %}
.. automethod:: __init__

{% if methods %}
.. rubric:: {{ _('Methods') }}

.. autosummary::
{% for item in methods %}
~{{ name }}.{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}

{% block attributes %}
{% if attributes %}
.. rubric:: {{ _('Attributes') }}

.. autosummary::
{% for item in attributes %}
~{{ name }}.{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}
63 changes: 63 additions & 0 deletions docs/source/_templates/autosummary/module.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{{ fullname | escape | underline}}


.. automodule:: {{ fullname }}

{% block attributes %}
{% if attributes %}
.. rubric:: {{ _('Module Attributes') }}

.. autosummary::
{% for item in attributes %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}

{% block functions %}
{% if functions %}
.. rubric:: {{ _('Functions') }}

.. autosummary::
{% for item in functions %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}

{% block classes %}
{% if classes %}
.. rubric:: {{ _('Classes') }}

.. autosummary::
:toctree:
:recursive:
{% for item in classes %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}

{% block exceptions %}
{% if exceptions %}
.. rubric:: {{ _('Exceptions') }}

.. autosummary::
{% for item in exceptions %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}

{% block modules %}
{% if modules %}
.. rubric:: Modules

.. autosummary::
:toctree:
:recursive:
{% for item in modules %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}
10 changes: 10 additions & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Python API Reference
====================


.. autosummary::
:recursive:
:toctree: generated

meshroom
tests
5 changes: 5 additions & 0 deletions docs/source/changes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Release Notes
=============


.. fetch_md:: CHANGES.md
Loading

0 comments on commit a0068c3

Please sign in to comment.