nirvash/archetype/eureka.go

148 lines
3.4 KiB
Go
Raw Normal View History

package archetype
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type EurekaAdapter struct {
Root string
Config map[string]interface{}
}
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
// TODO: read config.h and build self.Config
}
func (self *EurekaAdapter) Name() string {
return "eureka"
}
2022-06-05 04:34:20 +00:00
func (self *EurekaAdapter) EditableSlugs() bool {
return false
}
2022-06-05 15:56:52 +00:00
func (self *EurekaAdapter) BuildOptions() []string {
return []string{"twtxt"}
2022-06-05 15:56:52 +00:00
}
func (self *EurekaAdapter) GetConfig(key string) (interface{}, error) {
return nil, nil
}
func (self *EurekaAdapter) SetConfig(key string, value interface{}) error {
return nil
}
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,
Slug: filename,
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!"
}
2022-06-05 04:34:20 +00:00
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
}
2022-06-05 04:34:20 +00:00
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 {
// TODO: delete old html as well
os.Remove(filepath.Join(self.Root, "inc", oldSlug))
}
f.WriteString(content)
return nil
}
2022-06-05 04:34:20 +00:00
func (self *EurekaAdapter) DeletePage(slug string) error {
// TODO: delete old html as well
return os.Remove(filepath.Join(self.Root, "inc", slug))
}
2022-06-05 15:56:52 +00:00
func (self *EurekaAdapter) Build(buildOptions map[string]string) (bool, string) {
// TODO: shell out to build.sh with buildOptions, record exit status and output
2022-06-05 04:34:20 +00:00
return true, "Build successful"
}