Skip to content

Commit c4378b2

Browse files
authored
add sphinx doc template for k2. (k2-fsa#205)
* add sphinx doc template for k2. * fix style issues. * install k2 from pip.
1 parent 68a5d00 commit c4378b2

File tree

12 files changed

+254
-1
lines changed

12 files changed

+254
-1
lines changed

.flake8

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ max-line-length=80
55
exclude =
66
.git,
77
build,
8-
k2/python/host
8+
k2/python/host,
9+
k2/docs
910

1011
ignore =
1112
# E127 continuation line over-indented for visual indent

.readthedocs.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# .readthedocs.yml
2+
# Read the Docs configuration file
3+
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
4+
5+
# Required
6+
version: 2
7+
8+
# Build documentation in the docs/ directory with Sphinx
9+
sphinx:
10+
configuration: k2/docs/source/conf.py
11+
12+
# Build documentation with MkDocs
13+
#mkdocs:
14+
# configuration: mkdocs.yml
15+
16+
# Optionally build your docs in additional formats such as PDF
17+
formats:
18+
- pdf
19+
20+
# Optionally set the version of Python and requirements required to build your docs
21+
python:
22+
version: 3.6
23+
install:
24+
- requirements: k2/docs/requirements.txt
25+
system_packages: true

k2/docs/Makefile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Minimal makefile for Sphinx documentation
2+
#
3+
4+
# You can set these variables from the command line, and also
5+
# from the environment for the first two.
6+
SPHINXOPTS ?=
7+
SPHINXBUILD ?= sphinx-build
8+
SOURCEDIR = source
9+
BUILDDIR = build
10+
11+
# Put it first so that "make" without argument is like "make help".
12+
help:
13+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
14+
15+
.PHONY: help Makefile
16+
17+
# Catch-all target: route all unknown targets to Sphinx using the new
18+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
19+
%: Makefile
20+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

k2/docs/make.bat

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@ECHO OFF
2+
3+
pushd %~dp0
4+
5+
REM Command file for Sphinx documentation
6+
7+
if "%SPHINXBUILD%" == "" (
8+
set SPHINXBUILD=sphinx-build
9+
)
10+
set SOURCEDIR=source
11+
set BUILDDIR=build
12+
13+
if "%1" == "" goto help
14+
15+
%SPHINXBUILD% >NUL 2>NUL
16+
if errorlevel 9009 (
17+
echo.
18+
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
19+
echo.installed, then set the SPHINXBUILD environment variable to point
20+
echo.to the full path of the 'sphinx-build' executable. Alternatively you
21+
echo.may add the Sphinx directory to PATH.
22+
echo.
23+
echo.If you don't have Sphinx installed, grab it from
24+
echo.http://sphinx-doc.org/
25+
exit /b 1
26+
)
27+
28+
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
29+
goto end
30+
31+
:help
32+
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
33+
34+
:end
35+
popd

k2/docs/requirements.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
graphviz
2+
dataclasses
3+
sphinx_rtd_theme
4+
sphinx==3.1.2
5+
sphinx-autodoc-typehints==1.11.0
6+
torch
7+
k2-fsa==0.0.5

k2/docs/source/_static/.gitkeep

Whitespace-only changes.

k2/docs/source/_templates/.gitkeep

Whitespace-only changes.

k2/docs/source/conf.py

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Configuration file for the Sphinx documentation builder.
2+
#
3+
# This file only contains a selection of the most common options. For a full
4+
# list see the documentation:
5+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
6+
7+
# -- Path setup --------------------------------------------------------------
8+
9+
# If extensions (or modules to document with autodoc) are in another directory,
10+
# add these directories to sys.path here. If the directory is relative to the
11+
# documentation root, use os.path.abspath to make it absolute, like shown here.
12+
#
13+
import os
14+
import sys
15+
16+
import sphinx_rtd_theme
17+
18+
# -- Project information -----------------------------------------------------
19+
20+
project = 'k2'
21+
copyright = '2020, k2 development team'
22+
author = 'k2 development team'
23+
24+
# The full version, including alpha/beta/rc tags
25+
release = '0.0.1'
26+
version = '0.0.1'
27+
28+
# -- General configuration ---------------------------------------------------
29+
30+
# Add any Sphinx extension module names here, as strings. They can be
31+
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
32+
# ones.
33+
extensions = [
34+
'sphinx.ext.autodoc',
35+
'sphinx.ext.githubpages',
36+
'sphinx.ext.linkcode',
37+
'sphinx.ext.napoleon',
38+
'sphinx_autodoc_typehints',
39+
'sphinx_rtd_theme',
40+
]
41+
42+
# Add any paths that contain templates here, relative to this directory.
43+
templates_path = ['_templates']
44+
45+
source_suffix = ['.rst']
46+
master_doc = 'index'
47+
48+
# List of patterns, relative to source directory, that match files and
49+
# directories to ignore when looking for source files.
50+
# This pattern also affects html_static_path and html_extra_path.
51+
exclude_patterns = []
52+
53+
# -- Options for HTML output -------------------------------------------------
54+
55+
# The theme to use for HTML and HTML Help pages. See the documentation for
56+
# a list of builtin themes.
57+
#
58+
html_theme = 'sphinx_rtd_theme'
59+
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
60+
html_show_sourcelink = True
61+
62+
# Add any paths that contain custom static files (such as style sheets) here,
63+
# relative to this directory. They are copied after the builtin static files,
64+
# so a file named "default.css" will overwrite the builtin "default.css".
65+
html_static_path = ['_static']
66+
67+
pygments_style = 'sphinx'
68+
69+
html_context = {
70+
'display_github': True,
71+
'github_user': 'k2-fsa',
72+
'github_repo': 'k2',
73+
'github_version': 'master',
74+
'conf_py_path': '/k2/docs/source/',
75+
}
76+
77+
# refer to
78+
# https://sphinx-rtd-theme.readthedocs.io/en/latest/configuring.html
79+
html_theme_options = {
80+
'logo_only': False,
81+
'display_version': True,
82+
'prev_next_buttons_location': 'bottom',
83+
'style_external_links': True,
84+
}
85+
86+
autodoc_default_options = {
87+
'content': 'both',
88+
'members': None,
89+
'member-order': 'bysource',
90+
'special-members': '__init__',
91+
'undoc-members': True,
92+
'exclude-members': '__weakref__'
93+
}
94+
95+
96+
# Resolve function for the linkcode extension.
97+
# Modified from https://github.com/rwth-i6/returnn/blob/master/docs/conf.py
98+
def linkcode_resolve(domain, info):
99+
100+
def find_source():
101+
# try to find the file and line number, based on code from numpy:
102+
# https://github.com/numpy/numpy/blob/master/doc/source/conf.py#L286
103+
obj = sys.modules[info['module']]
104+
for part in info['fullname'].split('.'):
105+
obj = getattr(obj, part)
106+
import inspect
107+
import os
108+
fn = inspect.getsourcefile(obj)
109+
fn = os.path.relpath(fn, start='k2')
110+
source, lineno = inspect.getsourcelines(obj)
111+
return fn, lineno, lineno + len(source) - 1
112+
113+
if domain != 'py' or not info['module']:
114+
return None
115+
try:
116+
filename = '%s#L%d-L%d' % find_source()
117+
except Exception:
118+
filename = info['module'].replace('.', '/') + '.py'
119+
filename = filename.replace('../', '')
120+
return f'https://github.com/k2-fsa/k2/blob/master/k2/{filename}'

k2/docs/source/index.rst

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.. k2 documentation master file, created by
2+
sphinx-quickstart on Fri Oct 2 21:03:36 2020.
3+
You can adapt this file completely to your liking, but it should at least
4+
contain the root `toctree` directive.
5+
6+
K2 Documentation
7+
================
8+
9+
.. toctree::
10+
:maxdepth: 2
11+
:caption: Contents:
12+
13+
14+
.. toctree::
15+
:maxdepth: 2
16+
:caption: Python API
17+
18+
python_api/array.rst
19+
python_api/fsa.rst
20+
21+
22+
23+
Indices and tables
24+
==================
25+
26+
* :ref:`genindex`
27+
* :ref:`modindex`
28+
* :ref:`search`

k2/docs/source/python_api/array.rst

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
k2.array
2+
--------
3+
4+
.. currentmodule:: k2
5+
6+
.. autoclass:: Array
7+
:members:
8+

k2/docs/source/python_api/fsa.rst

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
k2.fsa
2+
------
3+
4+
.. currentmodule:: k2
5+
6+
.. autoclass:: Fsa
7+
:members:

k2/python/k2/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from .symbol_table import SymbolTable
44
from _k2 import Arc
55

6+
__version__ = '0.1'
7+
68
# please keep the list sorted
79
__all__ = [
810
'Arc',

0 commit comments

Comments
 (0)