mark2web/navigation.go

104 lines
2.4 KiB
Go
Raw Normal View History

2019-03-18 13:34:52 +01:00
package mark2web
2019-02-28 12:13:28 +01:00
import (
"path"
"regexp"
"strings"
2019-03-18 13:34:52 +01:00
"gitbase.de/apairon/mark2web/helper"
2019-02-28 12:13:28 +01:00
)
2019-02-28 15:13:59 +01:00
// NavElement is one element with ist attributes and subs
2019-02-28 12:13:28 +01:00
type NavElement struct {
Navname string
GoTo string
Active bool
2019-03-18 15:14:41 +01:00
ColMap MapString
2019-03-10 13:26:26 +01:00
2019-02-28 12:13:28 +01:00
Data interface{}
2019-03-18 15:14:41 +01:00
This ThisPathConfig
2019-02-28 12:13:28 +01:00
SubMap *map[string]*NavElement
SubSlice *[]*NavElement
}
2019-03-18 15:35:17 +01:00
// buildNavigation builds the navigation trees for use in templates
func buildNavigation(tree *TreeNode, curNavMap *map[string]*NavElement, curNavSlice *[]*NavElement, navActive *[]*NavElement, activeNav string) {
for _, el := range tree.Sub {
2019-02-28 14:14:31 +01:00
if el.Hidden {
continue // ignore hidden nav points from collections
}
2019-02-28 12:13:28 +01:00
var ignNav *string
if p := el.Config.Path; p != nil {
ignNav = p.IgnoreForNav
}
if ignNav != nil && *ignNav != "" {
regex, err := regexp.Compile(*ignNav)
if err != nil {
2019-03-18 13:34:52 +01:00
helper.Log.Panicf("could not compile IngoreForNav regexp '%s' in '%s': %s", *ignNav, el.InputPath, err)
2019-02-28 12:13:28 +01:00
}
if regex.MatchString(path.Base(el.InputPath)) {
2019-03-18 13:34:52 +01:00
helper.Log.Debugf("ignoring input directory '%s' in navigation", el.InputPath)
2019-02-28 12:13:28 +01:00
continue
}
}
2019-03-18 15:14:41 +01:00
elPath := strings.TrimPrefix(el.OutputPath, Config.Directories.Output+"/")
2019-02-28 12:13:28 +01:00
subMap := make(map[string]*NavElement)
subSlice := make([]*NavElement, 0)
navEl := NavElement{
Active: strings.HasPrefix(activeNav, elPath),
Data: el.Config.Data,
2019-03-10 13:26:26 +01:00
ColMap: el.ColMap,
2019-02-28 12:13:28 +01:00
SubMap: &subMap,
SubSlice: &subSlice,
}
navEl.This = el.Config.This
if navEl.Active {
// add to navActive level navigation
currentLevel := strings.Count(activeNav, "/")
if len(*navActive) <= currentLevel {
// not registered
*navActive = append(*navActive, &navEl)
}
}
n := el.Config.This.Navname
if n != nil {
navEl.Navname = *n
}
g := el.Config.This.GoTo
if g != nil {
if strings.HasPrefix(*g, "/") {
// abslute
navEl.GoTo = *g
} else {
// relative
navEl.GoTo = elPath + "/" + *g
}
} else {
navEl.GoTo = elPath + "/"
}
if activeNav != "" && activeNav != "/" {
// calculate relative path
2019-03-18 13:34:52 +01:00
bToRoot := helper.BackToRoot(activeNav)
2019-02-28 12:13:28 +01:00
navEl.GoTo = bToRoot + navEl.GoTo
navEl.GoTo = path.Clean(navEl.GoTo)
}
2019-03-11 15:29:05 +01:00
(*curNavMap)[path.Base(el.OutputPath)] = &navEl
2019-02-28 12:13:28 +01:00
if curNavSlice != nil {
*curNavSlice = append(*curNavSlice, &navEl)
}
2019-03-18 15:35:17 +01:00
buildNavigation(el, &subMap, &subSlice, navActive, activeNav)
2019-02-28 12:13:28 +01:00
}
}