all repos — nirvash @ 37d184b3e228e0b43aa54fb45275131f46540229

modular CMS using the quartzgun library

archetype/eureka.go (raw)

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
package archetype

import (
	"errors"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"
)

type EurekaAdapter struct {
	Root   string
	Config map[ConfigOption]string
}

func (self *EurekaAdapter) Init(cfg *Config) {
	fileInfo, err := os.Stat(cfg.Root)
	if os.IsNotExist(err) {
		panic("SSG content root does not exist! Ensure your configs are correct or create it!")
	} else if !fileInfo.IsDir() {
		panic("SSG content root is not a directory!")
	}

	self.Root = cfg.Root
	self.Config = make(map[ConfigOption]string)
	// TODO: read config.h and build self.Config
	err = self.readCfg()
	if err != nil {
		panic("config.h is malformed!")
	}
}

func (self *EurekaAdapter) Name() string {
	return "eureka"
}

func (self *EurekaAdapter) EditableSlugs() bool {
	return false
}

func (self *EurekaAdapter) BuildOptions() []string {
	return []string{"twtxt"}
}

func (self *EurekaAdapter) GetConfig() map[ConfigOption]string {
	return self.Config
}

func (self *EurekaAdapter) SetConfig(cfg map[ConfigOption]string) error {
	self.Config = cfg
	return self.writeCfg()
}

func (self *EurekaAdapter) ListPages() map[string]string {
	files, err := ioutil.ReadDir(
		filepath.Join(self.Root, "inc"))

	if err != nil {
		panic(err.Error())
	}

	pages := map[string]string{}
	for _, file := range files {
		filename := file.Name()
		if strings.HasSuffix(filename, ".htm") {
			pages[filename] = strings.Replace(
				strings.TrimSuffix(filename, ".htm"), "_", " ", -1)
		}
	}
	return pages
}

func (self *EurekaAdapter) GetPage(filename string) (Page, error) {
	fullPath := filepath.Join(self.Root, "inc", filename)
	f, err := os.ReadFile(fullPath)

	if err != nil {
		return Page{}, err
	}

	if !strings.HasSuffix(filename, ".htm") {
		return Page{}, errors.New("Page file extension is not '.htm'")
	}

	title := strings.Replace(
		strings.TrimSuffix(filename, ".htm"), "_", " ", -1)
	fileInfo, _ := os.Stat(fullPath)
	content := string(f[:])

	return Page{
		Title:   title,
		Content: content,
		Edited:  fileInfo.ModTime(),
	}, nil
}

func (self *EurekaAdapter) FormatPage(raw string) string {
	// TODO: implement Eureka formatter to show preview
	return raw
}

func (self *EurekaAdapter) FormattingHelp() string {
	// TODO: show Eureka formatting guide
	return "help!"
}

func (self *EurekaAdapter) CreatePage(slug, title, content string) error {
	// eureka creates titles from slugs, so we transform the title into the slug
	slug = strings.ReplaceAll(title, " ", "_") + ".htm"
	path := filepath.Join(self.Root, "inc", slug)

	_, err := os.Stat(path)
	if err == nil || !os.IsNotExist(err) {
		return errors.New("File already exists")
	}
	f, err := os.Create(path)
	if err != nil {
		return err
	}
	defer f.Close()
	f.WriteString(content)
	return nil
}

func (self *EurekaAdapter) SavePage(oldSlug, newSlug, title, content string) error {
	// eureka creates titles from slugs, so we transform the title into the slug
	newSlug = strings.ReplaceAll(title, " ", "_") + ".htm"
	f, err := os.Create(filepath.Join(self.Root, "inc", newSlug))
	if err != nil {
		return err
	}
	defer f.Close()

	if oldSlug != newSlug {
		siteRoot := self.Config[ConfigOption{
			Name: "SITEROOT",
			Type: "string",
		}]
		htmlFile := filepath.Join(self.Root, siteRoot, oldSlug+"l")
		_, err := os.Stat(htmlFile)
		if !os.IsNotExist(err) {
			os.Remove(htmlFile)
		}
		os.Remove(filepath.Join(self.Root, "inc", oldSlug))
	}

	f.WriteString(content)
	return nil
}

func (self *EurekaAdapter) DeletePage(slug string) error {
	siteRoot := self.Config[ConfigOption{
		Name: "SITEROOT",
		Type: "string",
	}]
	htmlFile := filepath.Join(self.Root, siteRoot, slug+"l")
	_, err := os.Stat(htmlFile)
	if !os.IsNotExist(err) {
		os.Remove(htmlFile)
	}
	return os.Remove(filepath.Join(self.Root, "inc", slug))
}

func (self *EurekaAdapter) Build(buildOptions map[string][]string) BuildStatus {

	twtxt := buildOptions["twtxt"][0]
	cmdArgs := ""
	if twtxt != "" {
		cmdArgs += "-t " + twtxt
	}

	cmd := exec.Command("./build.sh", cmdArgs)
	cmd.Dir = self.Root
	out, err := cmd.CombinedOutput()

	return BuildStatus{
		Success: err == nil,
		Message: string(out),
	}
}

func (self *EurekaAdapter) readCfg() error {
	configPath := filepath.Join(self.Root, "config.h")
	_, err := os.Stat(filepath.Join(self.Root, "config.h"))
	if os.IsNotExist(err) {
		configPath = filepath.Join(self.Root, "config.def.h")
	}
	f, err := os.ReadFile(configPath)

	if err != nil {
		return err
	}

	fileData := string(f[:])

	macros := strings.Split(fileData, "#define ")[1:]
	for _, macro := range macros {
		tokens := strings.Split(strings.TrimSpace(macro), " ")
		k := tokens[0]
		v := strings.TrimSpace(strings.Join(tokens[1:], " "))

		if strings.Contains(v, "\"") {
			if strings.Contains(v, "\\\r\n") || strings.Contains(v, "\\\n") {
				// process multiline string
				lines := strings.Split(v, "\n")
				cleanedString := ""
				for _, l := range lines {
					l = strings.TrimSuffix(l, "\r")
					l = strings.TrimSuffix(l, "\\")
					l = strings.TrimSpace(l)
					l = strings.TrimPrefix(l, "\"")
					l = strings.TrimSuffix(l, "\"")
					l = strings.ReplaceAll(l, "\\\"", "\"")
					l = strings.ReplaceAll(l, "\\n", "\n")
					cleanedString += l
				}
				self.Config[ConfigOption{
					Name: k,
					Type: "multilinestring",
				}] = cleanedString
			} else {
				cleanedString := strings.TrimSuffix(strings.TrimPrefix(v, "\""), "\"")
				cleanedString = strings.ReplaceAll(cleanedString, "\\n", "\n")
				cleanedString = strings.ReplaceAll(cleanedString, "\r", "")
				cleanedString = strings.ReplaceAll(cleanedString, "\\\"", "\"")
				self.Config[ConfigOption{
					Name: k,
					Type: "string",
				}] = cleanedString
			}
		} else if strings.Contains(v, ".") {
			_, err := strconv.ParseFloat(v, 64)
			if err != nil {
				return err
			}
			self.Config[ConfigOption{
				Name: k,
				Type: "float",
			}] = v
		} else {
			_, err := strconv.ParseInt(v, 10, 64)
			if err != nil {
				return err
			}
			self.Config[ConfigOption{
				Name: k,
				Type: "int",
			}] = v
		}
	}
	return nil
}

func (self *EurekaAdapter) writeCfg() error {
	f, err := os.Create(filepath.Join(self.Root, "config.h"))
	if err != nil {
		return err
	}

	defer f.Close()

	for k, v := range self.Config {
		switch k.Type {
		case "int":
			_, err := strconv.ParseInt(v, 10, 64)
			if err != nil {
				return err
			}
			f.WriteString("#define " + k.Name + " " + v + "\n")
		case "float":
			_, err := strconv.ParseFloat(v, 64)
			if err != nil {
				return err
			}
			f.WriteString("#define " + k.Name + " " + v + "\n")
		case "string":
			fallthrough
		case "multilinestring":
			v = strings.ReplaceAll(v, "\"", "\\\"")
			v = strings.ReplaceAll(v, "\n", "\\n\" \\\n\"")
			v = strings.ReplaceAll(v, "\r", "")
			f.WriteString("#define " + k.Name + " \"" + v + "\"\n")
		default:
			return errors.New("Unsupported config value type: " + k.Type)
		}
	}
	return nil
}