CalculusWithJuliaNotes.jl/docs/weave-support.jl

441 lines
13 KiB
Julia
Raw Normal View History

2022-05-24 19:51:49 +02:00
## Modified from
## https://github.com/SciML/SciMLTutorials.jl/blob/master/src/SciMLTutorials.jl
2022-05-28 13:14:56 +02:00
using CalculusWithJulia
import Pluto
using Weave
using Pkg
using SHA
using TOML
2022-05-24 19:51:49 +02:00
const repo_directory = joinpath(@__DIR__,"..")
2022-05-28 13:14:56 +02:00
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)
2022-05-28 17:00:36 +02:00
file = replace(file, r"\.jmd$" => "")
2022-05-28 16:32:31 +02:00
2022-05-28 13:14:56 +02:00
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
2022-05-28 16:32:31 +02:00
function jmd_file(folder, file)
occursin(r"\.jmd$", file) || (file *= ".jmd")
joinpath(repo_directory, "CwJ", folder, file)
end
2022-05-28 13:14:56 +02:00
# should we build this file?
function build_fileq(folder, file; force=true)
force && return force
2022-05-28 16:32:31 +02:00
file = replace(file, r"\.jmd$"=>"")
2022-05-28 13:14:56 +02:00
key = join((folder, file), "_")
2022-05-28 17:00:36 +02:00
2022-05-28 13:14:56 +02:00
D = read_cache()
jmd_sha = sha(jmd_file(folder, file))
cache_sha = get(D, key, nothing)
Δ = jmd_sha != cache_sha
return Δ
end
## ----
2022-05-24 19:51:49 +02:00
2022-05-24 20:19:45 +02:00
function build_toc(force=true)
2022-05-26 01:16:51 +02:00
@info "building table of contents"
jmd_dir = joinpath(repo_directory, "CwJ", "misc")
build_dir = joinpath(@__DIR__, "build")
2022-05-26 23:37:07 +02:00
isdir(build_dir) || mkpath(build_dir)
2022-05-26 01:16:51 +02:00
file = joinpath(jmd_dir, "toc.jmd")
outfile = joinpath(build_dir, "index.html")
2022-05-28 16:32:31 +02:00
# cd(jmd_dir)
2022-05-26 01:16:51 +02:00
2022-05-28 13:14:56 +02:00
build_fileq(file, outfile, force=force) || return nothing
2022-05-26 01:16:51 +02:00
header = CalculusWithJulia.WeaveSupport.header_cmd
#footer = CalculusWithJulia.WeaveSupport.footer_cmd(bnm, folder)
2022-05-28 16:32:31 +02:00
2022-05-26 01:16:51 +02:00
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())
2022-05-24 20:19:45 +02:00
end
2022-05-28 13:14:56 +02:00
# do we build the file check mtime
# function build_file(jmdfile, outfile; force=false)
2022-05-24 20:19:45 +02:00
2022-05-28 13:14:56 +02:00
# force && return true
# !isfile(outfile) && return true
# mtime(outfile) < mtime(jmdfile) && return true
# return false
# end
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# build pluto html
# file **has** ".jmd" extension
function build_file(folder, file, force)
## use jmd -> pluto notebook -> generate_html
@info "Building HTML: $file"
2022-05-24 19:51:49 +02:00
2022-05-28 14:44:38 +02:00
occursin(r"\.jmd$", file) || (file *= ".jmd")
2022-05-28 13:14:56 +02:00
o = build_fileq(folder, file, force=force)
o || return false
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
bnm = replace(file, r"\.jmd$"=>"")
jmd_dir = joinpath(repo_directory, "CwJ", folder)
2022-05-28 16:32:31 +02:00
#cd(jmd_dir)
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
dir = joinpath(build_dir, folder)
isdir(dir) || mkpath(dir)
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
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,)
)
2022-05-28 16:32:31 +02:00
outfile = joinpath(build_dir, folder, bnm * ".html")
2022-05-28 13:14:56 +02:00
open(outfile, "w") do io
write(io, html_content)
2022-05-24 19:51:49 +02:00
end
2022-05-28 13:14:56 +02:00
write_sha(folder, file)
end
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# 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)
2022-05-24 19:51:49 +02:00
end
2022-05-28 13:14:56 +02:00
end
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# ## --------------------------------------------------
# ## 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")
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# # build list ⊂ (:script,:html,:weave_html, :pdf,:github,:notebook,:pluto)
# function weave_file(folder, file; build_list=(:html,), force=false, kwargs...)
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# 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)
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# 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
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# Pkg.activate(dirname(jmd_dir))
# Pkg.instantiate()
# args = Dict{Symbol,String}(:folder=>folder,:file=>file)
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# if :script ∈ build_list
# println("Building Script")
# dir = joinpath(repo_directory,"script",folder)
# isdir(dir) || mkpath(dir)
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# ext = ".jl"
# outfile = joinpath(dir, bnm*ext)
# build_file(file, outfile, force=force) || return nothing
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# args[:doctype] = "script"
# tangle(tmp;out_path=dir)
# end
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# # use Pluto to build html pages
# if :html ∈ build_list
# ## use jmd -> pluto notebook -> generate_html
# println("Building HTML: $file")
# cd(jmd_dir)
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# dir = joinpath(build_dir, folder)
# isdir(dir) || mkpath(dir)
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# ext = ".html"
# outfile = joinpath(dir, bnm*ext)
# o = build_file(file, outfile, force=force)
# o || return nothing
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# header = CalculusWithJulia.WeaveSupport.header_cmd
# footer = CalculusWithJulia.WeaveSupport.footer_cmd(bnm, folder)
# html_content = md2html(file,
# header_cmds=(header,),
# footer_cmds=(footer,)
# )
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# open(outfile, "w") do io
# write(io, html_content)
# end
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# end
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# ## old html generation
# if :weave_html ∈ build_list
# println("Building HTML for $file")
# dir = joinpath(build_dir, folder)
# isdir(dir) || mkpath(dir)
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# figdir = joinpath(jmd_dir,"figures")
# htmlfigdir = joinpath(dir, "figures")
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# if isdir(figdir)
# isdir(htmlfigdir) && rm(htmlfigdir, recursive=true)
# cp(figdir, htmlfigdir)
# end
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# ext = ".html"
# outfile = joinpath(dir, bnm*ext)
# build_file(file, outfile, force=force) || return nothing
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# Weave.set_chunk_defaults!(:wrap=>false)
# args[:doctype] = "html"
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# # 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, "<div class=\"well well-sm\">")
# write(io, "<div class=\"output\">")
# show(io, "text/latex", x)
# write(io, "</div>")
# end
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# function Base.show(io::IO, ::MIME"text/html", x::Array{T}) where {T <: SymPy.SymbolicObject}
# #write(io, "<div class=\"well well-sm\">")
# write(io, "<div class=\"output\">")
# show(io, "text/latex", x)
# write(io, "</div>")
# end
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# function Base.show(io::IO, ::MIME"text/html", x::T) where {T <: Polynomials.AbstractPolynomial}
# # #write(io, "<div class=\"well well-sm\">")
# write(io, "<div class=\"output\">")
# show(io, "text/latex", x)
# write(io, "</div>")
# end
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# end)
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# #weave(tmp,doctype = "md2html",out_path=dir,args=args; fig_ext=".svg", css=cssfile, kwargs...)
# weave(tmp;
# doctype = "md2html",
# out_path=dir,
# mod = 𝐦,
# args=args,
# fig_ext=".svg",
# template=htmlfile,
# fig_path=tempdir(),
# kwargs...)
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# # clean up
# isdir(htmlfigdir) && rm(htmlfigdir, recursive=true)
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# end
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# if :pdf ∈ build_list
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# eval(quote using Tectonic end) # load Tectonic; wierd testing error
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# println("Building PDF")
# dir = joinpath(repo_directory,"pdf",folder)
# isdir(dir) || mkpath(dir)
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# ext = ".pdf"
# outfile = joinpath(dir, bnm*ext)
# build_file(file, outfile, force=force) || return nothing
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# fig_path = "_figures_" * bnm
# figdir = joinpath(jmd_dir,"figures")
# texfigdir = joinpath(dir, "figures")
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# if isdir(figdir)
# isdir(texfigdir) && rm(texfigdir, recursive=true)
# cp(figdir, texfigdir)
# end
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# args[:doctype] = "pdf"
# try
# weave(tmp,doctype="md2tex",out_path=dir,args=args;
# template=latexfile,
# fig_path=fig_path,
# kwargs...)
# texfile = joinpath(dir, bnm * ".tex")
# Base.invokelatest(Tectonic.tectonic, bin -> run(`$bin $texfile`))
# # clean up
# for ext in (".tex",)
# f = joinpath(dir, bnm * ext)
# isfile(f) && rm(f)
# end
# catch ex
# @warn "PDF generation failed" exception=(ex, catch_backtrace())
# end
# isdir(texfigdir) && rm(texfigdir, recursive=true)
# isdir(joinpath(dir,fig_path)) && rm(joinpath(dir,fig_path), recursive=true)
# for ext in (".aux", ".log", ".out")
# f = joinpath(dir, bnm * ext)
# isfile(f) && rm(f)
# end
# end
# if :github ∈ build_list
# println("Building Github Markdown")
# dir = joinpath(repo_directory,"markdown",folder)
# isdir(dir) || mkpath(dir)
# ext = ".md"
# outfile = joinpath(dir, bnm*ext)
# build_file(file, outfile, force=force) || return nothing
# args[:doctype] = "github"
# weave(tmp,doctype = "github",out_path=dir, args=args;
# fig_path=tempdir(),
# kwargs...)
# end
# if :notebook ∈ build_list
# println("Building Notebook")
# dir = joinpath(repo_directory,"notebook",folder)
# isdir(dir) || mkpath(dir)
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# ext = ".ipynb"
# outfile = joinpath(dir, bnm*ext)
# build_file(file, outfile, force=force) || return nothing
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# args[:doctype] = "notebook"
# Weave.convert_doc(tmp,outfile)
# end
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# if :pluto ∈ build_list
# println("Building Pluto notebook")
# dir = joinpath(repo_directory,"pluto",folder)
# isdir(dir) || mkpath(dir)
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# ext = ".jl"
# outfile = joinpath(dir, bnm*ext)
# build_file(file, outfile, force=force) || return nothing
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# md2jl(file, outfile)
# end
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# end
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# """
# weave_all(; force=false, build_list=(:script,:html,:pdf,:github,:notebook))
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# Run `weave` on all source files.
2022-05-24 19:51:49 +02:00
2022-05-28 13:14:56 +02:00
# * `force`: by default, only run `weave` on files with `html` file older than the source file in `CwJ`
# * `build_list`: list of output types to be built. The default is all types
# The files will be built as subdirectories in the package directory. This is returned by `pathof(CalculusWithJulia)`.
# """
# function weave_all(;force=false, build_list=(:script,:html,:pdf,:github,:notebook))
# folders = readdir(joinpath(repo_directory,"CwJ"))
# folders = filter(F -> isdir(joinpath(repo_directory, "CwJ", F)), folders)
# asyncmap(F -> weave_folder(F; force=force, build_list=build_list), folders)
# # for folder in readdir(joinpath(repo_directory,"CwJ"))
# # folder == "test.jmd" && continue
# # weave_folder(folder; force=force, build_list=build_list)
# # end
# end
# function weave_folder(folder; force=false, build_list=(:html,))
# !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 -> weave_file(folder, file; force=force, build_list=build_list), files)
# # for file in readdir(joinpath(repo_directory,"CwJ",folder))
# # !occursin(r".jmd$", basename(file)) && continue
# # println("Building $(joinpath(folder,file))")
# # try
# # weave_file(folder,file; force=force, build_list=build_list)
# # catch
# # end
# # end
# end