import re
import sys
import datetime
import pypandoc

sys.path.append("{{reverse_to_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"

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

html_title = "{{html_title}}"
html_favicon = "{{["static", "favicon.ico"] | path_join}}"

html_static_path = ["static"]
html_css_files = ["quickdocs.css"]

html_permalinks = False
html_add_permalinks = None

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

"""
    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 = "{{[reverse_to_root, apidoc_module_dir] | path_join}}"
apidoc_extra_args = ["-e"]

{% endif %}
"""
    Yummy sphinx theme settings
"""
html_theme = "yummy_sphinx_theme"
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("{{[reverse_to_root, readme_path] | path_join}}", "r") as stream:
    html_readme = pypandoc.convert(
        stream.read(),
        "html",
        format="md",
        extra_args=["-s", "-fmarkdown-implicit_figures"]
    )
    
    headerless_readme = re.sub("<h1.*>.*?</h1>", "", html_readme, flags=re.DOTALL)

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