예제 #1
0
// renderComponent - Renders a component.
// `main` is where `Styles` and `Scripts` are inserted.
// `res` is where `Body` is inserted.
func renderComponent(c *Compiled, main, res *Rendered, ctx template.Context) (err error) {
	// Set component defaults
	ctx, err = withComponentDefaults(c, ctx)
	if err != nil {
		return
	}

	if glog.V(11) {
		glog.Infof("[render] name=%q ctx=%#v", c.Name, ctx)
	}

	// Render required components
	for name, req := range c.Require {
		r := new(Rendered)
		err = renderComponent(req, main, r, ctx)
		if err != nil {
			return
		}

		// Add resulting body to context
		ctx[name] = pongo2.AsSafeValue(r.Body)
	}

	// Render `Main` component template
	if c.Main != nil {
		res.Body, err = template.ExecuteToString(c.Main, ctx)
		if err != nil {
			return
		}
	}

	// Extend a template if any
	if c.Extends != nil {
		if res.Body != "" {
			ctx["children"] = pongo2.AsSafeValue(res.Body)
		}
		err = renderComponent(c.Extends, main, res, ctx)
		if err != nil {
			return
		}
	}

	// Render component styles and scripts
	err = renderAssets(c, main, ctx)
	if err != nil {
		return
	}

	return
}
예제 #2
0
func filterRead(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
	data, err := ioutil.ReadFile(in.String())
	if err != nil {
		return nil, &pongo2.Error{
			Sender:   "filter:read",
			ErrorMsg: err.Error(),
		}
	}

	return pongo2.AsSafeValue(string(data)), nil
}
예제 #3
0
func filterTruncatesentencesHtml(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
	count := param.Integer()
	if count <= 0 {
		return pongo2.AsValue(""), nil
	}

	value := in.String()
	newLen := max(param.Integer(), 0)

	new_output := bytes.NewBuffer(nil)

	sentencefilter := 0

	filterTruncateHtmlHelper(value, new_output, func() bool {
		return sentencefilter >= newLen
	}, func(_ rune, _ int, idx int) int {
		// Get next word
		word_found := false

		for idx < len(value) {
			c2, size2 := utf8.DecodeRuneInString(value[idx:])
			if c2 == utf8.RuneError {
				idx += size2
				continue
			}

			if c2 == '<' {
				// HTML tag start, don't consume it
				return idx
			}

			new_output.WriteRune(c2)
			idx += size2

			if (c2 == '.' && !(idx+1 < len(value) && value[idx+1] >= '0' && value[idx+1] <= '9')) ||
				c2 == '!' || c2 == '?' || c2 == '\n' {
				// Sentence ends here, stop capturing it now
				break
			} else {
				word_found = true
			}
		}

		if word_found {
			sentencefilter++
		}

		return idx
	}, func() {})

	return pongo2.AsSafeValue(new_output.String()), nil
}
예제 #4
0
파일: html.go 프로젝트: hlandau/degoutils
// Generates HTML (as a pongo2 safe value) for the given form. The given
// function is used for field layout.
func FormHTML(s interface{}, req *http.Request, htmlFunc HTMLFunc) *pongo2.Value {
	buf := bytes.Buffer{}
	sv := reflect.Indirect(reflect.ValueOf(s))
	st := sv.Type()

	numFields := sv.NumField()
	for i := 0; i < numFields; i++ {
		fv := sv.Field(i)
		ff := st.Field(i)
		finfo := GetFieldInfo(ff)

		value := fmt.Sprintf("%v", fv.Interface())
		htmlFunc(&finfo, value, req, &buf)
	}

	return pongo2.AsSafeValue(buf.String())
}
예제 #5
0
func formatTextP2(s string) *pongo2.Value {
	return pongo2.AsSafeValue(formatText(s))
}
예제 #6
0
func filterMarkdown(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
	return pongo2.AsSafeValue(string(blackfriday.MarkdownCommon([]byte(in.String())))), nil
}