package server
import (
"fmt"
"html"
"regexp"
"strings"
)
var childrenMacroRe = regexp.MustCompile(`\{\{children(?::([^}]+))?\}\}`)
func (s *Server) expandMacros(
body string,
currentPath string,
) string {
return childrenMacroRe.ReplaceAllStringFunc(
body,
func(m string) string {
matches :=
childrenMacroRe.FindStringSubmatch(m)
targetPath :=
currentPath
if len(matches) >= 2 &&
strings.TrimSpace(matches[1]) != "" {
targetPath =
strings.TrimSpace(
matches[1],
)
}
if !strings.HasPrefix(targetPath, "/") {
targetPath = "/" + targetPath
}
children, err :=
s.store.ListChildPaths(
targetPath,
)
if err != nil {
return ""
}
var b strings.Builder
b.WriteString(
``,
)
b.WriteString("")
for _, child := range children {
name :=
strings.TrimPrefix(
child,
targetPath,
)
name =
strings.TrimPrefix(
name,
"/",
)
b.WriteString(
fmt.Sprintf(
`- %s
`,
html.EscapeString(child),
html.EscapeString(name),
),
)
}
b.WriteString("
")
b.WriteString("")
return b.String()
},
)
}