package rss2
import (
"fmt"
"github/fthvgb1/wp-go/helper"
"strings"
)
var template = `
{$title}
{$link}
{$description}
{$lastBuildDate}
{$lang}
{$updatePeriod}
{$updateFrequency}
{$generator}
{$items}
`
var templateItems = `
-
{$title}
{$link}
{$comments}
{$pubDate}
{$category}
{$guid}
{$description}
{$commentRss}
{$commentNumber}
`
type Rss2 struct {
Title string
AtomLink string
Link string
Description string
LastBuildDate string
Language string
UpdatePeriod string
UpdateFrequency int
Generator string
Items []Item
}
type Item struct {
Title string
Link string
CommentLink string
Creator string
PubDate string
Category string
Guid string
Description string
Content string
CommentRss string
SlashComments int
}
func (r Rss2) GetXML() (xml string) {
xml = template
for k, v := range map[string]string{
"{$title}": r.Title,
"{$link}": r.Link,
"{$feedLink}": r.AtomLink,
"{$description}": r.Description,
"{$lastBuildDate}": r.LastBuildDate,
"{$lang}": r.Language,
"{$updatePeriod}": r.UpdatePeriod,
"{$updateFrequency}": fmt.Sprintf("%d", r.UpdateFrequency),
"{$generator}": r.Generator,
"{$items}": strings.Join(helper.SliceMap(r.Items, func(t Item) string {
return t.GetXml()
}), ""),
} {
xml = strings.Replace(xml, k, v, -1)
}
return
}
func (i Item) GetXml() (xml string) {
xml = templateItems
for k, v := range map[string]string{
"{$title}": i.Title,
"{$link}": i.Link,
"{$comments}": i.getComments(),
"{$author}": i.Creator,
"{$pubDate}": i.PubDate,
"{$category}": i.getCategory(),
"{$guid}": i.Guid,
"{$description}": i.getDescription(),
"{$content}": i.Content,
"{$commentRss}": i.getCommentRss(),
"{$commentNumber}": i.getSlashComments(),
} {
xml = strings.Replace(xml, k, v, -1)
}
return
}
func (i Item) getCategory() string {
r := ""
if i.Category != "" {
r = fmt.Sprintf("", i.CommentLink)
}
return r
}
func (i Item) getDescription() string {
r := ""
if i.Description != "" {
r = fmt.Sprintf("", i.Description)
}
return r
}
func (i Item) getComments() string {
r := ""
if i.CommentLink != "" {
r = fmt.Sprintf("%s", i.CommentLink)
}
return r
}
func (i Item) getCommentRss() (r string) {
if i.CommentLink != "" && i.SlashComments > 0 {
r = fmt.Sprintf("%s", i.CommentRss)
}
return
}
func (i Item) getSlashComments() (r string) {
if i.SlashComments > 0 {
r = fmt.Sprintf("%d", i.SlashComments)
}
return
}