gopkg.in/easygen.v4@v4.1.0/template.go (about)

     1  ////////////////////////////////////////////////////////////////////////////
     2  // Package: easygen
     3  // Purpose: Easy to use universal code/text generator
     4  // Authors: Tong Sun (c) 2015-17, All rights reserved
     5  ////////////////////////////////////////////////////////////////////////////
     6  
     7  package easygen
     8  
     9  import (
    10  	"io"
    11  	"os"
    12  	"strings"
    13  	"text/template"
    14  )
    15  
    16  ////////////////////////////////////////////////////////////////////////////
    17  // Constant and data type/structure definitions
    18  
    19  // The Template defines the common ground for both text and html Template
    20  type Template interface {
    21  	Execute(wr io.Writer, data interface{}) error
    22  	ExecuteTemplate(wr io.Writer, name string, data interface{}) error
    23  	Parse(text string) (*template.Template, error)
    24  	ParseFiles(filenames ...string) (*template.Template, error)
    25  	Name() string
    26  }
    27  
    28  // EgBase -- EasyGen Template Base
    29  type EgBase struct {
    30  	*template.Template
    31  }
    32  
    33  // The FuncMap defined in easygen will shield the dependency of either
    34  // text or html template, giving an implementation agnostic abstraction
    35  // that will works for both cases.
    36  type FuncMap map[string]interface{}
    37  
    38  var egFuncMap = FuncMap{
    39  	// == standard strings function definitions
    40  	"stringsCompare":        strings.Compare,
    41  	"stringsContains":       strings.Contains,
    42  	"stringsContainsAny":    strings.ContainsAny,
    43  	"stringsContainsRune":   strings.ContainsRune,
    44  	"stringsCount":          strings.Count,
    45  	"stringsEqualFold":      strings.EqualFold,
    46  	"stringsFields":         strings.Fields,
    47  	"stringsFieldsFunc":     strings.FieldsFunc,
    48  	"stringsHasPrefix":      strings.HasPrefix,
    49  	"stringsHasSuffix":      strings.HasSuffix,
    50  	"stringsIndex":          strings.Index,
    51  	"stringsIndexAny":       strings.IndexAny,
    52  	"stringsIndexByte":      strings.IndexByte,
    53  	"stringsIndexFunc":      strings.IndexFunc,
    54  	"stringsIndexRune":      strings.IndexRune,
    55  	"stringsJoin":           strings.Join,
    56  	"stringsLastIndex":      strings.LastIndex,
    57  	"stringsLastIndexAny":   strings.LastIndexAny,
    58  	"stringsLastIndexByte":  strings.LastIndexByte,
    59  	"stringsLastIndexFunc":  strings.LastIndexFunc,
    60  	"stringsMap":            strings.Map,
    61  	"stringsRepeat":         strings.Repeat,
    62  	"stringsReplace":        strings.Replace,
    63  	"stringsSplit":          strings.Split,
    64  	"stringsSplitAfter":     strings.SplitAfter,
    65  	"stringsSplitAfterN":    strings.SplitAfterN,
    66  	"stringsSplitN":         strings.SplitN,
    67  	"stringsTitle":          strings.Title,
    68  	"stringsToLower":        strings.ToLower,
    69  	"stringsToLowerSpecial": strings.ToLowerSpecial,
    70  	"stringsToTitle":        strings.ToTitle,
    71  	"stringsToTitleSpecial": strings.ToTitleSpecial,
    72  	"stringsToUpper":        strings.ToUpper,
    73  	"stringsToUpperSpecial": strings.ToUpperSpecial,
    74  	"stringsTrim":           strings.Trim,
    75  	"stringsTrimFunc":       strings.TrimFunc,
    76  	"stringsTrimLeft":       strings.TrimLeft,
    77  	"stringsTrimLeftFunc":   strings.TrimLeftFunc,
    78  	"stringsTrimPrefix":     strings.TrimPrefix,
    79  	"stringsTrimRight":      strings.TrimRight,
    80  	"stringsTrimRightFunc":  strings.TrimRightFunc,
    81  	"stringsTrimSpace":      strings.TrimSpace,
    82  	"stringsTrimSuffix":     strings.TrimSuffix,
    83  	// aliases
    84  	"eqf":   strings.EqualFold,
    85  	"split": strings.Fields,
    86  
    87  	// == standard regexp function definitions
    88  	"regexpFindAllString":              regexpFindAllString,
    89  	"regexpFindAllStringIndex":         regexpFindAllStringIndex,
    90  	"regexpFindAllStringSubmatch":      regexpFindAllStringSubmatch,
    91  	"regexpFindAllStringSubmatchIndex": regexpFindAllStringSubmatchIndex,
    92  	"regexpFindString":                 regexpFindString,
    93  	"regexpFindStringIndex":            regexpFindStringIndex,
    94  	"regexpFindStringSubmatch":         regexpFindStringSubmatch,
    95  	"regexpFindStringSubmatchIndex":    regexpFindStringSubmatchIndex,
    96  	"regexpMatchString":                regexpMatchString,
    97  	"regexpReplaceAllLiteralString":    regexpReplaceAllLiteralString,
    98  	"regexpReplaceAllString":           regexpReplaceAllString,
    99  	"regexpReplaceAllStringFunc":       regexpReplaceAllStringFunc,
   100  	"regexpSplit":                      regexpSplit,
   101  
   102  	// == my added functions
   103  	"ENV":         os.Getenv,
   104  	"coalesce":    coalesce,
   105  	"quote4shell": quote4shell,
   106  
   107  	"minus1":    minus1,
   108  	"date":      date,
   109  	"timestamp": timestamp,
   110  }
   111  
   112  ////////////////////////////////////////////////////////////////////////////
   113  // Function definitions
   114  
   115  // FuncDefs returns the custom definition mapping for this specific package.
   116  func FuncDefs() template.FuncMap {
   117  	return template.FuncMap(egFuncMap)
   118  }
   119  
   120  // NewTemplate returns a new Template for this specific package.
   121  func NewTemplate() *EgBase {
   122  	return &EgBase{template.New("EgBase")}
   123  }
   124  
   125  // Customize allows customization for this specific package.
   126  func (t *EgBase) Customize() *EgBase {
   127  	return t
   128  }