package server
import (
"context"
"html"
"net/http"
"net/url"
"regexp"
"strings"
"pageswiki/internal/store"
)
var childrenMacroRe = regexp.MustCompile(`\{\{children([^}]*)\}\}`)
var cityMacroRe = regexp.MustCompile(`\{\{(city-breadcrumbs|spot-pages|quartier|street|spot|nearby|city)(?:\s+([^}]*))?\}\}`)
var cityMacroOptionRe = regexp.MustCompile(`([a-z_-]+)=(?:"([^"]*)"|'([^']*)'|([^\s]+))`)
type childrenMacroArgs struct {
TargetPath string
Excludes []string
View string
Offset int
Limit int
}
func parseChildrenMacroArgs(raw, currentPath string) childrenMacroArgs {
targetPath := currentPath
excludeRaw := ""
raw = strings.TrimSpace(raw)
parts := strings.Fields(raw)
if strings.HasPrefix(raw, ":") {
parts = strings.Fields(strings.TrimSpace(strings.TrimPrefix(raw, ":")))
if len(parts) > 0 && !strings.Contains(parts[0], "=") {
targetPath = parts[0]
parts = parts[1:]
}
}
for _, part := range parts {
switch {
case strings.HasPrefix(part, "exclude="):
excludeRaw = strings.TrimPrefix(part, "exclude=")
}
}
if !strings.HasPrefix(targetPath, "/") {
targetPath = "/" + targetPath
}
return childrenMacroArgs{
TargetPath: targetPath,
Excludes: splitExcludePatterns(excludeRaw),
View: "list",
Limit: 120,
}
}
func splitExcludePatterns(raw string) []string {
var out []string
for _, pattern := range strings.Split(raw, ",") {
pattern = strings.TrimSpace(pattern)
if pattern == "" {
continue
}
if !strings.HasPrefix(pattern, "/") {
pattern = "/" + pattern
}
out = append(out, pattern)
}
return out
}
func isExcludedPath(path string, patterns []string) bool {
path = strings.TrimRight(path, "/")
for _, pattern := range patterns {
pattern = strings.TrimRight(pattern, "/")
if pattern != "" &&
(path == pattern || strings.HasPrefix(path, pattern+"/")) {
return true
}
}
return false
}
func (s *Server) expandMacros(
body string,
currentPath string,
domainUUID string,
) string {
return s.expandMacrosFiltered(body, currentPath, domainUUID, nil)
}
func (s *Server) expandMacrosForRequest(r *http.Request, body, currentPath, domainUUID string) string {
return s.expandMacrosFilteredContext(r.Context(), body, currentPath, domainUUID, s.pathReadFilter(r))
}
func (s *Server) expandMacrosFiltered(body, currentPath, domainUUID string, allowed func(string) bool) string {
return s.expandMacrosFilteredContext(context.Background(), body, currentPath, domainUUID, allowed)
}
func (s *Server) expandMacrosFilteredContext(ctx context.Context, body, currentPath, domainUUID string, allowed func(string) bool) string {
body = s.expandCityMacrosContext(ctx, body, currentPath, domainUUID, allowed)
return s.expandChildrenMacros(body, currentPath)
}
func (s *Server) expandChildrenMacros(body, currentPath string) string {
return childrenMacroRe.ReplaceAllStringFunc(body, func(macro string) string {
match := childrenMacroRe.FindStringSubmatch(macro)
raw := ""
if len(match) >= 2 {
raw = match[1]
}
args := parseChildrenMacroArgs(raw, currentPath)
query := url.Values{
"path": {args.TargetPath},
}
if len(args.Excludes) > 0 {
query.Set("exclude", strings.Join(args.Excludes, ","))
}
source := "/v/children?" + query.Encode()
return ``
})
}
func (s *Server) expandCityMacros(body, currentPath, domainUUID string, allowed func(string) bool) string {
return s.expandCityMacrosContext(context.Background(), body, currentPath, domainUUID, allowed)
}
func (s *Server) expandCityMacrosContext(ctx context.Context, body, currentPath, domainUUID string, allowed func(string) bool) string {
if !cityMacroRe.MatchString(body) || s.store == nil {
return body
}
context, err := s.store.ResolveCityPathContextWithContext(ctx, domainUUID, currentPath)
if err != nil {
return body
}
var city store.CityMap
cityLoaded := false
if allowed != nil {
if context.Quartier != nil && !allowed(context.Quartier.PathPrefix) {
context.Quartier = nil
context.Street = nil
context.Spot = nil
}
if context.Street != nil && !allowed(context.Street.PathPrefix) {
context.Street = nil
context.Spot = nil
}
if context.Spot != nil && !allowed(context.Spot.PathPrefix) {
context.Spot = nil
}
}
link := func(kind string, node *store.CityNodeDetail) string {
if node == nil {
return `—`
}
quartierName := ""
if context.Quartier != nil {
quartierName = context.Quartier.Name
}
return `` + html.EscapeString(node.Title) + ``
}
return cityMacroRe.ReplaceAllStringFunc(body, func(macro string) string {
if ctx.Err() != nil {
return macro
}
match := cityMacroRe.FindStringSubmatch(macro)
name, raw := match[1], match[2]
options := map[string]string{}
for _, option := range cityMacroOptionRe.FindAllStringSubmatch(raw, -1) {
value := option[2]
if value == "" {
value = option[3]
}
if value == "" {
value = option[4]
}
options[option[1]] = value
}
switch name {
case "city":
return ``
case "quartier":
return `` + link("quartier", context.Quartier) + ``
case "street":
return `` + link("street", context.Street) + ``
case "spot":
return `` + link("spot", context.Spot) + ``
case "city-breadcrumbs":
parts := []string{`City`}
if context.Quartier != nil {
parts = append(parts, link("quartier", context.Quartier))
}
if context.Street != nil {
parts = append(parts, link("street", context.Street))
}
if context.Spot != nil {
parts = append(parts, link("spot", context.Spot))
}
return ``
case "spot-pages":
if context.Spot == nil {
return `
`
}
pages, err := s.store.ListSpotPagesContext(ctx, domainUUID, context.Spot.UUID)
if err != nil {
return macro
}
items := []string{}
for _, page := range pages {
if ctx.Err() != nil {
return macro
}
if allowed != nil && (page.Path == "" || !allowed(page.Path)) {
continue
}
if edge := options["edge"]; edge != "" && page.EdgeType != edge {
continue
}
label := page.Label
if label == "" {
label = page.Path
}
items = append(items, ``+html.EscapeString(label)+``)
}
return `` + strings.Join(items, "") + `
`
case "nearby":
if context.Spot == nil {
return ``
}
if !cityLoaded {
city, err = s.store.GetCityMapContext(ctx, domainUUID)
if err != nil {
return macro
}
if allowed != nil {
city = filterCityProjection(city, allowed)
}
cityLoaded = true
}
spots := map[string]store.CitySpot{}
for _, quartier := range city.Quartiers {
for _, street := range quartier.Streets {
for _, spot := range street.Spots {
spots[spot.UUID] = spot
}
}
}
items := []string{}
for _, relation := range city.Relations {
if relation.SourceSpotUUID != context.Spot.UUID || (options["relation"] != "" && relation.RelationType != options["relation"]) {
continue
}
target, ok := spots[relation.TargetSpotUUID]
if ok {
items = append(items, ``+html.EscapeString(target.Title)+` `+html.EscapeString(relation.RelationType)+``)
}
}
return `` + strings.Join(items, "") + `
`
}
return macro
})
}