funcmap.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package funcmap
  2. import (
  3. "fmt"
  4. "strings"
  5. "text/template"
  6. "github.com/gobwas/glob"
  7. "gogs.carducci-dante.gov.it/karmen/core/orm"
  8. )
  9. var FuncMap template.FuncMap = template.FuncMap{
  10. "comma": comma,
  11. "groupClasses": groupClasses,
  12. "abbrev": abbrev,
  13. "nbsp": nbsp,
  14. "globFilter": globFilter,
  15. }
  16. func globFilter(pattern, content string) bool {
  17. g := glob.MustCompile(pattern)
  18. return g.Match(content)
  19. }
  20. func comma(classes []*orm.Class) string {
  21. var names []string
  22. for _, c := range classes {
  23. names = append(names, c.Name)
  24. }
  25. return strings.Join(names, ",")
  26. }
  27. func abbrev(name string) string {
  28. var result string
  29. splits := strings.Split(name, " ")
  30. if len(splits) > 1 {
  31. for i := len(splits) - 1; i >= 0; i-- {
  32. if i == len(splits)-1 {
  33. result += splits[i] + " "
  34. continue
  35. }
  36. result += strings.ToUpper(string(splits[i][0]) + ".")
  37. }
  38. } else {
  39. result += strings.ToUpper(string(name[0]) + ".")
  40. }
  41. return result
  42. }
  43. func nbsp(num int, text string) string {
  44. return text + strings.Repeat(" ", num)
  45. }
  46. func groupClasses(classes []*orm.Class) string {
  47. var groups []string
  48. groupByAddresses := make(map[string][]string)
  49. addressesAbbrev := map[string]string{
  50. "Linguistico": "LIN",
  51. "Classico": "CLA",
  52. "Musicale": "M",
  53. "Economico sociale": "ES",
  54. "Scienze umane": "SU",
  55. }
  56. for _, c := range classes {
  57. groupByAddresses[c.Field] = append(groupByAddresses[c.Field], fmt.Sprintf("%d%s", c.Year, c.Section))
  58. }
  59. for address, classes := range groupByAddresses {
  60. var group string
  61. if len(classes) > 1 {
  62. group = fmt.Sprintf("[%s]%s", strings.Join(classes, ","), addressesAbbrev[address])
  63. } else {
  64. group = fmt.Sprintf("%s %s", strings.Join(classes, ","), addressesAbbrev[address])
  65. }
  66. groups = append(groups, group)
  67. }
  68. return strings.Join(groups, ",")
  69. }