Created
September 7, 2019 13:23
-
-
Save jackcallister/40f25069611247ca64d116fb7798bac3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"sort" | |
"net/http" | |
"time" | |
"github.com/radovskyb/watcher" | |
"github.com/jarsbe/leaf" | |
) | |
func WhereTypeIs(d []leaf.Document, t string) []leaf.Document { | |
var res []leaf.Document | |
for _, doc := range d { | |
if v, b := doc.Meta.StringIf("type"); b && t == v { | |
res = append(res, doc) | |
} | |
} | |
return res | |
} | |
func AtoZ(ingredients []leaf.Document) map[string][]leaf.Document { | |
dict := make(map[string][]leaf.Document) | |
sort.Slice(ingredients, func(i, j int) bool { | |
return ingredients[i].Meta.String("title") < ingredients[j].Meta.String("title") | |
}) | |
for _, i := range ingredients { | |
key := i.Meta.String("title")[0:1] | |
dict[key] = append(dict[key], i) | |
} | |
return dict | |
} | |
func main () { | |
helpers := func(c *leaf.Compiler, d leaf.Document) { | |
c.Helpers["Ingredients"] = WhereTypeIs(c.Documents, "ingredient") | |
c.Helpers["AtoZ"] = AtoZ(WhereTypeIs(c.Documents, "ingredient")) | |
} | |
site, err := leaf.Compile(helpers) | |
if err != nil { | |
panic(err) | |
} | |
w := watcher.New() | |
w.SetMaxEvents(1) | |
go func() { | |
for { | |
select { | |
case _ = <-w.Event: | |
fmt.Println("Recompiling...") | |
site.Recompile() | |
case err := <-w.Error: | |
log.Fatalln(err) | |
case <-w.Closed: | |
return | |
} | |
} | |
}() | |
if err := w.Add("./content"); err != nil { | |
log.Fatalln(err) | |
} | |
if err := w.Add("./templates"); err != nil { | |
log.Fatalln(err) | |
} | |
if err := w.Add("./client"); err != nil { | |
log.Fatalln(err) | |
} | |
for path, f := range w.WatchedFiles() { | |
fmt.Printf("Watching file: %s: %s\n", path, f.Name()) | |
} | |
go func() { | |
fmt.Println("Serving on port 8080") | |
http.ListenAndServe(":8080", http.FileServer(http.Dir(site.WriteDir))) | |
}() | |
if err := w.Start(time.Millisecond * 600); err != nil { | |
log.Fatalln(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment