Esempio n. 1
0
func (t *GoHTMLTemplate) AddAceTemplate(name, basePath, innerPath string, baseContent, innerContent []byte) error {
	t.checkState()
	var base, inner *ace.File
	name = name[:len(name)-len(filepath.Ext(innerPath))] + ".html"

	// Fixes issue #1178
	basePath = strings.Replace(basePath, "\\", "/", -1)
	innerPath = strings.Replace(innerPath, "\\", "/", -1)

	if basePath != "" {
		base = ace.NewFile(basePath, baseContent)
		inner = ace.NewFile(innerPath, innerContent)
	} else {
		base = ace.NewFile(innerPath, innerContent)
		inner = ace.NewFile("", []byte{})
	}
	parsed, 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), parsed, nil)
	if err != nil {
		t.errors = append(t.errors, &templateErr{name: name, err: err})
	}
	return err
}
Esempio n. 2
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

}
Esempio n. 3
0
File: ace.go Progetto: go-gonzo/ace
func Compile(options Options, data interface{}) gonzo.Stage {
	return func(ctx context.Context, in <-chan gonzo.File, out chan<- gonzo.File) error {

		options := ace.Options(options)

		fs := []*ace.File{}

		for {
			select {
			case file, ok := <-in:
				if !ok {
					return nil
				}

				buf := new(bytes.Buffer)
				_, err := buf.ReadFrom(file)
				file.Close()
				if err != nil {
					ctx.Error(err)
					continue
				}

				s, err := file.Stat()
				if err != nil {
					return err
				}

				name := s.Name() //Probably filepath.Rel(file.Dir, file.Path) ??
				f := ace.NewFile(name, buf.Bytes())
				source := ace.NewSource(
					ace.NewFile("", nil),
					f,
					fs,
				)

				fs = append(fs, f)

				r, err := ace.ParseSource(source, &options)
				if err != nil {
					ctx.Error(err)
					continue
				}

				t, err := ace.CompileResultWithTemplate(html.New(name), r, &options)
				if err != nil {
					ctx.Error(err)
					continue
				}

				if filepath.Base(file.FileInfo().Name())[0] != '_' {
					buf := new(bytes.Buffer)
					err = t.Execute(buf, data)
					if err != nil {
						ctx.Error(err)
						continue
					}

					file = gonzo.NewFile(ioutil.NopCloser(buf), file.FileInfo())
					file.FileInfo().SetSize(int64(buf.Len()))
					name = strings.TrimSuffix(file.FileInfo().Name(), ".ace") + ".html"
					file.FileInfo().SetName(name)
				}

				out <- file

			case <-ctx.Done():
				return ctx.Err()
			}
			return nil
		}
	}
}