Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

131 строка
3.8KB

  1. package domain
  2. import (
  3. "regexp"
  4. "strings"
  5. )
  6. const (
  7. WebsiteSectionHero = "hero"
  8. WebsiteSectionIntro = "intro"
  9. WebsiteSectionServices = "services"
  10. WebsiteSectionServiceItem = "service_item"
  11. WebsiteSectionAbout = "about"
  12. WebsiteSectionTeam = "team"
  13. WebsiteSectionTestimonials = "testimonials"
  14. WebsiteSectionCTA = "cta"
  15. WebsiteSectionContact = "contact"
  16. WebsiteSectionFooter = "footer"
  17. WebsiteSectionGallery = "gallery"
  18. WebsiteSectionOther = "other"
  19. )
  20. var websiteSectionOrder = []string{
  21. WebsiteSectionHero,
  22. WebsiteSectionIntro,
  23. WebsiteSectionServices,
  24. WebsiteSectionServiceItem,
  25. WebsiteSectionAbout,
  26. WebsiteSectionTeam,
  27. WebsiteSectionTestimonials,
  28. WebsiteSectionCTA,
  29. WebsiteSectionContact,
  30. WebsiteSectionFooter,
  31. WebsiteSectionGallery,
  32. WebsiteSectionOther,
  33. }
  34. var websiteSectionLabels = map[string]string{
  35. WebsiteSectionHero: "Hero",
  36. WebsiteSectionIntro: "Intro",
  37. WebsiteSectionServices: "Services",
  38. WebsiteSectionServiceItem: "Service Item",
  39. WebsiteSectionAbout: "About",
  40. WebsiteSectionTeam: "Team",
  41. WebsiteSectionTestimonials: "Testimonials",
  42. WebsiteSectionCTA: "CTA",
  43. WebsiteSectionContact: "Contact",
  44. WebsiteSectionFooter: "Footer",
  45. WebsiteSectionGallery: "Gallery",
  46. WebsiteSectionOther: "Other",
  47. }
  48. var serviceItemIndexPattern = regexp.MustCompile(`_\d+$`)
  49. func WebsiteSectionOptions() []string {
  50. out := make([]string, len(websiteSectionOrder))
  51. copy(out, websiteSectionOrder)
  52. return out
  53. }
  54. func WebsiteSectionLabel(section string) string {
  55. normalized := NormalizeWebsiteSection(section)
  56. if label, ok := websiteSectionLabels[normalized]; ok {
  57. return label
  58. }
  59. return websiteSectionLabels[WebsiteSectionOther]
  60. }
  61. func NormalizeWebsiteSection(section string) string {
  62. normalized := strings.ToLower(strings.TrimSpace(section))
  63. for _, candidate := range websiteSectionOrder {
  64. if normalized == candidate {
  65. return candidate
  66. }
  67. }
  68. return WebsiteSectionOther
  69. }
  70. func SuggestWebsiteSection(field TemplateField) string {
  71. section := strings.ToLower(strings.TrimSpace(field.Section))
  72. key := strings.ToLower(strings.TrimSpace(field.KeyName))
  73. path := strings.ToLower(strings.TrimSpace(field.Path))
  74. sample := strings.ToLower(strings.TrimSpace(field.SampleValue))
  75. fieldKind := strings.ToLower(strings.TrimSpace(field.FieldKind))
  76. combined := strings.Join([]string{section, key, path}, " ")
  77. if fieldKind == "image" || containsAny(combined, "gallery", "image", "img", "photo", "picture", "media") {
  78. return WebsiteSectionGallery
  79. }
  80. if containsAny(combined, "testimonial") {
  81. return WebsiteSectionTestimonials
  82. }
  83. if section == "services" || containsAny(combined, "service") {
  84. if containsAny(combined, "item", "card") || serviceItemIndexPattern.MatchString(key) {
  85. return WebsiteSectionServiceItem
  86. }
  87. return WebsiteSectionServices
  88. }
  89. if containsAny(combined, "team", "member", "staff", "employee", "founder") {
  90. return WebsiteSectionTeam
  91. }
  92. if containsAny(combined, "welcome", "headline", "hero", "banner") {
  93. return WebsiteSectionHero
  94. }
  95. if containsAny(combined, "intro", "introduction", "lead") {
  96. return WebsiteSectionIntro
  97. }
  98. if containsAny(combined, "about", "company", "mission", "story") || len(sample) > 180 {
  99. return WebsiteSectionAbout
  100. }
  101. if containsAny(combined, "cta", "highlight", "button", "calltoaction", "call_to_action") {
  102. return WebsiteSectionCTA
  103. }
  104. if containsAny(combined, "contact", "email", "phone", "address") {
  105. return WebsiteSectionContact
  106. }
  107. if containsAny(combined, "footer", "copyright", "imprint", "legal") {
  108. return WebsiteSectionFooter
  109. }
  110. return WebsiteSectionOther
  111. }
  112. func containsAny(value string, needles ...string) bool {
  113. for _, needle := range needles {
  114. if strings.Contains(value, needle) {
  115. return true
  116. }
  117. }
  118. return false
  119. }