-
Notifications
You must be signed in to change notification settings - Fork 1
/
blawg.go
103 lines (83 loc) · 1.87 KB
/
blawg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package blawg
import (
"io"
"os"
"path/filepath"
"strings"
)
// MakeBlawg creates a blog in `siteDirectory` out of a directory of posts, using
// standard go html/templates from a directory.
func MakeBlawg(postDirectory, pagesDirectory, templateDirectory, extrasDirectory, siteDirectory string) error {
posts, err := GetPosts(postDirectory)
if err != nil {
return err
}
siteMeta := GetSiteMeta()
if err != nil {
return err
}
SortPostsByDate(posts)
t, err := GetTemplates(templateDirectory)
if err != nil {
return err
}
err = makePosts(siteDirectory, posts, t)
if err != nil {
return err
}
err = makePostIndex(siteDirectory, posts, t)
if err != nil {
return err
}
err = makeHomepage(siteDirectory, posts, t)
if err != nil {
return err
}
pages, err := getPages(pagesDirectory)
if err != nil {
return err
}
err = makePages(siteDirectory, pages, t)
if err != nil {
return err
}
err = makeRSSFeed(siteDirectory, siteMeta, posts)
if err != nil {
return err
}
err = copyExtrasDirectoryContents(extrasDirectory, siteDirectory)
return err
}
func copyExtrasDirectoryContents(publicDirectory, siteDirectory string) (err error) {
err = filepath.Walk(publicDirectory, func(sourcePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
destinationPath := siteDirectory + strings.TrimPrefix(sourcePath, publicDirectory)
if info.IsDir() {
err = os.MkdirAll(destinationPath, info.Mode())
return err
}
source, err := os.Open(sourcePath)
defer source.Close()
if err != nil {
return err
}
destination, err := os.Create(destinationPath)
defer destination.Close()
if err != nil {
return err
}
_, err = io.Copy(destination, source)
if err != nil {
return err
}
err = destination.Sync()
if err != nil {
return err
}
err = destination.Chmod(info.Mode())
return err
})
return err
}