mirror of
https://github.com/danog/gojekyll.git
synced 2024-11-26 23:24:39 +01:00
Skip unpublished files; missing collections dirs
This commit is contained in:
parent
58d3e66604
commit
afe6425adc
6
build.go
6
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 {
|
||||
|
@ -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)
|
||||
|
19
main.go
19
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
|
||||
|
22
page.go
22
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())
|
||||
body = writer.Bytes()
|
||||
if ext == ".md" {
|
||||
body = blackfriday.MarkdownBasic(body)
|
||||
}
|
||||
|
||||
if !expand {
|
||||
} 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
|
||||
}
|
||||
|
33
site.go
33
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
|
||||
}
|
||||
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user