diff --git a/cmd/ssg/main.go b/cmd/ssg/main.go index e510a23..ea850ab 100644 --- a/cmd/ssg/main.go +++ b/cmd/ssg/main.go @@ -1,6 +1,7 @@ package main import ( + "errors" "flag" "io/ioutil" "log" @@ -12,6 +13,7 @@ import ( ) var ( + siteName = flag.String("site", "ewnl", "site id, either 'ewnl' or 'vkvnl'") resources = flag.String("resources", "./resources", "folder with templates and other resources") content = flag.String("content", "./content,/projectx", "comma separated list of folders search for content") statics = flag.String("statics", "./statics", "folder with static content") @@ -20,12 +22,22 @@ var ( func main() { flag.Parse() - if *resources == "" || *content == "" || *public == "" || *statics == "" { + if *siteName == "" || *resources == "" || *content == "" || *public == "" || *statics == "" { log.Fatal("missing parameter") } + var siteId site.SiteID + switch *siteName { + case "ewnl": + siteId = site.SITE_EWNL + case "vkvnl": + siteId = site.SITE_VKVNL + default: + log.Fatal(errors.New("unknown site")) + } + // initialize site - config, err := site.NewSiteConfig(site.SITE_EWNL) + config, err := site.NewSiteConfig(siteId) if err != nil { log.Fatal(err) } diff --git a/cmd/ssg/site/config.go b/cmd/ssg/site/config.go index 9589e20..7f52c4f 100644 --- a/cmd/ssg/site/config.go +++ b/cmd/ssg/site/config.go @@ -31,8 +31,8 @@ type TemplateConfig struct { type SiteConfig struct { ID SiteID BaseURL string + PathsWithKind bool TemplateConfigs []*TemplateConfig - RSS bool StaticPages []*StaticPage KindMap map[adoc.Kind]Kind } diff --git a/cmd/ssg/site/post.go b/cmd/ssg/site/post.go index 9655d26..e220d04 100644 --- a/cmd/ssg/site/post.go +++ b/cmd/ssg/site/post.go @@ -24,11 +24,13 @@ var pluralKind = map[Kind]string{ } type Post struct { - doc *adoc.ADoc - Date time.Time - Kind Kind - Language Language - Tags []Tag + doc *adoc.ADoc + baseURL string + prefixPath bool + Date time.Time + Kind Kind + Language Language + Tags []Tag } func NewPost(config *SiteConfig, doc *adoc.ADoc) *Post { @@ -37,11 +39,13 @@ func NewPost(config *SiteConfig, doc *adoc.ADoc) *Post { tags = append(tags, Tag(t)) } return &Post{ - doc: doc, - Date: doc.Date, - Kind: config.MapKind(doc.Kind), - Language: Language(doc.Language), - Tags: tags, + doc: doc, + baseURL: config.BaseURL, + prefixPath: config.PathsWithKind, + Date: doc.Date, + Kind: config.MapKind(doc.Kind), + Language: Language(doc.Language), + Tags: tags, } } @@ -54,11 +58,15 @@ func (p *Post) Year() string { } func (p *Post) Link() string { - return fmt.Sprintf("%s/", path.Join("/", pluralKind[p.Kind], p.Year(), p.Slug())) + link := "/" + if p.prefixPath { + link = path.Join(link, pluralKind[p.Kind]) + } + return fmt.Sprintf("%s/", path.Join(link, p.Year(), p.Slug())) } func (p *Post) FullLink() string { - return fmt.Sprintf("https://erikwinter.nl%s", p.Link()) + return fmt.Sprintf("%s%s", p.baseURL, p.Link()) } func (p *Post) HTMLSummary() *HTMLSummary { diff --git a/cmd/ssg/site/render.go b/cmd/ssg/site/renderewnl.go similarity index 79% rename from cmd/ssg/site/render.go rename to cmd/ssg/site/renderewnl.go index bf51ef3..4573078 100644 --- a/cmd/ssg/site/render.go +++ b/cmd/ssg/site/renderewnl.go @@ -11,29 +11,7 @@ import ( "time" ) -const dirMode = os.ModeDir | 0755 - -func resetTarget(targetPath string) error { - if err := os.RemoveAll(targetPath); err != nil { - return err - } - - return os.Mkdir(targetPath, dirMode) -} - -func moveResources(targetPath, resourcesPath string) error { - for _, dir := range []string{"css", "font"} { - srcPath := filepath.Join(resourcesPath, dir) - destPath := filepath.Join(targetPath, dir) - if err := copyFiles(filepath.Join(srcPath, "*"), destPath); err != nil { - return err - } - } - - return nil -} - -func renderStaticPages(targetPath string, tpl *template.Template, _ Posts, statics []*StaticPage) error { +func renderEWNLStaticPages(targetPath string, tpl *template.Template, _ Posts, statics []*StaticPage) error { for _, static := range statics { destPath := filepath.Join(targetPath, static.Name) if err := os.MkdirAll(destPath, dirMode); err != nil { @@ -68,7 +46,7 @@ func renderStaticPages(targetPath string, tpl *template.Template, _ Posts, stati return nil } -func renderHome(targetPath string, tpl *template.Template, posts Posts, _ []*StaticPage) error { +func renderEWNLHome(targetPath string, tpl *template.Template, posts Posts, _ []*StaticPage) error { var summaries []*HTMLSummary for _, p := range posts.RemoveKind(KIND_NOTE).Limit(10) { summaries = append(summaries, p.HTMLSummary()) @@ -91,7 +69,7 @@ func renderHome(targetPath string, tpl *template.Template, posts Posts, _ []*Sta return tpl.Execute(homeFile, data) } -func renderArchive(targetPath string, tpl *template.Template, posts Posts, _ []*StaticPage) error { +func renderEWNLArchive(targetPath string, tpl *template.Template, posts Posts, _ []*StaticPage) error { archPath := filepath.Join(targetPath, "archive") if err := os.MkdirAll(archPath, dirMode); err != nil { return err @@ -145,7 +123,7 @@ func renderArchive(targetPath string, tpl *template.Template, posts Posts, _ []* return tpl.Execute(archFile, data) } -func renderListings(targetPath string, tpl *template.Template, posts Posts, _ []*StaticPage) error { +func renderEWNLListings(targetPath string, tpl *template.Template, posts Posts, _ []*StaticPage) error { for _, kind := range []Kind{KIND_NOTE, KIND_STORY, KIND_ARTICLE} { for _, year := range posts.SelectKind(kind).YearList() { title := fmt.Sprintf("%s in %s", strings.Title(pluralKind[kind]), year) @@ -155,7 +133,7 @@ func renderListings(targetPath string, tpl *template.Template, posts Posts, _ [] summaries = append(summaries, p.HTMLSummary()) } path := filepath.Join(targetPath, pluralKind[kind], year) - if err := renderListing(path, tpl, title, summaries); err != nil { + if err := renderEWNLListing(path, tpl, title, summaries); err != nil { return err } } @@ -169,7 +147,7 @@ func renderListings(targetPath string, tpl *template.Template, posts Posts, _ [] summaries = append(summaries, p.HTMLSummary()) } path := filepath.Join(targetPath, "tags", tag) - if err := renderListing(path, tpl, title, summaries); err != nil { + if err := renderEWNLListing(path, tpl, title, summaries); err != nil { return err } } @@ -177,7 +155,7 @@ func renderListings(targetPath string, tpl *template.Template, posts Posts, _ [] return nil } -func renderListing(path string, tpl *template.Template, title string, summaries []*HTMLSummary) error { +func renderEWNLListing(path string, tpl *template.Template, title string, summaries []*HTMLSummary) error { data := struct { Title string Summaries []*HTMLSummary @@ -198,7 +176,7 @@ func renderListing(path string, tpl *template.Template, title string, summaries return tpl.Execute(f, data) } -func renderPosts(targetPath string, tpl *template.Template, posts Posts, _ []*StaticPage) error { +func renderEWNLPosts(targetPath string, tpl *template.Template, posts Posts, _ []*StaticPage) error { for _, post := range posts { data := post.HTMLPost() if data.Slug == "" { @@ -225,7 +203,7 @@ func renderPosts(targetPath string, tpl *template.Template, posts Posts, _ []*St return nil } -func renderRSS(targetPath string, tpl *template.Template, posts Posts, _ []*StaticPage) error { +func renderEWNLRSS(targetPath string, tpl *template.Template, posts Posts, _ []*StaticPage) error { rssPath := filepath.Join(targetPath, "index.xml") rssFile, err := os.Create(rssPath) if err != nil { diff --git a/cmd/ssg/site/rendervkvnl.go b/cmd/ssg/site/rendervkvnl.go new file mode 100644 index 0000000..e85875a --- /dev/null +++ b/cmd/ssg/site/rendervkvnl.go @@ -0,0 +1,91 @@ +package site + +import ( + "fmt" + "os" + "path/filepath" + "text/template" + "time" +) + +func renderVKVNLPosts(targetPath string, tpl *template.Template, posts Posts, _ []*StaticPage) error { + last, first := 0, len(posts)-1 // posts are sorted in reverse order + for i, post := range posts { + pData := post.HTMLPost() + if pData.Slug == "" { + return ErrInvalidPost + } + + data := struct { + Slug string + Title string + DateLong string + DateShort string + Content string + PreviousLink string + NextLink string + }{ + Slug: pData.Slug, + Title: pData.Title, + DateLong: pData.DateLong, + DateShort: pData.DateShort, + Content: pData.Content, + } + + path := targetPath + if i != first { + data.PreviousLink = posts[i+1].Link() + } + if i != last { + data.NextLink = posts[i-1].Link() + fmt.Printf("first %d, last %d, i %d\n", first, last, i) + if i == last+1 { + fmt.Println(data.Title) + data.NextLink = "/" + } + path = filepath.Join(targetPath, post.Year(), data.Slug) + } + if i == last-1 { + } + + if err := os.MkdirAll(path, dirMode); err != nil { + return err + } + + nPath := filepath.Join(path, "index.html") + f, err := os.Create(nPath) + if err != nil { + return err + } + defer f.Close() + + if err := tpl.Execute(f, data); err != nil { + return err + } + } + + return nil +} + +func renderVKVNLRSS(targetPath string, tpl *template.Template, posts Posts, _ []*StaticPage) error { + rssPath := filepath.Join(targetPath, "index.xml") + rssFile, err := os.Create(rssPath) + if err != nil { + return err + } + defer rssFile.Close() + + var xmlPosts []*XMLPost + for _, p := range posts.RemoveKind(KIND_NOTE).Limit(10) { + xmlPosts = append(xmlPosts, p.XMLPost()) + } + + data := struct { + DateFormal string + Posts []*XMLPost + }{ + DateFormal: time.Now().Format(time.RFC1123Z), + Posts: xmlPosts, + } + return tpl.Execute(rssFile, data) +} diff --git a/cmd/ssg/site/site.go b/cmd/ssg/site/site.go index 791b9e2..1182ae1 100644 --- a/cmd/ssg/site/site.go +++ b/cmd/ssg/site/site.go @@ -2,11 +2,14 @@ package site import ( "io/ioutil" + "os" "path/filepath" "git.sr.ht/~ewintr/shitty-ssg/pkg/adoc" ) +const dirMode = os.ModeDir | 0755 + type StaticPage struct { Name string Path string @@ -70,3 +73,23 @@ func (s *Site) RenderHTML(targetPath string) error { return nil } + +func resetTarget(targetPath string) error { + if err := os.RemoveAll(targetPath); err != nil { + return err + } + + return os.Mkdir(targetPath, dirMode) +} + +func moveResources(targetPath, resourcesPath string) error { + for _, dir := range []string{"css", "font"} { + srcPath := filepath.Join(resourcesPath, dir) + destPath := filepath.Join(targetPath, dir) + if err := copyFiles(filepath.Join(srcPath, "*"), destPath); err != nil { + return err + } + } + + return nil +} diff --git a/cmd/ssg/site/sites.go b/cmd/ssg/site/sites.go index de01f5e..f4941b7 100644 --- a/cmd/ssg/site/sites.go +++ b/cmd/ssg/site/sites.go @@ -4,44 +4,45 @@ import "git.sr.ht/~ewintr/shitty-ssg/pkg/adoc" var ( SITE_CONFIG_EWNL = &SiteConfig{ - ID: SITE_EWNL, - BaseURL: "https://erikwinter.nl", + ID: SITE_EWNL, + BaseURL: "https://erikwinter.nl", + PathsWithKind: true, TemplateConfigs: []*TemplateConfig{ { Name: "home", TemplateNames: []string{"list", "head", "menu"}, TemplateExt: "gohtml", - Render: renderHome, + Render: renderEWNLHome, }, { Name: "listings", TemplateNames: []string{"list", "head", "menu"}, TemplateExt: "gohtml", - Render: renderListings, + Render: renderEWNLListings, }, { Name: "archive", TemplateNames: []string{"archive", "head", "menu"}, TemplateExt: "gohtml", - Render: renderArchive, + Render: renderEWNLArchive, }, { Name: "static", TemplateNames: []string{"static", "head", "menu"}, TemplateExt: "gohtml", - Render: renderStaticPages, + Render: renderEWNLStaticPages, }, { Name: "posts", TemplateNames: []string{"post", "head", "menu"}, TemplateExt: "gohtml", - Render: renderPosts, + Render: renderEWNLPosts, }, { Name: "rss", TemplateNames: []string{"rss"}, TemplateExt: "goxml", - Render: renderRSS, + Render: renderEWNLRSS, }, }, KindMap: map[adoc.Kind]Kind{ @@ -52,5 +53,27 @@ var ( adoc.KIND_ARTICLE: KIND_ARTICLE, }, } - SITE_CONFIG_VKVNL = &SiteConfig{} + + SITE_CONFIG_VKVNL = &SiteConfig{ + ID: SITE_VKVNL, + BaseURL: "https://vrijkorteverhalen.nl", + PathsWithKind: false, + TemplateConfigs: []*TemplateConfig{ + { + Name: "post", + TemplateNames: []string{"post"}, + TemplateExt: "gohtml", + Render: renderVKVNLPosts, + }, + { + Name: "rss", + TemplateNames: []string{"rss"}, + TemplateExt: "goxml", + Render: renderVKVNLRSS, + }, + }, + KindMap: map[adoc.Kind]Kind{ + adoc.KIND_VKV: KIND_STORY, + }, + } ) diff --git a/resources/font/Amiko-Bold.ttf b/resources/ewnl/font/Amiko-Bold.ttf similarity index 100% rename from resources/font/Amiko-Bold.ttf rename to resources/ewnl/font/Amiko-Bold.ttf diff --git a/resources/font/Amiko-Regular.ttf b/resources/ewnl/font/Amiko-Regular.ttf similarity index 100% rename from resources/font/Amiko-Regular.ttf rename to resources/ewnl/font/Amiko-Regular.ttf diff --git a/resources/font/Amiko-SemiBold.ttf b/resources/ewnl/font/Amiko-SemiBold.ttf similarity index 100% rename from resources/font/Amiko-SemiBold.ttf rename to resources/ewnl/font/Amiko-SemiBold.ttf diff --git a/resources/font/Merriweather-Regular.otf b/resources/ewnl/font/Merriweather-Regular.otf similarity index 100% rename from resources/font/Merriweather-Regular.otf rename to resources/ewnl/font/Merriweather-Regular.otf diff --git a/resources/font/merriweather.regular.ttf b/resources/ewnl/font/merriweather.regular.ttf similarity index 100% rename from resources/font/merriweather.regular.ttf rename to resources/ewnl/font/merriweather.regular.ttf diff --git a/resources/template/archive.gohtml b/resources/ewnl/template/archive.gohtml similarity index 100% rename from resources/template/archive.gohtml rename to resources/ewnl/template/archive.gohtml diff --git a/resources/template/head.gohtml b/resources/ewnl/template/head.gohtml similarity index 100% rename from resources/template/head.gohtml rename to resources/ewnl/template/head.gohtml diff --git a/resources/template/list.gohtml b/resources/ewnl/template/list.gohtml similarity index 100% rename from resources/template/list.gohtml rename to resources/ewnl/template/list.gohtml diff --git a/resources/template/menu.gohtml b/resources/ewnl/template/menu.gohtml similarity index 100% rename from resources/template/menu.gohtml rename to resources/ewnl/template/menu.gohtml diff --git a/resources/template/post.gohtml b/resources/ewnl/template/post.gohtml similarity index 100% rename from resources/template/post.gohtml rename to resources/ewnl/template/post.gohtml diff --git a/resources/template/rss.goxml b/resources/ewnl/template/rss.goxml similarity index 100% rename from resources/template/rss.goxml rename to resources/ewnl/template/rss.goxml diff --git a/resources/template/static.gohtml b/resources/ewnl/template/static.gohtml similarity index 100% rename from resources/template/static.gohtml rename to resources/ewnl/template/static.gohtml diff --git a/resources/vkvnl/template/post.gohtml b/resources/vkvnl/template/post.gohtml new file mode 100644 index 0000000..cd47252 --- /dev/null +++ b/resources/vkvnl/template/post.gohtml @@ -0,0 +1,29 @@ + + + + + + + + {{ .Title }} + + + +
+

{{ .Title }}

+ - + . + {{ .Content }} +
+ +
+ + diff --git a/resources/vkvnl/template/rss.goxml b/resources/vkvnl/template/rss.goxml new file mode 100644 index 0000000..c05efb0 --- /dev/null +++ b/resources/vkvnl/template/rss.goxml @@ -0,0 +1,20 @@ + + + + ErikWinter.nl + https://erikwinter.nl/ + Activity on ErikWinter.nl + en-us + {{ .DateFormal }} + + {{- range .Posts -}} + + {{ .Title }} + {{ .Link }} + {{ .DateFormal }} + {{ .Link }} + {{ .Content }} + + {{- end -}} + +