From 59e3a7d2c8cafec5105e4a04a009f6521ab39598 Mon Sep 17 00:00:00 2001 From: Arpad Ryszka Date: Fri, 12 Sep 2025 01:58:50 +0200 Subject: [PATCH] wip --- lib.go | 32 ++++++++++++++++++++++++++++---- lib_test.go | 9 +++++---- notes.txt | 5 +++++ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/lib.go b/lib.go index e6f40e1..1afee05 100644 --- a/lib.go +++ b/lib.go @@ -164,6 +164,10 @@ func ScriptContent(t Tag) Tag { return t()(renderGuide{script: true}) } +func InlineChildren(t Tag) Tag { + return t +} + // inline tags are not broken into separate lines when rendering with indentation // deprecated in HTML, but only used for indentation func Inline(t Tag) Tag { @@ -210,11 +214,31 @@ func FromTemplate[Data any](f Template[Data]) Tag { } // in the functional programming sense -func Map(data []any, tag Tag) []Tag { - var tags []Tag +func Map[Data any](data []Data, tag Tag, tags ...Tag) []Tag { + var ret []Tag for _, d := range data { - tags = append(tags, tag(d)) + var retd Tag + for i := len(tags) - 1; i >= 0; i-- { + retd = tags[i](d) + } + + if retd == nil { + retd = tag(d) + continue + } + + retd = tag(retd) } - return tags + return ret +} + +func MapChildren[Data any](data []Data, tag Tag, tags ...Tag) []any { + var a []any + c := Map(data, tag, tags...) + for _, ci := range c { + a = append(a, ci) + } + + return a } diff --git a/lib_test.go b/lib_test.go index 23a52c8..b5df03a 100644 --- a/lib_test.go +++ b/lib_test.go @@ -4,6 +4,7 @@ import ( "code.squareroundforest.org/arpio/html" . "code.squareroundforest.org/arpio/html/tags" "testing" + "bytes" ) func TestLib(t *testing.T) { @@ -22,7 +23,7 @@ func TestLib(t *testing.T) { ) memberHTML := html.FromTemplate( - func(m member) Tag { + func(m member) html.Tag { return Li( Div("Name: ", m.name), Div("Level: ", m.level), @@ -31,11 +32,11 @@ func TestLib(t *testing.T) { ) teamHTML := html.FromTemplate( - func(t team) Tag { + func(t team) html.Tag { return Div( H3(t.name), P("Rank: ", t.rank), - Ul(html.Map(t.members, memberHTML)...), + Ul(html.MapChildren(t.members, memberHTML)...), ) }, ) @@ -56,7 +57,7 @@ func TestLib(t *testing.T) { } var b bytes.Buffer - if html.RenderIndent(&b, "\t", 0, teamHTML(myTeam)); err != nil { + if err := html.RenderIndent(&b, "\t", 0, teamHTML(myTeam)); err != nil { t.Fatal(err) } diff --git a/notes.txt b/notes.txt index 1c44364..7b4e7b9 100644 --- a/notes.txt +++ b/notes.txt @@ -3,3 +3,8 @@ like
: inline void inline
: block void like script: no escaping split the validation from the rendering +take copies of the children to ensure immutability +explain the immutability guarantee in the Go docs: for children yes, for children references no. The general +recommendation is not to mutate children. Ofc, creatively breaking the rules is always well appreciated by the +right audience +test wrapped templates