Skip to content

Commit

Permalink
tag support
Browse files Browse the repository at this point in the history
  • Loading branch information
pnmcosta committed Apr 5, 2024
1 parent 1a92ccc commit aecb066
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 26 deletions.
9 changes: 4 additions & 5 deletions internal/templates/index/view.templ
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package index

import "github.com/pnmcosta/csta.dev/internal/posts"
import "github.com/pnmcosta/csta.dev/internal/templates/layout"
import "path"
import "github.com/gosimple/slug"
import "github.com/pnmcosta/csta.dev/internal/templates/post"

templ View(posts []posts.Post) {
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 All @@ -32,11 +31,11 @@ templ View(posts []posts.Post) {
if len(posts) > 0 {
<p>I've also recently started sharing my experience and know-how on these blog articles:</p>
<p>
for i, post := range posts {
for i, p := range posts {
if i >0 {
;
}
<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>
@post.Link(p)
}
</p>
}
Expand Down
9 changes: 9 additions & 0 deletions internal/templates/post/link.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package post

import "github.com/pnmcosta/csta.dev/internal/posts"
import "path"
import "github.com/gosimple/slug"

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>
}
21 changes: 21 additions & 0 deletions internal/templates/tag/view.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tag

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) {
@layout.Base() {
if len(posts) > 0 {
<h1>{ tag } Posts</h1>
<p>
for i, p := range posts {
if i >0 {
;
}
@post.Link(p)
}
</p>
}
}
}
83 changes: 62 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import (
"github.com/gosimple/slug"
"github.com/pnmcosta/csta.dev/internal/posts"
"github.com/pnmcosta/csta.dev/internal/templates/index"
templates "github.com/pnmcosta/csta.dev/internal/templates/post"
postTempl "github.com/pnmcosta/csta.dev/internal/templates/post"
tagTempl "github.com/pnmcosta/csta.dev/internal/templates/tag"
)

type Post = posts.Post

var devFlag = flag.Bool("dev", false, "if true public folder will be served")

func main() {
Expand All @@ -23,56 +26,94 @@ func main() {

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

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

// Write it out.
err = index.View(posts).Render(context.Background(), f)
if err != nil {
log.Fatalf("failed to write index page: %v", err)
}
tags := map[string][]Post{}

// 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 && err != os.ErrExist {
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 := templates.Unsafe(string(post.Content))
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
}

for _, tag := range post.Tags {
tags[tag] = append(tags[tag], post)
}
}

// 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 = templates.View(post, content).Render(context.Background(), f)
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)
}

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

// Write it out.
err = index.View(posts, tagKeys).Render(context.Background(), f)
if err != nil {
log.Fatalf("failed to write index page: %v", err)
}

if !*devFlag {
return
}

// Static serve only on dev
http.Handle("/", http.FileServer(http.Dir("./public")))
log.Print("Listening on 127.0.0.1:3000...")
log.Println("Listening on 127.0.0.1:3000")
err = http.ListenAndServe("127.0.0.1:3000", nil)
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit aecb066

Please sign in to comment.