From afe6425adce738df9a3b36d975154ec0c3c902e5 Mon Sep 17 00:00:00 2001 From: Oliver Steele Date: Sat, 10 Jun 2017 19:32:39 -0400 Subject: [PATCH] Skip unpublished files; missing collections dirs --- build.go | 6 ++---- helpers.go | 9 +++++++++ main.go | 19 +++++++++++++++---- page.go | 24 ++++++++++++++---------- site.go | 35 ++++++++++++++++++++++------------- 5 files changed, 62 insertions(+), 31 deletions(-) diff --git a/build.go b/build.go index 3345f77..f72a8b1 100644 --- a/build.go +++ b/build.go @@ -22,12 +22,10 @@ func cleanDirectory() error { err = os.Remove(path) return err } - err := filepath.Walk(siteConfig.DestinationDir, removeFiles) - if err != nil { + if err := filepath.Walk(siteConfig.DestinationDir, removeFiles); err != nil { return err } - err = removeEmptyDirectories(siteConfig.DestinationDir) - return err + return removeEmptyDirectories(siteConfig.DestinationDir) } func build() error { diff --git a/helpers.go b/helpers.go index 0e577fc..99b140a 100644 --- a/helpers.go +++ b/helpers.go @@ -7,6 +7,15 @@ import ( "path/filepath" ) +func getBool(m map[interface{}]interface{}, k string, defaultValue bool) bool { + if val, found := m[k]; found { + if v, ok := val.(bool); ok { + return v + } + } + return defaultValue +} + // alternative to http://left-pad.io func leftPad(s string, n int) string { ws := make([]byte, n) diff --git a/main.go b/main.go index ddadd07..51c9053 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "sort" "time" "github.com/acstech/liquid" @@ -31,13 +32,16 @@ func main() { flag.StringVar(&siteConfig.DestinationDir, "destination", siteConfig.DestinationDir, "Destination directory") flag.StringVar(&siteConfig.SourceDir, "source", siteConfig.SourceDir, "Source directory") + + // routes subcommand + dynamic := flag.Bool("dynamic", false, "Dynamic routes only") + flag.Parse() configPath := filepath.Join(siteConfig.SourceDir, "_config.yml") // TODO error if file is e.g. unreadable if _, err := os.Stat(configPath); err == nil { - err := siteConfig.read(configPath) - if err != nil { + if err := siteConfig.read(configPath); err != nil { fmt.Println(err) return } @@ -66,8 +70,15 @@ func main() { printSetting("", fmt.Sprintf("done in %.2fs.", elapsed.Seconds())) case "routes": fmt.Printf("\nRoutes:\n") - for url, p := range siteMap { - fmt.Printf(" %s -> %s\n", url, p.Path) + urls := []string{} + for u, p := range siteMap { + if !(*dynamic && p.Static) { + urls = append(urls, u) + } + } + sort.Strings(urls) + for _, u := range urls { + fmt.Printf(" %s -> %s\n", u, siteMap[u].Path) } case "render": // build a single page, and print it to stdout; for testing diff --git a/page.go b/page.go index 841cc42..5be88b9 100644 --- a/page.go +++ b/page.go @@ -23,6 +23,7 @@ type Page struct { Permalink string Static bool Expanded bool + Published bool Body []byte } @@ -65,17 +66,18 @@ func readFile(path string, defaults map[interface{}]interface{}, expand bool) (* // title = title[:len(title)-len(ext)] // } - permalink := "/" + path[:len(path)-len(ext)] + permalink := path if val, ok := data["permalink"]; ok { permalink, ok = val.(string) if !ok { return nil, errors.New("Required string value for permalink") } } - templateVariables := map[string]string{} - templateVariables["output_ext"] = ".html" - templateVariables["path"] = regexp.MustCompile(`\.md$`).ReplaceAllLiteralString(path, "") - templateVariables["name"] = nonAlphanumericSequenceMatcher.ReplaceAllString(filepath.Base(path), "-") + templateVariables := map[string]string{ + "output_ext": ".html", + "path": regexp.MustCompile(`\.md$`).ReplaceAllLiteralString(path, ""), + "name": nonAlphanumericSequenceMatcher.ReplaceAllString(filepath.Base(path), "-"), + } if val, found := data["collection"]; found { collectionName := val.(string) collectionPath := "_" + collectionName + "/" @@ -86,17 +88,18 @@ func readFile(path string, defaults map[interface{}]interface{}, expand bool) (* return templateVariables[m[1:]] }) - if expand && ext == ".md" { + if expand { template, err := liquid.Parse(body, nil) if err != nil { return nil, err } writer := new(bytes.Buffer) template.Render(writer, stringMap(data)) - body = blackfriday.MarkdownBasic(writer.Bytes()) - } - - if !expand { + body = writer.Bytes() + if ext == ".md" { + body = blackfriday.MarkdownBasic(body) + } + } else { body = []byte{} } @@ -105,6 +108,7 @@ func readFile(path string, defaults map[interface{}]interface{}, expand bool) (* Permalink: permalink, Expanded: expand, Static: static, + Published: getBool(data, "published", true), Body: body, }, nil } diff --git a/site.go b/site.go index 40908d4..bf54b01 100644 --- a/site.go +++ b/site.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "strings" + "syscall" yaml "gopkg.in/yaml.v2" ) @@ -80,28 +81,30 @@ func buildSiteMap() (map[string]*Page, error) { if err != nil { return err } - fileMap[p.Permalink] = p + if p.Published { + fileMap[p.Permalink] = p + } return nil } - err := filepath.Walk(basePath, walkFn) - if err != nil { + + if err := filepath.Walk(basePath, walkFn); err != nil { return nil, err } for name, colVal := range siteConfig.Collections { - data := colVal.(map[interface{}]interface{}) - output := false - if val, found := data["output"]; found { - output = val.(bool) + data, ok := colVal.(map[interface{}]interface{}) + if !ok { + panic("expected collection value to be a map") } + output := getBool(data, "output", false) if output { - err = addCollectionFiles(fileMap, name, data) - if err != nil { + if err := addCollectionFiles(fileMap, name, data); err != nil { return nil, err } } } - return fileMap, err + + return fileMap, nil } func addCollectionFiles(fileMap map[string]*Page, name string, data map[interface{}]interface{}) error { @@ -111,6 +114,13 @@ func addCollectionFiles(fileMap map[string]*Page, name string, data map[interfac walkFn := func(path string, info os.FileInfo, err error) error { if err != nil { + // if the issue is simply that the directory doesn't exist, ignore the error + if pathErr, ok := err.(*os.PathError); ok { + if pathErr.Err == syscall.ENOENT { + fmt.Println("Missing directory for collection", name) + return nil + } + } return err } relPath, err := filepath.Rel(basePath, path) @@ -126,13 +136,12 @@ func addCollectionFiles(fileMap map[string]*Page, name string, data map[interfac } if p.Static { fmt.Printf("skipping static file inside collection: %s\n", path) - } else { + } else if p.Published { fileMap[p.Permalink] = p } return nil } - err := filepath.Walk(filepath.Join(basePath, "_"+name), walkFn) - return err + return filepath.Walk(filepath.Join(basePath, "_"+name), walkFn) } func getFileURL(path string) (string, bool) {