Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to filter potential demo cards #149

Merged
merged 3 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading