Beispiel #1
0
func prepTemplate(parent *template.Template, fn, base string) error {
	t := parent.New(base)

	f, err := os.Open(fn)
	if err != nil {
		return err
	}
	defer f.Close()

	msg, err := mail.ReadMessage(f)
	if err != nil {
		return err
	}

	for k, v := range defaultHeaders {
		if msg.Header.Get(k) == "" {
			msg.Header[k] = v
		}
	}

	data := &bytes.Buffer{}

	// This is the only place I could find this method.  :/
	http.Header(msg.Header).Write(data)
	data.Write([]byte{'\r', '\n'})

	_, err = io.Copy(data, msg.Body)
	if err != nil {
		return err
	}

	_, err = t.Parse(data.String())
	return err
}
Beispiel #2
0
func makeTemplates(t *[]TemplateConfig) (*template.Template, error) {
	var textTemplates *template.Template
	var err error
	templateFuncs := template.FuncMap{"padBefore": PadBefore}
	for i, templateConfig := range *t {
		t := ""
		h := ""
		for i, l := range templateConfig.Lines {
			if i > 0 {
				t = t + "\t"
				h = h + "\t"
			}
			t = t + l.Value
			h = h + l.Header
		}

		// Template proper
		if i == 0 {
			textTemplates, err = template.New(templateConfig.Name).Funcs(templateFuncs).Parse(t)
		} else {
			textTemplates, err = textTemplates.New(templateConfig.Name).Funcs(templateFuncs).Parse(t)
		}

		// Template header
		textTemplates, err = textTemplates.New(templateConfig.Name + "_H").Funcs(templateFuncs).Parse(h)

		if err != nil {
			break
		}
	}
	return textTemplates, err
}
Beispiel #3
0
// LoadText loads a template from a possibly inlined VFS as an associated template to the parent
func LoadTextOnto(fs vfs.Opener, parent *txttmpl.Template, tName, fName string) (*txttmpl.Template, error) {
	src, err := templateSource(fs, fName)
	if err != nil {
		return nil, err
	}
	var tpl *txttmpl.Template
	if parent == nil {
		tpl = txttmpl.New(tName)
	} else {
		tpl = parent.New(tName)
	}
	return tpl.Parse(src)
}
Beispiel #4
0
func NewTemplating(partials *txttmpl.Template, filePath, content string) (*Templating, error) {
	t := Templating{
		name:      filePath,
		content:   CleanupOfTemplate(content),
		functions: TemplateFunctions,
	}
	if partials == nil {
		partials = txttmpl.New(t.name)
	}

	tmpl, err := partials.New(t.name).Funcs(t.functions).Funcs(map[string]interface{}(gtf.GtfFuncMap)).Parse(t.content)
	t.template = tmpl
	return &t, err
}
Beispiel #5
0
func addTemplate(t *template.Template, tname, fname string) (*template.Template, error) {
	buf, err := ioutil.ReadFile(fname)
	if err != nil {
		return nil, err
	}

	var nt *template.Template
	if t != nil {
		nt = t.New(tname)
	} else {
		nt = template.New(tname)
	}

	return nt.Parse(string(buf))
}
Beispiel #6
0
// Parse parses declared templates.
func Parse(t *template.Template) (*template.Template, error) {
	for name, s := range templates {
		var tmpl *template.Template
		if t == nil {
			t = template.New(name)
		}
		if name == t.Name() {
			tmpl = t
		} else {
			tmpl = t.New(name)
		}
		if _, err := tmpl.Parse(s); err != nil {
			return nil, err
		}
	}
	return t, nil
}
Beispiel #7
0
func SetTemplate(t *template.Template, name, filename string) error {

	// Read template
	buf, err := ioutil.ReadFile(filename)
	if err != nil {
		return err
	}

	// Update if exists
	if tt := t.Lookup(name); tt != nil {
		_, err = tt.Parse(string(buf))
		return err
	}

	// Allocate new name if not
	_, err = t.New(name).Parse(string(buf))
	return err
}
Beispiel #8
0
func (t *TemplateDir) LoadPartial() error {
	partials := []string{}

	directory, err := os.Open(t.src)
	if err != nil {
		return errs.WithEF(err, t.fields, "Failed to open template dir")
	}
	objects, err := directory.Readdir(-1)
	if err != nil {
		return errs.WithEF(err, t.fields, "Failed to read template dir")
	}
	for _, obj := range objects {
		if !obj.IsDir() && strings.HasSuffix(obj.Name(), ".partial") {
			partials = append(partials, t.src+"/"+obj.Name())
		}
	}

	if len(partials) == 0 {
		return nil
	}
	var tmpl *txttmpl.Template
	for _, partial := range partials {
		if tmpl == nil {
			tmpl = txttmpl.New(partial).Funcs(TemplateFunctions).Funcs(map[string]interface{}(gtf.GtfFuncMap))
		} else {
			tmpl = tmpl.New(partial).Funcs(TemplateFunctions).Funcs(map[string]interface{}(gtf.GtfFuncMap))
		}

		content, err := ioutil.ReadFile(partial)
		if err != nil {
			return errs.WithEF(err, t.fields.WithField("partial", partial), "Cannot read partial file")
		}
		tmpl, err = tmpl.Funcs(TemplateFunctions).Parse(CleanupOfTemplate(string(content)))
		if err != nil {
			return errs.WithEF(err, t.fields.WithField("partial", partial), "Failed to parse partial")
		}
	}
	t.Partials = tmpl
	return nil
}
Beispiel #9
0
//loadTemplate 加载解析模板
func (d *dynamic) loadTemplate(currDir string) (*template.Template, error) {
	if len(d.Template) == 0 {
		return nil, fmt.Errorf(ErrorPageNotTemplate)
	}
	var (
		t                           *template.Template = nil
		filePath, fileName, content string
		b                           []byte
		err                         error
	)

	for _, filename := range d.Template {
		filePath = filepath.Join(currDir, filename)
		fileName = filepath.Base(filename)

		b, err = ioutil.ReadFile(filePath)
		if err != nil {
			return nil, err
		}
		content = d.formatTemplate(string(b))

		var tmpl *template.Template
		if t == nil {
			t = template.New(fileName)
		}
		if fileName == t.Name() {
			tmpl = t
		} else {
			tmpl = t.New(fileName)
		}
		tmpl.Delims(d.Delims.Left, d.Delims.Right)
		tmpl.Funcs(TemplateFuncMap)
		_, err = tmpl.Parse(content)
		if err != nil {
			return nil, err
		}
	}
	return t, nil
}
Beispiel #10
0
func NewSunnyPublisher(p string) *SunnyPublisher {
	var htmpl *htemplate.Template = htemplate.New("index.html")
	var tmpl *template.Template = template.New("index")
	var plen int = 0

	if strings.Contains(p, `\`) {
		p = strings.Replace(p, `\`, "/", -1)
	} else if p == "" {
		p = DEFAULT_VIEWPATH
	}

	if plen = len(p); p[plen-1] != "/" {
		p = p + "/"
		plen += 1
	}

	filepath.Walk(p, func(path string, info os.FileInfo, err error) error {
		if err == nil && !info.IsDir() {
			b, err := ioutil.ReadFile(path)

			if err == nil {
				if strings.HasSuffix(info.Name(), ".html") {
					htmpl.New(path[plen:]).Parse(string(b))
				} else {
					tmpl.New(path[plen:]).Parse(string(b))
				}
			}
		}

		return nil
	})

	return &SunnyPublisher{
		renderer: make(map[string]Renderer),
		htmpl:    htmpl,
		tmpl:     tmpl,
	}
}
Beispiel #11
0
func addTemplate(base *template.Template, name string, filename string) *template.Template {
	text := loadTemplateText(filename)
	return template.Must(base.New(name).Parse(text))
}