|
7 | 7 | //////////////////////////////////////////////////////////////////////////// |
8 | 8 | // Program start |
9 | 9 |
|
10 | | -package main |
| 10 | +package easygen |
11 | 11 |
|
12 | 12 | import ( |
13 | | -"flag" |
| 13 | +"bytes" |
14 | 14 | "fmt" |
| 15 | +ht "html/template" |
| 16 | +"io" |
| 17 | +"io/ioutil" |
| 18 | +"os" |
| 19 | +"path/filepath" |
| 20 | +"strings" |
| 21 | +tt "text/template" |
15 | 22 |
|
16 | | -"github.com/suntong/easygen/easygenapi" |
| 23 | +"gopkg.in/yaml.v2" |
17 | 24 | ) |
18 | 25 |
|
19 | 26 | //////////////////////////////////////////////////////////////////////////// |
20 | 27 | // Constant and data type/structure definitions |
21 | 28 |
|
| 29 | +// common type for a *(text|html).Template value |
| 30 | +type template interface { |
| 31 | +Execute(wr io.Writer, data interface{}) error |
| 32 | +ExecuteTemplate(wr io.Writer, name string, data interface{}) error |
| 33 | +Name() string |
| 34 | +} |
| 35 | + |
22 | 36 | //////////////////////////////////////////////////////////////////////////// |
23 | 37 | // Global variables definitions |
24 | 38 |
|
25 | 39 | //////////////////////////////////////////////////////////////////////////// |
26 | | -// Commandline definitions |
| 40 | +// Function definitions |
27 | 41 |
|
28 | | -//////////////////////////////////////////////////////////////////////////// |
29 | | -// Main |
| 42 | +// Generate2 will produce output according to the given template and driving data files |
| 43 | +func Generate2(HTML bool, fileNameTempl string, fileName string) string { |
| 44 | +Opts.TemplateFile = fileNameTempl |
| 45 | +ret := Generate(HTML, fileName) |
| 46 | +Opts.TemplateFile = "" |
| 47 | +return ret |
| 48 | +} |
| 49 | + |
| 50 | +// Generate0 will produce output according from driving data without a template file |
| 51 | +func Generate0(HTML bool, strTempl string, fileName string) string { |
| 52 | +Opts.TemplateStr = strTempl |
| 53 | +ret := Generate(HTML, fileName) |
| 54 | +Opts.TemplateStr = "" |
| 55 | +return ret |
| 56 | +} |
| 57 | + |
| 58 | +// Generate will produce output from the template according to driving data |
| 59 | +func Generate(HTML bool, fileName string) string { |
| 60 | +source, err := ioutil.ReadFile(fileName + Opts.ExtYaml) |
| 61 | +checkError(err) |
30 | 62 |
|
31 | | -func main() { |
32 | | -flag.Usage = easygenapi.Usage |
33 | | -flag.Parse() |
| 63 | +m := make(map[interface{}]interface{}) |
34 | 64 |
|
35 | | -// One mandatory non-flag arguments |
36 | | -if len(flag.Args()) < 1 { |
37 | | -easygenapi.Usage() |
| 65 | +err = yaml.Unmarshal(source, &m) |
| 66 | +checkError(err) |
| 67 | + |
| 68 | +// template file name |
| 69 | +fileNameT := fileName |
| 70 | +if len(Opts.TemplateFile) > 0 { |
| 71 | +fileNameT = Opts.TemplateFile |
38 | 72 | } |
39 | | -fileName := flag.Args()[0] |
40 | | -easygenapi.TFStringsInit() |
41 | 73 |
|
42 | | -fmt.Print(easygenapi.Generate(easygenapi.Opts.HTML, fileName)) |
| 74 | +t, err := parseFiles(HTML, fileNameT+Opts.ExtTmpl) |
| 75 | +checkError(err) |
| 76 | + |
| 77 | +buf := new(bytes.Buffer) |
| 78 | +err = t.Execute(buf, m) |
| 79 | +checkError(err) |
| 80 | + |
| 81 | +return buf.String() |
| 82 | +} |
| 83 | + |
| 84 | +// parseFiles, initialization. By Matt Harden @gmail.com |
| 85 | +func parseFiles(HTML bool, filenames ...string) (template, error) { |
| 86 | +tname := filepath.Base(filenames[0]) |
| 87 | + |
| 88 | +// use text template |
| 89 | +funcMapHT := ht.FuncMap{ |
| 90 | +"minus1": minus1, |
| 91 | +"cls2lc": cls2lc.String, |
| 92 | +"cls2uc": cls2uc.String, |
| 93 | +"cls2ss": cls2ss.String, |
| 94 | +"ck2lc": ck2lc.String, |
| 95 | +"ck2uc": ck2uc.String, |
| 96 | +"ck2ls": ck2ls.String, |
| 97 | +"ck2ss": ck2ss.String, |
| 98 | +"clc2ss": clc2ss.String, |
| 99 | +"cuc2ss": cuc2ss.String, |
| 100 | +} |
| 101 | + |
| 102 | +_ = funcMapHT |
| 103 | + |
| 104 | +if HTML { |
| 105 | +// use html template |
| 106 | +t, err := ht.ParseFiles(filenames...) |
| 107 | +//t, err := ht.New("HT").Funcs(funcMapHT).ParseFiles(filenames...) |
| 108 | +return t, err |
| 109 | +} |
| 110 | + |
| 111 | +// use text template |
| 112 | +funcMap := tt.FuncMap{ |
| 113 | +"eqf": strings.EqualFold, |
| 114 | +"split": strings.Fields, |
| 115 | +"minus1": minus1, |
| 116 | +"replace": replace, |
| 117 | +"replacec": replacec, |
| 118 | +"cls2lc": cls2lc.String, |
| 119 | +"cls2uc": cls2uc.String, |
| 120 | +"cls2ss": cls2ss.String, |
| 121 | +"ck2lc": ck2lc.String, |
| 122 | +"ck2uc": ck2uc.String, |
| 123 | +"ck2ls": ck2ls.String, |
| 124 | +"ck2ss": ck2ss.String, |
| 125 | +"clc2ss": clc2ss.String, |
| 126 | +"cuc2ss": cuc2ss.String, |
| 127 | +} |
| 128 | + |
| 129 | +if len(Opts.TemplateStr) > 0 { |
| 130 | +t, err := tt.New("TT").Funcs(funcMap).Parse(Opts.TemplateStr) |
| 131 | +return t, err |
| 132 | +} |
| 133 | + |
| 134 | +t, err := tt.New(tname).Funcs(funcMap).ParseFiles(filenames...) |
| 135 | +return t, err |
| 136 | +} |
| 137 | + |
| 138 | +// Exit if error occurs |
| 139 | +func checkError(err error) { |
| 140 | +if err != nil { |
| 141 | +fmt.Printf("[%s] Fatal error - %v", progname, err.Error()) |
| 142 | +os.Exit(1) |
| 143 | +} |
43 | 144 | } |
0 commit comments