most primitive rendering

This commit is contained in:
Niko Abeler 2022-07-21 19:02:37 +02:00
parent 407b4ce7a2
commit 4a0b7d8db6
7 changed files with 80 additions and 19 deletions

2
go.mod
View File

@ -1,3 +1,5 @@
module h4kor/kiss-social module h4kor/kiss-social
go 1.18 go 1.18
require github.com/yuin/goldmark v1.4.13 // indirect

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=

25
post.go Normal file
View File

@ -0,0 +1,25 @@
package kiss
import (
"io/ioutil"
"path"
)
type Post struct {
user User
id string
}
func (post Post) Dir() string {
return path.Join(post.user.Dir(), "public", post.id)
}
func (post Post) ContentFile() string {
return path.Join(post.Dir(), "index.md")
}
func (post Post) Content() []byte {
// read file
data, _ := ioutil.ReadFile(post.ContentFile())
return data
}

16
renderer.go Normal file
View File

@ -0,0 +1,16 @@
package kiss
import (
"bytes"
"github.com/yuin/goldmark"
)
func RenderPost(post Post) string {
var buf bytes.Buffer
if err := goldmark.Convert(post.Content(), &buf); err != nil {
panic(err)
}
return buf.String()
}

23
renderer_test.go Normal file
View File

@ -0,0 +1,23 @@
package kiss_test
import (
"h4kor/kiss-social"
"strings"
"testing"
)
func getTestUser() kiss.User {
repo, _ := kiss.CreateRepository(testRepoName())
user, _ := repo.CreateUser(randomUserName())
return user
}
func TestCanRenderPost(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost")
result := kiss.RenderPost(post)
if !strings.Contains(result, "<h1>testpost</h1>") {
t.Error("Post title not rendered as h1. Got: " + result)
}
}

17
user.go
View File

@ -12,11 +12,6 @@ type User struct {
name string name string
} }
type Post struct {
user User
title string
}
func (user User) Dir() string { func (user User) Dir() string {
return path.Join(user.repo.Dir(), "users", user.name) return path.Join(user.repo.Dir(), "users", user.name)
} }
@ -29,12 +24,12 @@ func (user User) Posts() ([]Post, error) {
postNames := listDir(path.Join(user.Dir(), "public")) postNames := listDir(path.Join(user.Dir(), "public"))
posts := make([]Post, len(postNames)) posts := make([]Post, len(postNames))
for i, name := range postNames { for i, name := range postNames {
posts[i] = Post{user: user, title: name} posts[i] = Post{user: user, id: name}
} }
return posts, nil return posts, nil
} }
func CreateNewPost(user User, title string) { func (user User) CreateNewPost(title string) (Post, error) {
timestamp := time.Now().UTC().Unix() timestamp := time.Now().UTC().Unix()
folder_name := fmt.Sprintf("%d-%s", timestamp, title) folder_name := fmt.Sprintf("%d-%s", timestamp, title)
post_dir := path.Join(user.Dir(), "public", folder_name) post_dir := path.Join(user.Dir(), "public", folder_name)
@ -50,13 +45,11 @@ func CreateNewPost(user User, title string) {
break break
} }
} }
post := Post{user: user, id: folder_name}
initial_content := "# " + title initial_content := "# " + title
// create post file // create post file
os.Mkdir(post_dir, 0755) os.Mkdir(post_dir, 0755)
os.WriteFile(path.Join(post_dir, "index.md"), []byte(initial_content), 0644) os.WriteFile(post.ContentFile(), []byte(initial_content), 0644)
} return post, nil
func PostDir(post Post) string {
return path.Join(post.user.Dir(), "public", post.title)
} }

View File

@ -13,7 +13,7 @@ func TestCreateNewPostCreatesEntryInPublic(t *testing.T) {
repo, _ := kiss.CreateRepository(testRepoName()) repo, _ := kiss.CreateRepository(testRepoName())
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
// Create a new post // Create a new post
kiss.CreateNewPost(user, "testpost") user.CreateNewPost("testpost")
files, err := ioutil.ReadDir(path.Join(user.Dir(), "public")) files, err := ioutil.ReadDir(path.Join(user.Dir(), "public"))
if err != nil { if err != nil {
t.Error("Error reading directory") t.Error("Error reading directory")
@ -28,9 +28,9 @@ func TestCreateNewPostMultipleCalls(t *testing.T) {
repo, _ := kiss.CreateRepository(testRepoName()) repo, _ := kiss.CreateRepository(testRepoName())
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
// Create a new post // Create a new post
kiss.CreateNewPost(user, "testpost") user.CreateNewPost("testpost")
kiss.CreateNewPost(user, "testpost") user.CreateNewPost("testpost")
kiss.CreateNewPost(user, "testpost") user.CreateNewPost("testpost")
files, err := ioutil.ReadDir(path.Join(user.Dir(), "public")) files, err := ioutil.ReadDir(path.Join(user.Dir(), "public"))
if err != nil { if err != nil {
t.Error("Error reading directory") t.Error("Error reading directory")
@ -45,9 +45,9 @@ func TestCanListUserPosts(t *testing.T) {
repo, _ := kiss.CreateRepository(testRepoName()) repo, _ := kiss.CreateRepository(testRepoName())
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
// Create a new post // Create a new post
kiss.CreateNewPost(user, "testpost") user.CreateNewPost("testpost")
kiss.CreateNewPost(user, "testpost") user.CreateNewPost("testpost")
kiss.CreateNewPost(user, "testpost") user.CreateNewPost("testpost")
posts, err := user.Posts() posts, err := user.Posts()
if err != nil { if err != nil {
t.Error("Error reading posts") t.Error("Error reading posts")