Exemplo n.º 1
0
func urlFunc(helper *structure.Helper, values *structure.RequestData) []byte {
	var buffer bytes.Buffer
	if len(helper.Arguments) != 0 {
		arguments := methods.ProcessHelperArguments(helper.Arguments)
		for key, value := range arguments {
			if key == "absolute" {
				if value == "true" {
					// Only write the blog url if navigation url does not begin with http/https
					if values.CurrentHelperContext == 4 && (!strings.HasPrefix(values.Blog.NavigationItems[values.CurrentNavigationIndex].Url, "http://") && !strings.HasPrefix(values.Blog.NavigationItems[values.CurrentNavigationIndex].Url, "https://")) { // navigation
						buffer.Write(values.Blog.Url)
					} else if values.CurrentHelperContext != 4 {
						buffer.Write(values.Blog.Url)
					}
				}
			}
		}
	}
	if values.CurrentHelperContext == 1 { // post
		buffer.WriteString("/")
		buffer.WriteString(values.Posts[values.CurrentPostIndex].Slug)
		buffer.WriteString("/")
		return evaluateEscape(buffer.Bytes(), helper.Unescaped)
	} else if values.CurrentHelperContext == 3 { // author
		buffer.WriteString("/author/")
		// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
		buffer.WriteString(values.Posts[values.CurrentPostIndex].Author.Slug)
		buffer.WriteString("/")
		return evaluateEscape(buffer.Bytes(), helper.Unescaped)
	} else if values.CurrentHelperContext == 4 { // author
		buffer.WriteString(values.Blog.NavigationItems[values.CurrentNavigationIndex].Url)
		return evaluateEscape(buffer.Bytes(), helper.Unescaped)
	}
	return []byte{}
}
Exemplo n.º 2
0
func dateFunc(helper *structure.Helper, values *structure.RequestData) []byte {
	showPublicationDate := false
	timeFormat := "MMM Do, YYYY" // Default time format
	// If in scope of a post, change default to published date
	if values.CurrentHelperContext == 1 { // post
		showPublicationDate = true
	}
	// Get the date
	if len(helper.Arguments) != 0 {
		arguments := methods.ProcessHelperArguments(helper.Arguments)
		for key, value := range arguments {
			if key == "published_at" {
				showPublicationDate = true
			} else if key == "timeago" {
				if value == "true" {
					// Compute time ago
					return evaluateEscape(generateTimeAgo(values.Posts[values.CurrentPostIndex].Date), helper.Unescaped)
				}
			} else if key == "format" {
				timeFormat = value
			}
		}
	}
	if showPublicationDate {
		return evaluateEscape(formatDate(timeFormat, values.Posts[values.CurrentPostIndex].Date), helper.Unescaped)
	}
	date := time.Now()
	return evaluateEscape(formatDate(timeFormat, &date), helper.Unescaped)
}
Exemplo n.º 3
0
func pluralFunc(helper *structure.Helper, values *structure.RequestData) []byte {
	if len(helper.Arguments) != 0 {
		// Get the number calculated by executing the first argument
		countString := string(helper.Arguments[0].Function(helper, values))
		if countString == "" {
			log.Println("Couldn't get count in plural helper")
			return []byte{}
		}
		arguments := methods.ProcessHelperArguments(helper.Arguments)
		for key, value := range arguments {
			if countString == "0" && key == "empty" {
				output := value
				output = strings.Replace(output, "%", countString, -1)
				return []byte(output)
			} else if countString == "1" && key == "singular" {
				output := value
				output = strings.Replace(output, "%", countString, -1)
				return []byte(output)
			} else if countString != "0" && countString != "1" && key == "plural" {
				output := value
				output = strings.Replace(output, "%", countString, -1)
				return []byte(output)
			}
		}
	}
	return []byte{}
}
Exemplo n.º 4
0
func convertArguments(vm *lua.LState, structureArguments []structure.Helper) *lua.LTable {
	table := vm.NewTable()
	arguments := methods.ProcessHelperArguments(structureArguments)
	for key, value := range arguments {
		table.RawSet(lua.LString(key), lua.LString(value))
	}
	return table
}
Exemplo n.º 5
0
func tagsFunc(helper *structure.Helper, values *structure.RequestData) []byte {
	if len(values.Posts[values.CurrentPostIndex].Tags) > 0 {
		separator := ", "
		suffix := ""
		prefix := ""
		makeLink := true
		if len(helper.Arguments) != 0 {
			arguments := methods.ProcessHelperArguments(helper.Arguments)
			for key, value := range arguments {
				if key == "separator" {
					separator = value
				} else if key == "suffix" {
					suffix = value
				} else if key == "prefix" {
					prefix = value
				} else if key == "autolink" {
					if value == "false" {
						makeLink = false
					}
				}
			}
		}
		var buffer bytes.Buffer
		if prefix != "" {
			buffer.WriteString(prefix)
			buffer.WriteString(" ")
		}
		for index, tag := range values.Posts[values.CurrentPostIndex].Tags {
			if index != 0 {
				buffer.WriteString(separator)
			}
			if makeLink {
				buffer.WriteString("<a href=\"")
				buffer.WriteString("/tag/")
				buffer.WriteString(tag.Slug)
				buffer.WriteString("/\">")
			}
			buffer.Write(evaluateEscape(tag.Name, helper.Unescaped))
			if makeLink {
				buffer.WriteString("</a>")
			}
		}
		if suffix != "" {
			buffer.WriteString(" ")
			buffer.WriteString(suffix)
		}
		return buffer.Bytes()
	}
	return []byte{}
}
Exemplo n.º 6
0
func excerptFunc(helper *structure.Helper, values *structure.RequestData) []byte {
	if values.CurrentHelperContext == 1 { // post
		if len(helper.Arguments) != 0 {
			arguments := methods.ProcessHelperArguments(helper.Arguments)
			for key, value := range arguments {
				if key == "words" {
					number, err := strconv.Atoi(value)
					if err == nil {
						excerpt := conversion.StripTagsFromHtml(values.Posts[values.CurrentPostIndex].Html)
						words := bytes.Fields(excerpt)
						if len(words) < number {
							return excerpt
						}
						return bytes.Join(words[:number], []byte(" "))
					}
				} else if key == "characters" {
					number, err := strconv.Atoi(value)
					if err == nil {
						// Use runes for UTF-8 support
						runes := []rune(string(conversion.StripTagsFromHtml(values.Posts[values.CurrentPostIndex].Html)))
						if len(runes) < number {
							return []byte(string(runes))
						}
						return []byte(string(runes[:number]))
					}
				}
			}
		}
		// Default to 50 words excerpt
		excerpt := conversion.StripTagsFromHtml(values.Posts[values.CurrentPostIndex].Html)
		words := bytes.Fields(excerpt)
		if len(words) < 50 {
			return excerpt
		}
		return bytes.Join(words[:50], []byte(" "))
	}
	return []byte{}
}
Exemplo n.º 7
0
func authorFunc(helper *structure.Helper, values *structure.RequestData) []byte {
	// Check if helper is block helper
	if len(helper.Block) != 0 {
		return executeHelper(helper, values, 3) // context = author
	}
	// Else return author name (as link)
	arguments := methods.ProcessHelperArguments(helper.Arguments)
	for key, value := range arguments {
		// If link is set to false, just return the name
		if key == "autolink" && value == "false" {
			return evaluateEscape(values.Posts[values.CurrentPostIndex].Author.Name, helper.Unescaped)
		}
	}
	var buffer bytes.Buffer
	buffer.WriteString("<a href=\"")
	buffer.WriteString("/author/")
	// TODO: Error handling if there i no Posts[values.CurrentPostIndex]
	buffer.WriteString(values.Posts[values.CurrentPostIndex].Author.Slug)
	buffer.WriteString("/\">")
	// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
	buffer.Write(evaluateEscape(values.Posts[values.CurrentPostIndex].Author.Name, helper.Unescaped))
	buffer.WriteString("</a>")
	return buffer.Bytes()
}