diff options
| -rw-r--r-- | .gitignore | 7 | ||||
| -rw-r--r-- | Makefile | 12 | ||||
| -rw-r--r-- | html.go | 22 | ||||
| -rw-r--r-- | main.go | 93 | ||||
| -rw-r--r-- | text.go | 22 |
5 files changed, 156 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..15bc182 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +tg +*.exe +*.a +*.o +*.gz +*.tar +test/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..43fa093 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +APP=tg +SOURCES=$(wildcard *.go) $(GENERATED) + +all: build + +build: tg + +tg: $(SOURCES) + go build -o $(APP) + +clean: + $(RM) $(RMFLAG) $(APP) @@ -0,0 +1,22 @@ +package main + +import ( + "html/template" + "log" + "os" +) + +func ParseHTML(templates []string, data interface{}) { + // Parse the template dir + tmpl := template.Must(template.New("template").ParseGlob(*template_dir_f + "/*" + *ext_f)) + for _, template := range templates { + if template == "" { + continue + } + + err := tmpl.ExecuteTemplate(os.Stdout, template, data) + if err != nil { + log.Fatal(err) + } + } +} @@ -0,0 +1,93 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "log" + "os" + "strings" +) + +var ( + conf_f = flag.String("c", "", "Path to the configuration file.") + template_dir_f = flag.String("d", ".", "Template directory.") + template_f = flag.String("f", "", "Templates to render, a comma separated list.") + ext_f = flag.String("e", ".tmpl", "Extension of template files") + is_html_f = flag.Bool("html", false, "Enable HTML context sensitivy.") +) + +func main() { + flag.Parse() + + if *template_f == "" { + fmt.Println("Please provide a template to render with the -f option.") + os.Exit(1) + } + + // Reading config from environment if not set (case insensitive) + *conf_f = GetVarFromEnv(*conf_f, "config") + *template_dir_f = GetVarFromEnv(*template_dir_f, "template_directory") + *template_f = GetVarFromEnv(*template_f, "templates") + + // Read configuration file with the template data + data := NewConfiguration(*conf_f) + + // Splits the templates + var templates []string + if strings.Contains(*template_f, ",") { + templates = strings.Split(*template_f, ",") + } else { + templates = strings.Split(*template_f, " ") + } + + // Clean out the filenames if they start with the template dirname (shell globbing) + for index, template := range templates { + if strings.HasPrefix(template, *template_dir_f+"/") { + templates[index] = strings.TrimPrefix(template, *template_dir_f+"/") + } else { + templates[index] = strings.TrimPrefix(template, *template_dir_f) + } + } + + // Enable HTML context sensitivy on demand + if *is_html_f { + ParseHTML(templates, data) + } else { + ParseText(templates, data) + } +} + +func NewConfiguration(path string) interface{} { + if path == "" { + return nil + } + file, err := os.Open(path) + if err != nil { + log.Print("INFO: Error opening config file. Is it a valid file?") + return nil + } + + fi, err := file.Stat() + if err != nil { + log.Fatal(err) + } + + data := make([]byte, int64(fi.Size())) + file.Read(data) + + var json_data interface{} + json.Unmarshal(data, &json_data) + + return json_data +} + +func GetVarFromEnv(s, key string) string { + if s == "" { + s = os.Getenv(key) + } + if s == "" { + s = os.Getenv(strings.ToUpper(key)) + } + return s +} @@ -0,0 +1,22 @@ +package main + +import ( + "log" + "os" + "text/template" +) + +func ParseText(templates []string, data interface{}) { + // Parse the template dir + tmpl := template.Must(template.New("template").ParseGlob(*template_dir_f + "/*" + *ext_f)) + for _, template := range templates { + if template == "" { + continue + } + + err := tmpl.ExecuteTemplate(os.Stdout, template, data) + if err != nil { + log.Fatal(err) + } + } +} |
