示例#1
0
func (t *TruncateWordsFilter) Truncate(input interface{}, data map[string]interface{}) interface{} {
	length, ok := core.ToInt(t.limit.Resolve(data))
	if ok == false {
		return input
	}

	var value string
	switch typed := input.(type) {
	case string:
		value = typed
	default:
		value = core.ToString(typed)
	}

	i := 2
	found := 0
	l := len(value)
	for ; i < l; i++ {
		if value[i] == ' ' && value[i-1] != ' ' && value[i-2] != ' ' {
			found++
			if found == length {
				break
			}
		}
	}

	if i == l {
		return input
	}
	return value[:i] + core.ToString(t.append.Resolve(data))
}
示例#2
0
文件: for.go 项目: karlseguin/liquid
func (f *For) Execute(writer io.Writer, data map[string]interface{}) core.ExecuteState {
	value := reflect.ValueOf(f.value.Resolve(data))
	kind := value.Kind()

	if kind == reflect.Array || kind == reflect.Slice || kind == reflect.String || kind == reflect.Map {
		length := value.Len()
		if length == 0 {
			return core.Normal
		}

		state := &LoopState{
			data:            data,
			writer:          writer,
			originalForLoop: data["forloop"],
			Length:          length,
		}
		data["forloop"] = state
		if f.limit != nil {
			if limit, ok := core.ToInt(f.limit.Resolve(data)); ok && limit < length {
				state.Length = limit
			}
		}

		if f.offset != nil {
			if offset, ok := core.ToInt(f.offset.Resolve(data)); ok {
				state.offset = offset
				if n := state.Length + state.offset; n < state.Length {
					state.Length = n
				} else if length < state.offset+state.Length {
					state.Length = length - offset
				}
			}
		}

		defer f.loopTeardown(state)
		if kind == reflect.Map {
			state.isMap = true
			state.key = data[f.keyName]
			state.value = data[f.valueName]
			f.iterateMap(state, value)
		} else {
			state.value = data[f.name]
			f.iterateArray(state, value, kind == reflect.String)
		}
	}
	return core.Normal
}
示例#3
0
文件: date.go 项目: karlseguin/liquid
func inputToTime(input interface{}) (time.Time, bool) {
	switch typed := input.(type) {
	case time.Time:
		return typed, true
	case string:
		return timeFromString(typed)
	case []byte:
		return timeFromString(string(typed))
	}
	if n, ok := core.ToInt(input); ok {
		return core.Now().Add(time.Minute * time.Duration(n)), true
	}
	return zeroTime, false
}
示例#4
0
// Creates a plus filter
func ModuloFactory(parameters []core.Value) core.Filter {
	if len(parameters) == 0 {
		return Noop
	}
	switch typed := parameters[0].(type) {
	case *core.StaticIntValue:
		return (&IntModuloFilter{typed.Value}).Mod
	case *core.DynamicValue:
		return (&DynamicModuloFilter{typed}).Mod
	default:
		if n, ok := core.ToInt(parameters[0].Underlying()); ok {
			return (&IntModuloFilter{int(n)}).Mod
		}
		return Noop
	}
}
示例#5
0
// Creates a time filter
func TimesFactory(parameters []core.Value) core.Filter {
	if len(parameters) == 0 {
		return Noop
	}
	switch typed := parameters[0].(type) {
	case *core.StaticIntValue:
		return (&IntTimesFilter{typed.Value}).Times
	case *core.StaticFloatValue:
		return (&FloatTimesFilter{typed.Value}).Times
	case *core.DynamicValue:
		return (&DynamicTimesFilter{typed}).Times
	default:
		if n, ok := core.ToInt(parameters[0].Underlying()); ok {
			return (&IntTimesFilter{n}).Times
		}
		return Noop
	}
}
示例#6
0
func (t *TruncateFilter) Truncate(input interface{}, data map[string]interface{}) interface{} {
	length, ok := core.ToInt(t.limit.Resolve(data))
	if ok == false {
		return input
	}

	var value string
	switch typed := input.(type) {
	case string:
		value = typed
	default:
		value = core.ToString(typed)
	}

	append := core.ToString(t.append.Resolve(data))
	length -= len(append)
	if length >= len(value) {
		return input
	}
	if length < 0 {
		return append
	}
	return value[:length] + append
}
示例#7
0
func (m *DynamicModuloFilter) Mod(input interface{}, data map[string]interface{}) interface{} {
	if n, ok := core.ToInt(m.value.Resolve(data)); ok {
		return modInt(n, input)
	}
	return input
}