Skip to content

Commit

Permalink
add section Next/Prev
Browse files Browse the repository at this point in the history
these are useful for generating links to the previous and next section
in HTML templates

Signed-off-by: Alex Suraci <suraci.alex@gmail.com>
  • Loading branch information
vito committed Mar 4, 2019
1 parent 6bdccf3 commit 595a7df
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions section.go
Expand Up @@ -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)
}
Expand Down

0 comments on commit 595a7df

Please sign in to comment.