Skip to content

Commit

Permalink
generate posts with data
Browse files Browse the repository at this point in the history
  • Loading branch information
mna committed Jul 10, 2013
1 parent a558b46 commit 08a91f5
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 17 deletions.
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
)

var (
postTpl *template.Template
postTpl *template.Template
postTplNm = "post.amber"
)

func main() {
Expand Down Expand Up @@ -40,13 +41,14 @@ func main() {
}

func compileTemplate() {
ap := filepath.Join(TemplatesDir, "post.amber")
ap := filepath.Join(TemplatesDir, postTplNm)
if _, err := os.Stat(ap); os.IsNotExist(err) {
// Amber post template does not exist, compile the native Go templates
postTpl, err = template.ParseGlob(filepath.Join(TemplatesDir, "*.html"))
if err != nil {
log.Fatal("FATAL ", err)
}
postTplNm = "post" // TODO : Validate this...
} else {
c := amber.New()
if err := c.ParseFile(ap); err != nil {
Expand Down
17 changes: 17 additions & 0 deletions public/test1
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html><!-- From HTML5 Boilerplate -->
<html class="no-js">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>test1</title>
<meta name="description" content="description" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="/public/css/normalize.css" />
<link rel="stylesheet" href="/public/css/main.css" />
</head>
<body>

<h1>test1</h1>
<div>[60 104 49 62 84 104 105 115 32 105 115 32 109 121 32 102 105 114 115 116 32 112 111 115 116 33 60 47 104 49 62 10 10 60 112 62 73 38 114 115 113 117 111 59 109 32 115 111 32 104 97 112 112 121 32 116 111 32 119 114 105 116 101 32 116 104 105 115 32 114 105 103 104 116 32 110 111 119 33 60 47 112 62 10]</div>
</body>
</html>
17 changes: 17 additions & 0 deletions public/test2
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html><!-- From HTML5 Boilerplate -->
<html class="no-js">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>test2</title>
<meta name="description" content="description" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="/public/css/normalize.css" />
<link rel="stylesheet" href="/public/css/main.css" />
</head>
<body>

<h1>test2</h1>
<div>[60 104 49 62 84 101 115 116 32 50 33 60 47 104 49 62 10]</div>
</body>
</html>
17 changes: 17 additions & 0 deletions public/test3
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html><!-- From HTML5 Boilerplate -->
<html class="no-js">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>test3</title>
<meta name="description" content="description" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="/public/css/normalize.css" />
<link rel="stylesheet" href="/public/css/main.css" />
</head>
<body>

<h1>test3</h1>
<div>[60 104 49 62 84 101 115 116 32 51 33 60 47 104 49 62 10]</div>
</body>
</html>
4 changes: 2 additions & 2 deletions templates/layout.amber
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ html.no-js
head
meta[charset="utf-8"]
meta[http-equiv="X-UA-Compatible"][content="IE=edge"]
title ${Title}
meta[name="description"][content=Description]
title #{Post.ShortPost.Title}
meta[name="description"][content=Post.ShortPost.Description]
meta[name="viewport"][content="width=device-width, initial-scale=1"]
link[rel="stylesheet"][href="/public/css/normalize.css"]
link[rel="stylesheet"][href="/public/css/main.css"]
Expand Down
3 changes: 2 additions & 1 deletion templates/post.amber
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extends layout

block content
h1 My Post!
h1 #{Post.ShortPost.Title}
div #{Post.Content}
98 changes: 86 additions & 12 deletions watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
"time"

"github.com/howeyc/fsnotify"
"github.com/russross/blackfriday"
)

const (
// Sometimes many events can be triggered in succession for the same file
// (i.e. Create followed by Modify, etc.). No need to rush to generate
// the HTML, just wait for it to calm down before processing.
watchEventDelay = 30 * time.Second
maxRecentPosts = 2
)

// Receive watcher events for the posts directory. All events require re-generating
Expand Down Expand Up @@ -68,9 +70,87 @@ func regeneratePosts() {
sfi := sortableFileInfo(fis)
sfi = FilterDir(sfi)
sort.Sort(sfi)
for _, fi := range sfi {
regenerateFile(fi)

recent := make([]*ShortPost, maxRecentPosts)
all := make([]*LongPost, len(sfi))
// First pass to get the recent posts (and others) so that
// they can be passed to all posts.
for i, fi := range sfi {
all[i] = newLongPost(fi)
if i < maxRecentPosts {
recent[i] = all[i].Short()
}
}

for i, p := range all {
td := newTemplateData(p, i, recent, all)
regenerateFile(td)
}
}

type TemplateData struct {
Post *LongPost
Recent []*ShortPost
Prev *ShortPost
Next *ShortPost
}

func newTemplateData(p *LongPost, i int, r []*ShortPost, all []*LongPost) *TemplateData {
td := &TemplateData{Post: p, Recent: r}

if i > 0 {
td.Prev = all[i-1].Short()
}
if i < len(all)-2 {
td.Next = all[i+1].Short()
}
return td
}

type ShortPost struct {
Slug string
Author string
Title string
Description string
PubTime time.Time
ModTime time.Time
}

type LongPost struct {
*ShortPost
Content []byte
}

func newLongPost(fi os.FileInfo) *LongPost {
slug := strings.Replace(fi.Name(), filepath.Ext(fi.Name()), "", 1)
sp := &ShortPost{
slug,
"author", // TODO : Complete...
slug, // TODO : Read first heading, or front matter
"description", // TODO : Read front matter
fi.ModTime(), // TODO : This is NOT the pub time...
fi.ModTime(),
}

f, err := os.Open(filepath.Join(PostsDir, fi.Name()))
if err != nil {
log.Fatal("FATAL ", err)
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal("FATAL ", err)
}
res := blackfriday.MarkdownCommon(b)
lp := &LongPost{
sp,
res,
}
return lp
}

func (lp *LongPost) Short() *ShortPost {
return lp.ShortPost
}

// TODO : Should pass to the template:
Expand All @@ -82,20 +162,14 @@ func regeneratePosts() {
// Next : The next (more recent) post
// Previous : The previous (older) post

func regenerateFile(fi os.FileInfo) {
f, err := os.Open(filepath.Join(PostsDir, fi.Name()))
func regenerateFile(td *TemplateData) {
fw, err := os.Create(filepath.Join(PublicDir, td.Post.Slug))
if err != nil {
log.Fatal("FATAL ", err)
}
defer f.Close()
// TODO : Blackfriday...

nm := fi.Name()
nm = strings.Replace(nm, filepath.Ext(nm), "", 1)
fw, err := os.Create(filepath.Join(PublicDir, nm))
defer fw.Close()
err = postTpl.ExecuteTemplate(fw, postTplNm, td)
if err != nil {
log.Fatal("FATAL ", err)
}
defer fw.Close()
postTpl.ExecuteTemplate(fw, "post", nil)
}

0 comments on commit 08a91f5

Please sign in to comment.