Skip to content

Commit

Permalink
feat: support path
Browse files Browse the repository at this point in the history
  • Loading branch information
sosolyht committed May 8, 2023
1 parent 2ecff80 commit 1e0c67a
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 33 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package main
import "github.com/sosolyht/go-sitemap/sitemap"

func main() {
// Create a standard sitemap
s := sitemap.NewSitemap()
// Create a sitemap with path
s := sitemap.NewSitemap().Path("util/sitemaps")

links := []string{
"https://google.com",
Expand Down
1 change: 0 additions & 1 deletion examples.go

This file was deleted.

15 changes: 15 additions & 0 deletions examples/default-sitemap/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import "github.com/sosolyht/go-sitemap/sitemap"

func main() {
s := sitemap.NewSitemap().Path("sitemaps")

links := []string{
"https://google.com",
"https://naver.com",
}
for i := range links {
s.AddURL(links[i])
}
}
16 changes: 2 additions & 14 deletions main.go → examples/video-sitemap/main.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
package main

import (
"github.com/sosolyht/go-sitemap/sitemap"
)
import "github.com/sosolyht/go-sitemap/sitemap"

func main() {
s := sitemap.NewSitemap()

links := []string{
"https://google.com",
"https://naver.com",
}
for i := range links {
s.AddURL(links[i])
}

vs := sitemap.NewVideoSitemap()
vs := sitemap.NewVideoSitemap().Path("sitemaps")

videoURLs := []sitemap.VideoURL{
{
Expand Down
7 changes: 0 additions & 7 deletions sitemap/required.go

This file was deleted.

29 changes: 28 additions & 1 deletion sitemap/sitemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package sitemap
import (
"encoding/xml"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
Expand All @@ -13,6 +15,7 @@ type sitemap struct {
XMLName xml.Name `xml:"urlset"`
Xmlns string `xml:"xmlns,attr"`
URL []URLs `xml:"url,omitempty"`
path string
}

type URLs struct {
Expand Down Expand Up @@ -60,7 +63,7 @@ func (s *sitemap) AddURL(url string) (err error) {
return err
}

sitemapFile, err := os.Create("sitemaps/sitemap.xml")
sitemapFile, err := os.Create(s.path)
if err != nil {
return err
}
Expand All @@ -78,6 +81,30 @@ func (s *sitemap) AddURL(url string) (err error) {
return
}

func (s *sitemap) Path(path string) *sitemap {
currentDir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}

projectRoot := filepath.Join(currentDir, "..", "..")
sitemapsDir := filepath.Join(projectRoot, path)

_, err = os.Stat(sitemapsDir)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(sitemapsDir, 0755)
if err != nil {
log.Fatal(err)
}
}
}

output := filepath.Join(sitemapsDir, "sitemap.xml")
s.path = output
return s
}

func (s *sitemap) createSitemapFromLinksFile() ([]string, error) {
linkFile, err := os.Open("sitemaps/links")
if err != nil {
Expand Down
29 changes: 28 additions & 1 deletion sitemap/video_sitemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package sitemap

import (
"encoding/xml"
"log"
"os"
"path/filepath"
"time"
)

Expand All @@ -11,6 +13,7 @@ type videoSitemap struct {
Xmlns string `xml:"xmlns,attr"`
XmlnsVideo string `xml:"xmlns:video,attr"`
URL []VideoURL
path string
}

type VideoURL struct {
Expand Down Expand Up @@ -67,7 +70,7 @@ func (v *videoSitemap) AddVideoURL(url VideoURL) (err error) {
return err
}

sitemapFile, err := os.Create("sitemaps/sitemap_video.xml")
sitemapFile, err := os.Create(v.path)
if err != nil {
return err
}
Expand All @@ -84,3 +87,27 @@ func (v *videoSitemap) AddVideoURL(url VideoURL) (err error) {

return
}

func (v *videoSitemap) Path(path string) *videoSitemap {
currentDir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}

projectRoot := filepath.Join(currentDir, "..", "..")
sitemapsDir := filepath.Join(projectRoot, path)

_, err = os.Stat(sitemapsDir)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(sitemapsDir, 0755)
if err != nil {
log.Fatal(err)
}
}
}

output := filepath.Join(sitemapsDir, "sitemap_video.xml")
v.path = output
return v
}
5 changes: 0 additions & 5 deletions sitemaps/links

This file was deleted.

4 changes: 2 additions & 2 deletions sitemaps/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://google.com</loc>
<lastmod>2023-04-28</lastmod>
<lastmod>2023-05-08</lastmod>
</url>
<url>
<loc>https://naver.com</loc>
<lastmod>2023-04-28</lastmod>
<lastmod>2023-05-08</lastmod>
</url>
</urlset>

0 comments on commit 1e0c67a

Please sign in to comment.