Skip to content

Commit

Permalink
Docscripts (#11)
Browse files Browse the repository at this point in the history
* fixes #10 

* allow to document scripts as well

* rename testscript

* Project.toml
  • Loading branch information
j-fu authored Nov 23, 2023
1 parent aa39bb6 commit b5c78c4
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ExampleJuggler"
uuid = "3bbe58f8-ed81-4c4e-a134-03e85fcf4a1a"
authors = ["Jürgen Fuhrmann <juergen-fuhrmann@web.de>"]
version = "0.3.3"
version = "0.4"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
3 changes: 3 additions & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ function mkdocs()

example_dir = joinpath(@__DIR__, "..", "examples")

scripts = ["ExampleScript.jl"]
modules = ["ExampleModule.jl"]

notebooks = ["PlutoTemplate.jl"
"Example with Graphics" => "ExamplePluto.jl"]

cleanexamples()

script_examples = @docscripts(example_dir, scripts)
module_examples = @docmodules(example_dir, modules, Plotter=CairoMakie)
html_examples = @docplutonotebooks(example_dir, notebooks, iframe=false)
pluto_examples = @docplutonotebooks(example_dir, notebooks, iframe=true)
Expand All @@ -30,6 +32,7 @@ function mkdocs()
"Home" => "index.md",
"api.md",
"Modules" => module_examples,
"Scripts" => script_examples,
"Notebooks" => html_examples,
"Notebooks in iframe" => pluto_examples,
"mock.md",
Expand Down
5 changes: 3 additions & 2 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
## Documentation
```@docs
@docmodules
@docscripts
docplutonotebooks
@docplutonotebooks
```

## Testing
```@docs
testplutonotebooks
@testplutonotebooks
@testmodules
@testscripts
testplutonotebooks
@testplutonotebooks
```

38 changes: 38 additions & 0 deletions examples/ExampleScript.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#=
# Script example
=#

#
# Import stuff
#
using Pkg
using ExampleJuggler
import CairoMakie
using Test
#
# Run stuff
#

x, fx = mock_x()
maxfx = maximum(fx)

#
# Plot of the result
# ![](ExampleScript.svg)
#
# We can test when the script runs
@test isapprox(maxfx, 1.0; rtol = 1.0e-3)

# If we don't want the tests in the script we
# can test in the runtests function
function runtests(; kwargs...)
println("runtests from testscript")
@test isapprox(maxfx, 1.0; rtol = 1.0e-3)
end

# Here we generate plots
function generateplots(dir; kwargs...)
CairoMakie.activate!(; type = "svg", visible = false)
p = CairoMakie.lines(x, fx)
CairoMakie.save(joinpath(dir, "ExampleScript.svg"), p)
end
11 changes: 0 additions & 11 deletions examples/testscript.jl

This file was deleted.

2 changes: 1 addition & 1 deletion src/ExampleJuggler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ include("modules.jl")
export @docmodules, @testmodules

include("scripts.jl")
export @testscripts
export @docscripts, @testscripts

include("pluto.jl")
export testplutonotebooks, @testplutonotebooks, docplutonotebooks, @docplutonotebooks
Expand Down
3 changes: 2 additions & 1 deletion src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ function verbose!(v::Bool)
verbosity = v
end

const script_examples = "script_examples"
const module_examples = "module_examples"
const pluto_examples = "pluto_examples"
const plutostatichtml_examples = "plutostatichtml_examples"
const all_examples = [module_examples, pluto_examples, plutostatichtml_examples]
const all_examples = [module_examples, pluto_examples, plutostatichtml_examples, script_examples]

function cleanexamples()
for examples in all_examples
Expand Down
10 changes: 5 additions & 5 deletions src/modules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ macro plotmodule(source, kwargs...)
if isdefined(mod, :generateplots)
ExampleJuggler.verbose() && @info "generating plots for " * normpath($(source))
Base.invokelatest(getproperty(mod, :generateplots), ExampleJuggler.example_md_dir(ExampleJuggler.module_examples);
$(kwargs...))
$(kwargs...))
end))
end

Expand All @@ -50,16 +50,16 @@ end
Generate markdown files for use with documenter from list of Julia code examples via [Literate.jl](/~https://github.com/fredrikekre/Literate.jl).
See [ExampleModule.jl](@ref) for an example.
"""
function docmodules(example_dir, modules; kwargs...)
md_dir = example_md_dir(module_examples)
function docmodules(example_dir, modules; x_examples = module_examples, kwargs...)
md_dir = example_md_dir(x_examples)
modulelist = homogenize_notebooklist(modules)
modules = last.(modulelist)
example_sources = joinpath.(example_dir, modules)
example_md = String[]
for example_source in example_sources
example_base, ext = splitext(example_source)
if ext == ".jl"
cp(example_source, joinpath(example_md_dir(ExampleJuggler.module_examples), basename(example_source)))
cp(example_source, joinpath(example_md_dir(x_examples), basename(example_source)))
Literate.markdown(example_source,
md_dir;
documenter = false,
Expand All @@ -68,7 +68,7 @@ function docmodules(example_dir, modules; kwargs...)
else
@warn "$(example_source) appears to be not a Julia file, skipping"
end
push!(example_md, joinpath(module_examples, splitext(basename(example_source))[1] * ".md"))
push!(example_md, joinpath(x_examples, splitext(basename(example_source))[1] * ".md"))
end
Pair.(first.(modulelist), example_md)
end
Expand Down
33 changes: 33 additions & 0 deletions src/scripts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,36 @@ macro testscripts(example_dir, sources, kwargs...)
end
end)
end

macro plotscript(source, kwargs...)
esc(:(mod = eval(ExampleJuggler.parsescript($(source)));
if isdefined(mod, :generateplots)
ExampleJuggler.verbose() && @info "generating plots for " * normpath($(source))
Base.invokelatest(getproperty(mod, :generateplots), ExampleJuggler.example_md_dir(ExampleJuggler.script_examples);
$(kwargs...))
end))
end

macro plotscripts(example_dir, modules, kwargs...)
esc(quote
sources = last.(ExampleJuggler.homogenize_notebooklist($(modules)))
for source in sources
base, ext = splitext(source)
if ext == ".jl"
ExampleJuggler.@plotscript(joinpath($example_dir, source), $(kwargs...))
end
end
end)
end

"""
@docscripts(example_dir, modules, kwargs...)
Generate markdown files and plots for use with documenter from list of Julia modules.
Wrapper macro for [`docmodules`](@ref).
"""
macro docscripts(example_dir, modules, kwargs...)
esc(:(ExampleJuggler.@plotscripts($example_dir, $modules, $(kwargs...));
ExampleJuggler.docmodules($example_dir, $modules; x_examples = ExampleJuggler.script_examples,
$(kwargs...))))
end
8 changes: 6 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ example_dir = joinpath(@__DIR__, "..", "examples")

modules = ["ExampleModule.jl"]
notebooks = ["PlutoTemplate.jl", "ExamplePluto.jl"]
scripts = ["testscript.jl", "PlutoTemplate.jl", "ExamplePluto.jl"]
scripts = ["ExampleScript.jl"]

@testset "pluto notebooks" begin
@testplutonotebooks(example_dir, notebooks)
end

@testset "notebooks as scripts" begin
@testscripts(example_dir, notebooks)
end

@testset "module examples" begin
@testmodules(example_dir, modules, a=2)
end

@testset "scripts + notebooks" begin
@testset "scripts" begin
@testscripts(example_dir, scripts)
end

2 comments on commit b5c78c4

@j-fu
Copy link
Owner Author

@j-fu j-fu commented on b5c78c4 Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/95839

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.0 -m "<description of version>" b5c78c4445574062fb174dc591dfbc53f1928308
git push origin v0.4.0

Please sign in to comment.