Пример #1
0
func (t *GoHtmlTemplate) LoadTemplates(absPath string) {
	walker := func(path string, fi os.FileInfo, err error) error {
		if err != nil {
			return nil
		}

		if !fi.IsDir() {
			if ignoreDotFile(path) {
				return nil
			}

			tplName := t.generateTemplateNameFrom(absPath, path)

			if strings.HasSuffix(path, ".amber") {
				compiler := amber.New()
				// Parse the input file
				if err := compiler.ParseFile(path); err != nil {
					return nil
				}

				// note t.New(tplName)
				if _, err := compiler.CompileWithTemplate(t.New(tplName)); err != nil {
					return err
				}

			} else {
				t.AddTemplateFile(tplName, path)
			}
		}
		return nil
	}

	filepath.Walk(absPath, walker)
}
Пример #2
0
func main() {
	input := flag.Arg(0)

	if len(input) == 0 {
		fmt.Fprintln(os.Stderr, "Please provide an input file. (amberc input.amber)")
		os.Exit(1)
	}

	cmp := amber.New()
	cmp.PrettyPrint = prettyPrint
	cmp.LineNumbers = lineNumbers

	err := cmp.ParseFile(input)

	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	err = cmp.CompileWriter(os.Stdout)

	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}
Пример #3
0
func (t *GoHtmlTemplate) AddTemplateFile(name, path string) error {
	// get the suffix and switch on that
	ext := filepath.Ext(path)
	switch ext {
	case ".amber":
		compiler := amber.New()
		// Parse the input file
		if err := compiler.ParseFile(path); err != nil {
			return nil
		}

		if _, err := compiler.CompileWithTemplate(t.New(name)); err != nil {
			return err
		}
	default:
		b, err := ioutil.ReadFile(path)
		if err != nil {
			return err
		}

		return t.AddTemplate(name, string(b))
	}

	return nil

}
Пример #4
0
func viewHandler(w http.ResponseWriter, r *http.Request) {
	title := r.URL.Path[1:]
	p, err := loadMarkdownPage(title)
	if err != nil {
		http.Redirect(w, r, "/error", http.StatusFound)
		return
	}

	file, _ := ioutil.ReadFile("./config.json")

	ps := PageStruct{
		"",
		"",
		string(p[0:]),
		readPagesDir()}

	json.Unmarshal(file, &ps)

	buff := bytes.NewBufferString("")

	compiler := amber.New()
	// Parse the input file
	cerr := compiler.ParseFile("./layout.amber")
	if cerr == nil {
		// Compile input file to Go template
		tmplHead, err := compiler.Compile()
		if err == nil {
			tmplHead.Execute(buff, ps)
			fmt.Fprintf(w, buff.String())
		} else {
			log.Fatal(err)
		}
	}
}
Пример #5
0
func (t *GoHTMLTemplate) AddTemplateFile(name, baseTemplatePath, path string) error {
	t.checkState()
	// get the suffix and switch on that
	ext := filepath.Ext(path)
	switch ext {
	case ".amber":
		templateName := strings.TrimSuffix(name, filepath.Ext(name)) + ".html"
		compiler := amber.New()
		b, err := afero.ReadFile(hugofs.Source(), path)

		if err != nil {
			return err
		}

		// Parse the input data
		if err := compiler.ParseData(b, path); err != nil {
			return err
		}

		if _, err := compiler.CompileWithTemplate(t.New(templateName)); err != nil {
			return err
		}
	case ".ace":
		var innerContent, baseContent []byte
		innerContent, err := afero.ReadFile(hugofs.Source(), path)

		if err != nil {
			return err
		}

		if baseTemplatePath != "" {
			baseContent, err = afero.ReadFile(hugofs.Source(), baseTemplatePath)
			if err != nil {
				return err
			}
		}

		return t.AddAceTemplate(name, baseTemplatePath, path, baseContent, innerContent)
	default:

		if baseTemplatePath != "" {
			return t.AddTemplateFileWithMaster(name, path, baseTemplatePath)
		}

		b, err := afero.ReadFile(hugofs.Source(), path)

		if err != nil {
			return err
		}

		jww.DEBUG.Printf("Add template file from path %s", path)

		return t.AddTemplate(name, string(b))
	}

	return nil

}
Пример #6
0
func (t *GoHTMLTemplate) AddTemplateFile(name, baseTemplatePath, path string) error {
	// get the suffix and switch on that
	ext := filepath.Ext(path)
	switch ext {
	case ".amber":
		compiler := amber.New()
		// Parse the input file
		if err := compiler.ParseFile(path); err != nil {
			return nil
		}

		if _, err := compiler.CompileWithTemplate(t.New(name)); err != nil {
			return err
		}
	case ".ace":
		b, err := ioutil.ReadFile(path)
		if err != nil {
			return err
		}

		var base, inner *ace.File

		name = name[:len(name)-len(ext)] + ".html"
		if baseTemplatePath != "" {
			b2, err := ioutil.ReadFile(baseTemplatePath)
			if err != nil {
				return err
			}
			base = ace.NewFile(baseTemplatePath, b2)
			inner = ace.NewFile(path, b)
		} else {
			base = ace.NewFile(path, b)
			inner = ace.NewFile("", []byte{})
		}
		rslt, err := ace.ParseSource(ace.NewSource(base, inner, []*ace.File{}), nil)
		if err != nil {
			t.errors = append(t.errors, &templateErr{name: name, err: err})
			return err
		}
		_, err = ace.CompileResultWithTemplate(t.New(name), rslt, nil)
		if err != nil {
			t.errors = append(t.errors, &templateErr{name: name, err: err})
		}
		return err
	default:
		b, err := ioutil.ReadFile(path)
		if err != nil {
			return err
		}

		return t.AddTemplate(name, string(b))
	}

	return nil

}
Пример #7
0
func hello(w http.ResponseWriter, r *http.Request) {
	compiler := amber.New()
	compiler.ParseFile("tpl/index.amber")
	tpl, err := compiler.Compile()
	if err != nil {
		fmt.Fprint(w, "Error: %v", err)
		return
	}
	tpl.Execute(w, map[string]interface{}{"date": time.Now().Format("Mon Jan 2 2006")})
}
Пример #8
0
// Implementation of the TemplateCompiler interface.
func (this *AmberCompiler) Compile(f string) (templates.Templater, error) {
	// amber.CompileFile creates a new compiler each time. To limit the number
	// of allocations, reuse a compiler.
	if this.c == nil {
		this.c = amber.New()
	}
	this.c.Options = this.Options
	if err := this.c.ParseFile(f); err != nil {
		return nil, err
	}
	return this.c.Compile()
}
Пример #9
0
func compile_template(n string) *template.Template {
	compiler := amber.New()
	err := compiler.ParseFile(fmt.Sprintf("views/%v", n))
	if err != nil {
		log.Fatalf("Bad template: %v: %v\n", n, err)
	}
	tpl, err := compiler.Compile()
	if err != nil {
		log.Fatalf("Error compiling template: %v: %v\n", n, err)
	}
	return tpl
}
Пример #10
0
func getTemplate() *template.Template {
	// template.Must(tmpl.Parse(status_server.ServerTemplate))
	// Amber template compiler
	compiler := amber.New()
	compiler.Options.PrettyPrint = false
	compiler.Options.LineNumbers = false

	// err := compiler.Parse(status_server.ServerTemplateAmber)
	err := compiler.Parse(ServerTemplateAmber)

	if nil != err {
		log.Println(err)
	}

	return template.Must(compiler.Compile())
}
Пример #11
0
func form(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		compiler := amber.New()
		compiler.ParseFile("tpl/form.amber")
		tpl, err := compiler.Compile()
		if err != nil {
			serveError(w, err)
		}
		tpl.Execute(w, map[string]interface{}{})

	} else if r.Method == "POST" {
		if err := r.ParseForm(); err != nil {
			serveError(w, err)
		}
		str := html.EscapeString(r.Form.Get("str"))
		fmt.Fprint(w, Reverse(str))
	}
}
Пример #12
0
func (t *GoHTMLTemplate) AddTemplateFile(name, baseTemplatePath, path string) error {
	// get the suffix and switch on that
	ext := filepath.Ext(path)
	switch ext {
	case ".amber":
		templateName := strings.TrimSuffix(name, filepath.Ext(name)) + ".html"
		compiler := amber.New()
		// Parse the input file
		if err := compiler.ParseFile(path); err != nil {
			return err
		}

		if _, err := compiler.CompileWithTemplate(t.New(templateName)); err != nil {
			return err
		}
	case ".ace":
		var innerContent, baseContent []byte
		innerContent, err := ioutil.ReadFile(path)

		if err != nil {
			return err
		}

		if baseTemplatePath != "" {
			baseContent, err = ioutil.ReadFile(baseTemplatePath)
			if err != nil {
				return err
			}
		}

		return t.AddAceTemplate(name, baseTemplatePath, path, baseContent, innerContent)
	default:
		b, err := ioutil.ReadFile(path)
		if err != nil {
			return err
		}

		return t.AddTemplate(name, string(b))
	}

	return nil

}
Пример #13
0
func compile(dir string) *template.Template {
	t := template.New(dir)

	filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
		r, err := filepath.Rel(dir, path)
		if err != nil {
			return err
		}

		ext := filepath.Ext(r)
		if ext == ".tmpl" {

			buf, err := ioutil.ReadFile(path)
			if err != nil {
				panic(err)
			}

			name := (r[0 : len(r)-len(ext)])
			tmpl := t.New(filepath.ToSlash(name))
			// Bomb out if parse fails. We don't want any silent server starts.
			template.Must(tmpl.Parse(string(buf)))
		} else if ext == ".amber" {
			fmt.Println("amber")
			compiler := amber.New()

			err := compiler.ParseFile(path)

			if err != nil {
				panic(err)
			}

			name := (r[0 : len(r)-len(ext)])
			tmpl := t.New(filepath.ToSlash(name))

			template.Must(compiler.CompileWithTemplate(tmpl))
		}

		return nil
	})

	return t
}
Пример #14
0
func ShowNodes(w http.ResponseWriter, req *http.Request) {
	ns, _ := GetNodes()

	var node_data map[string][]node = make(map[string][]node, 0)
	var nodes []node = make([]node, 0, 1)

	for _, n := range ns {
		reports := make([]node_report, 0, 2)
		for name, report := range n.Reports {
			var kv_pairs []string = make([]string, 0, 2)
			for key, value := range report {
				kv_pairs = append(kv_pairs, key+"="+value)
			}
			kv_str := strings.Join(kv_pairs, ", ")

			rep := node_report{name, kv_str, "success"}
			reports = append(reports, rep)
		}
		nodes = append(nodes, node{n.System["node_name"], reports, "success"})
	}

	node_data["Nodes"] = nodes

	compiler := amber.New()
	err := compiler.ParseFile("templates/index.amber")
	if err != nil {
		io.WriteString(w, "Amber parse error!\n"+err.Error())
		return
	}

	tpl, err := compiler.Compile()
	if err != nil {
		io.WriteString(w, "Amber compile error!\n"+err.Error())
		return
	}

	tpl.Execute(w, node_data)
} // End ShowNodes. ShowNodes should go somewhere else!
Пример #15
0
func (t *GoHtmlTemplate) loadTemplates(absPath string, prefix string) {
	walker := func(path string, fi os.FileInfo, err error) error {
		if err != nil {
			return nil
		}

		if !fi.IsDir() {
			if ignoreDotFile(path) {
				return nil
			}

			tplName := t.generateTemplateNameFrom(absPath, path)

			if prefix != "" {
				tplName = strings.Trim(prefix, "/") + "/" + tplName
			}

			// TODO move this into the AddTemplateFile function
			if strings.HasSuffix(path, ".amber") {
				compiler := amber.New()
				// Parse the input file
				if err := compiler.ParseFile(path); err != nil {
					return nil
				}

				if _, err := compiler.CompileWithTemplate(t.New(tplName)); err != nil {
					return err
				}

			} else {
				t.AddTemplateFile(tplName, path)
			}
		}
		return nil
	}

	filepath.Walk(absPath, walker)
}