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 |
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" } func (self *EurekaAdapter) EditableSlugs() bool { return false } func (self *EurekaAdapter) BuildOptions() []string { return []string{"twtxt"} } 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!" } 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 { // TODO: delete old html as well os.Remove(filepath.Join(self.Root, "inc", oldSlug)) } f.WriteString(content) return nil } func (self *EurekaAdapter) DeletePage(slug string) error { // TODO: delete old html as well return os.Remove(filepath.Join(self.Root, "inc", slug)) } func (self *EurekaAdapter) Build(buildOptions map[string]string) (bool, string) { // TODO: shell out to build.sh with buildOptions, record exit status and output return true, "Build successful" } |