# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

import os
from subprocess import DEVNULL, CalledProcessError, check_output

import decomon

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
project = "decomon"
copyright = "2022, Airbus"
author = "Airbus"

DEV_VERSION_PATTERN = "dev"
GIT_SYMREF_CMD = ["git", "symbolic-ref", "--short", "HEAD"]

version = decomon.__version__
if DEV_VERSION_PATTERN in version:
    # not a release but rather dev doc => we try to use the current branch name instead of version 0.0.0
    try:
        version = check_output(GIT_SYMREF_CMD, stderr=DEVNULL, encoding="utf-8").strip()
    except CalledProcessError:
        pass
release = version

master_doc = "index"
language = "en"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = [
    "myst_parser",
    "sphinx.ext.autodoc",
    "sphinx.ext.napoleon",
    "sphinx.ext.autosummary",
    "sphinx.ext.viewcode",
]

templates_path = ["_templates"]
exclude_patterns = ["*.template.*"]

# myST configuration
myst_enable_extensions = [
    "dollarmath",
    "amsmath",
    "deflist",
    "fieldlist",
    "html_admonition",
    "html_image",
    "colon_fence",
    "smartquotes",
    "replacements",
    "strikethrough",
    "substitution",
    "tasklist",
]
myst_number_code_blocks = ["typescript"]
myst_heading_anchors = 3  # up to h3 titles will have autogenerated anchors
myst_footnote_transition = True
myst_dmath_double_inline = True


# autosummary config
autosummary_generate = True

# napoleon settings
napoleon_google_docstring = True
napoleon_numpy_docstring = False


# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = "sphinx_rtd_theme"
html_static_path = ["_static"]
html_show_sourcelink = False
html_theme_options = {
    "style_external_links": True,
}

html_js_files = ["versions.js"]
AUTODOC_SHOW_VERSIONS = os.environ.get("AUTODOC_SHOW_VERSIONS", "True").lower() != "false"
html_context = {
    "AUTODOC_SHOW_VERSIONS": AUTODOC_SHOW_VERSIONS,
    "current_version": version,
}
