# To build the documentation:
#    - julia --project=docs docs/make.jl
#    - empty!(ARGS); include("make.jl")
# To build the documentation without running the tests:
#    - julia --project="." make.jl preview
#    - push!(ARGS,"preview"); include("make.jl")

using
    Documenter,
    Literate,
    LWFBrook90,
    Dates


#### code pertaining to Literate.jl
if haskey(ENV, "GITHUB_ACTIONS")
    ### From Literate.jl (https://github.com/fredrikekre/Literate.jl/blob/master/docs/make.jl):
    ENV["JULIA_DEBUG"] = "Documenter"
    ENV["GKSwstype"] = "100" # following https://github.com/JuliaPlots/Plots.jl/issues/1182
end

deployconfig = Documenter.auto_detect_deploy_system()
Documenter.post_status(deployconfig; type="pending", repo="github.com/fabern/LWFBrook90.jl")

# a) define preprocessor for Literate.jl
function update_date(content)
    content = replace(content, "DATEOFTODAY" => Date(now()))
    return content
end

# b) generate examples
# For Literate.jl use following formatting in the files (first # is removed):
    # # A markdown H1 title
    # A non-code markdown normal line
    ## A comment within the code chunk
OUTPUT_markdown_for_Docs = joinpath(@__DIR__, "src/generated")
# OUTPUT_scripts_for_examp = joinpath(@__DIR__, "src/generated")

INPUT_example_script_01 = joinpath(@__DIR__, "../examples/example-script-01.jl")
Literate.markdown(INPUT_example_script_01, OUTPUT_markdown_for_Docs;   name = "example-script-01", documenter = true, preprocess = update_date)
# Literate.script(  INPUT_example_script_01, OUTPUT_scripts_for_examp;   name = "example-script-01", documenter = true, preprocess = update_date, keep_comments = true)
# Literate.notebook(INPUT_example_script_01, OUTPUT_scripts_for_examp;   name = "example-script-01", documenter = true, preprocess = update_date) # TODO: reactivate
#### end code for Literate.jl


### code pertaining to Documenter.jl:
DocMeta.setdocmeta!(LWFBrook90, :DocTestSetup, :(using LWFBrook90); recursive=true)

makedocs(;
    modules=[LWFBrook90],
    authors="Fabian Bernhard",
    repo="https://github.com/fabern/LWFBrook90.jl/blob/{commit}{path}#L{line}",
    sitename="LWFBrook90.jl",
    format=Documenter.HTML(;
        prettyurls=get(ENV, "CI", "false") == "true",
        canonical="https://fabern.github.io/LWFBrook90.jl",
        assets=String[],
        size_threshold = 2000 * 2^10 #200 * 2^10, set to 2000 KiB instead of 200 KiB
    ),
    # workdir = joinpath(@__DIR__, ".."),
    pages=[
        "About"        => "index.md",
        "SVAT Model"   => "model.md",
        "User Guide"   => "user-guide.md",
        "Example-01 (autogenerated)"  => "generated/example-script-01.md",
        "Validation"   => "validation.md",
        "Code Listing" => "code-lst.md",
        "Function Documentations" => "function-docs.md",
    ],
)

deploydocs(;
    repo="github.com/fabern/LWFBrook90.jl",
    devbranch = "develop",
    devurl    = "dev",
    branch    = "gh-pages",
    versions = ["stable" => "v^", "v#.#.#", "dev" => "dev"],
)
#### end code for Documenter.jl
