aboutsummaryrefslogtreecommitdiffstats
path: root/site/init.go
blob: 82eda442ff7c2411d1c296d48d79b285e90b2313 (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
package site

import (
	"path"
	"adammathes.com/snkt/render"
	"adammathes.com/snkt/config"
	"os"
	"log"
	"gopkg.in/yaml.v2"
)

type skeleton struct {
	Dir string
	Filename string
	Content []byte
}

var skeletons = []skeleton {
	{
			Dir: "tmpl",
			Filename: "base",
			Content: []byte(
`{{define "base"}}
<!DOCTYPE html>
<html>
  <head>
      <title>{{template "title" .}}</title>
  </head>
  <body>
      {{template "content" .}}
      <hr />
      <p><a href="{{.Site.URL}}/index.html">Home</a></p>
      <p><a href="{{.Site.URL}}/archive.html">Archive</a></p>
  </body>
</html>
{{end}}
{{define "title"}}{{end}}
{{define "content"}}{{end}}
`)},
		{
			Dir: "tmpl",
			Filename: "home",
			Content: []byte(
`{{define "title"}}{{.Site.Title}}{{end}}
{{define "content"}}
<h1>{{.Site.Title}}</h1>
{{range .Posts.Limit 15}}
<h2><a href="{{.Permalink}}">{{.Title}}</a></h2>    
{{.Content}}
{{end}}
{{end}}
`)},
	{
			Dir: "tmpl",
			Filename: "post",
			Content: []byte(
`{{define "title"}}{{.Post.Title}} - {{.Site.Title}}{{end}}
{{define "content"}}
<h1>{{.Site.Title}}</h1>
<h2>{{.Post.Title}}</h2>
{{.Post.Content}}
{{if .Post.Next.Title}}
<p><a href="{{.Post.Next.Permalink}}">{{.Post.Next.Title}}</a></p>
{{end}}
    {{if .Post.Prev.Title}}
<p><a href="{{.Post.Prev.Permalink}}">{{.Post.Prev.Title}}</a></p>
{{end}}
{{end}}
`)},
		{
			Dir: "tmpl",
			Filename: "archive",
			Content: []byte(
`{{define "title"}}{{.Site.Title}} Archives{{end}}
{{define "content"}}
<h1>{{ .Site.Title }}</h1>
<h2>Archives</h2>
{{range .Posts}}
{{if .}}<h2><a href="{{.Permalink}}">{{.Title}}</a></h2>{{end}}
{{end}}
{{end}}
`)},
		{
			Dir: "tmpl",
			Filename: "rss",
			Content: []byte(
`{{define "base"}}
<?xml version="1.0" encoding="UTF-8" ?> 
<rss version="2.0">
<channel>
  <title>{{ .Site.Title }}</title>
  <link>{{ .Site.URL }}</link> 
  <description></description>
  {{ range .Posts.Limit 15 }}
  <item>    
    <link>{{ .Permalink }}</link>
    <title><![CDATA[{{ .Title }}]]></title>
    <pubDate>{{ .RssDate }}</pubDate>
    <description><![CDATA[ 
    {{ .AbsoluteContent }}
    ]]>
    </description>
  </item>
  {{ end }}
</channel>
</rss>
{{end}}
`)}}

/*
Init initializes a new site in `directory` This includes: create `directory` and populate with:
config.json with sane defaults
txt, html directories for input/output
tmpl directory with barebones [base,post,archive,rss] templates
*/
var init_dir = ""
func Init(directory string) {
	init_dir = directory

	if init_dir[0] != '/' {
		wd, err := os.Getwd()
		if err == nil {
			init_dir = path.Join(wd, init_dir)
		}
	}

	var cfg = config.Settings{
		SiteTitle: "snkt",
		SiteURL: "",
		TxtDir: path.Join(init_dir, "txt"),
		HtmlDir: path.Join(init_dir, "html"),
		TmplDir: path.Join(init_dir, "tmpl"),
	}

	cyaml, err := yaml.Marshal(cfg)
	if err != nil {
		log.Fatal("marshalling yaml error: ", err)
	}
	
	c := skeleton{
		Dir: "",
		Filename: "config.yml",
		Content: cyaml,
	}
	skeletons = append(skeletons, c)

	os.MkdirAll( cfg.TxtDir , 0755)
	os.MkdirAll( cfg.HtmlDir , 0755)
	os.MkdirAll( cfg.TmplDir , 0755)

	writeSkeletons()
}


func (s skeleton) Render() []byte {
	return s.Content
}

func (s skeleton) Target() string {
	return path.Join(init_dir, s.Dir, s.Filename)
}

func writeSkeletons() {
	for _,skeleton := range skeletons {
		render.Write(skeleton)
	}
}