Skip to content

Commit

Permalink
Add ability to filter potential demo cards (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamslc committed Aug 9, 2023
1 parent 5b81a6d commit a55ec71
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/generate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Processing pipeline:
* `credit::Bool`: `true` to show a "This page is generated by ..." info. By default it's `true`.
* `throw_error::Bool`: `true` to throw an error when the julia demo build fails; otherwise it will
continue the build with warnings.
* `filter_function::Function`: the function is passed each potential `DemoCard`, and the
card is excluded if the function returns false. By default, all potential cards are included.
# Examples
Expand Down Expand Up @@ -92,7 +94,8 @@ function makedemos(source::String, templates::Union{Dict, Nothing} = nothing;
branch::String = "gh-pages",
edit_branch::String = "master",
credit = true,
throw_error = false)
throw_error = false,
filter_function = x -> true)

if !(basename(pwd()) == "docs" || basename(root) == "docs" || root == preview_build_dir())
# special cases that warnings are not printed:
Expand All @@ -110,7 +113,7 @@ function makedemos(source::String, templates::Union{Dict, Nothing} = nothing;
startswith(source, root) || throw(ArgumentError("invalid demo page source $page_root"))
end

page = DemoPage(page_root)
page = DemoPage(page_root, filter_function)

relative_root = basename(page)

Expand Down
4 changes: 2 additions & 2 deletions src/types/page.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ end

basename(page::DemoPage) = basename(page.root)

function DemoPage(root::String)::DemoPage
function DemoPage(root::String, filter_func=x -> true)::DemoPage
root = replace(root, r"[/\\]" => Base.Filesystem.path_separator) # windows compatibility
isdir(root) || throw(ArgumentError("page root does not exist: $(root)"))
root = rstrip(root, '/') # otherwise basename(root) will returns `""`
Expand Down Expand Up @@ -133,7 +133,7 @@ function DemoPage(root::String)::DemoPage
config = parse(Val(:Markdown), template_file)
config = merge(json_config, config) # template has higher priority over config file

sections = filter(map(DemoSection, section_paths)) do sec
sections = filter(map(p -> DemoSection(p, filter_func), section_paths)) do sec
empty_section = isempty(sec.cards) && isempty(sec.subsections)
if empty_section
@warn "Empty section detected, remove from the demo page tree." section=relpath(sec.root, root)
Expand Down
6 changes: 3 additions & 3 deletions src/types/section.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ end

basename(sec::DemoSection) = basename(sec.root)

function DemoSection(root::String)::DemoSection
function DemoSection(root::String, filter_func=x -> true)::DemoSection
root = replace(root, r"[/\\]" => Base.Filesystem.path_separator) # windows compatibility
isdir(root) || throw(ArgumentError("section root should be a valid dir, instead it's $(root)"))

Expand All @@ -108,14 +108,14 @@ function DemoSection(root::String)::DemoSection
# throw warnings for it.
cards = map(democard, card_paths)
unmatches = filter(cards) do x
x isa UnmatchedCard
x isa UnmatchedCard || !filter_func(x)
end
if !isempty(unmatches)
msg = join(map(basename, unmatches), "\", \"")
@warn "skip unmatched file: \"$msg\"" section_dir=root
end
cards = filter!(cards) do x
!(x isa UnmatchedCard)
!(x isa UnmatchedCard || !filter_func(x))
end

section = DemoSection(root,
Expand Down

0 comments on commit a55ec71

Please sign in to comment.