1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 07:18:59 +01:00

Skip unpublished files; missing collections dirs

This commit is contained in:
Oliver Steele 2017-06-10 19:32:39 -04:00
parent 58d3e66604
commit afe6425adc
5 changed files with 62 additions and 31 deletions

View File

@ -22,12 +22,10 @@ func cleanDirectory() error {
err = os.Remove(path) err = os.Remove(path)
return err return err
} }
err := filepath.Walk(siteConfig.DestinationDir, removeFiles) if err := filepath.Walk(siteConfig.DestinationDir, removeFiles); err != nil {
if err != nil {
return err return err
} }
err = removeEmptyDirectories(siteConfig.DestinationDir) return removeEmptyDirectories(siteConfig.DestinationDir)
return err
} }
func build() error { func build() error {

View File

@ -7,6 +7,15 @@ import (
"path/filepath" "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 // alternative to http://left-pad.io
func leftPad(s string, n int) string { func leftPad(s string, n int) string {
ws := make([]byte, n) ws := make([]byte, n)

19
main.go
View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"sort"
"time" "time"
"github.com/acstech/liquid" "github.com/acstech/liquid"
@ -31,13 +32,16 @@ func main() {
flag.StringVar(&siteConfig.DestinationDir, "destination", siteConfig.DestinationDir, "Destination directory") flag.StringVar(&siteConfig.DestinationDir, "destination", siteConfig.DestinationDir, "Destination directory")
flag.StringVar(&siteConfig.SourceDir, "source", siteConfig.SourceDir, "Source directory") flag.StringVar(&siteConfig.SourceDir, "source", siteConfig.SourceDir, "Source directory")
// routes subcommand
dynamic := flag.Bool("dynamic", false, "Dynamic routes only")
flag.Parse() flag.Parse()
configPath := filepath.Join(siteConfig.SourceDir, "_config.yml") configPath := filepath.Join(siteConfig.SourceDir, "_config.yml")
// TODO error if file is e.g. unreadable // TODO error if file is e.g. unreadable
if _, err := os.Stat(configPath); err == nil { if _, err := os.Stat(configPath); err == nil {
err := siteConfig.read(configPath) if err := siteConfig.read(configPath); err != nil {
if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
@ -66,8 +70,15 @@ func main() {
printSetting("", fmt.Sprintf("done in %.2fs.", elapsed.Seconds())) printSetting("", fmt.Sprintf("done in %.2fs.", elapsed.Seconds()))
case "routes": case "routes":
fmt.Printf("\nRoutes:\n") fmt.Printf("\nRoutes:\n")
for url, p := range siteMap { urls := []string{}
fmt.Printf(" %s -> %s\n", url, p.Path) 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": case "render":
// build a single page, and print it to stdout; for testing // build a single page, and print it to stdout; for testing

24
page.go
View File

@ -23,6 +23,7 @@ type Page struct {
Permalink string Permalink string
Static bool Static bool
Expanded bool Expanded bool
Published bool
Body []byte Body []byte
} }
@ -65,17 +66,18 @@ func readFile(path string, defaults map[interface{}]interface{}, expand bool) (*
// title = title[:len(title)-len(ext)] // title = title[:len(title)-len(ext)]
// } // }
permalink := "/" + path[:len(path)-len(ext)] permalink := path
if val, ok := data["permalink"]; ok { if val, ok := data["permalink"]; ok {
permalink, ok = val.(string) permalink, ok = val.(string)
if !ok { if !ok {
return nil, errors.New("Required string value for permalink") return nil, errors.New("Required string value for permalink")
} }
} }
templateVariables := map[string]string{} templateVariables := map[string]string{
templateVariables["output_ext"] = ".html" "output_ext": ".html",
templateVariables["path"] = regexp.MustCompile(`\.md$`).ReplaceAllLiteralString(path, "") "path": regexp.MustCompile(`\.md$`).ReplaceAllLiteralString(path, ""),
templateVariables["name"] = nonAlphanumericSequenceMatcher.ReplaceAllString(filepath.Base(path), "-") "name": nonAlphanumericSequenceMatcher.ReplaceAllString(filepath.Base(path), "-"),
}
if val, found := data["collection"]; found { if val, found := data["collection"]; found {
collectionName := val.(string) collectionName := val.(string)
collectionPath := "_" + collectionName + "/" collectionPath := "_" + collectionName + "/"
@ -86,17 +88,18 @@ func readFile(path string, defaults map[interface{}]interface{}, expand bool) (*
return templateVariables[m[1:]] return templateVariables[m[1:]]
}) })
if expand && ext == ".md" { if expand {
template, err := liquid.Parse(body, nil) template, err := liquid.Parse(body, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
writer := new(bytes.Buffer) writer := new(bytes.Buffer)
template.Render(writer, stringMap(data)) template.Render(writer, stringMap(data))
body = blackfriday.MarkdownBasic(writer.Bytes()) body = writer.Bytes()
} if ext == ".md" {
body = blackfriday.MarkdownBasic(body)
if !expand { }
} else {
body = []byte{} body = []byte{}
} }
@ -105,6 +108,7 @@ func readFile(path string, defaults map[interface{}]interface{}, expand bool) (*
Permalink: permalink, Permalink: permalink,
Expanded: expand, Expanded: expand,
Static: static, Static: static,
Published: getBool(data, "published", true),
Body: body, Body: body,
}, nil }, nil
} }

35
site.go
View File

@ -6,6 +6,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"syscall"
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
) )
@ -80,28 +81,30 @@ func buildSiteMap() (map[string]*Page, error) {
if err != nil { if err != nil {
return err return err
} }
fileMap[p.Permalink] = p if p.Published {
fileMap[p.Permalink] = p
}
return nil return nil
} }
err := filepath.Walk(basePath, walkFn)
if err != nil { if err := filepath.Walk(basePath, walkFn); err != nil {
return nil, err return nil, err
} }
for name, colVal := range siteConfig.Collections { for name, colVal := range siteConfig.Collections {
data := colVal.(map[interface{}]interface{}) data, ok := colVal.(map[interface{}]interface{})
output := false if !ok {
if val, found := data["output"]; found { panic("expected collection value to be a map")
output = val.(bool)
} }
output := getBool(data, "output", false)
if output { if output {
err = addCollectionFiles(fileMap, name, data) if err := addCollectionFiles(fileMap, name, data); err != nil {
if err != nil {
return nil, err return nil, err
} }
} }
} }
return fileMap, err
return fileMap, nil
} }
func addCollectionFiles(fileMap map[string]*Page, name string, data map[interface{}]interface{}) error { 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 { walkFn := func(path string, info os.FileInfo, err error) error {
if err != nil { 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 return err
} }
relPath, err := filepath.Rel(basePath, path) relPath, err := filepath.Rel(basePath, path)
@ -126,13 +136,12 @@ func addCollectionFiles(fileMap map[string]*Page, name string, data map[interfac
} }
if p.Static { if p.Static {
fmt.Printf("skipping static file inside collection: %s\n", path) fmt.Printf("skipping static file inside collection: %s\n", path)
} else { } else if p.Published {
fileMap[p.Permalink] = p fileMap[p.Permalink] = p
} }
return nil return nil
} }
err := filepath.Walk(filepath.Join(basePath, "_"+name), walkFn) return filepath.Walk(filepath.Join(basePath, "_"+name), walkFn)
return err
} }
func getFileURL(path string) (string, bool) { func getFileURL(path string) (string, bool) {