Skip to content

Commit 0456426

Browse files
committed
refactoring for properties
1 parent 31b9cc5 commit 0456426

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

property.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,39 @@ package xlog
55
type Property interface {
66
// Icon returns the fontawesome icon class name or emoji
77
Icon() string
8-
// Name returns the link text
8+
// Name returns the name of the property
99
Name() string
10+
// Value returns the value of the property
11+
Value() any
1012
}
1113

12-
var props = []func(Page) []Property{defaultProps}
14+
var propsSources = []func(Page) []Property{defaultProps}
1315

1416
// RegisterProperty registers a function that returns a set of properties for
1517
// the page
1618
func RegisterProperty(a func(Page) []Property) {
17-
props = append(props, a)
19+
propsSources = append(propsSources, a)
1820
}
1921

2022
// Properties return a list of properties for a page. It executes all functions
2123
// registered with RegisterProperty and collect results in one slice. Can be
2224
// passed to the view to render a page properties
23-
func Properties(p Page) []Property {
24-
ps := []Property{}
25-
for pr := range props {
26-
ps = append(ps, props[pr](p)...)
25+
func Properties(p Page) map[string]Property {
26+
ps := map[string]Property{}
27+
for _, source := range propsSources {
28+
for _, pr := range source(p) {
29+
ps[pr.Name()] = pr
30+
}
2731
}
32+
2833
return ps
2934
}
3035

3136
type lastUpdateProp struct{ page Page }
3237

3338
func (a lastUpdateProp) Icon() string { return "fa-solid fa-clock" }
34-
func (a lastUpdateProp) Name() string { return ago(a.page.ModTime()) }
39+
func (a lastUpdateProp) Name() string { return "Modified" }
40+
func (a lastUpdateProp) Value() any { return ago(a.page.ModTime()) }
3541

3642
func defaultProps(p Page) []Property {
3743
return []Property{

templates/view.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<span class="icon-text">
88
{{ range . }}
99
<span class="icon"> <i class="{{.Icon}}"></i> </span>
10-
<span>{{.Name}}</span>
10+
<span>{{.Name}}: {{.Value}}</span>
1111
{{ end }}
1212
</span>
1313
</div>

0 commit comments

Comments
 (0)