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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
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 <time class='publish-date'>) {+2022-02-22}` } 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) if strings.Contains(slug, "../") || strings.Contains(slug, "..\\") { return errors.New("You cannot escape!") } _, 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(strings.ReplaceAll(content, "\r", "")) 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" if strings.Contains(newSlug, "../") || strings.Contains(newSlug, "..\\") || strings.Contains(oldSlug, "../") || strings.Contains(oldSlug, "..\\") { return errors.New("You cannot escape!") } 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(strings.ReplaceAll(content, "\r", "")) return nil } func (self *EurekaAdapter) DeletePage(slug string) error { if strings.Contains(slug, "../") || strings.Contains(slug, "..\\") { return errors.New("You cannot escape!") } 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[BuildOption]string) BuildStatus { twtxt := buildOptions[BuildOption{ Name: "twtxt", Type: "string", }] rmNewestTwtxt := buildOptions[BuildOption{ Name: "remove newest twtxt", Type: "bool", }] clearThumbs := buildOptions[BuildOption{ Name: "clear thumbnail cache", Type: "bool", }] cmdArgs := []string{} if clearThumbs != "" { cmdArgs = append(cmdArgs, "-r") } if rmNewestTwtxt != "" { cmdArgs = append(cmdArgs, "-d") } if twtxt != "" { cmdArgs = append(cmdArgs, "-t") cmdArgs = append(cmdArgs, 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 := strings.Replace( strings.Replace( string(f[:]), "/* clang-format on */", "", -1), "/* clang-format off */", "", -1) 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.HasSuffix(k, "_HTML") { // 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 } cfgType := "int" if strings.HasPrefix(k, "IS_") { cfgType = "bool" } self.Config[ConfigOption{ Name: k, Type: cfgType, }] = 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() f.WriteString("/* clang-format off */\n"); for k, v := range self.Config { switch k.Type { case "int": case "bool": _, 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: fmt.Println("Unsupported config value type: " + k.Type) } } f.WriteString("/* clang-format on */\n"); return nil } |