#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Custom Jupyter NBConvert Exporter for the OCaml language.

See http://nbconvert.readthedocs.io/en/latest/external_exporters.html#writing-a-custom-exporter if needed.

- MIT Licensed, (C) 2017-2020 Lilian Besson (Naereen)
  https://GitHub.com/Naereen/Jupyter-NBConvert-OCaml
"""

import os
import os.path

from traitlets import default
from traitlets.config import Config
from nbconvert.exporters.templateexporter import default_filters
from nbconvert.exporters.templateexporter import TemplateExporter


def comment_lines_ocaml(text, start='(* ', end=' *)'):
    """
    Build an OCaml comment line from input text.

    Parameters
    ----------
    text : str
        Text to comment out.
    start : str
        Character to open the multi-line comment.
    end : str
        Character to close the multi-line comment.
    """
    return start + ('\n').join(text.split('\n')) + end


def add_endsemicolon_ocaml(text, semicolon=';;'):
    """
    Build an OCaml indenpendent cell by adding a closing ';;' at the end.

    Parameters
    ----------
    text : str
        Text to comment out.
    semicolon : str
        By default ';;'.
    """
    if not text.endswith(semicolon):
        return text + semicolon
    else:
        return text


my_filters = {
    'comment_lines_ocaml': comment_lines_ocaml,
    'add_endsemicolon_ocaml': add_endsemicolon_ocaml,
}

default_filters.update(my_filters)


# The class

class OCamlExporter(TemplateExporter):
    """
    Custom Jupyter NBConvert Exporter for the OCaml language.
    """

    # If this custom exporter should add an entry to the
    # "File -> Download as" menu in the notebook, give it a name here in the
    # `export_from_notebook` class member
    export_from_notebook = "OCaml (.ml)"

    @default('file_extension')
    def _file_extension_default(self):
        """
        The new file extension is `.ml`
        """
        return '.ml'

    output_mimetype = 'text/x-ocaml'

    @property
    def template_path(self):
        """
        We want to inherit from script template, and have template under
        `./templates/` so append it to the search path. (see next section)
        """
        return super().template_path + [os.path.join(os.path.dirname(__file__), "templates")]


    @property
    def template_paths(self):
        """
        We want to inherit from script template, and have template under
        `./templates/` so append it to the search path. (see next section)

        Note: nbconvert 6.0 changed ``template_path`` to ``template_paths``
        """
        return super().template_paths + [os.path.join(os.path.dirname(__file__), "templates")]


    @default('template_file')
    def _template_file_default(self):
        """
        We want to use the new template we ship with our library.
        """
        return "ocaml_template"

    def default_filters(self):
        return default_filters.items()