This commit is contained in:
jverzani
2022-05-24 13:51:49 -04:00
parent 5c0fd1b6fe
commit 244f492f9e
240 changed files with 65211 additions and 0 deletions

2
docs/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
build/
site/

6
docs/Project.toml Normal file
View File

@@ -0,0 +1,6 @@
[deps]
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
CalculusWithJulia = "a2e0e22d-7d4c-5312-9169-8b992201a882"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Pluto = "c3e4b0f8-55cb-11ea-2926-15256bba5781"
Weave = "44d3d7a6-8a23-5bf8-98c5-b353f8df5ec9"

1
docs/build-pages.jl Normal file
View File

@@ -0,0 +1 @@
# build_pages

93
docs/make.jl Normal file
View File

@@ -0,0 +1,93 @@
using Documenter
using ArgParse
using CalculusWithJuliaNotes
using CalculusWithJulia
using Weave
using Pkg
import Markdown
import Pluto
include("weave-support.jl")
include("markdown-to-pluto.jl")
## The command line gathers
## folder[=nothing], file[=nothing], target[=html], force[=false]
function parse_commandline()
s = ArgParseSettings()
@add_arg_table! s begin
"--folder", "-F"
help = "Input folder name"
default = nothing
"--file", "-f"
help = "Input file name"
default = nothing
"--target", "-o"
help="target type: html, weave_html, ipynb, "
default = "html"
"--force"
help = "Force compliation"
default = "false"
end
return parse_args(s)
end
# what to build
function build_pages(folder=nothing, file=nothing, target=:html, force=false)
build_list = (target, )
if folder != nothing && file !=nothing
weave_file(folder, file * ".jmd", build_list=build_list, force=force)
elseif folder != nothing
if folder == "all"
weave_all(; build_list=build_list, force=all)
else
weave_folder(folder, build_list = build_list, force=force)
end
elseif force
weave_all(; build_list=build_list, force=true)
end
end
# build pages
d = parse_commandline()
folder, file = d["folder"], d["file"]
target = Symbol(d["target"])
force = parse(Bool, d["force"])
if isnothing(folder) && isnothing(file)
# # build full thing
# for folder ∈ ("precalc", "limits", "derivatives", "integrals", "ODEs",
# "differentiable_vector_calculus", "integral_vector_calculus")
# build_pages(folder, nothing, target, force)
# end
# # others need to integrate with Pluto
# for folder ∈ ("alternatives", "misc")
# build_pages(folder, nothing, "weave_html", force)
# end
build_pages("precalc", "functions", "html", true)
build_pages("misc", nothing, "weave_html", true)
toc = joinpath("build", "misc", "toc.html")
cp(toc, joinpath("build","index.html"), force=true)
else
build_pages(folder, file, target, force)
end
# Documenter can also automatically deploy documentation to gh-pages.
# See "Hosting Documentation" and deploydocs() in the Documenter manual
# for more information.
# Documenter can also automatically deploy documentation to gh-pages.
# See "Hosting Documentation" and deploydocs() in the Documenter manual
# for more information.
#=
Documenter.deploydocs(
repo = "github.com/jverzani/CalculusWithJuliaNotes.jl"
)
=#

138
docs/markdown-to-pluto.jl Normal file
View File

@@ -0,0 +1,138 @@
# causes issues
function Markdown.plain(io::IO, l::Markdown.LaTeX)
println(io, "```math")
println(io, l.formula)
println(io, "```")
end
# check if a string represents a single command
function issinglecommand(ex)
try
Meta.parse(ex)
true
catch err
false
end
end
"""
chunk_to_cell(::Val{:Code}, chunk)
Triple-braced code is treated as a code to be executed, though we adjust based on "langs".
* `hold=true` or `local` -- wrap in a `let` block, so variable names don't escape. Otherwise, a `begin` block is added for multiline chunks. (Had to leverage a Weave chunk option!)
* `echo=false` or `nocode` to hide cell using `code_folded`.
* `eval=false` or `noeval` or `verbatim` to put in an MD cell
* `term=true` wrap in PlutoUI `with_terminal`
"""
function chunk_to_cell(::Val{:Code}, chunk, args...)
txt = chunk.code
txt = rstrip(lstrip(txt, '\n'), '\n')
lang = chunk.language
lang = replace(lang, ";" => ",")
langs = map(lstrip, split(lang, ","))
langs = replace.(langs, r" "=>"")
# signal through langs
if "eval=false" langs || "noeval" langs || "verbatim" langs
cell = Pluto.Cell("md\"\"\"```\n$(txt)\n```\"\"\"")
cell.code_folded = true
return cell
end
if "term=true" langs
txt = replace(txt, "\n" => "\n\t")
txt = "with_terminal() do\n\t$(txt)\nend"
end
block_type = ("local" langs || "hold=true" langs) ? "let" : "begin"
#multiline = contains(txt, "\n")
multicommand = !issinglecommand(txt)
if multicommand || block_type == "let"
txt = replace(txt, "\n" => "\n\t")
txt = "$(block_type)\n\t$(txt)\nend"
end
cell = Pluto.Cell(txt)
if ("echo=false" langs || "nocode" langs)
cell.code_folded = true
end
return cell
end
function chunk_to_cell(::Val{:Markdown}, chunk, args...)
txt = sprint(io -> Markdown.plain(io, chunk))
cell = Pluto.Cell("md\"\"\"$(txt)\"\"\"")
cell.code_folded = true
return cell
end
function process_content(content)
T = isa(content, Markdown.Code) ? Val(:Code) : Val(:Markdown)
return chunk_to_cell(T, content)
end
function md2notebook(fname; header_cmds=(), footer_cmds=())
out = Markdown.parse_file(fname, flavor=Markdown.julia)
cells = process_content.(out.content)
for cmd reverse(header_cmds)
cell = Pluto.Cell(cmd)
cell.code_folded = true
pushfirst!(cells, cell)
end
# html"""
# <script src="https://utteranc.es/client.js"
# repo="jverzani/CalculusWithJulia.jl"
# issue-term="pathname"
# label="comment"
# theme="github-light"
# crossorigin="anonymous"
# async>
# </script>
# """)
footer_cmds = vcat(footer_cmds...,
"using PlutoUI",
"PlutoUI.TableOfContents()",
"html\"\"\"<script src=\"https://utteranc.es/client.js\" repo=\"jverzani/CalculusWithJulia.jl\" issue-term=\"pathname\" theme=\"github-light\" crossorigin=\"anonymous\" async> </script>\"\"\""
)
for cmd footer_cmds
cell = Pluto.Cell(cmd)
cell.code_folded = true
push!(cells, cell)
end
notebook = Pluto.Notebook(cells)
end
function nb2html(notebook, session=Pluto.ServerSession())
Pluto.update_run!(session, notebook, [last(e) for e in notebook.cells_dict])
Pluto.generate_html(notebook)
end
# save notebook to path
function nb2jl(notebook, path)
Pluto.save_notebook(notebook, path);
end
function md2html(fname; kwargs...)
nb2html(md2notebook(fname; kwargs...))
end
function md2jl(fname, path; kwargs...)
nb2jl(md2notebook(fname; kwargs...), path)
end

3
docs/src/index.md Normal file
View File

@@ -0,0 +1,3 @@
# CalculusWithJuliaNotes.jl
Documentation for CalculusWithJuliaNotes.jl

273
docs/weave-support.jl Normal file
View File

@@ -0,0 +1,273 @@
## Modified from
## https://github.com/SciML/SciMLTutorials.jl/blob/master/src/SciMLTutorials.jl
const repo_directory = joinpath(@__DIR__,"..")
const cssfile = joinpath(@__DIR__, "..", "templates", "skeleton_css.css")
const htmlfile = joinpath(@__DIR__, "..", "templates", "bootstrap.tpl")
const latexfile = joinpath(@__DIR__, "..", "templates", "julia_tex.tpl")
# do we build the file?
function build_file(jmdfile, outfile; force=false)
force && return true
!isfile(outfile) && return true
mtime(outfile) < mtime(jmdfile) && return true
return false
end
# 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(repo_directory, "docs", "build")
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, "<div class=\"well well-sm\">")
write(io, "<div class=\"output\">")
show(io, "text/latex", x)
write(io, "</div>")
end
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
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
end)
#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...)
# clean up
isdir(htmlfigdir) && rm(htmlfigdir, recursive=true)
end
if :pdf build_list
eval(quote using Tectonic end) # load Tectonic; wierd testing error
println("Building PDF")
dir = joinpath(repo_directory,"pdf",folder)
isdir(dir) || mkpath(dir)
ext = ".pdf"
outfile = joinpath(dir, bnm*ext)
build_file(file, outfile, force=force) || return nothing
fig_path = "_figures_" * bnm
figdir = joinpath(jmd_dir,"figures")
texfigdir = joinpath(dir, "figures")
if isdir(figdir)
isdir(texfigdir) && rm(texfigdir, recursive=true)
cp(figdir, texfigdir)
end
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)
ext = ".ipynb"
outfile = joinpath(dir, bnm*ext)
build_file(file, outfile, force=force) || return nothing
args[:doctype] = "notebook"
Weave.convert_doc(tmp,outfile)
end
if :pluto build_list
println("Building Pluto notebook")
dir = joinpath(repo_directory,"pluto",folder)
isdir(dir) || mkpath(dir)
ext = ".jl"
outfile = joinpath(dir, bnm*ext)
build_file(file, outfile, force=force) || return nothing
md2jl(file, outfile)
end
end
"""
weave_all(; force=false, build_list=(:script,:html,:pdf,:github,:notebook))
Run `weave` on all source files.
* `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))
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
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