diff --git a/embed/initial/base.html b/embed/initial/base.html index 2343827..563dddc 100644 --- a/embed/initial/base.html +++ b/embed/initial/base.html @@ -7,10 +7,27 @@ {{ .Title }} + +
+ +
{{ .Content }}
diff --git a/renderer.go b/renderer.go index 712b941..4220a75 100644 --- a/renderer.go +++ b/renderer.go @@ -22,8 +22,35 @@ type PostRenderData struct { Post template.HTML } +func renderIntoBaseTemplate(user User, data PageContent) (string, error) { + baseTemplate, _ := user.Template() + t, err := template.New("index").Parse(baseTemplate) + if err != nil { + return "", err + } + + user_config, _ := user.Config() + full_data := struct { + Title string + Content template.HTML + UserTitle string + UserSubtitle string + HeaderColor string + }{ + Title: data.Title, + Content: data.Content, + UserTitle: user_config.Title, + UserSubtitle: user_config.SubTitle, + HeaderColor: user_config.HeaderColor, + } + + var html bytes.Buffer + t.Execute(&html, full_data) + + return html.String(), nil +} + func RenderPost(post Post) (string, error) { - baseTemplate, _ := post.user.Template() buf, _ := post.MarkdownData() postTemplate, _ := template.New("post").Parse(postTemplateStr) @@ -41,16 +68,10 @@ func RenderPost(post Post) (string, error) { Content: template.HTML(postHtml.String()), } - var html bytes.Buffer - t, err := template.New("page").Parse(baseTemplate) - - t.Execute(&html, data) - - return html.String(), err + return renderIntoBaseTemplate(post.user, data) } func RenderIndexPage(user User) (string, error) { - baseTemplate, _ := user.Template() posts, _ := user.Posts() postHtml := "" @@ -64,15 +85,7 @@ func RenderIndexPage(user User) (string, error) { Content: template.HTML(postHtml), } - var html bytes.Buffer - t, err := template.New("index").Parse(baseTemplate) - if err != nil { - return "", err - } - - t.Execute(&html, data) - - return html.String(), nil + return renderIntoBaseTemplate(user, data) } diff --git a/renderer_test.go b/renderer_test.go index 172a28b..12c8441 100644 --- a/renderer_test.go +++ b/renderer_test.go @@ -82,3 +82,24 @@ func TestRenderUserList(t *testing.T) { t.Error("Post title not rendered. Got: " + result) } } + +func TestRendersHeaderTitle(t *testing.T) { + user := getTestUser() + user.SetConfig(kiss.UserConfig{ + Title: "Test Title", + SubTitle: "Test SubTitle", + HeaderColor: "#ff1337", + }) + post, _ := user.CreateNewPost("testpost") + + result, _ := kiss.RenderPost(post) + if !strings.Contains(result, "Test Title") { + t.Error("Header title not rendered. Got: " + result) + } + if !strings.Contains(result, "Test SubTitle") { + t.Error("Header subtitle not rendered. Got: " + result) + } + if !strings.Contains(result, "#ff1337") { + t.Error("Header color not rendered. Got: " + result) + } +} diff --git a/repository.go b/repository.go index 0bb7a09..f8815d4 100644 --- a/repository.go +++ b/repository.go @@ -7,6 +7,8 @@ import ( "io/ioutil" "os" "path" + + "gopkg.in/yaml.v2" ) //go:embed embed/initial/base.html @@ -108,6 +110,13 @@ func (repo Repository) CreateUser(name string) (User, error) { os.WriteFile(path.Join(user_dir, "meta", "VERSION"), []byte(VERSION), 0644) os.WriteFile(path.Join(user_dir, "meta", "base.html"), []byte(base_template), 0644) + meta, _ := yaml.Marshal(UserConfig{ + Title: name, + SubTitle: "", + HeaderColor: "#bdd6be", + }) + os.WriteFile(new_user.ConfigFile(), meta, 0644) + return new_user, nil } diff --git a/repository_test.go b/repository_test.go index c408c23..a8466b9 100644 --- a/repository_test.go +++ b/repository_test.go @@ -63,6 +63,15 @@ func TestCreateUserAddsBaseHtmlFile(t *testing.T) { } } +func TestCreateUserAddConfigYml(t *testing.T) { + // Create a new user + repo, _ := kiss.CreateRepository(testRepoName()) + user, _ := repo.CreateUser(randomUserName()) + if _, err := os.Stat(path.Join(user.Dir(), "/meta/config.yml")); err != nil { + t.Error("Config file not created") + } +} + func TestCreateUserAddsPublicFolder(t *testing.T) { // Create a new user repo, _ := kiss.CreateRepository(testRepoName()) diff --git a/user.go b/user.go index c6ba20e..374adb9 100644 --- a/user.go +++ b/user.go @@ -7,6 +7,8 @@ import ( "path" "strings" "time" + + "gopkg.in/yaml.v2" ) type User struct { @@ -14,6 +16,12 @@ type User struct { name string } +type UserConfig struct { + Title string `yaml:"title"` + SubTitle string `yaml:"subtitle"` + HeaderColor string `yaml:"header_color"` +} + func (user User) Dir() string { return path.Join(user.repo.UsersDir(), user.name) } @@ -26,6 +34,14 @@ func (user User) PostDir() string { return path.Join(user.Dir(), "public") } +func (user User) MetaDir() string { + return path.Join(user.Dir(), "meta") +} + +func (user User) ConfigFile() string { + return path.Join(user.MetaDir(), "config.yml") +} + func (user User) Name() string { return user.name } @@ -90,3 +106,30 @@ func (user User) Template() (string, error) { } return string(base_html), nil } + +func (user User) Config() (UserConfig, error) { + config_path := user.ConfigFile() + config_data, err := ioutil.ReadFile(config_path) + if err != nil { + return UserConfig{}, err + } + var meta UserConfig + err = yaml.Unmarshal(config_data, &meta) + if err != nil { + return UserConfig{}, err + } + return meta, nil +} + +func (user User) SetConfig(new_config UserConfig) error { + config_path := user.ConfigFile() + config_data, err := yaml.Marshal(new_config) + if err != nil { + return err + } + err = ioutil.WriteFile(config_path, config_data, 0644) + if err != nil { + return err + } + return nil +}