Skip to content

Commit f70a6e9

Browse files
committed
move file validation to the constructor
some other small changes: - validate_order: make sure `order` field is unique - show: remove extra new line for AbstractDemoCard
1 parent f97534b commit f70a6e9

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

src/show.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Base: show
33
const indent_spaces = " "
44

55
function show(io::IO, card::AbstractDemoCard)
6-
println(io, basename(card))
6+
print(io, basename(card))
77
end
88

99
function show(io::IO, sec::DemoSection; level=1)
@@ -15,6 +15,7 @@ function show(io::IO, sec::DemoSection; level=1)
1515
foreach(sec.cards) do x
1616
print(io, repeat(indent_spaces, level))
1717
show(io, x)
18+
println(io)
1819
end
1920

2021
foreach(x->show(io, x; level=level+1), sec.subsections)

src/types/card.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ to your demofile. Currently supported types are:
1818
1919
"""
2020
function democard(path::String)::AbstractDemoCard
21-
validate_file(path)
2221
_, ext = splitext(path)
2322
if ext in markdown_exts
2423
return MarkdownDemoCard(path)

src/types/julia.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ mutable struct JuliaDemoCard <: AbstractDemoCard
7676
julia::VersionNumber
7777
hidden::Bool
7878
notebook::Union{Nothing, Bool}
79+
function JuliaDemoCard(
80+
path::String,
81+
cover::Union{String, Nothing},
82+
id::String,
83+
title::String,
84+
description::String,
85+
author::String,
86+
date::DateTime,
87+
julia::VersionNumber,
88+
hidden::Bool,
89+
notebook::Union{Nothing, Bool}
90+
)
91+
isfile(path) || throw(ArgumentError("$(path) is not a valid file"))
92+
new(path, cover, id, title, description, author, date, julia, hidden, notebook)
93+
end
7994
end
8095

8196
function JuliaDemoCard(path::String)::JuliaDemoCard

src/types/markdown.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ mutable struct MarkdownDemoCard <: AbstractDemoCard
6767
author::String
6868
date::DateTime
6969
hidden::Bool
70+
function MarkdownDemoCard(
71+
path::String,
72+
cover::Union{String, Nothing},
73+
id::String,
74+
title::String,
75+
description::String,
76+
author::String,
77+
date::DateTime,
78+
hidden::Bool
79+
)
80+
isfile(path) || throw(ArgumentError("$(path) is not a valid file"))
81+
new(path, cover, id, title, description, author, date, hidden)
82+
end
7083
end
7184

7285
function MarkdownDemoCard(path::String)::MarkdownDemoCard

src/utils.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
function validate_file(path, filetype = :text)
2-
isfile(path) || throw(ArgumentError("$(path) is not a valid file"))
3-
check_ext(path, filetype)
4-
end
5-
6-
71
function check_ext(path, filetype = :text)
82
_, ext = splitext(path)
93
if filetype == :text
@@ -19,6 +13,7 @@ end
1913
### common utils for DemoPage and DemoSection
2014

2115
function validate_order(order::AbstractArray, x::Union{DemoPage, DemoSection})
16+
length(unique(order)) == length(order) || throw(ArgumentError("`\"order\"` entry should be unique."))
2217
default_order = get_default_order(x)
2318
if intersect(order, default_order) == union(order, default_order)
2419
return true

0 commit comments

Comments
 (0)