package archetype import ( "errors" "fmt" "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) err = self.readCfg() if err != nil { fmt.Printf(err.Error()) panic("config.h is malformed!") } } func (self *EurekaAdapter) Name() string { return "eureka" } func (self *EurekaAdapter) EditableSlugs() bool { return false } func (self *EurekaAdapter) BuildOptions() []BuildOption { return []BuildOption{ BuildOption{ Name: "twtxt", Type: "string", }, BuildOption{ Name: "remove newest twtxt", Type: "bool", }, BuildOption{ Name: "clear thumbnail cache", Type: "bool", }, } } 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 { if strings.Contains(filename, "../") || strings.Contains(filename, "..\\") { return Page{ Error: "You cannot escape!", } } fullPath := filepath.Join(self.Root, "inc", filename) f, err := os.ReadFile(fullPath) if err != nil { return Page{ Error: err.Error(), } } if !strings.HasSuffix(filename, ".htm") { return Page{ Error: "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: strings.ReplaceAll(content, "\r", ""), Edited: fileInfo.ModTime(), } } 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 `// shorthand for linking other pages {page name} // shorthand for page transclusion {/page name} // shorthand for arbitary link {*destination url|text} // shorthand for an image you can click to see the full sized version {:anchor-id|image url|alt text} // shorthand for an image with arbitrary link destination {?anchor-id|destination url|image url|alt text} // shorthand for an audio player {_/path/to/media} // shorthand for paragraphs, can embed other markup inside it {¶graph text {with a link} {@and some bold text}} // shorthand for ordered lists, can embed other markup inside it {# {-item one} {-item two} {-item three} } // shorthand for unordered lists, can embed other markup inside it {, {-item one} {-item two} {-item three} } // shorthand for bold {@bold text} // shorthand for italic {~italic text} // shorthand for code {` + "`" + `short code} // shorthand for pre {$longer code} // shorthand for quote {'short quote} // shorthand for blockquote {>longer quote} // shorthand for strikethrough {\crossed-out text} // shorthand for level 3 heading {!heading text} // shorthand for level 4 heading {.heading text} // shorthand for publish date (renders as