"""
Configuration file for the Sphinx documentation builder:

https://www.sphinx-doc.org/

"""

# SPDX-License-Identifier: BSD-3-Clause

import os
import sys
from importlib.metadata import version as get_version

import django
from django.conf import settings

settings.configure(
    INSTALLED_APPS=[
        "django_contact_form",
    ],
    DEBUG=True,
)

django.setup()

extensions = [
    "notfound.extension",
    "sphinx.ext.autodoc",
    "sphinx.ext.intersphinx",
    "sphinx.ext.viewcode",
    "sphinxext.opengraph",
    "sphinx_copybutton",
    "sphinx_inline_tabs",
]
templates_path = ["_templates"]
source_suffix = {".rst": "restructuredtext"}
master_doc = "index"
project = "django-contact-form"
copyright = "James Bennett and contributors"
if "DEB_VERSION_UPSTREAM" in os.environ:
    version = os.environ["DEB_VERSION_UPSTREAM"]
else:
    version = get_version("django-contact-form")
release = version
exclude_trees = ["_build"]
pygments_style = "sphinx"
htmlhelp_basename = "django-contact-formdoc"
html_theme = "furo"
latex_documents = [
    (
        "index",
        "django-contact-form.tex",
        "django-contact-form Documentation",
        "James Bennett",
        "manual",
    )
]

def check_object_path(key, url, path):
    if os.path.isfile(path):
        return {key: (url, path)}
    return {}

intersphinx_mapping = {}
intersphinx_mapping.update(
    check_object_path(
        "python",
        "https://docs.python.org/{v}/".format(
            v=".".join(map(str, sys.version_info[0:2]))
        ),
        "/usr/share/doc/python{v}/html/objects.inv".format(
            v=".".join(map(str, sys.version_info[0:2]))
        )
    )
)

intersphinx_mapping.update(
    check_object_path(
        "django",
        "https://django.readthedocs.org/en/latest/",
        "/usr/share/doc/python-django-doc/html/objects.inv"
    )
)

# Spelling check needs an additional module that is not installed by default.
# Add it only if spelling check is requested so docs can be generated without it.
if "spelling" in sys.argv:
    extensions.append("sphinxcontrib.spelling")

# Spelling language.
spelling_lang = "en_US"

# Location of word list.
spelling_word_list_filename = "spelling_wordlist.txt"

# The documentation does not include contributor names, so we skip this because it's
# flaky about needing to scan commit history.
spelling_ignore_contributor_names = False

# OGP metadata configuration.
ogp_enable_meta_description = True
ogp_site_url = "https://django-contact-form.readthedocs.io/"

# Django settings for sphinxcontrib-django.
sys.path.insert(0, os.path.abspath("."))
django_settings = "docs_settings"
