Esempio n. 1
0
func directiveInsertWordBreaks(value data.Value, args []data.Value) data.Value {
	var (
		input    = template.HTMLEscapeString(value.String())
		maxChars = int(args[0].(data.Int))
		chars    = 0
		output   *bytes.Buffer // create the buffer lazily
	)
	for i, ch := range input {
		switch {
		case ch == ' ':
			chars = 0
		case chars >= maxChars:
			if output == nil {
				output = bytes.NewBufferString(input[:i])
			}
			output.WriteString("<wbr>")
			chars = 1
		default:
			chars++
		}
		if output != nil {
			output.WriteRune(ch)
		}
	}
	if output == nil {
		return value
	}
	return data.String(output.String())
}
Esempio n. 2
0
func directiveTruncate(value data.Value, args []data.Value) data.Value {
	if !isInt(args[0]) {
		panic(fmt.Errorf("First parameter of '|truncate' is not an integer: %v", args[0]))
	}
	var maxLen = int(args[0].(data.Int))
	var str = value.String()
	if len(str) <= maxLen {
		return value
	}

	var ellipsis = data.Bool(true)
	if len(args) == 2 {
		var ok bool
		ellipsis, ok = args[1].(data.Bool)
		if !ok {
			panic(fmt.Errorf("Second parameter of '|truncate' is not a bool: %v", args[1]))
		}
	}

	if ellipsis {
		if maxLen > 3 {
			maxLen -= 3
		} else {
			ellipsis = false
		}
	}

	for !utf8.RuneStart(str[maxLen]) {
		maxLen--
	}

	str = str[:maxLen]
	if ellipsis {
		str += "..."
	}
	return data.String(str)
}
Esempio n. 3
0
func directiveChangeNewlineToBr(value data.Value, _ []data.Value) data.Value {
	return data.String(newlinePattern.ReplaceAllString(
		template.HTMLEscapeString(value.String()),
		"<br>"))
}
Esempio n. 4
0
func directiveEscapeJsString(value data.Value, _ []data.Value) data.Value {
	return data.String(template.JSEscapeString(value.String()))
}
Esempio n. 5
0
func directiveEscapeUri(value data.Value, _ []data.Value) data.Value {
	return data.String(url.QueryEscape(value.String()))
}
Esempio n. 6
0
func directiveEscapeHtml(value data.Value, _ []data.Value) data.Value {
	return data.String(template.HTMLEscapeString(value.String()))
}