## Modified from ## https://github.com/SciML/SciMLTutorials.jl/blob/master/src/SciMLTutorials.jl using CalculusWithJulia import Pluto using Weave using Pkg using SHA using TOML const repo_directory = joinpath(@__DIR__,"..") const cache_file = joinpath(@__DIR__, "build_cache.toml") const build_dir = joinpath(@__DIR__, "build") sha(s::IO) = bytes2hex(sha256(s)) sha(path::AbstractString) = open(path, "r") do io sha(io) end read_cache() = TOML.parsefile(cache_file) write_cache(D) = open(cache_file, "w") do io for (k,v) ∈ D println(io, k, " = ", "\"$v\"") end end function write_sha(folder, file) file = replace(file, r"\.jmd$" => "") key = join((folder, file), "_") jmd_sha = sha(jmd_file(folder, file)) # may need to block! D = read_cache() D[key] = jmd_sha write_cache(D) end # build file check sha in cache function jmd_file(folder, file) occursin(r"\.jmd$", file) || (file *= ".jmd") joinpath(repo_directory, "CwJ", folder, file) end # should we build this file? function build_fileq(folder, file; force=true) force && return force file = replace(file, r"\.jmd$"=>"") key = join((folder, file), "_") D = read_cache() jmd_sha = sha(jmd_file(folder, file)) cache_sha = get(D, key, nothing) Δ = jmd_sha != cache_sha return Δ end ## ---- function build_toc(force=true) @info "building table of contents" jmd_dir = joinpath(repo_directory, "CwJ", "misc") build_dir = joinpath(@__DIR__, "build") isdir(build_dir) || mkpath(build_dir) file = joinpath(jmd_dir, "toc.jmd") outfile = joinpath(build_dir, "index.html") # cd(jmd_dir) build_fileq(file, outfile, force=force) || return nothing header = CalculusWithJulia.WeaveSupport.header_cmd #footer = CalculusWithJulia.WeaveSupport.footer_cmd(bnm, folder) html_content = md2html(file, header_cmds=(header,), footer_cmds=() ) open(outfile, "w") do io write(io, html_content) end # to use weave, not pluto # weave(file; # out_path=outfile, # doctype="md2html", # fig_ext=".svg", # template=htmlfile, # fig_path=tempdir()) end # do we build the file check mtime # function build_file(jmdfile, outfile; force=false) # force && return true # !isfile(outfile) && return true # mtime(outfile) < mtime(jmdfile) && return true # return false # end # build pluto html # file **has** ".jmd" extension function build_file(folder, file, force) ## use jmd -> pluto notebook -> generate_html @info "Building HTML: $file" occursin(r"\.jmd$", file) || (file *= ".jmd") o = build_fileq(folder, file, force=force) o || return false bnm = replace(file, r"\.jmd$"=>"") jmd_dir = joinpath(repo_directory, "CwJ", folder) #cd(jmd_dir) dir = joinpath(build_dir, folder) isdir(dir) || mkpath(dir) header = CalculusWithJulia.WeaveSupport.header_cmd footer = CalculusWithJulia.WeaveSupport.footer_cmd(bnm, folder) html_content = md2html(jmd_file(folder, file), header_cmds=(header,), footer_cmds=(footer,) ) outfile = joinpath(build_dir, folder, bnm * ".html") open(outfile, "w") do io write(io, html_content) end write_sha(folder, file) end # what to build function build_all(force) folders = readdir(joinpath(repo_directory,"CwJ")) folders = filter(F -> isdir(joinpath(repo_directory, "CwJ", F)), folders) asyncmap(F -> build_folder(F, force), folders) end function build_folder(folder, force) !isnothing(match(r"\.ico$", folder)) && return nothing files = readdir(joinpath(repo_directory,"CwJ",folder)) files = filter(f -> occursin(r".jmd$", basename(f)), files) asyncmap(file -> build_file(folder, file, force), files) end function build_pages(folder=nothing, file=nothing, target=:html, force=false) if folder == nothing build_all(force) elseif file == nothing build_folder(folder, force) else # build file in folder build_file(folder, file, force) end end # ## -------------------------------------------------- # ## Build more generally, but not use right now # const cssfile = joinpath(@__DIR__, "..", "templates", "skeleton_css.css") # const htmlfile = joinpath(@__DIR__, "..", "templates", "bootstrap.tpl") # const latexfile = joinpath(@__DIR__, "..", "templates", "julia_tex.tpl") # # build list ⊂ (:script,:html,:weave_html, :pdf,:github,:notebook,:pluto) # function weave_file(folder, file; build_list=(:html,), force=false, kwargs...) # jmd_dir = isdir(folder) ? folder : joinpath(repo_directory, "CwJ", folder) # jmd_file = joinpath(jmd_dir, file) # bnm = replace(basename(jmd_file), r".jmd$" => "") # build_dir = joinpath(@__DIR__, "build") # isdir(build_dir) || mkpath(build_dir) # if !force # #testfile = joinpath(repo_directory, "html", folder, bnm*".html") # testfile = joinpath(build_dir, folder, bnm*".html") # if isfile(testfile) && (mtime(testfile) >= mtime(tmp)) # return # end # force=true # end # Pkg.activate(dirname(jmd_dir)) # Pkg.instantiate() # args = Dict{Symbol,String}(:folder=>folder,:file=>file) # if :script ∈ build_list # println("Building Script") # dir = joinpath(repo_directory,"script",folder) # isdir(dir) || mkpath(dir) # ext = ".jl" # outfile = joinpath(dir, bnm*ext) # build_file(file, outfile, force=force) || return nothing # args[:doctype] = "script" # tangle(tmp;out_path=dir) # end # # use Pluto to build html pages # if :html ∈ build_list # ## use jmd -> pluto notebook -> generate_html # println("Building HTML: $file") # cd(jmd_dir) # dir = joinpath(build_dir, folder) # isdir(dir) || mkpath(dir) # ext = ".html" # outfile = joinpath(dir, bnm*ext) # o = build_file(file, outfile, force=force) # o || return nothing # header = CalculusWithJulia.WeaveSupport.header_cmd # footer = CalculusWithJulia.WeaveSupport.footer_cmd(bnm, folder) # html_content = md2html(file, # header_cmds=(header,), # footer_cmds=(footer,) # ) # open(outfile, "w") do io # write(io, html_content) # end # end # ## old html generation # if :weave_html ∈ build_list # println("Building HTML for $file") # dir = joinpath(build_dir, folder) # isdir(dir) || mkpath(dir) # figdir = joinpath(jmd_dir,"figures") # htmlfigdir = joinpath(dir, "figures") # if isdir(figdir) # isdir(htmlfigdir) && rm(htmlfigdir, recursive=true) # cp(figdir, htmlfigdir) # end # ext = ".html" # outfile = joinpath(dir, bnm*ext) # build_file(file, outfile, force=force) || return nothing # Weave.set_chunk_defaults!(:wrap=>false) # args[:doctype] = "html" # # override printing for Polynomials, SymPy with weave # 𝐦 = Core.eval(@__MODULE__, :(module $(gensym(:WeaveHTMLTestModule)) end)) # Core.eval(𝐦, quote # using SymPy, Polynomials # function Base.show(io::IO, ::MIME"text/html", x::T) where {T <: SymPy.SymbolicObject} # #write(io, "