-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinkssg.go
More file actions
541 lines (445 loc) · 11.6 KB
/
Copy pathinkssg.go
File metadata and controls
541 lines (445 loc) · 11.6 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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
package inkssg
import (
"bytes"
"embed"
"fmt"
"html/template"
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/yuin/goldmark"
"gopkg.in/yaml.v3"
)
//go:embed themes/*/*.html themes/*/*.css themes/*/*.js
var embeddedThemes embed.FS
var themeFS embed.FS = embeddedThemes
var hasThemes bool = true
func hasEmbeddedThemes() bool {
return hasThemes
}
type Site struct {
Dir string
Pages []Page
Output string
Theme string
PagesDir string
Config *SiteConfig
}
type Page struct {
Name string
Dir string
Content string
Title string
Description string
Theme string
ContentHTML string
}
type SiteConfig struct {
Name string `yaml:"name"`
Avatar string `yaml:"avatar"`
Bio string `yaml:"bio"`
Status string `yaml:"status"`
Install string `yaml:"install"`
DefaultTheme string `yaml:"default_theme"`
OutputDir string `yaml:"output_dir"`
PagesDir string `yaml:"pages_dir"`
Links []Link `yaml:"links"`
Projects []Project `yaml:"projects"`
Meta Meta `yaml:"meta"`
Contact Contact `yaml:"contact"`
}
type Project struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
URL string `yaml:"url"`
Image string `yaml:"image"`
Badge string `yaml:"badge"`
}
type Link struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
}
type Meta struct {
Lang string `yaml:"lang"`
Description string `yaml:"description"`
Title string `yaml:"title"`
Author string `yaml:"author"`
SiteUrl string `yaml:"siteUrl"`
}
type Contact struct {
Socials []Link `yaml:"socials"`
}
type TemplateData struct {
Site *SiteConfig
Page Page
Theme map[string]string
Content template.HTML
}
func Scaffold(dir string) error {
if dir == "" {
dir = "."
}
absDir, err := filepath.Abs(dir)
if err != nil {
return fmt.Errorf("invalid path: %w", err)
}
pagesDir := filepath.Join(absDir, "pages")
if _, err := os.Stat(pagesDir); err == nil {
return fmt.Errorf("pages/ already exists at %s — refusing to overwrite", absDir)
}
indexDir := filepath.Join(pagesDir, "index")
if err := os.MkdirAll(indexDir, 0755); err != nil {
return fmt.Errorf("creating pages/index: %w", err)
}
indexContent := `---
title: Hello
description: My new inkssg site
---
# Hello
Edit ` + "`pages/index/content.md`" + ` and run ` + "`inkssg build`" + `.
`
if err := os.WriteFile(filepath.Join(indexDir, "content.md"), []byte(indexContent), 0644); err != nil {
return fmt.Errorf("writing content.md: %w", err)
}
configPath := filepath.Join(absDir, "ink.yaml")
if _, err := os.Stat(configPath); os.IsNotExist(err) {
sample := `name: My Site
default_theme: minimal
output_dir: public
meta:
lang: en
title: My Site
description: A small site built with inkssg
`
if err := os.WriteFile(configPath, []byte(sample), 0644); err != nil {
return fmt.Errorf("writing ink.yaml: %w", err)
}
}
fmt.Printf("scaffolded site at %s\n", absDir)
fmt.Println("next: inkssg build")
return nil
}
func Build(paths ...string) error {
dir := "."
if len(paths) > 0 {
dir = paths[0]
}
absDir, err := filepath.Abs(dir)
if err != nil {
return fmt.Errorf("invalid path: %w", err)
}
site, err := NewSite(absDir)
if err != nil {
return err
}
return site.Build()
}
func NewSite(dir string) (*Site, error) {
site := &Site{
Dir: dir,
Pages: []Page{},
Output: "public",
Theme: "minimal",
PagesDir: "pages",
Config: &SiteConfig{},
}
if err := site.loadConfig(); err != nil {
// config is optional
}
if err := site.detect(); err != nil {
return nil, err
}
return site, nil
}
func (s *Site) loadConfig() error {
configPath := filepath.Join(s.Dir, "ink.yaml")
data, err := os.ReadFile(configPath)
if err != nil {
return nil
}
var config SiteConfig
if err := yaml.Unmarshal(data, &config); err != nil {
return fmt.Errorf("invalid ink.yaml: %w", err)
}
if config.OutputDir != "" {
s.Output = config.OutputDir
}
if config.DefaultTheme != "" {
s.Theme = config.DefaultTheme
}
if config.PagesDir != "" {
s.PagesDir = config.PagesDir
}
s.Config = &config
return nil
}
func (s *Site) detect() error {
pagesDir := filepath.Join(s.Dir, s.PagesDir)
if _, err := os.Stat(pagesDir); os.IsNotExist(err) {
return fmt.Errorf("no %s/ folder found. Run 'inkssg new .' to scaffold a site.", s.PagesDir)
}
entries, err := os.ReadDir(pagesDir)
if err != nil {
return fmt.Errorf("cannot read %s/: %w", s.PagesDir, err)
}
for _, entry := range entries {
if !entry.IsDir() {
continue
}
pageDir := filepath.Join(pagesDir, entry.Name())
contentPath := filepath.Join(pageDir, "content.md")
contentType := "md"
if _, err := os.Stat(contentPath); os.IsNotExist(err) {
contentPath = filepath.Join(pageDir, "content.html")
contentType = "html"
if _, err := os.Stat(contentPath); os.IsNotExist(err) {
continue
}
}
page := Page{
Name: entry.Name(),
Dir: pageDir,
Content: contentPath,
}
if err := page.parseFrontmatter(contentType); err != nil {
return fmt.Errorf("%s: %w", entry.Name(), err)
}
if page.Theme == "" {
page.Theme = s.Theme
}
s.Pages = append(s.Pages, page)
}
if len(s.Pages) == 0 {
return fmt.Errorf("no pages found. Add a pages/<name>/content.md or content.html file.")
}
return nil
}
func (p *Page) parseFrontmatter(contentType string) error {
data, err := os.ReadFile(p.Content)
if err != nil {
return fmt.Errorf("cannot read content: %w", err)
}
frontmatter := &struct {
Title string `yaml:"title"`
Description string `yaml:"description"`
Theme string `yaml:"theme"`
}{}
str := string(data)
if strings.HasPrefix(str, "---") {
str = strings.TrimPrefix(str, "---\n")
endIdx := strings.Index(str, "\n---")
if endIdx > 0 {
yamlData := str[:endIdx]
if err := yaml.Unmarshal([]byte(yamlData), frontmatter); err != nil {
return fmt.Errorf("invalid frontmatter: %w", err)
}
content := str[endIdx+4:]
p.ContentHTML = p.renderMarkdown([]byte(content), contentType)
} else {
p.ContentHTML = p.renderMarkdown(data, contentType)
}
} else {
p.ContentHTML = p.renderMarkdown(data, contentType)
}
p.Title = frontmatter.Title
p.Description = frontmatter.Description
p.Theme = frontmatter.Theme
if p.Title == "" {
p.Title = p.Name
}
return nil
}
func (p *Page) renderMarkdown(data []byte, contentType string) string {
if contentType == "html" {
return string(data)
}
var buf bytes.Buffer
md := goldmark.New()
if err := md.Convert(data, &buf); err != nil {
return ""
}
return buf.String()
}
func (s *Site) validateOutput() error {
if strings.TrimSpace(s.Output) == "" {
return fmt.Errorf("output_dir cannot be empty")
}
absDir, err := filepath.Abs(s.Dir)
if err != nil {
return fmt.Errorf("invalid project dir: %w", err)
}
absOut, err := filepath.Abs(filepath.Join(s.Dir, s.Output))
if err != nil {
return fmt.Errorf("invalid output_dir: %w", err)
}
if absOut == absDir {
return fmt.Errorf("output_dir cannot be the project root (would delete sources on rebuild)")
}
rel, err := filepath.Rel(absDir, absOut)
if err != nil || strings.HasPrefix(rel, "..") {
return fmt.Errorf("output_dir must be inside the project directory, got %q", s.Output)
}
return nil
}
func (s *Site) Build() error {
start := time.Now()
if err := s.validateOutput(); err != nil {
return err
}
if err := os.RemoveAll(filepath.Join(s.Dir, s.Output)); err != nil {
return fmt.Errorf("cannot clean output dir: %w", err)
}
if err := os.MkdirAll(filepath.Join(s.Dir, s.Output), 0755); err != nil {
return fmt.Errorf("cannot create output dir: %w", err)
}
themeDir, err := s.resolveTheme()
if err != nil {
return err
}
if err := s.copyTheme(themeDir); err != nil {
return fmt.Errorf("copying theme: %w", err)
}
if err := s.copyAssets(); err != nil {
return fmt.Errorf("copying assets: %w", err)
}
failed := 0
for _, page := range s.Pages {
if err := s.buildPage(page, themeDir); err != nil {
fmt.Fprintf(os.Stderr, "✗ %s: %v\n", page.Name, err)
failed++
} else {
fmt.Printf("✓ %s → %s.html\n", page.Name, page.Name)
}
}
total := len(s.Pages)
duration := time.Since(start)
if failed > 0 {
fmt.Printf("%d page failed (of %d) in %s\n", failed, total, duration)
return fmt.Errorf("build failed")
}
fmt.Printf("built %d pages in %s\n", total, duration)
return nil
}
func (s *Site) resolveTheme() (string, error) {
localTheme := filepath.Join(s.Dir, "themes", s.Theme)
if _, err := os.Stat(localTheme); err == nil {
return localTheme, nil
}
if hasEmbeddedThemes() {
return "", nil // use embedded
}
return "", fmt.Errorf("theme %q not found (local or built-in)", s.Theme)
}
func (s *Site) copyTheme(themeDir string) error {
outputTheme := filepath.Join(s.Dir, s.Output, "themes", s.Theme)
if err := os.MkdirAll(outputTheme, 0755); err != nil {
return err
}
if themeDir == "" && hasEmbeddedThemes() {
files := []string{"styles.css", "script.js"}
for _, f := range files {
data, err := themeFS.ReadFile("themes/" + s.Theme + "/" + f)
if err == nil {
os.WriteFile(filepath.Join(outputTheme, f), data, 0644)
}
}
return nil
}
entries, _ := os.ReadDir(themeDir)
for _, entry := range entries {
if entry.IsDir() {
continue
}
if entry.Name() == "layout.html" {
continue
}
src := filepath.Join(themeDir, entry.Name())
dst := filepath.Join(outputTheme, entry.Name())
data, _ := os.ReadFile(src)
os.WriteFile(dst, data, 0644)
}
return nil
}
func (s *Site) copyAssets() error {
assetsDir := filepath.Join(s.Dir, "assets")
if _, err := os.Stat(assetsDir); os.IsNotExist(err) {
return nil
}
outputAssets := filepath.Join(s.Dir, s.Output, "assets")
if err := os.MkdirAll(outputAssets, 0755); err != nil {
return err
}
return copyDir(assetsDir, outputAssets)
}
func copyDir(src, dst string) error {
entries, err := os.ReadDir(src)
if err != nil {
return err
}
for _, entry := range entries {
srcPath := filepath.Join(src, entry.Name())
dstPath := filepath.Join(dst, entry.Name())
if entry.IsDir() {
os.MkdirAll(dstPath, 0755)
copyDir(srcPath, dstPath)
} else {
data, _ := os.ReadFile(srcPath)
os.WriteFile(dstPath, data, 0644)
}
}
return nil
}
func (s *Site) buildPage(page Page, themeDir string) error {
var layout string
themeName := page.Theme
if themeName == "" {
themeName = s.Theme
}
if themeDir == "" && hasEmbeddedThemes() {
data, err := themeFS.ReadFile("themes/" + themeName + "/layout.html")
if err != nil {
return fmt.Errorf("cannot read embedded layout for theme %q: %w", themeName, err)
}
layout = string(data)
} else if themeDir != "" {
data, err := os.ReadFile(filepath.Join(themeDir, "layout.html"))
if err != nil {
return fmt.Errorf("cannot read layout: %w", err)
}
layout = string(data)
} else {
return fmt.Errorf("no layout found for theme %q", themeName)
}
data := TemplateData{
Site: s.Config,
Page: page,
Theme: nil,
Content: template.HTML(page.ContentHTML),
}
tmpl, err := template.New("layout").Parse(layout)
if err != nil {
return fmt.Errorf("parsing template: %w", err)
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return fmt.Errorf("executing template: %w", err)
}
outputPath := filepath.Join(s.Dir, s.Output, page.Name+".html")
return os.WriteFile(outputPath, buf.Bytes(), 0644)
}
func CopyFile(src, dst string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()
destFile, err := os.Create(dst)
if err != nil {
return err
}
defer destFile.Close()
_, err = io.Copy(destFile, sourceFile)
return err
}