Skip to content

Commit

Permalink
tadaa
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Feb 8, 2024
1 parent f68aef2 commit cefe241
Show file tree
Hide file tree
Showing 24 changed files with 2,142 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/Manifest.toml
.DS_Store
10 changes: 8 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "PlutoPages"
uuid = "d5dc3dd1-4774-47c7-8860-0a1ad9e34b8c"
authors = ["Luca Ferranti and Fons van Der Plas"]
authors = ["Luca Ferranti", "Fons van Der Plas"]
version = "0.1.0"

[deps]
Expand All @@ -9,14 +9,17 @@ CommonMark = "a80b9123-70ca-4bc0-993e-6e3bcb318db6"
Gumbo = "708ec375-b3d6-5a57-a7ce-8257bf98657a"
HypertextLiteral = "ac1192a8-f4b3-4bfe-ba22-af5b92cd3ab2"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
LiveServer = "16fef848-5104-11e9-1b77-fb7a48bbb589"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
MarkdownLiteral = "736d6165-7244-6769-4267-6b50796e6954"
Pluto = "c3e4b0f8-55cb-11ea-2926-15256bba5781"
PlutoHooks = "0ff47ea0-7a50-410d-8455-4348d5de0774"
PlutoLinks = "0ff47ea0-7a50-410d-8455-4348d5de0420"
PlutoSliderServer = "2fc8631c-6f24-4c5b-bca7-cbb509c42db4"
PlutoUI = "7f904dfe-b85e-4ff6-b463-dae2292396a8"
ProgressLogging = "33c8b6b6-d38a-422a-b730-caa89a2f386c"
RelocatableFolders = "05181044-ff0b-4ac5-8273-598c1e38db00"
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
ThreadsX = "ac1d9e8a-700a-412c-b207-f0111f4b6c0d"
Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
Expand All @@ -28,14 +31,17 @@ CommonMark = "0.8"
Gumbo = "0.8"
HypertextLiteral = "0.9"
InteractiveUtils = "1"
LiveServer = "1.2"
Logging = "1"
Markdown = "1"
MarkdownLiteral = "0.1"
Pluto = "0.19.36"
Pluto = "0.19.38"
PlutoHooks = "0.0.5"
PlutoLinks = "0.1.6"
PlutoSliderServer = "0.3.28"
PlutoUI = "0.7.55"
ProgressLogging = "0.1"
RelocatableFolders = "1"
SHA = "0.7"
ThreadsX = "0.1"
Unicode = "1"
Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,33 @@ This repository will contain "PlutoPages", the site generation system that power
Currently this code is in https://github.com/mitmath/computational-thinking/blob/Fall23/PlutoPages.jl

Contact https://github.com/LucaFerranti for more info!


## During development
Use `PlutoPages.develop` to start developing your website. It will launch two browser tabs: one with the PlutoPages development dashboard, and one with a preview of your website.

When you make edits to the website source files, they should get detected automatically, and the site is regenerated. If changes are not detected, go to the PlutoPages dashboard and click "Read input files again".

```julia
import PlutoPages

# replace this with the path of your own website
my_site_source = PlutoPages.create_test_basic_site()

PlutoPages.develop(my_site_source)
```


## Generating the site
Use `PlutoPages.generate` if you want to generate your website once, without a development server.



```julia
import PlutoPages

# replace this with the path of your own website
my_site_source = PlutoPages.create_test_basic_site()

output_dir = PlutoPages.generate(my_site_source)
```
143 changes: 142 additions & 1 deletion src/PlutoPages.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,146 @@
module PlutoPages

include("plutopages.jl")
import Pluto
using RelocatableFolders
import LiveServer

include("./pluto control.jl")
include("./open in browser.jl")


# """
# Generate
# """
# function generate()



# end

const PlutoPages_notebook_path = @path joinpath(dirname(@__DIR__), "src", "notebook.jl")


function run_plutopages_notebook(;
input_dir::String,
output_dir::String,
cache_dir::String,
kwargs...
)
mkpath(output_dir)
mkpath(cache_dir)
run_with_replacements(
PlutoPages_notebook_path,
Dict(
:input_dir => input_dir,
:output_dir => output_dir,
:cache_dir => cache_dir,
);
kwargs...
)
end



function create_subdirs(root_dir::String)
@assert(isdir(root_dir))
@assert(isdir(joinpath(root_dir, "src")))

(;
input_dir = joinpath(root_dir, "src"),
output_dir = joinpath(root_dir, "_site"),
cache_dir = joinpath(root_dir, "_cache"),
)
end

function develop(root_dir::String)
develop(;create_subdirs(root_dir)...)
end



const isolated_cell_ids = (
"cf27b3d3-1689-4b3a-a8fe-3ad639eb2f82",
"7f7f1981-978d-4861-b840-71ab611faf74",
"7d9cb939-da6b-4961-9584-a905ad453b5d",
"4e88cf07-8d85-4327-b310-6c71ba951bba",
"079a6399-50eb-4dee-a36d-b3dcb81c8456",
"b0006e61-b037-41ed-a3e4-9962d15584c4",
"06edb2d7-325f-4f80-8c55-dc01c7783054",
"e0a25f24-a7de-4eac-9f88-cb7632de09eb",
)
const isolated_cell_query = join("&isolated_cell_id=$(i)" for i in isolated_cell_ids)


function develop(;
input_dir::String,
output_dir::String,
cache_dir::String,
)
app = run_plutopages_notebook(; input_dir, output_dir, cache_dir, run_server=true)

notebook = fetch(app.notebook_task)

ccall(:jl_exit_on_sigint, Cvoid, (Cint,), 0)
@info "PlutoPages: Press Ctrl+C multiple times to stop the server."
file_server_port = rand(8100:8900)

file_server_task = Threads.@spawn LiveServer.serve(port=file_server_port, dir=output_dir)

sleep(2)

dev_server_url = "http://localhost:$(file_server_port)/"
pluto_server_url = "http://localhost:$(app.pluto_server_port)/edit?secret=$(app.session.secret)&id=$(notebook.notebook_id)$(isolated_cell_query)"


@info """
✅✅✅
Ready! To see the website, visit:
➡️ $(dev_server_url)
To inspect the generation process, go to:
➡️ $(pluto_server_url)
✅✅✅
"""

open_in_default_browser(dev_server_url)
open_in_default_browser(pluto_server_url)

wait(file_server_task)
wait(app.pluto_server_instance)
app
end




function generate(;
input_dir::String,
output_dir::String,
cache_dir::String,
)
app = run_plutopages_notebook(; input_dir, output_dir, cache_dir, run_server=false)
fetch(app.notebook_task)
@info "PlutoPages: cleaning up..."
shutdown(app)

return output_dir
end

generate(root_dir::String) = generate(;create_subdirs(root_dir)...)


function create_test_basic_site()
original = joinpath(dirname(@__DIR__), "test", "basic_site")
new_dir = mktempdir()
cp(original, new_dir; force=true)
new_dir
end




end
18 changes: 9 additions & 9 deletions src/notebook.jl
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ md"""
"""

# ╔═╡ c52c9786-a25f-11ec-1fdc-9b13922d7ccb
const dir = joinpath(@__DIR__, "src")
const input_dir = joinpath(@__DIR__, "src")

# ╔═╡ cf27b3d3-1689-4b3a-a8fe-3ad639eb2f82
md"""
Expand All @@ -409,7 +409,7 @@ const this_file = split(@__FILE__, "#==#")[1]

# ╔═╡ d38dc2aa-d5ba-4cf7-9f9e-c4e4611a57ac
function ignore(abs_path; allow_special_dirs::Bool=false)
p = relpath(abs_path, dir)
p = relpath(abs_path, input_dir)

# (_cache, _site, _andmore)
any(x -> ignored_dirname(x; allow_special_dirs), splitpath(p)) ||
Expand All @@ -424,8 +424,8 @@ dir_changed_time = let

@info "Starting watch task"

@use_task([dir]) do
BetterFileWatching.watch_folder(dir) do e
@use_task([input_dir]) do
BetterFileWatching.watch_folder(input_dir) do e
@debug "File event" e
try
is_caused_by_me = all(x -> ignore(x; allow_special_dirs=true), e.paths)
Expand All @@ -444,12 +444,12 @@ dir_changed_time = let
end

# ╔═╡ 7d9cb939-da6b-4961-9584-a905ad453b5d
allfiles = filter(PlutoSliderServer.list_files_recursive(dir)) do p
allfiles = filter(PlutoSliderServer.list_files_recursive(input_dir)) do p
# reference to retrigger when files change
dir_changed_time
manual_update_trigger

!ignore(joinpath(dir, p))
!ignore(joinpath(input_dir, p))
end

# ╔═╡ d314ab46-b866-44c6-bfca-9a413bc06514
Expand Down Expand Up @@ -737,7 +737,7 @@ template_results = let

# let's go! running all the template handlers
progressmap_async(allfiles; ntasks=NUM_PARALLEL_WORKERS) do f
absolute_path = joinpath(dir, f)
absolute_path = joinpath(input_dir, f)

input = TemplateInput(;
contents=read(absolute_path),
Expand Down Expand Up @@ -803,7 +803,7 @@ function process_layouts(page::Page)::Page

layoutname = output.frontmatter["layout"]
@assert layoutname isa String
layout_file = joinpath(dir, "_includes", layoutname)
layout_file = joinpath(input_dir, "_includes", layoutname)
@assert isfile(layout_file) "$layout_file is not a valid layout path"


Expand All @@ -816,7 +816,7 @@ function process_layouts(page::Page)::Page
input = TemplateInput(;
contents=read(layout_file),
absolute_path=layout_file,
relative_path=relpath(layout_file, dir),
relative_path=relpath(layout_file, input_dir),
frontmatter=merge(output.frontmatter,
FrontMatter(
"content" => content,
Expand Down
25 changes: 25 additions & 0 deletions src/open in browser.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# copy paste from pluto source code
function detectwsl()
Sys.islinux() &&
isfile("/proc/sys/kernel/osrelease") &&
occursin(r"Microsoft|WSL"i, read("/proc/sys/kernel/osrelease", String))
end

function open_in_default_browser(url::AbstractString)::Bool
try
if Sys.isapple()
Base.run(`open $url`)
true
elseif Sys.iswindows() || detectwsl()
Base.run(`powershell.exe Start "'$url'"`)
true
elseif Sys.islinux()
Base.run(`xdg-open $url`)
true
else
false
end
catch ex
false
end
end
Loading

4 comments on commit cefe241

@fonsp
Copy link
Member Author

@fonsp fonsp commented on cefe241 Feb 8, 2024

Choose a reason for hiding this comment

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

@lucaferranti Should be ready now!

@fonsp
Copy link
Member Author

@fonsp fonsp commented on cefe241 Feb 8, 2024

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@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/100493

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.1.0 -m "<description of version>" cefe2412565b3ce018f18994399cb55cbec2397b
git push origin v0.1.0

@lucaferranti
Copy link
Member

Choose a reason for hiding this comment

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

Fantastic, I'll test that tomorrow to see how it plays in the template! 💯

Please sign in to comment.