import re
import sys
import datetime
import pypandoc

sys.path.append("{{project_root}}")

{% if debug %}
"""
    Debugging tools:
    -> pprint local variables
"""
from pprint import pprint
pprint(locals())

{% endif %}
"""
    Sphinx core settings
"""
project = "{{ project }}"
version = "{{ version }}"
author = "{{ author }}"

master_doc = "index"

exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", "venv"]

extensions = [
    "sphinx.ext.autodoc",
    "sphinx_autodoc_annotation",
    "sphinx.ext.napoleon",
    "sphinx.ext.viewcode",
    "sphinxcontrib.apidoc",
]

"""
    Sphinx autodoc settings
    -> Declares apidoc_module_dir if defined
"""
autodoc_typehints = "description"
typehints_fully_qualified = True
autodoc_default_flags = ["members", "undoc-members"]
napoleon_google_docstring = True

{% if apidoc_module_dir %}
apidoc_module_dir = "{{[project_root, apidoc_module_dir] | path_join}}"
apidoc_extra_args = ["-e"]

{% endif %}
"""
    Yummy sphinx theme settings
"""
html_theme = "yummy_sphinx_theme"
html_title = "{{html_title}}"

static_dir = "static"

html_favicon = "{{["static", "favicon.ico"] | path_join}}"
html_css_files = "{{["static", "styles.css"] | path_join}}"

html_add_permalinks = ""
html_theme_options = {
    "navbar_icon": "spin fa-book",
    "github_url": "{{github_url}}"
}

"""
    Runtime work
    -> Generates a copyright for this year
    -> Converts the project readme to HTML
"""
copyright = f"{datetime.datetime.now().year} {author}"
{% if markup_readme %}

with open("{{[project_root, readme_name] | path_join}}", "r") as stream:
    html = re.sub(
        "<h1.*>.*?</h1>",
        "",
        pypandoc.convert(stream.read(), "html", format="md"),
        flags=re.DOTALL
    )

with open("{{readme_name.replace(".md", ".html")}}", "w") as stream:
    stream.write(html)
{% endif %}