forked from kushaldas/shonku
/
scrdkd.go
420 lines (371 loc) · 7.96 KB
/
scrdkd.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package main
import (
"bufio"
"bytes"
"code.google.com/p/go-sqlite/go1/sqlite3"
"crypto/sha256"
"database/sql"
"encoding/base64"
"fmt"
"github.com/russross/blackfriday"
"html/template"
"io"
"io/ioutil"
"net/url"
"os"
"sort"
"strings"
"time"
"flag"
"encoding/json"
)
type Configuration struct {
Author string
Title string
URL string
Content_footer string
Disqus string
}
type Article struct {
Id int
Path string
Hash string
}
type Post struct {
Title string
Slug string
Body template.HTML
Date time.Time
Tags []string
}
type Indexposts struct {
Posts []Post
NextF bool
PreviousF bool
Next int
Previous int
NextLast bool
}
type ByDate []Post
func (a ByDate) Len() int { return len(a) }
func (a ByDate) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByDate) Less(i, j int) bool { return a[i].Date.After(a[j].Date) }
type ByODate []Post
func (a ByODate) Len() int { return len(a) }
func (a ByODate) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByODate) Less(i, j int) bool { return a[j].Date.After(a[i].Date) }
/*
Creates the empty build database.
*/
func createdb() {
filename := ".scrdkd.db"
if _, err := os.Stat(filename); os.IsNotExist(err) {
// Our db is missing, time to recreate it.
conn, err := sqlite3.Open(filename)
if err != nil {
fmt.Println("Unable to open the database: %s", err)
os.Exit(1)
}
defer conn.Close()
conn.Exec("CREATE TABLE builds(id INTEGER PRIMARY KEY AUTOINCREMENT, path TEXT, hash TEXT);")
}
}
/*
Finds all the files from our posts directory.
*/
func findfiles() []string {
files, _ := ioutil.ReadDir("./posts/")
names := make([]string, 0)
for _, f := range files {
names = append(names, "./posts/"+f.Name())
}
return names
}
/*
Creates the hash for each files.
*/
func create_hash(filename string) string {
md, err := ioutil.ReadFile(filename)
if err == nil {
data := sha256.Sum256(md)
data2 := data[:]
s := base64.URLEncoding.EncodeToString(data2)
return s
//fmt.Println(s)
}
return ""
}
/*
Finds out if the file content changed from the last build.
*/
func changed_ornot(filename, hash string) bool {
db := ".scrdkd.db"
if _, err := os.Stat(db); err == nil {
// Our db is missing, time to recreate it.
conn, err := sql.Open("sqlite3", db)
if err != nil {
fmt.Println("Unable to open the database: %s", err)
os.Exit(1)
}
defer conn.Close()
stmt := fmt.Sprintf("SELECT id, path, hash FROM builds where path='%s';", filename)
rows, err := conn.Query(stmt)
defer rows.Close()
if rows.Next() {
var article Article
err = rows.Scan(&article.Id, &article.Path, &article.Hash)
if article.Hash == hash {
return false
} else { // File hash has changed, we need to update the db
stmt = fmt.Sprintf("UPDATE builds SET hash='%s' where id=%d;", hash, article.Id)
rows.Close()
_, err = conn.Exec(stmt)
if err != nil {
fmt.Println(err)
}
return true
}
} else { // Should be insert into DB
conn.Exec("INSERT INTO builds(path, hash) VALUES (?, ?)", filename, hash)
return true
}
}
return true
}
/*
Returns the slug of the article
TODO:
We need to improve this function much more.
*/
func get_slug(s string) string {
s = strings.Replace(s, "(", "-", -1)
s = strings.Replace(s, ")", "-", -1)
s = strings.Replace(s, " ", "-", -1)
s = strings.Replace(s, "(", "-", -1)
s = url.QueryEscape(s)
return strings.ToLower(s)
}
/*
Reads a post and gets all details from it.
*/
func read_post(filename string) Post {
var buffer bytes.Buffer
var p Post
flag := false
f, err := os.Open(filename)
if err != nil {
fmt.Println(err)
return p
}
defer f.Close()
r := bufio.NewReader(f)
titleline, err := r.ReadString('\n')
dateline, err := r.ReadString('\n')
line, err := r.ReadString('\n')
tagline := line
for err == nil {
if line == "====\n" {
flag = true
line, err = r.ReadString('\n')
continue
}
if flag {
buffer.WriteString(line)
}
line, err = r.ReadString('\n')
}
if err == io.EOF {
title := titleline[6:]
title = strings.TrimSpace(title)
date := dateline[5:]
date = strings.TrimSpace(date)
tagsnonstripped := strings.Split(tagline[5:], ",")
tags := make([]string, 0)
for i := range tagsnonstripped {
word := strings.TrimSpace(tagsnonstripped[i])
tags = append(tags, word)
}
p.Title = title
p.Slug = get_slug(title)
body := blackfriday.MarkdownCommon(buffer.Bytes())
p.Body = template.HTML(string(body))
p.Date = get_time(date)
p.Tags = tags
}
return p
}
func get_time(text string) time.Time {
const longform = "2006-01-02 15:04:05.999999999 -0700 MST"
//now := time.Now()
//x := now.String()
//fmt.Println(x)
new_now, err := time.Parse(longform, text)
if err != nil {
fmt.Println(err)
}
return new_now
}
/*
Builds a post based on the template
*/
func build_post(ps Post) string {
var doc bytes.Buffer
var body string
tml, _ := template.ParseFiles("./templates/post.html")
err := tml.Execute(&doc, ps)
if err != nil {
fmt.Println(err)
}
body = doc.String()
name := "./output/" + ps.Slug + ".html"
f, err := os.Create(name)
defer f.Close()
n, err := io.WriteString(f, body)
if err != nil {
fmt.Println(n, err)
}
return body
}
/*
Creates index pages.
*/
func build_index(pss []Post, index, pre, next int) {
fmt.Println(index, pre, next)
var doc bytes.Buffer
var body, name string
var ips Indexposts
ips.Posts = pss
if pre != 0 {
ips.PreviousF = true
ips.Previous = pre
} else {
ips.PreviousF = false
}
if next > 0 {
ips.NextF = true
ips.Next = next
} else if next == -1 {
ips.NextF = false
} else {
ips.NextF = true
ips.Next = next
}
if next == 0 {
ips.NextLast = true
}
tml, _ := template.ParseFiles("./templates/index.html")
err := tml.Execute(&doc, ips)
if err != nil {
fmt.Println(err)
}
body = doc.String()
if next == -1 {
name = "./output/index.html"
} else {
name = fmt.Sprintf("./output/index-%d.html", index)
}
f, err := os.Create(name)
defer f.Close()
n, err := io.WriteString(f, body)
if err != nil {
fmt.Println(n, err)
}
}
/*
Checks for path.
*/
func exists(path string) bool {
_, err := os.Stat(path)
if err == nil { return true }
if os.IsNotExist(err) { return false }
return false
}
/*
Creates the required directory structure.
*/
func create_dirs(){
if !exists("./templates/") {
os.Mkdir("./templates/", 0777)
}
if !exists("./output/") {
os.Mkdir("./output/", 0777)
}
if !exists("./posts/") {
os.Mkdir("./posts/", 0777)
}
}
/*
Creates new site
*/
func create_site(){
create_dirs()
}
/*
Reads and returns the configuration.
*/
func get_conf()Configuration{
file, _ := os.Open("conf.json")
decoder := json.NewDecoder(file)
//configuration := make(Configuration)
var configuration Configuration
decoder.Decode(&configuration)
return configuration
}
func main() {
new_site := flag.Bool("new_site", false, "Creates a new site.")
flag.Parse()
if *new_site {
create_site()
os.Exit(0)
}
conf := get_conf()
fmt.Println(conf)
createdb()
rebuild_index := false
ps := make([]Post, 0)
//sort_index := make([]Post, 0)
names := findfiles()
for i := range names {
hash := create_hash(names[i])
post := read_post(names[i])
ps = append(ps, post)
if changed_ornot(names[i], hash) {
fmt.Println(names[i])
build_post(post)
rebuild_index = true
}
}
sort.Sort(ByODate(ps))
if rebuild_index == true {
var prev, next int
index := 1
num := 0
length := len(ps)
sort_index := make([]Post, 0)
for i := range ps {
sort_index = append(sort_index, ps[i])
num = num + 1
if num == 3 {
sort.Sort(ByODate(sort_index))
if index == 1 {
prev = 0
} else {
prev = index -1
}
if (index * 3) < length && (length - index*3) > 3 {
next = index +1
} else {
next = 0
}
build_index(sort_index, index, prev, next)
sort_index = make([]Post, 0)
index = index + 1
num = 0
}
}
if len(sort_index) > 0 {
sort.Sort(ByODate(sort_index))
build_index(sort_index, 0, index-1, -1)
}
}
}