diff --git a/sitemap.go b/sitemap.go index 2e277a3..7c7afd2 100644 --- a/sitemap.go +++ b/sitemap.go @@ -47,6 +47,7 @@ type Entry interface { GetLastModified() *time.Time GetChangeFrequency() Frequency GetPriority() float32 + GetNews() *News } // IndexEntry is an interface describes an element \ an URL in a sitemap index file. diff --git a/sitemap_test.go b/sitemap_test.go index 53be30c..988d0aa 100644 --- a/sitemap_test.go +++ b/sitemap_test.go @@ -125,6 +125,54 @@ func TestParseSitemapIndex(t *testing.T) { } } +func TestParseSitemapNews(t *testing.T) { + var ( + counter int + sb strings.Builder + ) + err := ParseFromFile("./testdata/sitemap-news.xml", func(e Entry) error { + counter++ + + fmt.Fprintln(&sb, e.GetLocation()) + lastmod := e.GetLastModified() + if lastmod != nil { + fmt.Fprintln(&sb, lastmod.Format(time.RFC3339)) + } + fmt.Fprintln(&sb, e.GetChangeFrequency()) + fmt.Fprintln(&sb, e.GetPriority()) + news := e.GetNews() + if news == nil { + return nil + } + fmt.Fprintln(&sb, news.Publication.Name) + fmt.Fprintln(&sb, news.Publication.Language) + if news.GetPublicationDate() != nil { + fmt.Fprintln(&sb, news.GetPublicationDate().Format(time.RFC3339)) + } + fmt.Fprintln(&sb, news.Title) + return nil + }) + + if err != nil { + t.Errorf("Parsing failed with error %s", err) + } + + if counter != 4 { + t.Errorf("Expected 4 elements, but given only %d", counter) + } + + expected, err := ioutil.ReadFile("./testdata/sitemap-news.golden") + if err != nil { + t.Errorf("Can't read golden file due to %s", err) + } + + if sb.String() != string(expected) { + t.Logf("%s", sb.String()) + t.Logf("%s", expected) + t.Error("Unxepected result") + } +} + /* * Private API tests */ diff --git a/sitemap_types.go b/sitemap_types.go index a79a796..8497ebf 100644 --- a/sitemap_types.go +++ b/sitemap_types.go @@ -8,6 +8,17 @@ type sitemapEntry struct { ParsedLastModified *time.Time ChangeFrequency Frequency `xml:"changefreq,omitempty"` Priority float32 `xml:"priority,omitempty"` + News *News `xml:"news,omitempty"` +} + +type News struct { + Publication struct { + Name string `xml:"name,omitempy"` + Language string `xml:"language,omitempy"` + } `xml:"publication,omitempy"` + PublicationDate string `xml:"publication_date,omitempy"` + ParsedPublicationDate *time.Time + Title string `xml:"title,omitempy"` } func newSitemapEntry() *sitemapEntry { @@ -33,6 +44,17 @@ func (e *sitemapEntry) GetPriority() float32 { return e.Priority } +func (e *sitemapEntry) GetNews() *News { + return e.News +} + +func (n *News) GetPublicationDate() *time.Time { + if n.ParsedPublicationDate == nil && n.PublicationDate != "" { + n.ParsedPublicationDate = parseDateTime(n.PublicationDate) + } + return n.ParsedPublicationDate +} + type sitemapIndexEntry struct { Location string `xml:"loc"` LastModified string `xml:"lastmod,omitempty"` @@ -61,11 +83,14 @@ func parseDateTime(value string) *time.Time { t, err := time.Parse(time.RFC3339, value) if err != nil { - // second chance - // try parse as short format - t, err = time.Parse("2006-01-02", value) + t, err = time.Parse("2006-01-02T15:04-07:00", value) if err != nil { - return nil + // second chance + // try parse as short format + t, err = time.Parse("2006-01-02", value) + if err != nil { + return nil + } } } diff --git a/testdata/sitemap-news.golden b/testdata/sitemap-news.golden new file mode 100644 index 0000000..b34cf7f --- /dev/null +++ b/testdata/sitemap-news.golden @@ -0,0 +1,27 @@ +http://HOST/ +always +0.5 +http://HOST/tools/ +2015-05-07T19:13:09+09:00 +always +0.5 +HOST news +en +2015-05-07T00:00:00Z +tools +http://HOST/contribution-to-oss/ +2015-05-07T00:00:00Z +monthly +0.5 +HOST news +en +2015-05-07T19:20:00+01:00 +contribution-to-oss +http://HOST/page-1/ +2015-05-07T19:13:09+09:00 +monthly +0.9 +HOST news +en +2015-05-07T19:20:30+01:00 +page-1 diff --git a/testdata/sitemap-news.xml b/testdata/sitemap-news.xml new file mode 100644 index 0000000..a87f9ba --- /dev/null +++ b/testdata/sitemap-news.xml @@ -0,0 +1,45 @@ + + + + http://HOST/ + + + http://HOST/tools/ + 2015-05-07T19:13:09+09:00 + + + HOST news + en + + 2015-05-07 + tools + + + + http://HOST/contribution-to-oss/ + 2015-05-07 + monthly + + + HOST news + en + + 2015-05-07T19:20+01:00 + contribution-to-oss + + + + http://HOST/page-1/ + 2015-05-07T19:13:09+09:00 + monthly + 0.9 + + + HOST news + en + + 2015-05-07T19:20:30.45+01:00 + page-1 + + + \ No newline at end of file