### A Nbdev script file ###
### Autogenerated file. Don't modify. ###

module Export
#export
using ReusePatterns

#export
import Pluto: Notebook, Cell, load_notebook

#export
function processMdChunk(mdChunk::String)
	chopMdMarker(mdChunk)
end

#export
begin
Base.@kwdef mutable struct Nucleus
	code::String=""
	parsedcode::Union{Nothing,Expr}=nothing
	end
	
code(cell::Cell) = cell.code
parsedcode(cell::Cell) = cell.parsedcode

Nucleus(code, parsedcode) = Nucleus(code = code, parsedcode = parsedcode)
end

#export
begin
mutable struct Nb
	nuclei::Array{Nucleus,1}
	name::String
end

nuclei(nuclei::Array) = nuclei
name(name::String) = name
end

#export
begin
const _header = "### A Nbdev script file ###"
const _sub_header = "### Autogenerated file. Don't modify. ###"
end

#export
function collect_nuclei(notebook::Notebook, marker::String)
	notebook_cells=notebook.cells
	nuclei=[]
	for i in 1:length(notebook_cells)
		raw_code=code(notebook_cells[i])
		parsed_code=parsedcode(notebook_cells[i])
		nucleus=Nucleus(raw_code, parsed_code)
		
		if startswith(raw_code, marker)
	        push!(nuclei,nucleus)
        end
		
	end
	nuclei
end

#export
begin
	
load_nb(filename::String, marker::String) = _load_notebook(filename, marker)
	
function _load_notebook(filename::String, marker::String)
		notebook=load_notebook(filename)
		collected_nuclei=nuclei(collect_nuclei(notebook, marker))
		Nb(collected_nuclei, filename)
end

end

#export
strip=(x,y) -> replace(x, y=>"")

#export
begin
	
function save_nb(io, notebook)
    println(io, _header)
    println(io, _sub_header)
    println(io, "")
    println(io, "module $(uppercasefirst(strip(strip(notebook.name, r"[0-9_]"), r".jl")))")
	notebook
	print(io, "end")	
end

function save_nb(notebook::Nb, path::String)
	open(path, "w") do io
        save_nb(io, notebook)
    end
end
end

#export
begin
function readfilenames()
	files=readdir()
	files
end

function readfilenames(dir::String)
	files=cd(readdir, dir)
	files
end
end

#export
function export_Code(file::String, code_cells)
	io=IO
    open(file, "w") do io
		for code in code_cells
            print(io, code*"\n\n")
        end
	end
end

#export
begin
function export_file(file, path::String, marker::String)
	    code_cells=collect_codecells(file, marker)
		file_path=joinpath(path,file)
        export_Code(file_path, code_cells)
end
end

#export
function export_content(files::AbstractVector, path::String, marker::String)
	map(file->export_file(file, path, marker), files)
end

#export
function getfile_extension(filename)
    return filename[findlast(isequal('.'),filename):end]
end

#export
#TODO: Too many loops. Need to clear this either 
#by broadcasting or something more efficient
#TODO: User should eb able to get messages about which
#files are updated
#TODO: currently output inside loop is not shown in pluto notebooks.
#so, the show_output displays all stuffs in the console.
#maybe a progress bar would be better
function notebook2script()
	files=[file for file in readfilenames() if getfile_extension(file)== ".jl"]
	export_content(files, "../src", "#export")
end

end