87 lines
1.2 KiB
Go
87 lines
1.2 KiB
Go
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(
|
|
`<section class="children-list">`,
|
|
)
|
|
|
|
b.WriteString("<ul>")
|
|
|
|
for _, child := range children {
|
|
|
|
name :=
|
|
strings.TrimPrefix(
|
|
child,
|
|
targetPath,
|
|
)
|
|
|
|
name =
|
|
strings.TrimPrefix(
|
|
name,
|
|
"/",
|
|
)
|
|
|
|
b.WriteString(
|
|
fmt.Sprintf(
|
|
`<li><a href="%s">%s</a></li>`,
|
|
html.EscapeString(child),
|
|
html.EscapeString(name),
|
|
),
|
|
)
|
|
}
|
|
|
|
b.WriteString("</ul>")
|
|
b.WriteString("</section>")
|
|
|
|
return b.String()
|
|
},
|
|
)
|
|
}
|