194 lines
2.6 KiB
Go
194 lines
2.6 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"html"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var childrenMacroRe = regexp.MustCompile(`\{\{children([^}]*)\}\}`)
|
|
|
|
func parseChildrenMacroArgs(
|
|
raw string,
|
|
currentPath string,
|
|
) (string, []string) {
|
|
|
|
targetPath := currentPath
|
|
excludeRaw := ""
|
|
|
|
raw = strings.TrimSpace(raw)
|
|
|
|
if strings.HasPrefix(raw, ":") {
|
|
|
|
raw = strings.TrimSpace(
|
|
strings.TrimPrefix(raw, ":"),
|
|
)
|
|
|
|
parts :=
|
|
strings.Fields(raw)
|
|
|
|
if len(parts) > 0 {
|
|
|
|
targetPath = parts[0]
|
|
|
|
for _, part := range parts[1:] {
|
|
|
|
if strings.HasPrefix(part, "exclude=") {
|
|
|
|
excludeRaw =
|
|
strings.TrimPrefix(
|
|
part,
|
|
"exclude=",
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, part := range strings.Fields(raw) {
|
|
|
|
if strings.HasPrefix(part, "exclude=") {
|
|
|
|
excludeRaw =
|
|
strings.TrimPrefix(
|
|
part,
|
|
"exclude=",
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
if !strings.HasPrefix(targetPath, "/") {
|
|
targetPath = "/" + targetPath
|
|
}
|
|
|
|
return targetPath, splitExcludePatterns(excludeRaw)
|
|
}
|
|
|
|
func splitExcludePatterns(
|
|
raw string,
|
|
) []string {
|
|
|
|
var out []string
|
|
|
|
for _, p := range strings.Split(raw, ",") {
|
|
|
|
p = strings.TrimSpace(p)
|
|
|
|
if p == "" {
|
|
continue
|
|
}
|
|
|
|
if !strings.HasPrefix(p, "/") {
|
|
p = "/" + p
|
|
}
|
|
|
|
out = append(out, p)
|
|
}
|
|
|
|
return out
|
|
}
|
|
|
|
func isExcludedPath(
|
|
path string,
|
|
patterns []string,
|
|
) bool {
|
|
|
|
path = strings.TrimRight(path, "/")
|
|
|
|
for _, p := range patterns {
|
|
|
|
p = strings.TrimRight(p, "/")
|
|
|
|
if p == "" {
|
|
continue
|
|
}
|
|
|
|
if path == p ||
|
|
strings.HasPrefix(path, p+"/") {
|
|
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (s *Server) expandMacros(
|
|
body string,
|
|
currentPath string,
|
|
) string {
|
|
|
|
return childrenMacroRe.ReplaceAllStringFunc(
|
|
body,
|
|
func(m string) string {
|
|
|
|
matches :=
|
|
childrenMacroRe.FindStringSubmatch(m)
|
|
|
|
raw := ""
|
|
|
|
if len(matches) >= 2 {
|
|
raw = matches[1]
|
|
}
|
|
|
|
targetPath, excludes :=
|
|
parseChildrenMacroArgs(
|
|
raw,
|
|
currentPath,
|
|
)
|
|
|
|
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 {
|
|
|
|
if isExcludedPath(child, excludes) {
|
|
continue
|
|
}
|
|
|
|
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()
|
|
},
|
|
)
|
|
}
|