From 5aa51d6648e65df5a2cdfda77491a118470ec649 Mon Sep 17 00:00:00 2001 From: John Jago Date: Sun, 21 Jul 2019 13:03:27 -0500 Subject: [PATCH] Generate template files when making new blog GitHub #1 --- blognow.go | 14 ++++++++---- templates.go | 48 ++++++++++++++++++++++++++++++++++++++++++ templates/archive.html | 11 ---------- templates/base.html | 14 ------------ templates/header.html | 4 ---- templates/post.html | 10 --------- 6 files changed, 58 insertions(+), 43 deletions(-) create mode 100644 templates.go delete mode 100644 templates/archive.html delete mode 100644 templates/base.html delete mode 100644 templates/header.html delete mode 100644 templates/post.html diff --git a/blognow.go b/blognow.go index 6a1f77e..3a7697d 100644 --- a/blognow.go +++ b/blognow.go @@ -19,13 +19,11 @@ import ( "gitlab.com/golang-commonmark/markdown" ) -const sampleConfig string = ` -baseURL = "https://example.org/" +const sampleConfig string = `baseURL = "https://example.org/" title = "My Blog" tagline = "Don't sail too close to the wind" ` -const samplePost string = ` ---- +const samplePost string = `--- title = "My First Post" date = 2019-05-05 --- @@ -73,6 +71,14 @@ func makeBlogDir(path string) { fmt.Println("Created a new blog: " + path) createFile(path+"/config.toml", sampleConfig) createFile(postsPath+"/sample.md", samplePost) + + // Template files + templatesPath := filepath.Join(path, "templates") + os.MkdirAll(templatesPath, os.ModePerm) + createFile(filepath.Join(templatesPath, "base.html"), baseTmpl) + createFile(filepath.Join(templatesPath, "header.html"), headerTmpl) + createFile(filepath.Join(templatesPath, "post.html"), postTmpl) + createFile(filepath.Join(templatesPath, "archive.html"), archiveTmpl) } // build collects all the necessary information from configuration files diff --git a/templates.go b/templates.go new file mode 100644 index 0000000..e67bb6f --- /dev/null +++ b/templates.go @@ -0,0 +1,48 @@ +package main + +const baseTmpl string = `{{define "base"}} + + + + + + {{template "title" .}} + + + {{template "header" .}} + {{template "body" .}} + + +{{end}} +` + +const headerTmpl string = `{{define "header"}} +

{{.Blog.Title}}

+

{{.Blog.Tagline}}

+{{end}} +` + +const postTmpl string = `{{define "title"}} + {{.Post.Title}} - {{.Blog.Title}} +{{end}} + +{{define "body"}} +

{{.Post.Title}}

+ {{.Post.Content}} +

Posted: {{.Post.Date | formatDate}}

+ See all posts » +{{end}} +` + +const archiveTmpl string = `{{define "title"}} +All Posts - {{.Blog.Title}} +{{end}} + +{{define "body"}} + +{{end}} +` diff --git a/templates/archive.html b/templates/archive.html deleted file mode 100644 index 7370b62..0000000 --- a/templates/archive.html +++ /dev/null @@ -1,11 +0,0 @@ -{{define "title"}} - All Posts - {{.Blog.Title}} -{{end}} - -{{define "body"}} - -{{end}} diff --git a/templates/base.html b/templates/base.html deleted file mode 100644 index 48e6689..0000000 --- a/templates/base.html +++ /dev/null @@ -1,14 +0,0 @@ -{{define "base"}} - - - - - - {{template "title" .}} - - - {{template "header" .}} - {{template "body" .}} - - -{{end}} diff --git a/templates/header.html b/templates/header.html deleted file mode 100644 index e808de0..0000000 --- a/templates/header.html +++ /dev/null @@ -1,4 +0,0 @@ -{{define "header"}} -

{{.Blog.Title}}

-

{{.Blog.Tagline}}

-{{end}} diff --git a/templates/post.html b/templates/post.html deleted file mode 100644 index 9a09557..0000000 --- a/templates/post.html +++ /dev/null @@ -1,10 +0,0 @@ -{{define "title"}} - {{.Post.Title}} - {{.Blog.Title}} -{{end}} - -{{define "body"}} -

{{.Post.Title}}

- {{.Post.Content}} -

Posted: {{.Post.Date | formatDate}}

- See all posts » -{{end}}