render page with user config

This commit is contained in:
Niko Abeler 2022-07-27 21:53:56 +02:00
parent 09a084e5fa
commit 03a27f890c
6 changed files with 129 additions and 17 deletions

View File

@ -7,10 +7,27 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ .Title }}</title> <title>{{ .Title }}</title>
<link rel="stylesheet" href="/static/pico.min.css"> <link rel="stylesheet" href="/static/pico.min.css">
<style>
header {
background-color: {{.HeaderColor}};
}
</style>
</head> </head>
<body> <body>
<header>
<nav class="container">
<ul>
<li>
<hgroup>
<h2>{{ .UserTitle }}</h2>
<h3>{{ .UserSubtitle }}</h3>
</hgroup>
</li>
</ul>
</nav>
</header>
<main class="container"> <main class="container">
{{ .Content }} {{ .Content }}
</main> </main>

View File

@ -22,8 +22,35 @@ type PostRenderData struct {
Post template.HTML 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) { func RenderPost(post Post) (string, error) {
baseTemplate, _ := post.user.Template()
buf, _ := post.MarkdownData() buf, _ := post.MarkdownData()
postTemplate, _ := template.New("post").Parse(postTemplateStr) postTemplate, _ := template.New("post").Parse(postTemplateStr)
@ -41,16 +68,10 @@ func RenderPost(post Post) (string, error) {
Content: template.HTML(postHtml.String()), Content: template.HTML(postHtml.String()),
} }
var html bytes.Buffer return renderIntoBaseTemplate(post.user, data)
t, err := template.New("page").Parse(baseTemplate)
t.Execute(&html, data)
return html.String(), err
} }
func RenderIndexPage(user User) (string, error) { func RenderIndexPage(user User) (string, error) {
baseTemplate, _ := user.Template()
posts, _ := user.Posts() posts, _ := user.Posts()
postHtml := "" postHtml := ""
@ -64,15 +85,7 @@ func RenderIndexPage(user User) (string, error) {
Content: template.HTML(postHtml), Content: template.HTML(postHtml),
} }
var html bytes.Buffer return renderIntoBaseTemplate(user, data)
t, err := template.New("index").Parse(baseTemplate)
if err != nil {
return "", err
}
t.Execute(&html, data)
return html.String(), nil
} }

View File

@ -82,3 +82,24 @@ func TestRenderUserList(t *testing.T) {
t.Error("Post title not rendered. Got: " + result) 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)
}
}

View File

@ -7,6 +7,8 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
"gopkg.in/yaml.v2"
) )
//go:embed embed/initial/base.html //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", "VERSION"), []byte(VERSION), 0644)
os.WriteFile(path.Join(user_dir, "meta", "base.html"), []byte(base_template), 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 return new_user, nil
} }

View File

@ -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) { func TestCreateUserAddsPublicFolder(t *testing.T) {
// Create a new user // Create a new user
repo, _ := kiss.CreateRepository(testRepoName()) repo, _ := kiss.CreateRepository(testRepoName())

43
user.go
View File

@ -7,6 +7,8 @@ import (
"path" "path"
"strings" "strings"
"time" "time"
"gopkg.in/yaml.v2"
) )
type User struct { type User struct {
@ -14,6 +16,12 @@ type User struct {
name string name string
} }
type UserConfig struct {
Title string `yaml:"title"`
SubTitle string `yaml:"subtitle"`
HeaderColor string `yaml:"header_color"`
}
func (user User) Dir() string { func (user User) Dir() string {
return path.Join(user.repo.UsersDir(), user.name) return path.Join(user.repo.UsersDir(), user.name)
} }
@ -26,6 +34,14 @@ func (user User) PostDir() string {
return path.Join(user.Dir(), "public") 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 { func (user User) Name() string {
return user.name return user.name
} }
@ -90,3 +106,30 @@ func (user User) Template() (string, error) {
} }
return string(base_html), nil 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
}