add BeeTemplateExt
Showing
1 changed file
with
169 additions
and
156 deletions
| 1 | package beego | 1 | package beego |
| 2 | 2 | ||
| 3 | //@todo add template funcs | 3 | //@todo add template funcs |
| 4 | 4 | ||
| 5 | import ( | 5 | import ( |
| 6 | "errors" | 6 | "errors" |
| 7 | "fmt" | 7 | "fmt" |
| 8 | "github.com/russross/blackfriday" | 8 | "github.com/russross/blackfriday" |
| 9 | "html/template" | 9 | "html/template" |
| 10 | "os" | 10 | "os" |
| 11 | "path" | 11 | "path" |
| 12 | "path/filepath" | 12 | "path/filepath" |
| 13 | "strings" | 13 | "strings" |
| 14 | "time" | 14 | "time" |
| 15 | ) | 15 | ) |
| 16 | 16 | ||
| 17 | var ( | 17 | var ( |
| 18 | beegoTplFuncMap template.FuncMap | 18 | beegoTplFuncMap template.FuncMap |
| 19 | BeeTemplates map[string]*template.Template | 19 | BeeTemplates map[string]*template.Template |
| 20 | BeeTemplateExt string | 20 | BeeTemplateExt []string |
| 21 | ) | 21 | ) |
| 22 | 22 | ||
| 23 | func init() { | 23 | func init() { |
| 24 | BeeTemplates = make(map[string]*template.Template) | 24 | BeeTemplates = make(map[string]*template.Template) |
| 25 | beegoTplFuncMap = make(template.FuncMap) | 25 | beegoTplFuncMap = make(template.FuncMap) |
| 26 | BeeTemplateExt = "tpl" | 26 | BeeTemplateExt = make([]string, 0) |
| 27 | beegoTplFuncMap["markdown"] = MarkDown | 27 | BeeTemplateExt = append(BeeTemplateExt, "tpl", "html") |
| 28 | beegoTplFuncMap["dateformat"] = DateFormat | 28 | beegoTplFuncMap["markdown"] = MarkDown |
| 29 | beegoTplFuncMap["date"] = Date | 29 | beegoTplFuncMap["dateformat"] = DateFormat |
| 30 | beegoTplFuncMap["compare"] = Compare | 30 | beegoTplFuncMap["date"] = Date |
| 31 | } | 31 | beegoTplFuncMap["compare"] = Compare |
| 32 | 32 | } | |
| 33 | // MarkDown parses a string in MarkDown format and returns HTML. Used by the template parser as "markdown" | 33 | |
| 34 | func MarkDown(raw string) (output template.HTML) { | 34 | // MarkDown parses a string in MarkDown format and returns HTML. Used by the template parser as "markdown" |
| 35 | input := []byte(raw) | 35 | func MarkDown(raw string) (output template.HTML) { |
| 36 | bOutput := blackfriday.MarkdownBasic(input) | 36 | input := []byte(raw) |
| 37 | output = template.HTML(string(bOutput)) | 37 | bOutput := blackfriday.MarkdownBasic(input) |
| 38 | return | 38 | output = template.HTML(string(bOutput)) |
| 39 | } | 39 | return |
| 40 | 40 | } | |
| 41 | // DateFormat takes a time and a layout string and returns a string with the formatted date. Used by the template parser as "dateformat" | 41 | |
| 42 | func DateFormat(t time.Time, layout string) (datestring string) { | 42 | // DateFormat takes a time and a layout string and returns a string with the formatted date. Used by the template parser as "dateformat" |
| 43 | datestring = t.Format(layout) | 43 | func DateFormat(t time.Time, layout string) (datestring string) { |
| 44 | return | 44 | datestring = t.Format(layout) |
| 45 | } | 45 | return |
| 46 | 46 | } | |
| 47 | // Date takes a PHP like date func to Go's time fomate | 47 | |
| 48 | func Date(t time.Time, format string) (datestring string) { | 48 | // Date takes a PHP like date func to Go's time fomate |
| 49 | patterns := []string{ | 49 | func Date(t time.Time, format string) (datestring string) { |
| 50 | // year | 50 | patterns := []string{ |
| 51 | "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003 | 51 | // year |
| 52 | "y", "06", //A two digit representation of a year Examples: 99 or 03 | 52 | "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003 |
| 53 | 53 | "y", "06", //A two digit representation of a year Examples: 99 or 03 | |
| 54 | // month | 54 | |
| 55 | "m", "01", // Numeric representation of a month, with leading zeros 01 through 12 | 55 | // month |
| 56 | "n", "1", // Numeric representation of a month, without leading zeros 1 through 12 | 56 | "m", "01", // Numeric representation of a month, with leading zeros 01 through 12 |
| 57 | "M", "Jan", // A short textual representation of a month, three letters Jan through Dec | 57 | "n", "1", // Numeric representation of a month, without leading zeros 1 through 12 |
| 58 | "F", "January", // A full textual representation of a month, such as January or March January through December | 58 | "M", "Jan", // A short textual representation of a month, three letters Jan through Dec |
| 59 | 59 | "F", "January", // A full textual representation of a month, such as January or March January through December | |
| 60 | // day | 60 | |
| 61 | "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31 | 61 | // day |
| 62 | "j", "2", // Day of the month without leading zeros 1 to 31 | 62 | "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31 |
| 63 | 63 | "j", "2", // Day of the month without leading zeros 1 to 31 | |
| 64 | // week | 64 | |
| 65 | "D", "Mon", // A textual representation of a day, three letters Mon through Sun | 65 | // week |
| 66 | "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday | 66 | "D", "Mon", // A textual representation of a day, three letters Mon through Sun |
| 67 | 67 | "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday | |
| 68 | // time | 68 | |
| 69 | "g", "3", // 12-hour format of an hour without leading zeros 1 through 12 | 69 | // time |
| 70 | "G", "15", // 24-hour format of an hour without leading zeros 0 through 23 | 70 | "g", "3", // 12-hour format of an hour without leading zeros 1 through 12 |
| 71 | "h", "03", // 12-hour format of an hour with leading zeros 01 through 12 | 71 | "G", "15", // 24-hour format of an hour without leading zeros 0 through 23 |
| 72 | "H", "15", // 24-hour format of an hour with leading zeros 00 through 23 | 72 | "h", "03", // 12-hour format of an hour with leading zeros 01 through 12 |
| 73 | 73 | "H", "15", // 24-hour format of an hour with leading zeros 00 through 23 | |
| 74 | "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm | 74 | |
| 75 | "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM | 75 | "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm |
| 76 | 76 | "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM | |
| 77 | "i", "04", // Minutes with leading zeros 00 to 59 | 77 | |
| 78 | "s", "05", // Seconds, with leading zeros 00 through 59 | 78 | "i", "04", // Minutes with leading zeros 00 to 59 |
| 79 | } | 79 | "s", "05", // Seconds, with leading zeros 00 through 59 |
| 80 | replacer := strings.NewReplacer(patterns...) | 80 | } |
| 81 | format = replacer.Replace(format) | 81 | replacer := strings.NewReplacer(patterns...) |
| 82 | datestring = t.Format(format) | 82 | format = replacer.Replace(format) |
| 83 | return | 83 | datestring = t.Format(format) |
| 84 | } | 84 | return |
| 85 | 85 | } | |
| 86 | // Compare is a quick and dirty comparison function. It will convert whatever you give it to strings and see if the two values are equal. | 86 | |
| 87 | // Whitespace is trimmed. Used by the template parser as "eq" | 87 | // Compare is a quick and dirty comparison function. It will convert whatever you give it to strings and see if the two values are equal. |
| 88 | func Compare(a, b interface{}) (equal bool) { | 88 | // Whitespace is trimmed. Used by the template parser as "eq" |
| 89 | equal = false | 89 | func Compare(a, b interface{}) (equal bool) { |
| 90 | if strings.TrimSpace(fmt.Sprintf("%v", a)) == strings.TrimSpace(fmt.Sprintf("%v", b)) { | 90 | equal = false |
| 91 | equal = true | 91 | if strings.TrimSpace(fmt.Sprintf("%v", a)) == strings.TrimSpace(fmt.Sprintf("%v", b)) { |
| 92 | } | 92 | equal = true |
| 93 | return | 93 | } |
| 94 | } | 94 | return |
| 95 | 95 | } | |
| 96 | // AddFuncMap let user to register a func in the template | 96 | |
| 97 | func AddFuncMap(key string, funname interface{}) error { | 97 | // AddFuncMap let user to register a func in the template |
| 98 | if _, ok := beegoTplFuncMap[key]; ok { | 98 | func AddFuncMap(key string, funname interface{}) error { |
| 99 | return errors.New("funcmap already has the key") | 99 | if _, ok := beegoTplFuncMap[key]; ok { |
| 100 | } | 100 | return errors.New("funcmap already has the key") |
| 101 | beegoTplFuncMap[key] = funname | 101 | } |
| 102 | return nil | 102 | beegoTplFuncMap[key] = funname |
| 103 | } | 103 | return nil |
| 104 | 104 | } | |
| 105 | type templatefile struct { | 105 | |
| 106 | root string | 106 | type templatefile struct { |
| 107 | files map[string][]string | 107 | root string |
| 108 | } | 108 | files map[string][]string |
| 109 | 109 | } | |
| 110 | func (self *templatefile) visit(paths string, f os.FileInfo, err error) error { | 110 | |
| 111 | if f == nil { | 111 | func (self *templatefile) visit(paths string, f os.FileInfo, err error) error { |
| 112 | return err | 112 | if f == nil { |
| 113 | } | 113 | return err |
| 114 | if f.IsDir() { | 114 | } |
| 115 | return nil | 115 | if f.IsDir() { |
| 116 | } else if (f.Mode() & os.ModeSymlink) > 0 { | 116 | return nil |
| 117 | return nil | 117 | } else if (f.Mode() & os.ModeSymlink) > 0 { |
| 118 | } else { | 118 | return nil |
| 119 | if strings.HasSuffix(paths, BeeTemplateExt) { | 119 | } else { |
| 120 | a := []byte(paths) | 120 | hasExt := false |
| 121 | a = a[len([]byte(self.root)):] | 121 | for _, v := range BeeTemplateExt { |
| 122 | subdir := path.Dir(strings.TrimLeft(string(a), "/")) | 122 | if strings.HasSuffix(paths, v) { |
| 123 | if _, ok := self.files[subdir]; ok { | 123 | hasExt = true |
| 124 | self.files[subdir] = append(self.files[subdir], paths) | 124 | break |
| 125 | } else { | 125 | } |
| 126 | m := make([]string, 1) | 126 | } |
| 127 | m[0] = paths | 127 | if hasExt { |
| 128 | self.files[subdir] = m | 128 | a := []byte(paths) |
| 129 | } | 129 | a = a[len([]byte(self.root)):] |
| 130 | 130 | subdir := path.Dir(strings.TrimLeft(string(a), "/")) | |
| 131 | } | 131 | if _, ok := self.files[subdir]; ok { |
| 132 | } | 132 | self.files[subdir] = append(self.files[subdir], paths) |
| 133 | return nil | 133 | } else { |
| 134 | } | 134 | m := make([]string, 1) |
| 135 | 135 | m[0] = paths | |
| 136 | func SetGlobalTemplateExt(ext string) { | 136 | self.files[subdir] = m |
| 137 | BeeTemplateExt = ext | 137 | } |
| 138 | } | 138 | |
| 139 | 139 | } | |
| 140 | func BuildTemplate(dir string) error { | 140 | } |
| 141 | self := templatefile{ | 141 | return nil |
| 142 | root: dir, | 142 | } |
| 143 | files: make(map[string][]string), | 143 | |
| 144 | } | 144 | func AddTemplateExt(ext string) { |
| 145 | err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error { | 145 | for _, v := range BeeTemplateExt { |
| 146 | return self.visit(path, f, err) | 146 | if v == ext { |
| 147 | }) | 147 | return |
| 148 | if err != nil { | 148 | } |
| 149 | fmt.Printf("filepath.Walk() returned %v\n", err) | 149 | } |
| 150 | return err | 150 | BeeTemplateExt = append(BeeTemplateExt, ext) |
| 151 | } | 151 | } |
| 152 | for k, v := range self.files { | 152 | |
| 153 | BeeTemplates[k] = template.Must(template.New("beegoTemplate" + k).Funcs(beegoTplFuncMap).ParseFiles(v...)) | 153 | func BuildTemplate(dir string) error { |
| 154 | } | 154 | self := templatefile{ |
| 155 | return nil | 155 | root: dir, |
| 156 | } | 156 | files: make(map[string][]string), |
| 157 | } | ||
| 158 | err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error { | ||
| 159 | return self.visit(path, f, err) | ||
| 160 | }) | ||
| 161 | if err != nil { | ||
| 162 | fmt.Printf("filepath.Walk() returned %v\n", err) | ||
| 163 | return err | ||
| 164 | } | ||
| 165 | for k, v := range self.files { | ||
| 166 | BeeTemplates[k] = template.Must(template.New("beegoTemplate" + k).Funcs(beegoTplFuncMap).ParseFiles(v...)) | ||
| 167 | } | ||
| 168 | return nil | ||
| 169 | } | ... | ... |
-
Please register or sign in to post a comment