Browse Source

Add funcmap package

Andrea Fazzi 5 năm trước cách đây
mục cha
commit
3a3818ed94
3 tập tin đã thay đổi với 81 bổ sung62 xóa
  1. 76 0
      generator/funcmap/funcmap.go
  2. 3 1
      generator/generators.go
  3. 2 61
      generator/generators/list/list.go

+ 76 - 0
generator/funcmap/funcmap.go

@@ -0,0 +1,76 @@
+package funcmap
+
+import (
+	"fmt"
+	"strings"
+	"text/template"
+
+	"github.com/gobwas/glob"
+	"gogs.carducci-dante.gov.it/karmen/core/orm"
+)
+
+var FuncMap template.FuncMap = template.FuncMap{
+	"comma":        comma,
+	"groupClasses": groupClasses,
+	"abbrev":       abbrev,
+	"nbsp":         nbsp,
+	"globFilter":   globFilter,
+}
+
+func globFilter(pattern, content string) bool {
+	g := glob.MustCompile(pattern)
+
+	return g.Match(content)
+}
+
+func comma(classes []*orm.Class) string {
+	var names []string
+	for _, c := range classes {
+		names = append(names, c.Name)
+	}
+	return strings.Join(names, ",")
+}
+
+func abbrev(name string) string {
+	var result string
+	splits := strings.Split(name, " ")
+	for i := len(splits) - 1; i >= 0; i-- {
+		if i == len(splits)-1 {
+			result += splits[i] + " "
+			continue
+		}
+		result += strings.ToUpper(string(splits[i][0]) + ".")
+	}
+	return result
+}
+
+func nbsp(num int, text string) string {
+	return text + strings.Repeat(" ", num)
+}
+
+func groupClasses(classes []*orm.Class) string {
+	var groups []string
+
+	groupByAddresses := make(map[string][]string)
+	addressesAbbrev := map[string]string{
+		"Linguistico":       "LIN",
+		"Classico":          "CLA",
+		"Musicale":          "M",
+		"Economico sociale": "ES",
+		"Scienze umane":     "SU",
+	}
+	for _, c := range classes {
+		groupByAddresses[c.Field] = append(groupByAddresses[c.Field], fmt.Sprintf("%d%s", c.Year, c.Section))
+	}
+
+	for address, classes := range groupByAddresses {
+		var group string
+		if len(classes) > 1 {
+			group = fmt.Sprintf("[%s]%s", strings.Join(classes, ","), addressesAbbrev[address])
+		} else {
+			group = fmt.Sprintf("%s %s", strings.Join(classes, ","), addressesAbbrev[address])
+		}
+		groups = append(groups, group)
+	}
+	return strings.Join(groups, ",")
+}

+ 3 - 1
generator/generators.go

@@ -5,4 +5,6 @@ type Generator interface {
 	SetJobId(id uint)
 }
 
-var Generators map[string]Generator
+var (
+	Generators map[string]Generator
+)

+ 2 - 61
generator/generators/list/list.go

@@ -6,7 +6,6 @@ import (
 	"log"
 	"os"
 	"path/filepath"
-	"strings"
 	"sync"
 	"text/template"
 	"time"
@@ -15,6 +14,7 @@ import (
 
 	karmen_client "gogs.carducci-dante.gov.it/karmen/client"
 	"gogs.carducci-dante.gov.it/karmen/core/config"
+	"gogs.carducci-dante.gov.it/karmen/core/generator/funcmap"
 	"gogs.carducci-dante.gov.it/karmen/core/orm"
 	"gogs.carducci-dante.gov.it/karmen/util/fileutil"
 	"gogs.carducci-dante.gov.it/karmen/util/libreoffice"
@@ -53,13 +53,6 @@ func (l *JobLogger) Write(p []byte) (n int, err error) {
 	return origLen, nil
 }
 
-var funcMap template.FuncMap = template.FuncMap{
-	"comma":        comma,
-	"groupClasses": groupClasses,
-	"abbrev":       abbrev,
-	"nbsp":         nbsp,
-}
-
 func (generator *ListGenerator) generate(outputPath string, cloudPath string, teachers []*orm.Teacher, funcMap template.FuncMap) {
 	filename := filepath.Join(outputPath, "elenco_docenti")
 
@@ -112,58 +105,6 @@ func (generator *ListGenerator) generate(outputPath string, cloudPath string, te
 
 }
 
-func comma(classes []*orm.Class) string {
-	var names []string
-	for _, c := range classes {
-		names = append(names, c.Name)
-	}
-	return strings.Join(names, ",")
-}
-
-func abbrev(name string) string {
-	var result string
-	splits := strings.Split(name, " ")
-	for i := len(splits) - 1; i >= 0; i-- {
-		if i == len(splits)-1 {
-			result += splits[i] + " "
-			continue
-		}
-		result += strings.ToUpper(string(splits[i][0]) + ".")
-	}
-	return result
-}
-
-func nbsp(num int, text string) string {
-	return text + strings.Repeat(" ", num)
-}
-
-func groupClasses(classes []*orm.Class) string {
-	var groups []string
-
-	groupByAddresses := make(map[string][]string)
-	addressesAbbrev := map[string]string{
-		"Linguistico":       "LIN",
-		"Classico":          "CLA",
-		"Musicale":          "M",
-		"Economico sociale": "ES",
-		"Scienze umane":     "SU",
-	}
-	for _, c := range classes {
-		groupByAddresses[c.Field] = append(groupByAddresses[c.Field], fmt.Sprintf("%d%s", c.Year, c.Section))
-	}
-
-	for address, classes := range groupByAddresses {
-		var group string
-		if len(classes) > 1 {
-			group = fmt.Sprintf("[%s]%s", strings.Join(classes, ","), addressesAbbrev[address])
-		} else {
-			group = fmt.Sprintf("%s %s", strings.Join(classes, ","), addressesAbbrev[address])
-		}
-		groups = append(groups, group)
-	}
-	return strings.Join(groups, ",")
-}
-
 func NewListGenerator(config *config.ConfigT) *ListGenerator {
 	return &ListGenerator{Config: config}
 }
@@ -236,7 +177,7 @@ func (generator *ListGenerator) Run() {
 		log.Println(err)
 	}
 
-	generator.generate(outputPath, cloudPath, teachers, funcMap)
+	generator.generate(outputPath, cloudPath, teachers, funcmap.FuncMap)
 
 	if generator.JobID > 0 {
 		job.End = time.Now()