~adnano/kiln

ref: ec0209f928227f65493c4488af36e204f0fbf44b kiln/main.go -rw-r--r-- 3.0 KiB
ec0209f9Adnan Maolood page: Fix output path for index pages 7 months ago
                                                                                
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
package main

import (
	"embed"
	"flag"
	"fmt"
	"io"
	"io/fs"
	"log"
	"os"
	"path"
	"path/filepath"
	"strings"
)

var (
	Version string
)

func main() {
	if len(os.Args) >= 2 {
		switch os.Args[1] {
		case "build":
			build()
			return
		case "new":
			if len(os.Args) < 3 {
				fmt.Println("usage: kiln new <path>")
				os.Exit(1)
			}
			newSite(os.Args[2])
			return
		case "version":
			fmt.Println("kiln", Version)
			return
		}
	}

	fmt.Println("usage: kiln <build | new | version> args...")
	os.Exit(1)
}

func build() {
	var (
		config string
	)

	flags := flag.NewFlagSet("kiln build", flag.ExitOnError)
	flags.StringVar(&config, "c", "config.toml", "the configuration file to use")
	flags.Parse(os.Args[2:])

	site, err := LoadSite(config)
	if err != nil {
		log.Fatalf("ERROR: %v", err)
	}

	if err := site.run(); err != nil {
		log.Fatalf("ERROR: %v", err)
	}
}

func (site *Site) run() error {
	for _, task := range site.Tasks {
		err := site.runTask(task)
		if err != nil {
			return err
		}
	}
	return nil
}

func (s *Site) runTask(task *Task) error {
	// Read content
	s.root = &Page{Permalink: "/", FilePath: ""}
	if err := s.root.read("content", task, s); err != nil {
		return err
	}
	s.root.sort()
	// Process content
	if err := s.root.process(s, task); err != nil {
		return err
	}
	// Write content
	if err := s.root.write(task.OutputDir, task); err != nil {
		return err
	}
	// Copy static files
	if task.StaticDir != "" {
		err := copyAll(task.StaticDir, task.OutputDir)
		if err != nil {
			if os.IsNotExist(err) {
				log.Printf("static_dir '%s' does not exist\n", task.StaticDir)
				return nil
			}
			return err
		}
	}
	return nil
}

func copyAll(srcDir, dstDir string) error {
	return filepath.Walk(srcDir, func(path string, info fs.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() {
			// Do nothing
			return nil
		}

		src, err := os.Open(path)
		if err != nil {
			return err
		}
		defer src.Close()

		dstPath := filepath.Join(dstDir, strings.TrimPrefix(path, srcDir))
		os.MkdirAll(filepath.Dir(dstPath), 0755)
		dst, err := os.Create(dstPath)
		if err != nil {
			return err
		}
		defer dst.Close()

		if _, err := io.Copy(dst, src); err != nil {
			return err
		}
		return nil
	})
}

//go:embed templates/_default config.toml
var builtin embed.FS

func newSite(name string) {
	name = path.Clean(name)
	os.Mkdir(name, 0755)
	os.Mkdir(path.Join(name, "content"), 0755)
	os.Mkdir(path.Join(name, "templates"), 0755)
	os.Mkdir(path.Join(name, "templates/_default"), 0755)
	os.Mkdir(path.Join(name, "static"), 0755)
	os.Mkdir(path.Join(name, "public"), 0755)

	config, _ := builtin.ReadFile("config.toml")
	os.WriteFile(path.Join(name, "config.toml"), config, 0644)

	index := []byte("---\ntitle: Hello, world!\n---\n")
	os.WriteFile(path.Join(name, "content/_index.gmi"), index, 0644)

	templates := []string{"atom.xml", "index.gmi", "page.gmi"}
	for _, template := range templates {
		b, _ := builtin.ReadFile(path.Join("templates/_default", template))
		os.WriteFile(path.Join(name, "templates/_default", template), b, 0644)
	}
}