-
Notifications
You must be signed in to change notification settings - Fork 3
/
genstatic.go
191 lines (167 loc) · 4.35 KB
/
genstatic.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
// +build ignore
// TODO: rewrite this in Neugram!
package main
import (
"bytes"
"compress/gzip"
"encoding/base64"
"flag"
"fmt"
"go/format"
"html/template"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"sort"
"time"
blackfriday "gopkg.in/russross/blackfriday.v2"
)
var outfile = flag.String("o", "", "result will be written file")
var neusvg = loadTmpl("neu.svg")
var bloglist = loadTmpl("blog.html")
var blogatom = loadTmpl("atom.xml")
var blogentry = loadTmpl("blogentry.html")
var mainhtml = loadTmpl("main.html")
var nghtml = loadTmpl("ng.html")
var stylecss = loadTmpl("style.css")
var footer = template.Must(template.New("footer").Parse(`<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-92251090-1', 'auto');
ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
`))
func loadTmpl(filename string) (t *template.Template) {
b, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
return template.Must(footer.New(filename).Parse(string(b)))
}
type BlogEntry struct {
URL string
Title string
Contents template.HTML
ContentsQuoted string
Date string // for humans
PubDate string // for machines: "Mon, 2 Jan 2006 00:00:00 +0000"
}
func writeBytes(buf *bytes.Buffer, name string, src []byte) {
log.Printf("writing %s", name)
gzbuf := new(bytes.Buffer)
gzw, err := gzip.NewWriterLevel(gzbuf, gzip.BestCompression)
if err != nil {
log.Fatal(err)
}
gzw.Write(src)
if err := gzw.Close(); err != nil {
log.Fatal(err)
}
data := base64.StdEncoding.EncodeToString(gzbuf.Bytes())
fmt.Fprintf(buf, "func init() {\n\tstaticHTML[%q] = `` + \n", name)
chunk := ""
for len(data) > 0 {
l := len(data)
if l > 72 {
l = 72
}
chunk, data = data[:l], data[l:]
fmt.Fprintf(buf, "\t`%s` + \n", chunk)
}
fmt.Fprintf(buf, "\t``\n}\n\n")
}
var mdNameRE = regexp.MustCompile(`(\d\d\d\d-\d\d-\d\d)-(.*).md`)
var titleRE = regexp.MustCompile(`^# (.*)\n`)
func writeBlogFiles(buf *bytes.Buffer) {
blogFiles, err := filepath.Glob("blog/*.*")
if err != nil {
log.Fatal(err)
}
sort.Sort(sort.Reverse(sort.StringSlice(blogFiles)))
var entries []BlogEntry
for _, blogFile := range blogFiles {
src, err := ioutil.ReadFile(blogFile)
if err != nil {
log.Fatal(err)
}
match := mdNameRE.FindStringSubmatch(filepath.Base(blogFile))
date, urlTitle := match[1], match[2]
dateVal, err := time.Parse("2006-01-02", date)
if err != nil {
log.Fatalf("%q date: %v", blogFile, err)
}
titleMatch := titleRE.FindSubmatch(src)
if titleMatch == nil {
log.Fatalf("%s: no title found", blogFile)
}
title := string(titleMatch[1])
url := "/blog/" + urlTitle
out := blackfriday.Run(src)
srcbuf := new(bytes.Buffer)
entry := BlogEntry{
URL: url,
Title: title,
Date: date,
PubDate: dateVal.Format("Mon, 2 Jan 2006 00:00:00 +0000"),
Contents: template.HTML(out),
ContentsQuoted: string(out),
}
if err = blogentry.Execute(srcbuf, entry); err != nil {
log.Fatal(err)
}
entries = append(entries, entry)
writeBytes(buf, url, srcbuf.Bytes())
}
srcbuf := new(bytes.Buffer)
if err := bloglist.Execute(srcbuf, entries); err != nil {
log.Fatal(err)
}
writeBytes(buf, "/blog/", srcbuf.Bytes())
type RSS struct {
LatestDate string
Entries []BlogEntry
}
rss := RSS{
LatestDate: entries[0].PubDate,
Entries: entries,
}
srcbuf = new(bytes.Buffer)
if err := blogatom.Execute(srcbuf, rss); err != nil {
log.Fatal(err)
}
writeBytes(buf, "/atom.xml", srcbuf.Bytes())
if len(entries) > 5 {
entries = entries[:5]
}
srcbuf = new(bytes.Buffer)
if err := mainhtml.Execute(srcbuf, entries); err != nil {
log.Fatal(err)
}
writeBytes(buf, "/", srcbuf.Bytes())
}
func main() {
flag.Parse()
buf := new(bytes.Buffer)
fmt.Fprint(buf, "package main\n\n")
srcbuf := new(bytes.Buffer)
if err := nghtml.Execute(srcbuf, nil); err != nil {
log.Fatal(err)
}
writeBytes(buf, "/ng/", srcbuf.Bytes())
b, err := ioutil.ReadFile("favicon.png")
if err != nil {
log.Fatal(err)
}
writeBytes(buf, "/favicon.png", b)
writeBlogFiles(buf)
out, err := format.Source(buf.Bytes())
if err != nil {
buf.WriteTo(os.Stderr)
log.Fatal(err)
}
if err := ioutil.WriteFile(*outfile, out, 0666); err != nil {
log.Fatal(err)
}
}