aboutsummaryrefslogtreecommitdiffstats
path: root/site/site.go
blob: b69a4af22478b3508204fedc0cd86a6125be791e (plain) (blame)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package site

import (
	"adammathes.com/snkt/archive"
	"adammathes.com/snkt/config"
	"adammathes.com/snkt/post"
	"adammathes.com/snkt/render"
	"adammathes.com/snkt/vlog"
	"io/ioutil"
	"log"
	"path"
	"sort"
	"strings"
)

type Site struct {
	Title string
	URL   string

	Posts post.Posts

	// all archives are optional based on presence of template
	Archive      *archive.ListArchive
	Home         *archive.ListArchive
	Rss          *archive.ListArchive
	Paged        *archive.PagedArchives
	Tagged       *archive.TagArchives
	ListArchives []*archive.ListArchive
}

/*
Read reads post data from the filesystem and populates posts and archives
*/
func (s *Site) Read() {
	s.Title = config.Config.SiteTitle
	s.URL = config.Config.SiteURL
	s.ReadPosts()

	if render.TmplExists("archive") {
		s.Archive = archive.NewListArchive(s.Posts)
		s.Archive.Site = s
		sort.Sort(sort.Reverse(s.Archive.Posts))
	}
	if render.TmplExists("rss") {
		s.Rss = archive.NewRssArchive(s.Posts)
		s.Rss.Site = s
	}
	if render.TmplExists("paged") {
		s.Paged = archive.CreatePaged(15, s.Posts)
	}
	if render.TmplExists("tag") {
		s.Tagged = archive.ParseTags(s.Posts)
	}
	if render.TmplExists("home") {
		s.Home = archive.NewListArchive(s.Posts)
		s.Home.Tgt = path.Join(config.Config.HtmlDir, "index.html")
		s.Home.Template = "home"
		s.Home.Site = s
	}

	// generic list templates
	for _, t := range render.TemplateNames() {
		if strings.HasSuffix(t, ".list") {
			la := archive.NewGenericListArchive(s.Posts, t, strings.TrimSuffix(t, ".list"))
			s.ListArchives = append(s.ListArchives, la)
		}
	}
}

/*
ReadPosts reads all files from the Config.TxtDir, parses them and stores in s.Posts
*/
func (s *Site) ReadPosts() {
	// TODO: filter this as needed
	files, err := ioutil.ReadDir(config.Config.TxtDir)
	if err != nil {
		log.Fatal(err)
	}

	for _, file := range files {

		if config.IgnoredFile(file.Name()) {
			vlog.Printf("ignoring file: %s\n", file.Name())
			continue
		}

		// ignore dotfiles
		if file.Name()[:1] == "." {
			continue
		}

		p := post.NewPost(s)
		p.Read(file)

		// Ignore post-dated posts unless overriden in config
		if !config.Config.ShowFuture && p.InFuture {
			vlog.Printf("Skipping future dated post: %s\n", p.SourceFile)
			continue
		}

		s.Posts = append(s.Posts, p)
	}

	// Sort the posts by date, earliest first
	sort.Sort(s.Posts)

	// Add next/previous to each post.
	// An allocated but empty post is set at start/end
	// This prevents templates from failing at start/end if nil checks are not made properly
	for i, p := range s.Posts {
		if i > 0 {
			p.Prev = s.Posts[i-1]
		} else {
			p.Prev = new(post.Post)
		}
		if i+1 < len(s.Posts) {
			p.Next = s.Posts[i+1]
		} else {
			p.Next = new(post.Post)
		}
	}
}

/*
Write writes posts and archives to the filesystem
*/
func (s *Site) Write() {
	s.WritePosts()
	s.WriteArchives()
}

func (s *Site) WriteArchives() {
	if render.TmplExists("archive") {
		render.Write(s.Archive)
	}
	if render.TmplExists("rss") {
		render.Write(s.Rss)
	}
	if render.TmplExists("home") {
		render.Write(s.Home)
	}
	if render.TmplExists("paged") {
		for _, p := range *s.Paged {
			p.Site = s
			render.Write(p)
		}
	}
	if render.TmplExists("tag") {
		for _, t := range *s.Tagged {
			t.Site = s
			render.Write(t)
		}
	}
	for _, t := range s.ListArchives {
		t.Site = s
		render.Write(t)
	}

}

func (s *Site) WritePosts() {
	for _, p := range s.Posts {
		render.Write(p)
	}
}

func (s Site) GetTitle() string {
	return s.Title
}

func (s Site) GetURL() string {
	return s.URL
}