evennia/docs/source/conf.py

382 lines
12 KiB
Python
Raw Normal View History

2020-04-05 00:02:02 +02:00
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
import os
2020-07-10 14:14:34 +02:00
import re
2022-11-10 22:21:12 +01:00
import sys
2022-11-14 21:30:05 +01:00
from collections import namedtuple
2022-02-08 13:03:52 +01:00
2021-10-21 21:04:14 +02:00
# from recommonmark.transform import AutoStructify
2020-04-05 00:02:02 +02:00
from sphinx.util.osutil import cd
# -- Project information -----------------------------------------------------
2020-06-14 11:45:10 +02:00
project = "Evennia"
2022-11-14 20:42:10 +01:00
copyright = "2022, The Evennia developer community"
2020-06-14 11:45:10 +02:00
author = "The Evennia developer community"
# The full Evennia version covered by these docs, including alpha/beta/rc tags
# This will be used for multi-version selection options.
release = "1.0-dev"
2020-04-05 00:02:02 +02:00
# -- General configuration ---------------------------------------------------
extensions = [
2020-04-05 15:02:38 +02:00
"sphinx_multiversion",
2020-04-05 16:50:52 +02:00
"sphinx.ext.napoleon",
2020-04-07 23:13:24 +02:00
"sphinx.ext.autosectionlabel",
2020-04-10 17:38:26 +02:00
"sphinx.ext.viewcode",
2020-06-14 11:45:10 +02:00
"sphinx.ext.todo",
"sphinx.ext.githubpages",
2022-02-08 13:03:52 +01:00
"myst_parser",
2020-04-05 00:02:02 +02:00
]
2020-06-16 14:50:02 +02:00
source_suffix = [".md", ".rst"]
master_doc = "index"
2020-06-14 20:19:31 +02:00
2020-04-05 15:02:38 +02:00
# make sure sectionlabel references can be used as path/to/file:heading
autosectionlabel_prefix_document = True
2020-04-05 00:02:02 +02:00
# Add any paths that contain templates here, relative to this directory.
2020-06-14 11:45:10 +02:00
templates_path = ["_templates"]
2020-04-09 08:39:55 +02:00
# 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".
2020-06-14 11:45:10 +02:00
html_static_path = ["_static"]
# -- Sphinx-multiversion config ----------------------------------------------
# which branches to include in multi-versioned docs
2022-11-14 21:30:05 +01:00
# smv_branch_whitelist = r"^develop$|^v[0-9\.]+?$"
# smv_branch_whitelist = r"^develop$|^master$|^v1.0$"
smv_branch_whitelist = r"^develop$"
2020-06-14 11:45:10 +02:00
smv_outputdir_format = "{config.release}"
# don't make docs for tags
smv_tag_whitelist = r"^$"
2022-11-14 21:30:05 +01:00
# used to fill in versioning.html links for versions that are not actually built
legacy_versions = ["0.9.5"]
def add_legacy_versions_to_html_page_context(app, pagename, templatename, context, doctree):
LVersion = namedtuple("legacy_version", ["release", "name", "url"])
context["legacy_versions"] = [
LVersion(release=f"{vers}", name=f"v{vers}", url=f"../{vers}/index.html")
for vers in legacy_versions
]
2020-06-14 11:45:10 +02:00
# -- Options for HTML output -------------------------------------------------
2020-06-16 14:50:02 +02:00
html_theme = "nature"
2020-04-09 08:39:55 +02:00
# Custom extras for sidebar
html_sidebars = {
2020-06-14 11:45:10 +02:00
"**": [
2020-04-09 08:39:55 +02:00
"searchbox.html",
"localtoc.html",
# "globaltoc.html",
"relations.html",
"sourcelink.html",
2021-03-06 01:15:28 +01:00
"links.html",
2020-04-09 08:39:55 +02:00
"versioning.html",
]
}
2020-06-15 21:30:49 +02:00
html_favicon = "_static/images/favicon.ico"
html_logo = "_static/images/evennia_logo.png"
html_short_title = "Evennia"
2020-04-05 00:02:02 +02:00
2020-06-14 11:45:10 +02:00
# HTML syntax highlighting style
2021-11-10 01:25:53 +01:00
pygments_style = "friendly"
2020-04-05 00:02:02 +02:00
2020-06-14 20:19:31 +02:00
# -- Options for LaTeX output ------------------------------------------------
# experimental, not working well atm
2020-06-16 14:50:02 +02:00
latex_engine = "xelatex"
latex_show_urls = "footnote"
2020-06-14 20:19:31 +02:00
latex_elements = {
2020-06-16 14:50:02 +02:00
"papersize": "a4paper",
"fncychap": r"\usepackage[Bjarne]{fncychap}",
"fontpkg": r"\usepackage{times,amsmath,amsfonts,amssymb,amsthm}",
"preamble": r"""
2020-06-14 20:19:31 +02:00
\usepackage[utf8]{fontenc}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage[math-style=literal]{unicode-math}
\usepackage{newunicodechar}
\usepackage{graphicx}
2020-06-16 14:50:02 +02:00
""",
2020-06-14 20:19:31 +02:00
}
latex_documents = [
2020-06-16 14:50:02 +02:00
(master_doc, "main.tex", "Sphinx format", "Evennia", "report"),
("toc", "toc.tex", "TOC", "Evennia", "report"),
2020-06-14 20:19:31 +02:00
]
2021-10-21 21:04:14 +02:00
# -- MyST Markdown parsing -----------------------------------------------------
myst_enable_extensions = [
"amsmath",
"colon_fence", # Use ::: instead of ``` for some extra features
"deflist", # use : to mark sublevel of list
"dollarmath",
"html_admonition", # Add admonitions in html (usually use ```{admonition} directive)
"html_image", # parse raw <img ...> </img> tags
"linkify", # convert bare urls to md links
"replacements", # (c) to copyright sign etc
"smartquotes",
"substitution",
"tasklist",
]
myst_dmath_allow_space = False # requires $a$, not $ a$ or $a $
myst_dmath_allow_digits = False # requires $a$, not 1$a$ or $a$2
myst_dmath_double_inline = True # allow $$ ... $$ math blocks
myst_substitution = {
# used with Jinja2. Can also be set in a substitutions: block in front-matter of page
}
# add anchors to h1, h2, ... level headings
myst_heading_anchors = 4
suppress_warnings = ["myst.ref"]
2020-04-07 23:13:24 +02:00
# reroute to github links or to the api
2020-04-05 12:32:52 +02:00
2021-10-21 21:04:14 +02:00
# shortcuts
_githubstart = "github:"
_apistart = "api:"
_choose_issue = "github:issue"
_sourcestart = "src:"
# remaps
2020-06-14 20:19:31 +02:00
_github_code_root = "https://github.com/evennia/evennia/blob/"
2020-04-07 23:13:24 +02:00
_github_doc_root = "https://github.com/evennia/tree/master/docs/sources/"
_github_issue_choose = "https://github.com/evennia/evennia/issues/new/choose"
2021-10-21 21:04:14 +02:00
_ref_regex = re.compile( # normal reference-links [txt](url)
2022-02-08 13:03:52 +01:00
r"\[(?P<txt>[\w -\[\]\`\n]+?)\]\((?P<url>.+?)\)", re.I + re.S + re.U + re.M
)
2021-10-21 21:04:14 +02:00
_ref_doc_regex = re.compile( # in-document bottom references [txt]: url
2022-02-08 13:03:52 +01:00
r"\[(?P<txt>[\w -\`]+?)\\n]:\s+?(?P<url>.+?)(?=$|\n)", re.I + re.S + re.U + re.M
)
2020-04-05 16:50:52 +02:00
2021-10-21 21:04:14 +02:00
def url_resolver(app, docname, source):
2020-06-23 00:13:21 +02:00
"""
2021-10-21 21:04:14 +02:00
A handler acting on the `source-read` signal. The `source`
is a list with one element that should be updated.
2020-07-10 14:14:34 +02:00
Convert urls by catching special markers.
2021-10-21 21:04:14 +02:00
Supported replacements (used e.g. as [txt](github:...)
github:master/<url> - add path to Evennia github master branch
github:develop/<url> - add path to Evennia github develop branch
github:issue - add link to the Evennia github issue-create page
src:foo.bar#Foo - add link to source doc in _modules
api:foo.bar#Foo - add link to api autodoc page
2020-06-23 00:13:21 +02:00
"""
2022-02-08 13:03:52 +01:00
2021-10-21 21:04:14 +02:00
def _url_remap(url):
# determine depth in tree of current document
2022-02-08 13:03:52 +01:00
docdepth = docname.count("/") + 1
2021-10-21 21:04:14 +02:00
relative_path = "../".join("" for _ in range(docdepth))
if url.endswith(_choose_issue):
# github:issue shortcut
return _github_issue_choose
elif _githubstart in url:
# github:develop/... shortcut
2022-02-08 13:03:52 +01:00
urlpath = url[url.index(_githubstart) + len(_githubstart) :]
2021-10-21 21:04:14 +02:00
if not (urlpath.startswith("develop/") or urlpath.startswith("master")):
urlpath = "master/" + urlpath
return _github_code_root + urlpath
elif _sourcestart in url:
ind = url.index(_sourcestart)
2022-02-08 13:03:52 +01:00
modpath, *inmodule = url[ind + len(_sourcestart) :].rsplit("#", 1)
2021-10-21 21:04:14 +02:00
modpath = "/".join(modpath.split("."))
inmodule = "#" + inmodule[0] if inmodule else ""
modpath = modpath + ".html" + inmodule
urlpath = relative_path + "_modules/" + modpath
return urlpath
return url
def _re_ref_sub(match):
2022-02-08 13:03:52 +01:00
txt = match.group("txt")
url = _url_remap(match.group("url"))
2021-10-21 21:04:14 +02:00
return f"[{txt}]({url})"
def _re_docref_sub(match):
2022-02-08 13:03:52 +01:00
txt = match.group("txt")
url = _url_remap(match.group("url"))
2021-10-21 21:04:14 +02:00
return f"[{txt}]: {url}"
src = source[0]
src = _ref_regex.sub(_re_ref_sub, src)
src = _ref_doc_regex.sub(_re_docref_sub, src)
source[0] = src
2020-06-14 11:45:10 +02:00
2021-10-21 21:04:14 +02:00
# # -- API/Autodoc ---------------------------------------------------------------
# # automatic creation of API documentation. This requires a valid Evennia setup
2020-06-14 11:45:10 +02:00
_no_autodoc = os.environ.get("NOAUTODOC")
ansi_clean = None
2020-06-14 11:45:10 +02:00
if not _no_autodoc:
# we must set up Evennia and its paths for autodocs to work
EV_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
2020-06-14 11:45:10 +02:00
sys.path.insert(1, EV_ROOT)
with cd(EV_ROOT):
2020-06-14 11:45:10 +02:00
# set up Evennia so its sources can be parsed
os.environ["DJANGO_SETTINGS_MODULE"] = "evennia.settings_default"
2020-06-14 11:45:10 +02:00
import django # noqa
django.setup()
import evennia # noqa
evennia._init()
from evennia.utils.ansi import strip_raw_ansi as ansi_clean
2020-06-14 11:45:10 +02:00
if _no_autodoc:
exclude_patterns = ["api/*"]
else:
2021-10-21 21:04:14 +02:00
exclude_patterns = ["api/*migrations.rst", "api/*migrations.md"]
2020-06-14 11:45:10 +02:00
autodoc_default_options = {
"members": True,
2020-06-18 23:30:11 +02:00
"undoc-members": False,
2020-06-14 11:45:10 +02:00
"show-inheritance": True,
"special-members": "__init__",
"enable_eval_rst": True,
2022-02-08 13:03:52 +01:00
"inherited_members": True,
}
2020-06-18 23:30:11 +02:00
autodoc_member_order = "bysource"
# autodoc_typehints = "description"
2020-04-05 12:32:52 +02:00
2020-06-14 11:45:10 +02:00
def autodoc_skip_member(app, what, name, obj, skip, options):
2020-06-18 23:30:11 +02:00
"""Which members the autodoc should ignore."""
2020-06-14 11:45:10 +02:00
if _no_autodoc:
return True
2020-06-18 23:30:11 +02:00
if name.startswith("_") and name != "__init__":
2020-06-14 11:45:10 +02:00
return True
return False
2020-07-10 14:14:34 +02:00
def autodoc_post_process_docstring(app, what, name, obj, options, lines):
"""
Post-process docstring in various ways. Must modify lines-list in-place.
"""
try:
# clean out ANSI colors
if ansi_clean:
for il, line in enumerate(lines):
lines[il] = ansi_clean(line)
# post-parse docstrings to convert any remaining
# markdown -> reST since napoleon doesn't know Markdown
def _sub_codeblock(match):
code = match.group(1)
2020-07-27 21:09:13 +02:00
return "::\n\n {}".format("\n ".join(lne for lne in code.split("\n")))
2020-07-10 14:14:34 +02:00
2020-07-10 18:04:55 +02:00
underline_map = {
1: "-",
2: "=",
2020-07-27 21:09:13 +02:00
3: "^",
2020-07-10 18:04:55 +02:00
4: '"',
}
def _sub_header(match):
# add underline to convert a markdown #header to ReST
groupdict = match.groupdict()
hashes, title = groupdict["hashes"], groupdict["title"]
title = title.strip()
lvl = min(max(1, len(hashes)), 4)
return f"{title}\n" + (underline_map[lvl] * len(title))
2020-07-10 14:14:34 +02:00
doc = "\n".join(lines)
2020-07-27 21:09:13 +02:00
doc = re.sub(
r"```python\s*\n+(.*?)```", _sub_codeblock, doc, flags=re.MULTILINE + re.DOTALL
)
2020-07-10 14:14:34 +02:00
doc = re.sub(r"```", "", doc, flags=re.MULTILINE)
doc = re.sub(r"`{1}", "**", doc, flags=re.MULTILINE)
2020-07-27 21:09:13 +02:00
doc = re.sub(
r"^(?P<hashes>#{1,4})\s*?(?P<title>.*?)$", _sub_header, doc, flags=re.MULTILINE
2020-07-27 21:09:13 +02:00
)
2020-07-10 18:04:55 +02:00
2020-07-10 14:14:34 +02:00
newlines = doc.split("\n")
# we must modify lines in-place
lines[:] = newlines[:]
except Exception as err:
# if we don't print here we won't see what the error actually is
print(f"Post-process docstring exception: {err}")
raise
2020-06-18 23:30:11 +02:00
2020-06-14 11:45:10 +02:00
# Napoleon Google-style docstring parser for autodocs
napoleon_google_docstring = True
napoleon_numpy_docstring = False
napoleon_include_init_with_doc = False
napoleon_include_private_with_doc = False
napoleon_include_special_with_doc = False
2020-07-10 14:14:34 +02:00
napoleon_use_admonition_for_examples = False
2020-06-14 11:45:10 +02:00
napoleon_use_admonition_for_notes = False
napoleon_use_admonition_for_references = False
napoleon_use_ivar = False
napoleon_use_param = True
napoleon_use_keyword = True
2020-06-18 23:30:11 +02:00
napoleon_use_rtype = False
2020-06-14 11:45:10 +02:00
# -- Main config setup ------------------------------------------
# last setup steps for some plugins
2020-06-16 14:50:02 +02:00
2020-04-05 00:02:02 +02:00
def setup(app):
2020-04-05 16:50:52 +02:00
app.connect("autodoc-skip-member", autodoc_skip_member)
2020-07-10 14:14:34 +02:00
app.connect("autodoc-process-docstring", autodoc_post_process_docstring)
2021-10-21 21:04:14 +02:00
app.connect("source-read", url_resolver)
2022-11-14 21:30:05 +01:00
app.connect("html-page-context", add_legacy_versions_to_html_page_context)
# build toctree file
sys.path.insert(1, os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
2022-11-14 20:42:10 +01:00
from docs.pylib import (
auto_link_remapper,
contrib_readmes2docs,
update_default_cmd_index,
update_dynamic_pages,
)
2020-06-16 14:50:02 +02:00
_no_autodoc = os.environ.get("NOAUTODOC")
2021-10-21 21:04:14 +02:00
update_default_cmd_index.run_update(no_autodoc=_no_autodoc)
contrib_readmes2docs.readmes2docs()
update_dynamic_pages.update_dynamic_pages()
auto_link_remapper.auto_link_remapper(no_autodoc=_no_autodoc)
# custom lunr-based search
# from docs import search
# custom search
# search.setup(app)