Skip to content

Commit

Permalink
Merge pull request #35 from disberd/remove_plutodevmacros
Browse files Browse the repository at this point in the history
  • Loading branch information
disberd authored Jul 4, 2024
2 parents 0905dd8 + 2e5acd0 commit 6d06fa5
Show file tree
Hide file tree
Showing 16 changed files with 1,689 additions and 11 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,20 @@ jobs:
os:
- ubuntu-latest
- windows-latest
- macOS-latest
arch:
- x64
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
- uses: julia-actions/setup-julia@v2
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: julia-actions/cache@v1
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v3
- uses: codecov/codecov-action@v4
with:
files: lcov.info
files: lcov.info
token: ${{ secrets.CODECOV_TOKEN }}
8 changes: 5 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
name = "PlutoExtras"
uuid = "ed5d0301-4775-4676-b788-cf71e66ff8ed"
authors = ["Alberto Mengali <[email protected]>"]
version = "0.7.12"
version = "0.7.13-DEV"

[deps]
AbstractPlutoDingetjes = "6e696c72-6542-2067-7265-42206c756150"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
HypertextLiteral = "ac1192a8-f4b3-4bfe-ba22-af5b92cd3ab2"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
PlutoDevMacros = "a0499f29-c39b-4c5c-807c-88074221b949"
PlutoUI = "7f904dfe-b85e-4ff6-b463-dae2292396a8"
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
AbstractPlutoDingetjes = "1"
DocStringExtensions = "0.9"
HypertextLiteral = "0.9"
InteractiveUtils = "1"
Markdown = "1"
PlutoDevMacros = "0.6, 0.7"
PlutoUI = "0.7.51"
REPL = "1"
Random = "1"
julia = "1.9"
2 changes: 1 addition & 1 deletion notebooks/test_bondstable.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### A Pluto.jl notebook ###
# v0.19.22
# v0.19.43

#> custom_attrs = ["hide-enabled"]

Expand Down
4 changes: 2 additions & 2 deletions src/PlutoExtras.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module PlutoExtras
using HypertextLiteral
using PlutoDevMacros
using PlutoDevMacros.PlutoCombineHTL.WithTypes
using AbstractPlutoDingetjes.Bonds
using AbstractPlutoDingetjes
import PlutoUI
Expand All @@ -21,6 +19,8 @@ end
export Editable, StringOnEnter # from basic_widgets.jl
export ToggleReactiveBond # From within StructBondModule

include("combine_htl/PlutoCombineHTL.jl")

include("basic_widgets.jl")
include("latex_equations.jl")
module ExtendedToc include("extended_toc.jl") end
Expand Down
53 changes: 53 additions & 0 deletions src/combine_htl/PlutoCombineHTL.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module PlutoCombineHTL

using Random
using HypertextLiteral
using HypertextLiteral: Result, Bypass, Reprint, Render
using AbstractPlutoDingetjes: is_inside_pluto, AbstractPlutoDingetjes
using AbstractPlutoDingetjes.Display
using DocStringExtensions
using Markdown

export make_node, make_html, make_script, formatted_code

const LOCAL_MODULE_URL = Ref("https://cdn.jsdelivr.net/gh/disberd/PlutoExtras@$(pkgversion(@__MODULE__))/src/combine_htl/pluto_compat.js")

include("typedef.jl")
include("helpers.jl")
include("constructors.jl")
include("js_events.jl")
# include("combine.jl")
include("show.jl")
# include("docstrings.jl")

module WithTypes
_ex_names = (
:PlutoCombineHTL,
:make_node, :make_html, :make_script,
:formatted_code, :print_html, :print_javascript, :to_string,
:ScriptContent,
:PrintToScript,
:Node, :DualNode, :CombinedNodes, :PlutoNode, :NormalNode,
:Script, :DualScript, :CombinedScripts, :PlutoScript, :NormalScript,
:SingleDisplayLocation, :DisplayLocation, :InsidePluto, :OutsidePluto, :InsideAndOutsidePluto,
:ShowWithPrintHTML, :AbstractHTML
)
for n in _ex_names
eval(:(import ..PlutoCombineHTL: $n))
eval(:(export $n))
end
end

module HelperFunctions
_ex_names = (
:shouldskip, :haslisteners, :hasreturn, :returned_element,
:script_id, :add_pluto_compat, :hasinvalidation, :plutodefault,
:displaylocation, :children, :inner_node,
)
for n in _ex_names
eval(:(import ..PlutoCombineHTL: $n))
eval(:(export $n))
end
end

end
132 changes: 132 additions & 0 deletions src/combine_htl/constructors.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Abstract Constructors #

Script(::InsidePluto) = PlutoScript
Script(::OutsidePluto) = NormalScript
Script(::InsideAndOutsidePluto) = DualScript

Node(::InsidePluto) = PlutoNode
Node(::OutsidePluto) = NormalNode
Node(::InsideAndOutsidePluto) = DualNode

# ScriptContent #

## AbstractString constructor ##
function ScriptContent(s::AbstractString; kwargs...)
# We strip eventual leading newline or trailing `isspace`
str = strip_nl(s)
ael = get(kwargs, :addedEventListeners) do
contains(str, "addScriptEventListeners(")
end
ScriptContent(str, ael)
end

## Result constructor ##
function ScriptContent(r::Result; iocontext = IOContext(devnull), kwargs...)
temp = IOContext(IOBuffer(), iocontext)
show(temp, r)
str_content = strip(String(take!(temp.io)))
isempty(str_content) && return ScriptContent()
n_matches = 0
first_idx = 0
first_offset = 0
last_idx = 0
start_regexp = r"<script[^>]*>"
end_regexp = r"</script>"
for m in eachmatch(r"<script[^>]*>", str_content)
n_matches += 1
n_matches > 1 && break
first_offset = m.offset
first_idx = first_offset + length(m.match)
m_end = match(end_regexp, str_content, first_idx)
m_end === nothing && error("No closing </script> tag was found in the input")
last_idx = m_end.offset - 1
end
if n_matches === 0
@warn "No <script> tag was found.
Remember that the `ScriptContent` constructor only extract the content between the first <script> tag it finds when using an input of type `HypertextLiteral.Result`" maxlog = 1
return ScriptContent()
elseif n_matches > 1
@warn "More than one <script> tag was found.
Only the contents of the first one have been extracted" maxlog = 1
elseif first_offset > 1 || last_idx < length(str_content) - length("</script>")
@warn "The provided input also contained contents outside of the <script> tag.
This content has been discarded" maxlog = 1
end
ScriptContent(str_content[first_idx:last_idx]; kwargs...)
end

## Other Constructors ##
ScriptContent(p::ScriptContent; kwargs...) = p
ScriptContent() = ScriptContent("", false)
ScriptContent(::Union{Missing, Nothing}; kwargs...) = missing

# PlutoScript #
PlutoScript(;body = missing, invalidation = missing, id = missing, returned_element = missing, kwargs...) = PlutoScript(body, invalidation, id, returned_element; kwargs...)
# Custom Constructors
PlutoScript(body; kwargs...) = PlutoScript(;body, kwargs...)
PlutoScript(body, invalidation; kwargs...) = PlutoScript(body; invalidation, kwargs...)
# Identity/Copy with modification
function PlutoScript(s::PlutoScript; kwargs...)
(;body, invalidation, id, returned_element) = s
PlutoScript(;body, invalidation, id, returned_element, kwargs...)
end
# From other scripts
PlutoScript(n::NormalScript; kwargs...) = error("You can't construct a PlutoScript with a NormalScript as input")
PlutoScript(ds::DualScript; kwargs...) = PlutoScript(inner_node(ds, InsidePluto()); kwargs...)

# NormalScript #
NormalScript(;body = missing, add_pluto_compat = true, id = missing, returned_element = missing, kwargs...) = NormalScript(body, add_pluto_compat, id, returned_element; kwargs...)
# Custom constructor
NormalScript(body; kwargs...) = NormalScript(;body, kwargs...)
# Identity/Copy with modification
function NormalScript(s::NormalScript; kwargs...)
(;body, add_pluto_compat, id, returned_element) = s
NormalScript(;body, add_pluto_compat, id, returned_element, kwargs...)
end
# From other scripts
NormalScript(ps::PlutoScript; kwargs...) = error("You can't construct a `NormalScript` from a `PlutoScript`")
NormalScript(ds::DualScript; kwargs...) = NormalScript(inner_node(ds, OutsidePluto()); kwargs...)

# DualScript #
# Constructor with single non-script body. It mirrors the body both in the Pluto and Normal
DualScript(body; kwargs...) = DualScript(body, body; kwargs...)
# From Other Scripts
DualScript(i::PlutoScript; kwargs...) = DualScript(i, NormalScript(); kwargs...)
DualScript(o::NormalScript; kwargs...) = DualScript(PlutoScript(), o; kwargs...)
DualScript(ds::DualScript; kwargs...) = DualScript(ds.inside_pluto, ds.outside_pluto; kwargs...)

# CombinedScripts #
function CombinedScripts(v::Vector; kwargs...)
pts_vec = map(v) do el
make_script(el) |> PrintToScript
end
filtered = filter(pts_vec) do el
skip_both = shouldskip(el, InsidePluto()) && shouldskip(el, OutsidePluto())
!skip_both
end
CombinedScripts(filtered; kwargs...)
end
CombinedScripts(cs::CombinedScripts) = cs
CombinedScripts(el) = CombinedScripts([el])

# CombinedNodes #
CombinedNodes(cn::CombinedNodes) = cn
CombinedNodes(el) = CombinedNodes([el])

# ShowWithPrintHTML #
ShowWithPrintHTML(@nospecialize(t); display_type = displaylocation(t)) = ShowWithPrintHTML(t, displaylocation(display_type))
ShowWithPrintHTML(@nospecialize(t::ShowWithPrintHTML); display_type = displaylocation(t)) = ShowWithPrintHTML(t.el, displaylocation(display_type))
ShowWithPrintHTML(@nospecialize(t::PrintToScript); kwargs...) = error("You can't wrap object of type $(typeof(t)) with ShowWithPrintHTML")

# PrintToScript #
PrintToScript(@nospecialize(t); display_type = displaylocation(t)) = PrintToScript(t, displaylocation(display_type))
PrintToScript(@nospecialize(t::PrintToScript); display_type = displaylocation(t)) = PrintToScript(t.el, displaylocation(display_type))
PrintToScript(@nospecialize(t::AbstractHTML); kwargs...) = error("You can't wrap object of type $(typeof(t)) with PrintToScript")
PrintToScript(@nospecialize(t::Union{SingleScript, DualScript}); display_type = displaylocation(t)) = PrintToScript(t, displaylocation(display_type))
PrintToScript(x::Union{AbstractString, ScriptContent, Result}; kwargs...) = PrintToScript(DualScript(ScriptContent(x)); kwargs...)

# DualNode #
DualNode(i, o) = DualNode(PlutoNode(i), NormalNode(o))
DualNode(i::PlutoNode) = DualNode(i, NormalNode())
DualNode(o::NormalNode) = DualNode(PlutoNode(), o)
DualNode(dn::DualNode) = dn
Loading

0 comments on commit 6d06fa5

Please sign in to comment.