# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/06_export.ipynb.

# %% auto 0
__all__ = ['default_line_filter', 'filter_out_lines', 'export_cell', 'colab_url', 'export', 'read_list_file', 'export_nbs']

# %% ../nbs/06_export.ipynb 3
import os
from pathlib import Path
from rich.markdown import Markdown
from fastcore.script import call_parse, Param, store_true

from .colab import has_colab_badge, get_colab_url
from .utils import find_nbs, read_nb, is_nb

# %% ../nbs/06_export.ipynb 8
def _export_code_cell(source):
    "Wrap code around python"
    return f"\n```python\n{source}\n```\n\n"

def default_line_filter(line):
    if "colab.research" in line:
        return False
    elif "@wandbcode" in line:
        return False
    else: 
        return True

def filter_out_lines(source, f=default_line_filter):
    "Filter lines from source with filter f"
    lines = source.split("\n")
    return "\n".join(filter(f, lines))

def _export_md_cell(source):
    source = filter_out_lines(source)
    return f"{source} \n"

# %% ../nbs/06_export.ipynb 9
def export_cell(cell, debug=False):
    "Export cell source to string"
    source = cell["source"]
    if debug:
        print(source)
    if cell["cell_type"] == "markdown":
        return _export_md_cell(source)
        
    elif cell["cell_type"] == "code":
        return  _export_code_cell(source)
    else:
        return ""

# %% ../nbs/06_export.ipynb 12
def colab_url(file):
    "Create a fresh colab URL from file, must be on github repo"
    url = get_colab_url(file, branch="master")
    return f'\n[![](https://colab.research.google.com/assets/colab-badge.svg)]({url})\n\n'

# %% ../nbs/06_export.ipynb 17
def _export_one(path, outfile, verbose=False):
    
    notebook = read_nb(path)
    
    cells = notebook["cells"]
    
    exported_cells = []
    for cell in cells:
        md_cell = export_cell(cell)
        source = cell["source"]
        if verbose:
            print(f"=================\nInput: \n {source}")
            print(f"-----------------\nMD: \n {md_cell}")
        exported_cells.append(md_cell)
    
    with open(outfile, "w") as f:
        f.writelines(exported_cells)

# %% ../nbs/06_export.ipynb 18
@call_parse
def export(
    path: Param("A path to nb files", Path, nargs="?", opt=False) = os.getcwd(),
    verbose: Param("Print errors along the way", store_true) = False,
    outfile: Param("An output file to save") = None,
):
    if not is_nb(path):
        raise Exception(f"This {path} is not a notebook!")
    
    if outfile is None:
        outfile = path.with_suffix(".md")
        print(f"Exporting notebook {path} -> {outfile}")
    
    _export_one(path, outfile, verbose)

# %% ../nbs/06_export.ipynb 20
def read_list_file(fname):
    "Reads a file into a list"
    with open(fname, "r") as f:
        lines = f.readlines()
        
    return [Path(l.replace("\n", "")) for l in lines if l is not ""]

# %% ../nbs/06_export.ipynb 21
@call_parse
def export_nbs(
    file: Param("A text file containing a list of notebooks to export", Path),
    verbose: Param("Print errors along the way", store_true) = False,
    outpath: Param("An output folder to save the exported notebooks", Path) = Path(".") ,
):
    nb_paths = read_list_file(file)
    
    for nb_path in nb_paths:
        if not is_nb(nb_path):
            raise Exception(f"This {nb_path} is not a notebook!")
        outfile = outpath/(nb_path.with_suffix(".md").name)
        print(f"Exporting notebook {nb_path} -> {outfile}")
        _export_one(nb_path, outfile, verbose=verbose)
