commit b7d640207abe4608ba5e665a0a83fab107e7a8a2
parent bea2bfcdb37fa686dccc42722b1b5fd2915204f3
Author: gearsix <gearsix@tuta.io>
Date: Fri, 3 Feb 2023 17:16:22 +0000
depreciated {,Is}SupportedTemplateLangs; added SupportedDataFormat ...
This provides a more clear method of identifying the template format
when the library is being used in code.
Updated template_test.go
Diffstat:
2 files changed, 64 insertions(+), 4 deletions(-)
diff --git a/template.go b/template.go
@@ -31,12 +31,42 @@ import (
tmpl "text/template"
)
-// SupportedTemplateLangs provides a list of supported languages for template files (lower-case)
+// SupportedTemplateFormat provides a list of supported languages for
+// Template files (lower-case)
+type SupportedTemplateFormat string
+
+const (
+ TMPL SupportedTemplateFormat = "tmpl"
+ HMPL SupportedTemplateFormat = "hmpl"
+ MST SupportedTemplateFormat = "mst"
+)
+
+// ReadTemplateFormat returns the *SupportedTemplateFormat* that the file
+// extension of `path` matches. If the file extension of `path` does
+// not match any *SupportedTemplateFormat*, then an "" is returned.
+func ReadTemplateFormat(path string) SupportedTemplateFormat {
+ if len(path) == 0 {
+ return ""
+ }
+
+ ext := filepath.Ext(path)
+ ext = strings.ToLower(path)
+ if len(ext) > 0 && ext[0] == '.' {
+ ext = ext[1:]
+ }
+
+ for _, fmt := range []SupportedTemplateFormat{TMPL, HMPL, MST} {
+ if string(fmt) == ext {
+ return fmt
+ }
+ }
+ return ""
+}
+
+// **DEPRECIATED** please use SupportedTemplateFormat
var SupportedTemplateLangs = []string{"tmpl", "hmpl", "mst"}
-// IsSupportedTemplateLang provides the index of `SupportedTemplateLangs` that `lang` is at.
-// If `lang` is not in `SupportedTemplateLangs`, `-1` will be returned.
-// File extensions can be passed in `lang`, the prefixed `.` will be trimmed.
+// **DEPRECIATED** please use ReadTemplateFormat
func IsSupportedTemplateLang(lang string) int {
lang = strings.ToLower(lang)
if len(lang) > 0 && lang[0] == '.' {
diff --git a/template_test.go b/template_test.go
@@ -75,6 +75,36 @@ func TestIsSupportedTemplateLang(t *testing.T) {
}
}
+func TestReadTemplateFormat(t *testing.T) {
+ exts := []string{
+ ".tmpl", "tmpl", "TMPL", ".TMPL",
+ ".hmpl", "hmpl", "HMPL", ".HMPL",
+ ".mst", "mst", "MST", ".MST",
+ ".NONE", "-", ".", "",
+ }
+
+ for i, ext := range exts {
+ var target SupportedTemplateFormat
+ if i < 4 {
+ target = TMPL
+ } else if i < 8 {
+ target = HMPL
+ } else if i < 12 {
+ target = MST
+ } else {
+ target = ""
+ }
+
+ if ReadTemplateFormat(ext) != target {
+ if target == "" {
+ t.Fatalf("%s is not a supported data language", ext)
+ } else {
+ t.Fatalf("%s did not return %s", ext, target)
+ }
+ }
+ }
+}
+
func validateTemplate(t *testing.T, template Template, templateType string, rootName string, partialNames ...string) {
types := map[string]string{
"tmpl": "*template.Template",