-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen.go
More file actions
246 lines (200 loc) · 5.2 KB
/
gen.go
File metadata and controls
246 lines (200 loc) · 5.2 KB
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
package main
import (
"bufio"
"encoding/json"
"fmt"
"html/template"
"log"
"os"
"os/exec"
"sort"
"time"
)
const (
contentDir = "content"
outputDir = "posts"
postDateLayout = "Jan 2, 2006"
)
type Post struct {
Name string `json:"name"` // Always present.
Title string `json:"title"` // Always present.
Date string `json:"date"` // Always present.
RedirectTo string `json:"redirect-to,omitempty"` // "" in case of a regular post.
}
func (post *Post) inputFilename() string {
return fmt.Sprintf("%s/%s.md", contentDir, post.Name)
}
func (post *Post) outputFilename() string {
return fmt.Sprintf("%s/%s.html", outputDir, post.Name)
}
func (post *Post) parseDate() time.Time {
date, err := time.Parse(postDateLayout, post.Date)
if err != nil {
log.Fatalf("Cannot parse date in '%s': %v.", post.Name, err)
}
return date
}
func main() {
if len(os.Args) == 2 && os.Args[1] == "new" {
newPost()
return
}
posts := readPostsMetadata()
genPostPages(posts)
genIndexHtml(posts)
}
func readPostsMetadata() []Post {
postsData, err := os.ReadFile("metadata/posts.json")
if err != nil {
log.Fatalf("Cannot read 'metadata/posts.json': %v.", err)
}
var posts []Post
if err := json.Unmarshal(postsData, &posts); err != nil {
log.Fatal(err)
}
return posts
}
func genPostPages(posts []Post) {
var outputFilenames []string
for _, post := range posts {
if post.RedirectTo != "" {
genRedirectHtml(&post)
} else {
invokePandoc(&post)
outputFilenames = append(outputFilenames, post.outputFilename())
}
}
transformDOM(outputFilenames)
}
func genRedirectHtml(post *Post) {
htmlFilename := fmt.Sprintf("%s/%s.html", outputDir, post.Name)
file, err := os.Create(htmlFilename)
defer file.Close()
if err != nil {
log.Fatalf("Cannot create '%s': %v.", htmlFilename, err)
}
w := bufio.NewWriter(file)
defer w.Flush()
t := template.Must(template.ParseFiles("templates/redirect.tmpl"))
err = t.ExecuteTemplate(w, "redirect.tmpl", post)
if err != nil {
log.Fatal(err)
}
}
func invokePandoc(post *Post) {
cmd := exec.Command(
"pandoc", post.inputFilename(),
"--output", post.outputFilename(),
"--metadata", fmt.Sprint("author=hirrolot"),
"--metadata", fmt.Sprintf("title=%s", post.Title),
"--metadata", fmt.Sprintf("date=%s", post.Date),
"--standalone",
"--mathjax",
"-H", "header.html",
"--table-of-contents",
"--citeproc",
"--css", "../style.css",
"--include-after-body", "giscus.html",
"--include-in-header", "post-header.html")
output, err := cmd.CombinedOutput()
fmt.Print(string(output))
if err != nil {
log.Fatal(err)
}
}
func transformDOM(outputFilenames []string) {
args := append([]string{"transform.js"}, outputFilenames...)
cmd := exec.Command("node", args...)
output, err := cmd.CombinedOutput()
defer fmt.Print(string(output))
if err != nil {
log.Fatal(err)
}
}
type IndexTemplate struct {
OutputDir string
Posts []Post
Contacts []Contact
}
type Contact struct {
Link, Img, Description string
}
func genIndexHtml(posts []Post) {
file, err := os.Create("index.html")
if err != nil {
log.Fatalf("Cannot create 'index.html': %v.", err)
}
defer file.Close()
w := bufio.NewWriter(file)
defer w.Flush()
t := template.Must(template.ParseFiles("templates/index.tmpl", "header.html"))
sort.Slice(posts, func(i, j int) bool {
date_i := posts[i].parseDate()
date_j := posts[j].parseDate()
return date_i.After(date_j)
})
err = t.ExecuteTemplate(w, "index.tmpl",
IndexTemplate{OutputDir: outputDir, Posts: posts, Contacts: readContacts()})
if err != nil {
log.Fatal(err)
}
}
func readContacts() []Contact {
contactsData, err := os.ReadFile("metadata/contacts.json")
if err != nil {
log.Fatalf("Cannot read 'metadata/contacts.json': %v.", err)
}
var contacts []Contact
if err := json.Unmarshal(contactsData, &contacts); err != nil {
log.Fatal(err)
}
return contacts
}
func newPost() {
scanner := bufio.NewScanner((os.Stdin))
var post Post
fmt.Print("Title: ")
if hasMore := scanner.Scan(); !hasMore {
log.Fatal("Cannot read more lines.")
}
post.Title = scanner.Text()
fmt.Print("Name: ")
if hasMore := scanner.Scan(); !hasMore {
log.Fatal("Cannot read more lines.")
}
post.Name = scanner.Text()
fmt.Print("Is it a redirect? (y/n): ")
if hasMore := scanner.Scan(); !hasMore {
log.Fatal("Cannot read more lines.")
}
isRedirect := scanner.Text()
switch isRedirect {
case "y":
fmt.Print("Redirect to: ")
if hasMore := scanner.Scan(); !hasMore {
log.Fatal("Cannot read more lines.")
}
post.RedirectTo = scanner.Text()
case "n":
fmt.Println("Ok, no redirecting.")
default:
log.Fatal("Invalid answer; aborting.")
}
post.Date = time.Now().Format(postDateLayout)
posts := readPostsMetadata()
posts = append([]Post{post}, posts...)
postsData, err := json.MarshalIndent(posts, "", " ")
if err != nil {
log.Fatal(err)
}
if err := os.WriteFile("metadata/posts.json", postsData, 0644); err != nil {
log.Fatal(err)
}
if isRedirect == "n" {
postContent := "<div class=\"introduction\">\n\n\n\n</div>\n"
if err := os.WriteFile(post.inputFilename(), []byte(postContent), 0644); err != nil {
log.Fatal(err)
}
}
fmt.Println("Done.")
}