-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
359 lines (312 loc) · 7.16 KB
/
main.go
File metadata and controls
359 lines (312 loc) · 7.16 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
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
package main
import (
"bytes"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/yuin/goldmark"
highlighting "github.com/yuin/goldmark-highlighting/v2"
"github.com/yuin/goldmark/extension"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
type Post struct {
Title string
Date string
Author string
Content template.HTML
FileName string
}
type PageData struct {
PageTitle string
Title string
Date string
Author string
Posts []Post
Body template.HTML
About template.HTML
}
var md goldmark.Markdown
func main() {
// Configure Markdown parser with syntax highlighting
md = goldmark.New(
goldmark.WithExtensions(
extension.GFM,
highlighting.NewHighlighting(
highlighting.WithStyle("github"),
),
),
)
// Set up HTTP handlers
http.HandleFunc("/", indexHandler)
http.HandleFunc("/post/", postHandler)
// Start server
log.Println("Server running on http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
posts, err := listPosts()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl := template.Must(template.New("base").Parse(baseTemplate))
tmpl = template.Must(tmpl.Parse(indexTemplate))
data := PageData{
PageTitle: "Academic Blog",
Posts: posts,
About: template.HTML("<p>Hello everyone! I am a software engineer with a passion for AI. I am currently working on <b>Reinforcement Learning</b> and <b>LLM Reasoning</b>. Feel free to send a cold email to <a href='mailto:okan@detorch.xyz'>okan@detorch.xyz</a> if you have questions, or want to offer collaborations or jobs.</p>"),
}
if err := tmpl.Execute(w, data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func postHandler(w http.ResponseWriter, r *http.Request) {
fileName := strings.TrimPrefix(r.URL.Path, "/post/")
contentBytes, err := os.ReadFile(filepath.Join("posts", fileName))
if err != nil {
http.Error(w, "Post not found", http.StatusNotFound)
return
}
var buf bytes.Buffer
contentStr := string(contentBytes)
// Parse YAML front matter if present
var author, date string
if strings.HasPrefix(contentStr, "---") {
parts := strings.SplitN(contentStr, "\n", -1)
end := -1
for i := 1; i < len(parts); i++ {
if strings.TrimSpace(parts[i]) == "---" {
end = i
break
}
}
if end != -1 {
for _, l := range parts[1:end] {
kv := strings.SplitN(l, ":", 2)
if len(kv) != 2 {
continue
}
key := strings.TrimSpace(kv[0])
val := strings.Trim(strings.TrimSpace(kv[1]), "\"")
switch strings.ToLower(key) {
case "author":
author = val
case "date":
date = val
case "title":
// optional: override title
}
}
contentStr = strings.Join(parts[end+1:], "\n")
}
}
if err := md.Convert([]byte(contentStr), &buf); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Extract date and title from filename (format: YYYY-MM-DD-title.md)
baseName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
parts := strings.SplitN(baseName, "-", 4)
if len(parts) != 4 {
http.Error(w, "Invalid post filename format", http.StatusInternalServerError)
return
}
date = parts[0] + "-" + parts[1] + "-" + parts[2]
caser := cases.Title(language.English)
title := caser.String(strings.ReplaceAll(parts[3], "-", " "))
post := Post{
Title: title,
Date: date,
Author: author,
Content: template.HTML(buf.String()),
FileName: fileName,
}
tmpl := template.Must(template.New("base").Parse(baseTemplate))
tmpl = template.Must(tmpl.Parse(postTemplate))
data := PageData{
PageTitle: "Academic Blog",
Title: post.Title,
Date: post.Date,
Author: post.Author,
Body: post.Content,
}
if err := tmpl.Execute(w, data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func listPosts() ([]Post, error) {
files, err := os.ReadDir("posts")
if err != nil {
return nil, err
}
var posts []Post
for _, f := range files {
if filepath.Ext(f.Name()) == ".md" {
baseName := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))
parts := strings.SplitN(baseName, "-", 4)
if len(parts) != 4 {
log.Printf("Skipping file with invalid format: %s", f.Name())
continue
}
date := parts[0] + "-" + parts[1] + "-" + parts[2]
caser := cases.Title(language.English)
title := caser.String(strings.ReplaceAll(parts[3], "-", " "))
posts = append(posts, Post{
Title: title,
Date: date,
FileName: f.Name(),
})
}
}
return posts, nil
}
const baseTemplate = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{.Title}}</title>
<style>
{{template "css"}}
</style>
<script>
MathJax = {
tex: {
inlineMath: [['\\(', '\\)']],
displayMath: [['\\[', '\\]']],
processEscapes: true,
},
options: {
skipHtmlTags: ['script', 'noscript', 'style', 'textarea', 'pre']
}
};
</script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
</head>
<body>
<div class="container">
<header>
<h1><a href="/">{{.PageTitle}}</a></h1>
</header>
<main>
{{template "content" .}}
</main>
<footer>
<hr>
<p>Academic Blog · Built with Go and ❤️</p>
</footer>
</div>
</body>
</html>`
const indexTemplate = `
{{define "css"}}` + css + `{{end}}
{{define "content"}}
<h2>About</h2>
{{.About}}
<h2>Latest Posts</h2>
<ul class="post-list">
{{range .Posts}}
<li>
<a href="/post/{{.FileName}}">{{.Title}}</a>
<time datetime="{{.Date}}">{{.Date}}</time>
</li>
{{end}}
</ul>
{{end}}`
const postTemplate = `{{define "css"}}` + css + `{{end}}
{{define "content"}}
<article>
<header>
<p class="meta"><time datetime="{{.Date}}">{{.Date}}</time> · <span class="author">{{.Author}}</span></p>
</header>
<div class="content">
{{.Body}}
</div>
</article>
<div class="back-link">
<a href="/">← Back to all posts</a>
</div>
{{end}}`
const css = `
body {
font-family: "Computer Modern Serif", serif;
line-height: 1.6;
margin: 0;
padding: 0;
background-color: white;
color: black;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 2rem;
}
header {
text-align: center;
margin-bottom: 2rem;
}
h1, h2, h3 {
font-weight: normal;
}
.post-list {
list-style: none;
padding: 0;
}
.post-list li {
margin-bottom: 1rem;
}
.post-list a {
text-decoration: none;
color: black;
}
.post-list time {
color: #666;
font-size: 0.9em;
margin-left: 1rem;
}
article header h1 {
font-size: 2rem;
margin-bottom: 0.5rem;
}
.content {
margin-top: 2rem;
}
.back-link {
margin-top: 2rem;
}
pre {
background-color: #f5f5f5;
padding: 1rem;
overflow-x: auto;
font-size: 0.9em;
}
code {
font-family: "Computer Modern Typewriter", monospace;
font-size: 0.9em;
}
hr {
border: 0;
border-top: 1px solid #ccc;
margin: 2rem 0;
}
footer {
text-align: center;
margin-top: 4rem;
color: #666;
}
.MathJax {
font-size: 1.1em;
}
.math {
text-align: center;
margin: 1.5em 0;
}
/* Add some spacing around equations */
.mjx-chtml {
padding: 10px 0;
}`