# -*- coding: utf-8 -*-
#
# NiftyNet documentation build configuration file, created by
# sphinx-quickstart on Wed Aug 30 14:13:50 2017.
#
# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.

import subprocess
import os
import sys
from recommonmark.parser import CommonMarkParser
#import pip

# sphinx readable theme
# pip.main(['install', 'sphinx-readable-theme'])
# import sphinx_readable_theme as sp_theme

# alternatively sphinx nameko theme
# pip.main(['install', 'sphinx-nameko-theme'])
import sphinx_nameko_theme as sp_theme

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# sys.path.insert(0, os.path.abspath('.'))

working_dir = os.path.abspath(os.path.dirname(__file__))
root_dir_rel = os.path.join('..', '..')
root_dir_abs = os.path.abspath(root_dir_rel)
module_path = root_dir_abs
sys.path.insert(0, module_path)
logo_file = 'niftynet-logo.png'
static_images_folder = 'images'
logo_path = os.path.join('..', '..', logo_file)

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This patterns also effect to html_static_path and html_extra_path
exclude_patterns = [
    'tests',
    'run_*',
    'net_*',
    'setup.py',
]

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
static_folder = '_static'
html_static_path = [static_folder]


def generate_apidocs(*args):
    """Generate API docs automatically by trawling the available modules"""
    global working_dir, module_path
    output_path = working_dir
    apidoc_command_path = 'sphinx-apidoc'
    if hasattr(sys, 'real_prefix'):  # called from a virtualenv
        apidoc_command_path = os.path.join(sys.prefix, 'bin', 'sphinx-apidoc')
        apidoc_command_path = os.path.abspath(apidoc_command_path)
    subprocess.check_call(
        [apidoc_command_path, '-f', '--separate'] +
        ['-o', output_path, module_path] +
        [os.path.join(root_dir_abs, pattern) for pattern in exclude_patterns])


def setup(app):
    # Hook to allow for automatic generation of API docs
    # before doc deployment begins.
    app.connect('builder-inited', generate_apidocs)

# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.imgmath',
    'sphinx.ext.napoleon',
    'sphinx.ext.intersphinx',
    'sphinx.ext.viewcode',
    'sphinx_markdown_tables',
]
autodoc_default_flags = ['members', 'show-inheritance']
autodoc_member_order = 'bysource'

# Don't display module names before objects titles, it's more readable.
add_module_names = False

# napoleon config ------------
napoleon_google_docstring = True
napoleon_include_init_with_doc = True
# -------------------------

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
source_parsers = {
    # '.md': CommonMarkParser
    '.md': 'recommonmark.parser.CommonMarkParser',
}
source_suffix = ['.rst', '.md']
#source_suffix = '.rst'

# The master toctree document.
master_doc = 'index'

# This is for instance for niftynet.engine to be shown under the letter 'e'
# rather than 'n'. That way the Python module index is displayed more nicely.
modindex_common_prefix = [
    'niftynet.'
]

# General information about the project.
project = u'NiftyNet'
copyright = u'2018, the NiftyNet Consortium'
author = u'the NiftyNet Consortium'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
from niftynet.utilities.versioning import get_niftynet_version
try:
    # The short X.Y version.
    version = get_niftynet_version().split('+')[0]
except (IndexError, AttributeError):
    version = u''
# The full version, including alpha/beta/rc tags.
release = version

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = False


# -- Options for HTML output ----------------------------------------------

## The theme to use for HTML and HTML Help pages.  See the documentation for
## a list of builtin themes.
##
#html_theme = 'classic'
#
## Theme options are theme-specific and customize the look and feel of a theme
## further.  For a list of options available for each theme, see the
## documentation.
##
nn_black = '#0A0A0A'
nn_red = '#CC0000'
nn_gray = '#DDDDDD'
nn_dark_red = '#AE1111'
nn_white = '#FEFEFE'
#html_theme_options = {
#    'footerbgcolor': nn_gray,
#    'footertextcolor': nn_black,
#    'sidebarbgcolor': nn_white,
#    'sidebartextcolor': nn_black,
#    'sidebarlinkcolor': nn_red,
#    'relbarbgcolor': nn_white,
#    'relbartextcolor': nn_black,
#    'relbarlinkcolor': nn_red,
#    'bgcolor': nn_white,
#    'textcolor': nn_black,
#    'linkcolor': nn_red,
#    'visitedlinkcolor': nn_dark_red,
#    'headbgcolor': nn_white,
#    'headtextcolor': nn_black,
#    'headlinkcolor': nn_red,
#    'codebgcolor': nn_gray,
#    'codetextcolor': nn_black,
#    'stickysidebar': 'true',
#}
#
html_logo = logo_path
html_theme_path = [sp_theme.get_html_theme_path()]
#html_theme = 'readable'
html_theme = 'nameko'
html_sidebars = {
   '**': ['badge.html', 'localtoc.html', 'globaltoc.html', 'searchbox.html']
}

# -- Options for HTMLHelp output ------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = 'NiftyNetdoc'


# -- Options for LaTeX output ---------------------------------------------

latex_elements = {
    # The paper size ('letterpaper' or 'a4paper').
    #
    # 'papersize': 'letterpaper',

    # The font size ('10pt', '11pt' or '12pt').
    #
    # 'pointsize': '10pt',

    # Additional stuff for the LaTeX preamble.
    #
    # 'preamble': '',

    # Latex figure (float) alignment
    #
    # 'figure_align': 'htbp',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
    (master_doc, 'NiftyNet.tex', u'NiftyNet Documentation',
     u'NiftyNet Consortium', 'manual'),
]


# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
    (master_doc, 'niftynet', u'NiftyNet Documentation',
     [author], 1)
]


# -- Options for Texinfo output -------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
    (master_doc, 'NiftyNet', u'NiftyNet Documentation',
     author, 'NiftyNet', 'One line description of project.',
     'Miscellaneous'),
]
