Skip to content

Commit

Permalink
concurrenctly generate files
Browse files Browse the repository at this point in the history
  • Loading branch information
pnmcosta committed Apr 5, 2024
1 parent aecb066 commit 62fae65
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ assets: clean
@cp -a assets/. ./public/

generate: ## Go and Templ generate
@npx tailwindcss -i style.css -o ./public/style.css
@npx tailwindcss -i style.css -o ./public/style.css --minify
@templ generate

run: ## run and watch
Expand Down
6 changes: 3 additions & 3 deletions internal/posts/posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ type Post struct {
}

type walker struct {
posts []Post
posts []*Post
}

func ParsePosts() []Post {
func ParsePosts() []*Post {
w := walker{}
filepath.WalkDir("./posts", w.walk)
// stort by latest
Expand Down Expand Up @@ -115,7 +115,7 @@ func (w *walker) walk(s string, d fs.DirEntry, err error) error {
}

log.Printf("%s: post parsed\n", s)
w.posts = append(w.posts, post)
w.posts = append(w.posts, &post)
}
return nil
}
2 changes: 1 addition & 1 deletion internal/templates/index/view.templ
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "github.com/pnmcosta/csta.dev/internal/posts"
import "github.com/pnmcosta/csta.dev/internal/templates/layout"
import "github.com/pnmcosta/csta.dev/internal/templates/post"

templ View(posts []posts.Post, tags []string) {
templ View(posts []*posts.Post, tags []string) {
@layout.Base() {
<p>
Olá, I'm <a href="https://www.linkedin.com/in/pnmcosta/" target="_blank">Pedro Maia Costa</a> a product
Expand Down
2 changes: 1 addition & 1 deletion internal/templates/post/link.templ
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import "github.com/pnmcosta/csta.dev/internal/posts"
import "path"
import "github.com/gosimple/slug"

templ Link(post posts.Post) {
templ Link(post *posts.Post) {
<a href={ templ.SafeURL(path.Join("/", post.Date.Format("2006/01/02"), slug.Make(post.Title), "/")) }>{ post.Title } ({ post.Date.Format("2006/01/02") })</a>
}
2 changes: 1 addition & 1 deletion internal/templates/post/view.templ
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package post
import "github.com/pnmcosta/csta.dev/internal/posts"
import "github.com/pnmcosta/csta.dev/internal/templates/layout"

templ View(post posts.Post, content templ.Component) {
templ View(post *posts.Post, content templ.Component) {
@layout.Base() {
<div class="content">
@content
Expand Down
2 changes: 1 addition & 1 deletion internal/templates/tag/view.templ
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "github.com/pnmcosta/csta.dev/internal/posts"
import "github.com/pnmcosta/csta.dev/internal/templates/layout"
import "github.com/pnmcosta/csta.dev/internal/templates/post"

templ View(tag string, posts []posts.Post) {
templ View(tag string, posts []*posts.Post) {
@layout.Base() {
if len(posts) > 0 {
<h1>{ tag } Posts</h1>
Expand Down
118 changes: 67 additions & 51 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"path"
"sync"

"github.com/gosimple/slug"
"github.com/pnmcosta/csta.dev/internal/posts"
Expand All @@ -26,74 +27,89 @@ func main() {

// Output path.
rootPath := "public"
if err := os.Mkdir(rootPath, 0755); err != nil && !os.IsExist(err) {
if err := os.Mkdir(rootPath, 0755); err != nil && os.IsNotExist(err) {
log.Fatalf("failed to create output directory: %v", err)
}

tags := map[string][]Post{}
tags := map[string][]*Post{}

var wg sync.WaitGroup
wg.Add(len(posts))
// Create a page for each post.
for _, post := range posts {
// Create the output directory.
dir := path.Join(rootPath, post.Date.Format("2006/01/02"), slug.Make(post.Title))
if err := os.MkdirAll(dir, 0755); err != nil && !os.IsExist(err) {
log.Fatalf("failed to create dir %q: %v", dir, err)
continue
}

// Create the output file.
name := path.Join(dir, "index.html")
f, err := os.Create(name)
if err != nil {
log.Fatalf("failed to create output file: %v", err)
continue
}

// Create an unsafe component containing raw HTML.
content := postTempl.Unsafe(string(post.Content))

// Use templ to render the template containing the raw HTML.
err = postTempl.View(post, content).Render(context.Background(), f)
if err != nil {
log.Fatalf("failed to write output file: %v", err)
continue
}

go func() {
defer wg.Done()

// Create the output directory.
dir := path.Join(rootPath, post.Date.Format("2006/01/02"), slug.Make(post.Title))
if err := os.MkdirAll(dir, 0755); err != nil && os.IsNotExist(err) {
log.Fatalf("failed to create dir %q: %v", dir, err)
return
}

// Create the output file.
name := path.Join(dir, "index.html")
f, err := os.Create(name)
if err != nil {
log.Fatalf("failed to create output file: %v", err)
return
}

// Create an unsafe component containing raw HTML.
content := postTempl.Unsafe(string(post.Content))

// Use templ to render the template containing the raw HTML.
err = postTempl.View(post, content).Render(context.Background(), f)
if err != nil {
log.Fatalf("failed to write output file: %v", err)
return
}
}()

// note failure to render will still generate tag ref
for _, tag := range post.Tags {
tags[tag] = append(tags[tag], post)
}
}
wg.Wait()

wg.Add(len(tags))
// Create a page for each tag.
tagKeys := make([]string, 0, len(tags))
for tag, posts := range tags {
slug := slug.Make(tag)

// Create the output directory.
dir := path.Join(rootPath, "tag", slug)
if err := os.MkdirAll(dir, 0755); err != nil && !os.IsExist(err) {
log.Fatalf("failed to create dir %q: %v", dir, err)
continue
}

// Create the output file.
name := path.Join(dir, "index.html")
f, err := os.Create(name)
if err != nil {
log.Fatalf("failed to create output file: %v", err)
continue
}

// Use templ to render the template containing the raw HTML.
err = tagTempl.View(tag, posts).Render(context.Background(), f)
if err != nil {
log.Fatalf("failed to write output file: %v", err)
continue
}

tagKeys = append(tagKeys, tag)

go func() {
defer wg.Done()

slug := slug.Make(tag)

// Create the output directory.
dir := path.Join(rootPath, "tag", slug)
if err := os.MkdirAll(dir, 0755); err != nil && os.IsNotExist(err) {
log.Fatalf("failed to create dir %q: %v", dir, err)
return
}

// Create the output file.
name := path.Join(dir, "index.html")
f, err := os.Create(name)
if err != nil {
log.Fatalf("failed to create output file: %v", err)
return
}

// Use templ to render the template containing the raw HTML.
err = tagTempl.View(tag, posts).Render(context.Background(), f)
if err != nil {
log.Fatalf("failed to write output file: %v", err)
return
}
}()
}

wg.Wait()

// Create an index page.
name := path.Join(rootPath, "index.html")
f, err := os.Create(name)
Expand Down

0 comments on commit 62fae65

Please sign in to comment.