funcmap.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package funcmap
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "text/template"
  7. "github.com/gobwas/glob"
  8. "gogs.carducci-dante.gov.it/karmen/core/orm"
  9. )
  10. type Stringer interface {
  11. String() string
  12. }
  13. var FuncMap template.FuncMap = template.FuncMap{
  14. "comma": comma,
  15. "groupClasses": groupClasses,
  16. "abbrev": abbrev,
  17. "nbsp": nbsp,
  18. "globFilter": globFilter,
  19. "toUpper": toUpper,
  20. "order": order,
  21. "stringSlice": stringSlice,
  22. }
  23. func order(items []string) []string {
  24. sort.Strings(items)
  25. return items
  26. }
  27. func stringSlice(items interface{}) []string {
  28. // var (
  29. // interfaces []interface{}
  30. // result []string
  31. // )
  32. // val := reflect.ValueOf(items)
  33. // interfaces = make([]interface{}, val.Len())
  34. // for i := 0; i < val.Len(); i++ {
  35. // interfaces = append(interfaces, items[i])
  36. // }
  37. // for _, i := range interfaces {
  38. // result = append(result, i.(Stringer).String())
  39. // }
  40. // return result
  41. return []string{}
  42. }
  43. func toUpper(content string) string {
  44. return strings.ToUpper(content)
  45. }
  46. func globFilter(pattern, content string) bool {
  47. g := glob.MustCompile(pattern)
  48. return g.Match(content)
  49. }
  50. func comma(classes []*orm.Class) string {
  51. var names []string
  52. for _, c := range classes {
  53. names = append(names, c.Name)
  54. }
  55. return strings.Join(names, ",")
  56. }
  57. func abbrev(name string) string {
  58. var result string
  59. splits := strings.Split(name, " ")
  60. if len(splits) > 1 {
  61. for i := len(splits) - 1; i >= 0; i-- {
  62. if i == len(splits)-1 {
  63. result += splits[i] + " "
  64. continue
  65. }
  66. result += strings.ToUpper(string(splits[i][0]) + ".")
  67. }
  68. } else {
  69. result += strings.ToUpper(string(name[0]) + ".")
  70. }
  71. return result
  72. }
  73. func nbsp(num int, text string) string {
  74. return text + strings.Repeat("&nbsp;", num)
  75. }
  76. func groupClasses(classes []*orm.Class) string {
  77. var groups []string
  78. groupByAddresses := make(map[string][]string)
  79. addressesAbbrev := map[string]string{
  80. "Linguistico": "LIN",
  81. "Classico": "CLA",
  82. "Musicale": "M",
  83. "Economico sociale": "ES",
  84. "Scienze umane": "SU",
  85. }
  86. for _, c := range classes {
  87. groupByAddresses[c.Field] = append(groupByAddresses[c.Field], fmt.Sprintf("%d%s", c.Year, c.Section))
  88. }
  89. for address, classes := range groupByAddresses {
  90. var group string
  91. if len(classes) > 1 {
  92. group = fmt.Sprintf("[%s]%s", strings.Join(classes, ","), addressesAbbrev[address])
  93. } else {
  94. group = fmt.Sprintf("%s %s", strings.Join(classes, ","), addressesAbbrev[address])
  95. }
  96. groups = append(groups, group)
  97. }
  98. return strings.Join(groups, ",")
  99. }