Skip to content

Commit

Permalink
Use pluto default frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Dsantra92 committed Oct 17, 2022
1 parent 6866506 commit 40d4048
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 59 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DemoCards"
uuid = "311a05b2-6137-4a5a-b473-18580a3d38b5"
authors = ["Johnny Chen <johnnychen94@hotmail.com>"]
version = "0.4.10"
version = "0.4.11"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand All @@ -19,7 +19,7 @@ YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"

[compat]
Documenter = "0.22, 0.23, 0.24, 0.25, 0.26, 0.27"
Pluto = "^0.19"
Pluto = "^0.19.13"
PlutoStaticHTML = "^6"
FileIO = "1"
HTTP = "0.6, 0.7, 0.8, 0.9, 1"
Expand Down
21 changes: 10 additions & 11 deletions src/types/pluto.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,16 @@ function save_democards(card_dir::AbstractString,

# copy to card dir and do things
cardname = splitext(basename(card.path))[1]
curr_dir = pwd()
src_dir = dirname(card.path)
# pluto outputs are expensive, we save the output to a cache dir
# these cache dir contains the render files from previous runs,
# saves time, while rendering
render_dir = joinpath(src_dir, "..", "..", "pluto_output") |> abspath
render_dir = joinpath(project_dir, "pluto_output") |> abspath
isdir(render_dir) || mkpath(render_dir)

nb_path = joinpath(card_dir, "$(cardname).jl")
card_path = joinpath(card_dir, "$(cardname).md")
md_path = joinpath(card_dir, "$(cardname).md")

_, _, body = split_pluto_frontmatter(readlines(card.path))
write(nb_path, join(body, "\n"))
cp(card.path, nb_path)

if VERSION < card.julia
# It may work, it may not work; I hope it would work.
Expand All @@ -146,23 +143,25 @@ function save_democards(card_dir::AbstractString,
bopts = BuildOptions(card_dir;previous_dir=render_dir,
output_format=output_format)
# don't run notebooks in parallel
# TODO: User option to run it parallel or not
build_notebooks(bopts, ["$(cardname).jl"], oopts)

cache_path = joinpath(render_dir, basename(card_path))
cp(card_path, cache_path; force=true)
# move rendered files to cache
cache_path = joinpath(render_dir, basename(md_path))
cp(md_path, cache_path; force=true)

badges = make_badges(card;
src=src,
card_dir=card_dir,
nbviewer_root_url=nbviewer_root_url,
project_dir=project_dir,
build_notebook=false)
build_notebook=false)

header = "# [$(card.title)](@id $(card.id))\n"
footer = pluto_footer

body = join(readlines(card_path), "\n")
write(card_path, header, badges * "\n\n", body, footer)
body = join(readlines(md_path), "\n")
write(md_path, header, badges * "\n\n", body, footer)

return nothing
end
Expand Down
48 changes: 2 additions & 46 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,28 +143,6 @@ function get_regex(::Val{:Markdown}, regex_type)
end
end


function get_regex(::Val{:Pluto}, regex_type)
if regex_type == :image
# Example: ![title](path)
return r"^\s*!\[(?<title>[^\]]*)\]\((?<path>[^\s]*)\)"
elseif regex_type == :title
# Example: # [title](@id id)
regex_title = r"md[\"]{1,3}.*\n#\s(\S.*)\n"
# Example: # title
regex_simple_title = r"md[\"]{1,3}.*\n#\s(\S.*)\n"

# Note: return the complete one first
return (regex_title, regex_simple_title)
elseif regex_type == :content
# lines that are not title, image, link, list
# FIXME: list is also captured by this regex
return r"^\s*(?<content>[^#\-*!].*)"
else
error("Unrecognized regex type: $(regex_type)")
end
end

function get_regex(::Val{:Julia}, regex_type)
if regex_type == :image
# Example: #md ![title](path)
Expand Down Expand Up @@ -213,8 +191,8 @@ Currently supported items are: `title`, `id`, `cover`, `description`.
function parse(T::Val, card::AbstractDemoCard)
# TODO: generalize
if T === Val(:Pluto)
header, frontmatter, body = split_pluto_frontmatter(readlines(card.path))
config = parse(T, body) # should ideally not pickup
frontmatter = []
config = Pluto.frontmatter(card.path)
else
header, frontmatter, body = split_frontmatter(readlines(card.path))
config = parse(T, body)
Expand Down Expand Up @@ -370,28 +348,6 @@ function split_frontmatter(contents::AbstractArray{<:AbstractString})
end


# special case, generalize it with rest of functions later
function split_pluto_frontmatter(contents)
first_cell_regex = r"#\s╔═╡\sCell\sorder:\n#\s?[╠╟][─═]\s?([0-9a-f\-]{36})"
content = join(contents, "\n")
first_cell_id = match(first_cell_regex, content)[1]
m1 = r"#\s╔═╡\s"
m2 = Regex("$(first_cell_id)\n")
m3 = r"""md\"\"\"\n([\s\S]*?)\"\"\""""
first_cell_regex = m1 * m2 * m3
frontmatter = match(first_cell_regex, content)[1]
frontmatter = split(frontmatter, "\n") |> Vector{String}
content_id_repr = r"#\s?[╠╟][─═]\s?" * Regex("$(first_cell_id)")

offset = map(contents) do line
m = match(content_id_repr, line)
m isa RegexMatch
end |> findlast

return String[], frontmatter, vcat(contents[1:offset-1], contents[offset+1:end])
end


function get_default_title(x::Union{AbstractDemoCard, DemoSection, DemoPage})
name_without_ext = splitext(basename(x))[1]
strip(replace(uppercasefirst(name_without_ext), r"[_-]" => " "))
Expand Down

2 comments on commit 40d4048

@Dsantra92
Copy link
Collaborator Author

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/70369

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.11 -m "<description of version>" 40d4048ce152fc634f8ea234bb12dcfbdb6e88dd
git push origin v0.4.11

Please sign in to comment.