From 595a7df8893b0c3451dc47252810e2bf132a219a Mon Sep 17 00:00:00 2001 From: Alex Suraci Date: Mon, 4 Mar 2019 12:21:39 -0500 Subject: [PATCH] add section Next/Prev these are useful for generating links to the previous and next section in HTML templates Signed-off-by: Alex Suraci --- section.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/section.go b/section.go index a54ab1e..8d11b61 100644 --- a/section.go +++ b/section.go @@ -185,6 +185,52 @@ func (con *Section) IsOrHasChild(sub *Section) bool { return false } +func (con *Section) Prev() *Section { + if con.Parent == nil { + return nil + } + + var lastChild *Section + for _, child := range con.Parent.Children { + if lastChild != nil && child == con { + return lastChild + } + + lastChild = child + } + + return con.Parent +} + +func (con *Section) Next() *Section { + if con.SplitSections { + if len(con.Children) > 0 { + return con.Children[0] + } + } + + return con.NextSibling() +} + +func (con *Section) NextSibling() *Section { + if con.Parent == nil { + return nil + } + + var sawSelf bool + for _, child := range con.Parent.Children { + if sawSelf { + return child + } + + if child == con { + sawSelf = true + } + } + + return con.Parent.NextSibling() +} + func (con *Section) FindTag(tagName string) []Tag { return con.findTag(tagName, true, nil) }