Mercurial > yakumo_izuru > aya
comparison cmd/aya/main.go @ 68:4b79810863f6 draft
Ready to release 0.6.0
Signed-off-by: Izuru Yakumo <yakumo.izuru@chaotic.ninja>
author | yakumo.izuru |
---|---|
date | Wed, 13 Sep 2023 10:49:50 +0000 |
parents | 4edfa07d5fe0 |
children | 03019bbf2e0e |
comparison
equal
deleted
inserted
replaced
67:4edfa07d5fe0 | 68:4b79810863f6 |
---|---|
1 // $TheSupernovaDuo: cmd/aya/main.go,v 0.5.x 2023/5/8 18:6:18 yakumo_izuru Exp $ | 1 // $TheSupernovaDuo: cmd/aya/main.go,v 0.6.0 2023/9/13 07:50:00 yakumo_izuru Exp $ |
2 package main | 2 package main |
3 | 3 |
4 import ( | 4 import ( |
5 "bytes" | 5 "bytes" |
6 "fmt" | 6 "fmt" |
11 "path/filepath" | 11 "path/filepath" |
12 "strings" | 12 "strings" |
13 "text/template" | 13 "text/template" |
14 "time" | 14 "time" |
15 | 15 |
16 "github.com/eknkc/amber" | |
16 "github.com/russross/blackfriday/v2" | 17 "github.com/russross/blackfriday/v2" |
18 log "github.com/sirupsen/logrus" | |
19 "github.com/yosssi/gcss" | |
17 "gopkg.in/yaml.v2" | 20 "gopkg.in/yaml.v2" |
18 "marisa.chaotic.ninja/aya" | 21 "marisa.chaotic.ninja/aya" |
19 log "github.com/sirupsen/logrus" | |
20 "github.com/eknkc/amber" | |
21 "github.com/yosssi/gcss" | |
22 ) | 22 ) |
23 | 23 |
24 const ( | 24 const ( |
25 AYADIR = ".aya" | 25 AYADIR = ".aya" |
26 PUBDIR = ".pub" | 26 PUBDIR = ".pub" |
27 ) | 27 ) |
28 | 28 |
29 type Vars map[string]string | 29 type Vars map[string]string |
30 | 30 |
170 fmt.Println(err) | 170 fmt.Println(err) |
171 } | 171 } |
172 } | 172 } |
173 } | 173 } |
174 } | 174 } |
175 | 175 |
176 } | 176 } |
177 | 177 |
178 // Renders markdown with the given layout into html expanding all the macros | 178 // Renders markdown with the given layout into html expanding all the macros |
179 func buildMarkdown(path string, w io.Writer, vars Vars) error { | 179 func buildMarkdown(path string, w io.Writer, vars Vars) error { |
180 v, body, err := getVars(path, vars) | 180 v, body, err := getVars(path, vars) |
184 content, err := render(body, v) | 184 content, err := render(body, v) |
185 if err != nil { | 185 if err != nil { |
186 return err | 186 return err |
187 } | 187 } |
188 v["content"] = string(blackfriday.Run([]byte(content), | 188 v["content"] = string(blackfriday.Run([]byte(content), |
189 blackfriday.WithExtensions(blackfriday.CommonExtensions|blackfriday.AutoHeadingIDs), | 189 blackfriday.WithExtensions(blackfriday.CommonExtensions|blackfriday.AutoHeadingIDs|blackfriday.Strikethrough|blackfriday.Footnotes), |
190 )) | 190 )) |
191 if w == nil { | 191 if w == nil { |
192 out, err := os.Create(filepath.Join(PUBDIR, renameExt(path, "", ".html"))) | 192 out, err := os.Create(filepath.Join(PUBDIR, renameExt(path, "", ".html"))) |
193 if err != nil { | 193 if err != nil { |
194 return err | 194 return err |
258 w = f | 258 w = f |
259 } | 259 } |
260 _, err = io.WriteString(w, body) | 260 _, err = io.WriteString(w, body) |
261 return err | 261 return err |
262 } | 262 } |
263 | |
263 // Compiles .gcss into .css | 264 // Compiles .gcss into .css |
264 func buildGCSS(path string, w io.Writer) error { | 265 func buildGCSS(path string, w io.Writer) error { |
265 f, err := os.Open(path) | 266 f, err := os.Open(path) |
266 if err != nil { | 267 if err != nil { |
267 return err | 268 return err |
297 } | 298 } |
298 } | 299 } |
299 _, err = io.Copy(w, in) | 300 _, err = io.Copy(w, in) |
300 return err | 301 return err |
301 } | 302 } |
303 | |
302 // This function passes the files to build to their corresponding functions | 304 // This function passes the files to build to their corresponding functions |
303 func build(path string, w io.Writer, vars Vars) error { | 305 func build(path string, w io.Writer, vars Vars) error { |
304 ext := filepath.Ext(path) | 306 ext := filepath.Ext(path) |
305 if ext == ".md" || ext == ".mkd" || ext == ".markdown" { | 307 if ext == ".md" || ext == ".mkd" || ext == ".markdown" { |
306 return buildMarkdown(path, w, vars) | 308 return buildMarkdown(path, w, vars) |
357 } | 359 } |
358 lastModified = time.Now() | 360 lastModified = time.Now() |
359 time.Sleep(1 * time.Second) | 361 time.Sleep(1 * time.Second) |
360 } | 362 } |
361 } | 363 } |
362 // Serve the public directory over HTTP | 364 |
363 func servePubDir() { | 365 // Serve the public directory over HTTP and watch for changes |
364 rootdir := http.Dir(PUBDIR) | 366 func serve() { |
365 http.Handle("/", http.FileServer(rootdir)) | 367 http.Handle("/", http.FileServer(http.Dir(PUBDIR))) |
366 log.Printf("Serving %v at port 8000, see http://localhost:8000", rootdir) | |
367 log.Fatal(http.ListenAndServe(":8000", nil)) | 368 log.Fatal(http.ListenAndServe(":8000", nil)) |
368 } | 369 } |
370 | |
369 // Initialize the environment | 371 // Initialize the environment |
370 func init() { | 372 func init() { |
371 // prepend .aya to $PATH, so plugins will be found before OS commands | 373 // prepend .aya to $PATH, so plugins will be found before OS commands |
372 p := os.Getenv("PATH") | 374 p := os.Getenv("PATH") |
373 p = os.Getenv("PWD") + "/" + AYADIR + ":" + p | 375 p = os.Getenv("PWD") + "/" + AYADIR + ":" + p |
374 os.Setenv("PATH", p) | 376 os.Setenv("PATH", p) |
375 } | 377 } |
378 | |
376 // Print usage notes | 379 // Print usage notes |
377 func printUsage() { | 380 func printUsage() { |
378 fmt.Printf("%v <command> [args]\n", os.Args[0]) | 381 fmt.Printf("%v <command> [args]\n", os.Args[0]) |
379 fmt.Printf("\n") | 382 fmt.Printf("\n") |
380 fmt.Printf("Where <command> is:\n") | 383 fmt.Printf("Where <command> is:\n") |
381 fmt.Printf("\tbuild\tGenerate site\n") | 384 fmt.Printf("\tbuild\tGenerate site\n") |
382 fmt.Printf("\tclean\tRemoves the generated site directory\n") | 385 fmt.Printf("\tclean\tRemoves the generated site directory\n") |
383 fmt.Printf("\tserve\tServe %v over HTTP\n", PUBDIR) | |
384 fmt.Printf("\tvar\tQuery variable(s) from a markdown file\n") | 386 fmt.Printf("\tvar\tQuery variable(s) from a markdown file\n") |
385 fmt.Printf("\tversion\tPrint program version and exit\n") | 387 fmt.Printf("\tversion\tPrint program version and exit\n") |
386 fmt.Printf("\twatch\t(Re)generate site while looking for changes\n") | 388 fmt.Printf("\twatch\t(Re)generate site while looking for changes (it also serves on HTTP)\n") |
387 fmt.Printf("\n") | 389 fmt.Printf("\n") |
388 fmt.Printf("Other commands may be dynamically added by plugins found in %v\n", AYADIR) | 390 fmt.Printf("Other commands may be dynamically added by plugins found in %v\n", AYADIR) |
389 os.Exit(0) | 391 os.Exit(0) |
390 } | 392 } |
391 | 393 |
407 log.Fatal("ERROR: too many arguments") | 409 log.Fatal("ERROR: too many arguments") |
408 } | 410 } |
409 case "clean": | 411 case "clean": |
410 log.Println("Removing generated site directory") | 412 log.Println("Removing generated site directory") |
411 os.RemoveAll(PUBDIR) | 413 os.RemoveAll(PUBDIR) |
412 case "serve": | 414 case "help": |
413 servePubDir() | 415 printUsage() |
414 case "var": | 416 case "var": |
415 if len(args) == 0 { | 417 if len(args) == 0 { |
416 log.Fatal("var: filename expected") | 418 log.Fatal("var: filename expected") |
417 } else { | 419 } else { |
418 s := "" | 420 s := "" |
434 case "version": | 436 case "version": |
435 fmt.Printf("%v\n", aya.FullVersion()) | 437 fmt.Printf("%v\n", aya.FullVersion()) |
436 os.Exit(0) | 438 os.Exit(0) |
437 case "watch": | 439 case "watch": |
438 buildAll(true) | 440 buildAll(true) |
441 serve() | |
439 default: | 442 default: |
440 if s, err := run(globals(), cmd, args...); err != nil { | 443 if s, err := run(globals(), cmd, args...); err != nil { |
441 log.Println(err) | 444 log.Println(err) |
442 } else { | 445 } else { |
443 log.Println(s) | 446 log.Println(s) |