github.com/suntong/easygen@v5.3.0+incompatible/template.go (about)

     1  ////////////////////////////////////////////////////////////////////////////
     2  // Package: easygen
     3  // Purpose: Easy to use universal code/text generator
     4  // Authors: Tong Sun (c) 2015-2021, All rights reserved
     5  ////////////////////////////////////////////////////////////////////////////
     6  
     7  package easygen
     8  
     9  import (
    10  	"fmt"
    11  	"io"
    12  	"os"
    13  	"strings"
    14  	"text/template"
    15  )
    16  
    17  ////////////////////////////////////////////////////////////////////////////
    18  // Constant and data type/structure definitions
    19  
    20  // The Template defines the common ground for both text and html Template
    21  type Template interface {
    22  	Execute(wr io.Writer, data interface{}) error
    23  	ExecuteTemplate(wr io.Writer, name string, data interface{}) error
    24  	Parse(text string) (*template.Template, error)
    25  	ParseFiles(filenames ...string) (*template.Template, error)
    26  	Name() string
    27  }
    28  
    29  // EgBase -- EasyGen Template Base
    30  /*
    31  
    32    stringsCompare is wrapper for strings.Compare
    33    stringsContains is wrapper for strings.Contains
    34    stringsContainsAny is wrapper for strings.ContainsAny
    35    stringsContainsRune is wrapper for strings.ContainsRune
    36    stringsCount is wrapper for strings.Count
    37    stringsEqualFold is wrapper for strings.EqualFold
    38    stringsFields is wrapper for strings.Fields
    39    stringsFieldsFunc is wrapper for strings.FieldsFunc
    40    stringsHasPrefix is wrapper for strings.HasPrefix
    41    stringsHasSuffix is wrapper for strings.HasSuffix
    42    stringsIndex is wrapper for strings.Index
    43    stringsIndexAny is wrapper for strings.IndexAny
    44    stringsIndexByte is wrapper for strings.IndexByte
    45    stringsIndexFunc is wrapper for strings.IndexFunc
    46    stringsIndexRune is wrapper for strings.IndexRune
    47    stringsJoin is wrapper for strings.Join
    48    stringsLastIndex is wrapper for strings.LastIndex
    49    stringsLastIndexAny is wrapper for strings.LastIndexAny
    50    stringsLastIndexByte is wrapper for strings.LastIndexByte
    51    stringsLastIndexFunc is wrapper for strings.LastIndexFunc
    52    stringsMap is wrapper for strings.Map
    53    stringsRepeat is wrapper for strings.Repeat
    54    stringsReplace is wrapper for strings.Replace
    55    stringsSplit is wrapper for strings.Split
    56    stringsSplitAfter is wrapper for strings.SplitAfter
    57    stringsSplitAfterN is wrapper for strings.SplitAfterN
    58    stringsSplitN is wrapper for strings.SplitN
    59    stringsTitle is wrapper for strings.Title
    60    stringsToLower is wrapper for strings.ToLower
    61    stringsToLowerSpecial is wrapper for strings.ToLowerSpecial
    62    stringsToTitle is wrapper for strings.ToTitle
    63    stringsToTitleSpecial is wrapper for strings.ToTitleSpecial
    64    stringsToUpper is wrapper for strings.ToUpper
    65    stringsToUpperSpecial is wrapper for strings.ToUpperSpecial
    66    stringsTrim is wrapper for strings.Trim
    67    stringsTrimFunc is wrapper for strings.TrimFunc
    68    stringsTrimLeft is wrapper for strings.TrimLeft
    69    stringsTrimLeftFunc is wrapper for strings.TrimLeftFunc
    70    stringsTrimPrefix is wrapper for strings.TrimPrefix
    71    stringsTrimRight is wrapper for strings.TrimRight
    72    stringsTrimRightFunc is wrapper for strings.TrimRightFunc
    73    stringsTrimSpace is wrapper for strings.TrimSpace
    74    stringsTrimSuffix is wrapper for strings.TrimSuffix
    75  
    76    eqf is wrapper for strings.EqualFold
    77    split is wrapper for strings.Fields
    78    sprintf is wrapper for fmt.Sprintf
    79  
    80    regexpFindAllString is template function for RegexpFindAllString
    81    regexpFindAllStringIndex is template function for RegexpFindAllStringIndex
    82    regexpFindAllStringSubmatch is template function for RegexpFindAllStringSubmatch
    83    regexpFindAllStringSubmatchIndex is template function for RegexpFindAllStringSubmatchIndex
    84    regexpFindString is template function for RegexpFindString
    85    regexpFindStringIndex is template function for RegexpFindStringIndex
    86    regexpFindStringSubmatch is template function for RegexpFindStringSubmatch
    87    regexpFindStringSubmatchIndex is template function for RegexpFindStringSubmatchIndex
    88    regexpMatchString is template function for RegexpMatchString
    89    regexpReplaceAllLiteralString is template function for RegexpReplaceAllLiteralString
    90    regexpReplaceAllString is template function for RegexpReplaceAllString
    91    regexpReplaceAllStringFunc is template function for RegexpReplaceAllStringFunc
    92    regexpSplit is template function for RegexpSplit
    93  
    94    ENV is template function for os.Getenv
    95    substr is template function for Substr
    96    coalesce is template function for Coalesce
    97    quote4shell is template function for Quote4shell
    98  
    99    minus1 is template function for Minus1
   100    date is template function for Date
   101    timestamp is template function for Timestamp
   102  
   103  */
   104  type EgBase struct {
   105  	*template.Template
   106  }
   107  
   108  // The FuncMap defined in easygen will shield the dependency of either
   109  // text or html template, giving an implementation agnostic abstraction
   110  // that will works for both cases.
   111  type FuncMap map[string]interface{}
   112  
   113  var egFuncMap = FuncMap{
   114  	// == standard strings function definitions
   115  	"stringsCompare":        strings.Compare,
   116  	"stringsContains":       strings.Contains,
   117  	"stringsContainsAny":    strings.ContainsAny,
   118  	"stringsContainsRune":   strings.ContainsRune,
   119  	"stringsCount":          strings.Count,
   120  	"stringsEqualFold":      strings.EqualFold,
   121  	"stringsFields":         strings.Fields,
   122  	"stringsFieldsFunc":     strings.FieldsFunc,
   123  	"stringsHasPrefix":      strings.HasPrefix,
   124  	"stringsHasSuffix":      strings.HasSuffix,
   125  	"stringsIndex":          strings.Index,
   126  	"stringsIndexAny":       strings.IndexAny,
   127  	"stringsIndexByte":      strings.IndexByte,
   128  	"stringsIndexFunc":      strings.IndexFunc,
   129  	"stringsIndexRune":      strings.IndexRune,
   130  	"stringsJoin":           strings.Join,
   131  	"stringsLastIndex":      strings.LastIndex,
   132  	"stringsLastIndexAny":   strings.LastIndexAny,
   133  	"stringsLastIndexByte":  strings.LastIndexByte,
   134  	"stringsLastIndexFunc":  strings.LastIndexFunc,
   135  	"stringsMap":            strings.Map,
   136  	"stringsRepeat":         strings.Repeat,
   137  	"stringsReplace":        strings.Replace,
   138  	"stringsSplit":          strings.Split,
   139  	"stringsSplitAfter":     strings.SplitAfter,
   140  	"stringsSplitAfterN":    strings.SplitAfterN,
   141  	"stringsSplitN":         strings.SplitN,
   142  	"stringsTitle":          strings.Title,
   143  	"stringsToLower":        strings.ToLower,
   144  	"stringsToLowerSpecial": strings.ToLowerSpecial,
   145  	"stringsToTitle":        strings.ToTitle,
   146  	"stringsToTitleSpecial": strings.ToTitleSpecial,
   147  	"stringsToUpper":        strings.ToUpper,
   148  	"stringsToUpperSpecial": strings.ToUpperSpecial,
   149  	"stringsTrim":           strings.Trim,
   150  	"stringsTrimFunc":       strings.TrimFunc,
   151  	"stringsTrimLeft":       strings.TrimLeft,
   152  	"stringsTrimLeftFunc":   strings.TrimLeftFunc,
   153  	"stringsTrimPrefix":     strings.TrimPrefix,
   154  	"stringsTrimRight":      strings.TrimRight,
   155  	"stringsTrimRightFunc":  strings.TrimRightFunc,
   156  	"stringsTrimSpace":      strings.TrimSpace,
   157  	"stringsTrimSuffix":     strings.TrimSuffix,
   158  	// aliases
   159  	"eqf":     strings.EqualFold,
   160  	"split":   strings.Fields,
   161  	"sprintf": fmt.Sprintf,
   162  
   163  	// == standard regexp function definitions
   164  	"regexpFindAllString":              RegexpFindAllString,
   165  	"regexpFindAllStringIndex":         RegexpFindAllStringIndex,
   166  	"regexpFindAllStringSubmatch":      RegexpFindAllStringSubmatch,
   167  	"regexpFindAllStringSubmatchIndex": RegexpFindAllStringSubmatchIndex,
   168  	"regexpFindString":                 RegexpFindString,
   169  	"regexpFindStringIndex":            RegexpFindStringIndex,
   170  	"regexpFindStringSubmatch":         RegexpFindStringSubmatch,
   171  	"regexpFindStringSubmatchIndex":    RegexpFindStringSubmatchIndex,
   172  	"regexpMatchString":                RegexpMatchString,
   173  	"regexpReplaceAllLiteralString":    RegexpReplaceAllLiteralString,
   174  	"regexpReplaceAllString":           RegexpReplaceAllString,
   175  	"regexpReplaceAllStringFunc":       RegexpReplaceAllStringFunc,
   176  	"regexpSplit":                      RegexpSplit,
   177  
   178  	// == my added functions
   179  	"ENV":         os.Getenv,
   180  	"substr":      Substr,
   181  	"indent":      Indent,
   182  	"pindent":     PIndent,
   183  	"coalesce":    Coalesce,
   184  	"quote4shell": Quote4shell,
   185  
   186  	"iterate":   Iterate,
   187  	"argsa":     ArgsA,
   188  	"argsm":     ArgsM,
   189  	"add":       Add,
   190  	"minus":     Minus,
   191  	"minus1":    Minus1,
   192  	"date":      Date,
   193  	"timestamp": Timestamp,
   194  }
   195  
   196  ////////////////////////////////////////////////////////////////////////////
   197  // Function definitions
   198  
   199  // FuncDefs returns the custom definition mapping for this specific package.
   200  func FuncDefs() template.FuncMap {
   201  	return template.FuncMap(egFuncMap)
   202  }
   203  
   204  // NewTemplate returns a new Template for this specific package.
   205  func NewTemplate() *EgBase {
   206  	return &EgBase{template.New("EgBase")}
   207  }
   208  
   209  // Customize allows customization for this specific package.
   210  func (t *EgBase) Customize() *EgBase {
   211  	return t
   212  }