File: helper_method_yield.go

package info (click to toggle)
golang-github-yosssi-ace 0.0.4%2Bgit20160102.51.71afeb7-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 468 kB
  • ctags: 249
  • sloc: makefile: 3; sh: 1
file content (60 lines) | stat: -rw-r--r-- 1,462 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package ace

import (
	"bytes"
	"fmt"
	"io"
)

// helperMethodYield represents a helper method yield.
type helperMethodYield struct {
	elementBase
	templateName string
}

// WriteTo writes data to w.
func (e *helperMethodYield) WriteTo(w io.Writer) (int64, error) {
	var bf bytes.Buffer

	inner := e.src.inner
	if inner == nil {
		return 0, fmt.Errorf("inner is not specified [file: %s][line: %d]", e.ln.fileName(), e.ln.no)
	}

	var templateExists bool

	for _, innerE := range e.rslt.inner {
		ln := innerE.Base().ln
		if ln.isHelperMethodOf(helperMethodNameContent) && len(ln.tokens) > 2 && ln.tokens[2] == e.templateName {
			templateExists = true
			break
		}
	}

	if templateExists {
		bf.WriteString(fmt.Sprintf(actionTemplateWithPipeline, e.opts.DelimLeft, inner.path+doubleColon+e.templateName, dot, e.opts.DelimRight))
	} else {
		// Write the children's HTML.
		if i, err := e.writeChildren(&bf); err != nil {
			return i, err
		}
	}

	i, err := w.Write(bf.Bytes())

	return int64(i), err
}

// newHelperMethodYield creates and returns a helper method yield.
func newHelperMethodYield(ln *line, rslt *result, src *source, parent element, opts *Options) (*helperMethodYield, error) {
	if len(ln.tokens) < 3 {
		return nil, fmt.Errorf("no template name is specified [file: %s][line: %d]", ln.fileName(), ln.no)
	}

	e := &helperMethodYield{
		elementBase:  newElementBase(ln, rslt, src, parent, opts),
		templateName: ln.tokens[2],
	}

	return e, nil
}