# Builds the documentation in this directory, as markdown that GitHub serves.
#
#   make documents    regenerate everything committed here (what you want)
#   make strict       the HTML build with warnings as errors, for checking
#   make inventory    refetch the committed Python intersphinx inventory
#
# **Run this in an environment that has the project's `docs` extra installed**:
# `pip install -e '.[docs]'`. Every target but `inventory` needs `sphinx` and
# `sphinx-markdown-builder`, and `check-env` says so when they are missing,
# rather than leaving Make to report an import error naming neither.
#
# PYTHON is whatever the activated environment resolves, and can be overridden:
#   make documents PYTHON=/path/to/python
# Never a path to a particular interpreter here: this file is committed.

PYTHON        ?= python
SPHINXOPTS    ?=
SPHINXBUILD   ?= $(PYTHON) -m sphinx
SOURCEDIR     = sphinx
BUILDDIR      = _build

.PHONY: help check-env strict markdown documents inventory clean Makefile

help:
	@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

# Both packages are checked because only one of them is the obvious one: with
# `sphinx` and no `sphinx-markdown-builder`, the build fails on an unknown
# builder name several screens in.
check-env:
	@$(PYTHON) -c 'import sphinx, sphinx_markdown_builder' 2>/dev/null || { \
		echo "$(PYTHON) has no sphinx and sphinx-markdown-builder."; \
		echo "Activate the project environment, or install the extra: pip install -e '.[docs]'"; \
		echo "Or name an interpreter: make $(MAKECMDGOALS) PYTHON=/path/to/python"; \
		exit 1; }

# Warnings are errors, and every page is rebuilt.
strict: check-env
	@$(SPHINXBUILD) -M html "$(SOURCEDIR)" "$(BUILDDIR)" -W --keep-going -a -E $(SPHINXOPTS) $(O)

# The API pages as markdown, left in `$(BUILDDIR)`; `documents` moves them here.
#
# **Its own doctree cache.** `-M` gives every builder one `_build/doctrees`, and
# smartquotes are applied at parse time, so a doctree cached by the HTML build
# would carry en dashes into the markdown despite `smartquotes_excludes` in
# conf.py.
#
# **Warnings are errors**: a node type the builder does not handle is dropped
# from the output with only a warning to say so.
#
# **The output directory goes first.** `-a -E` rebuilds every page Sphinx
# renders but says nothing about one it has stopped rendering, so a removed
# module's page would otherwise be copied back in as a live one.
markdown: check-env
	@rm -rf "$(BUILDDIR)/markdown"
	@$(SPHINXBUILD) -b markdown "$(SOURCEDIR)" "$(BUILDDIR)/markdown" \
		-d "$(BUILDDIR)/doctrees-markdown" -W --keep-going -a -E $(SPHINXOPTS) $(O)

# Every committed document here, from its source. Three generators: Sphinx
# renders `reference.md` and `reference/` from the docstrings, `render_cli.py`
# renders `cli.md` from the argument parser, and `render_tools.py` renders
# `tools.md` from the built MCP server. `tests/test_reference.py`,
# `tests/test_cli_docs.py` and `tests/test_tool_docs.py` fail when a source
# changes without its document following it.
documents: check-env markdown
	@$(PYTHON) ../tools/render_reference.py "$(BUILDDIR)/markdown" .
	@$(PYTHON) ../tools/render_cli.py cli.md
	@$(PYTHON) ../tools/render_tools.py tools.md

# Refetch the committed intersphinx inventory, the only target that touches the
# network and the only thing that can move where the reference's links to the
# Python documentation point. Run `documents` after it and review the diff.
inventory:
	@curl -fsS -o "$(SOURCEDIR)/python_objects.inv" https://docs.python.org/3/objects.inv

clean:
	@rm -rf "$(BUILDDIR)"

%: Makefile
	@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
