From 5d646b47e50963abd934e6eb786807a90dbd0659 Mon Sep 17 00:00:00 2001 From: Oliver Steele Date: Tue, 11 Jul 2017 19:23:42 -0400 Subject: [PATCH] Distinguish better between site-relative and collection-relative paths --- cmd/gojekyll/main.go | 12 ++++++++---- cmd/gojekyll/main_test.go | 5 ++++- collection/read.go | 30 ++++++++++++++++-------------- config/config_test.go | 3 +-- pages/file.go | 5 +++-- pages/interfaces.go | 4 ++-- site/site.go | 9 +++++---- site/write.go | 2 +- 8 files changed, 40 insertions(+), 30 deletions(-) diff --git a/cmd/gojekyll/main.go b/cmd/gojekyll/main.go index 240936f..4e99754 100644 --- a/cmd/gojekyll/main.go +++ b/cmd/gojekyll/main.go @@ -60,13 +60,17 @@ func init() { } func main() { - parseAndRun(os.Args[1:]) + err := parseAndRun(os.Args[1:]) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } } -func parseAndRun(args []string) { +func parseAndRun(args []string) error { if reflect.DeepEqual(args, []string{"--version"}) { printVersion() - return + return nil } cmd := kingpin.MustParse(app.Parse(args)) if configFlags.Destination != nil { @@ -77,7 +81,7 @@ func parseAndRun(args []string) { if buildOptions.DryRun { buildOptions.Verbose = true } - app.FatalIfError(run(cmd), "") + return run(cmd) } func printVersion() { diff --git a/cmd/gojekyll/main_test.go b/cmd/gojekyll/main_test.go index 82997a0..a5f4096 100644 --- a/cmd/gojekyll/main_test.go +++ b/cmd/gojekyll/main_test.go @@ -2,8 +2,11 @@ package main import ( "testing" + + "github.com/stretchr/testify/require" ) func TestBuild(t *testing.T) { - parseAndRun([]string{"build", "-s", "../../testdata/example", "-q"}) + err := parseAndRun([]string{"build", "-s", "../../testdata/example", "-q"}) + require.NoError(t, err) } diff --git a/collection/read.go b/collection/read.go index ac99284..148e36b 100644 --- a/collection/read.go +++ b/collection/read.go @@ -32,11 +32,10 @@ func (c *Collection) ReadPages() error { // // This function is distinct from ReadPages so that the posts collection can call it twice. func (c *Collection) scanDirectory(dirname string) error { - sitePath := c.config.Source - pageDefaults := map[string]interface{}{ - "collection": c.Name, - "permalink": c.PermalinkPattern(), - } + var ( + sitePath = c.config.Source + dir = filepath.Join(sitePath, dirname) + ) walkFn := func(filename string, info os.FileInfo, err error) error { if err != nil { if os.IsNotExist(err) { @@ -44,31 +43,34 @@ func (c *Collection) scanDirectory(dirname string) error { } return err } - relname := utils.MustRel(sitePath, filename) + siteRel := utils.MustRel(sitePath, filename) switch { case info.IsDir(): return nil - case c.site.Exclude(relname): + case c.site.Exclude(siteRel): return nil default: - fm := templates.MergeVariableMaps(pageDefaults, c.config.GetFrontMatterDefaults(c.Name, relname)) - return c.readFile(filename, relname, fm) + return c.readFile(filename, utils.MustRel(dir, filename)) } } - return filepath.Walk(filepath.Join(sitePath, dirname), walkFn) + return filepath.Walk(dir, walkFn) } -// readFile mutates fm. -func (c *Collection) readFile(abs string, rel string, fm map[string]interface{}) error { +func (c *Collection) readFile(abs string, rel string) error { + siteRel := utils.MustRel(c.config.Source, abs) strategy := c.strategy() switch { case !strategy.collectible(rel): return nil case strategy.future(rel) && !c.config.Future: return nil - default: - strategy.addDate(rel, fm) } + pageDefaults := map[string]interface{}{ + "collection": c.Name, + "permalink": c.PermalinkPattern(), + } + fm := templates.MergeVariableMaps(pageDefaults, c.config.GetFrontMatterDefaults(c.Name, siteRel)) + strategy.addDate(rel, fm) f, err := pages.NewFile(c.site, abs, filepath.ToSlash(rel), fm) switch { case err != nil: diff --git a/config/config_test.go b/config/config_test.go index 5e8c33a..8f05722 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,7 +1,6 @@ package config import ( - "fmt" "strings" "testing" @@ -37,7 +36,7 @@ func TestUnmarshal(t *testing.T) { c = Default() require.NoError(t, Unmarshal([]byte(`collections: \n- x\n-y`), &c)) - fmt.Println(c.Collections) + // fmt.Println(c.Collections) } func TestConfig_IsMarkdown(t *testing.T) { diff --git a/pages/file.go b/pages/file.go index 02ff732..27a44ca 100644 --- a/pages/file.go +++ b/pages/file.go @@ -7,6 +7,7 @@ import ( "github.com/osteele/gojekyll/frontmatter" "github.com/osteele/gojekyll/templates" + "github.com/osteele/gojekyll/utils" ) // file is embedded in StaticFile and page @@ -25,10 +26,10 @@ func (f *file) String() string { } func (f *file) OutputExt() string { return f.outputExt } -func (f *file) Path() string { return f.relpath } +func (f *file) Path() string { return utils.MustRel(f.site.Config().Source, f.filename) } func (f *file) Permalink() string { return f.permalink } func (f *file) Published() bool { return templates.VariableMap(f.frontMatter).Bool("published", true) } -func (f *file) SourcePath() string { return f.relpath } +func (f *file) SourcePath() string { return f.filename } // NewFile creates a Page or StaticFile. // diff --git a/pages/interfaces.go b/pages/interfaces.go index 673e563..1b6c4ea 100644 --- a/pages/interfaces.go +++ b/pages/interfaces.go @@ -10,8 +10,8 @@ import ( // A Document is a Jekyll post, page, or file. type Document interface { // Paths - Permalink() string // relative URL path - SourcePath() string // relative to the site source directory + Permalink() string // relative URL path + SourcePath() string OutputExt() string // Output diff --git a/site/site.go b/site/site.go index 286308b..a6b7e54 100644 --- a/site/site.go +++ b/site/site.go @@ -111,11 +111,11 @@ func (s *Site) SetAbsoluteURL(url string) { } } -// FilenameURLs returns a map of relative filenames to URL paths +// FilenameURLs returns a map of site-relative pathnames to URL paths func (s *Site) FilenameURLs() map[string]string { urls := map[string]string{} for _, page := range s.Pages() { - urls[page.SourcePath()] = page.Permalink() + urls[utils.MustRel(s.SourceDir(), page.SourcePath())] = page.Permalink() } return urls } @@ -126,9 +126,10 @@ func (s *Site) KeepFile(filename string) bool { } // FilePathPage returns a Page, give a file path relative to site source directory. -func (s *Site) FilePathPage(relpath string) (pages.Document, bool) { +func (s *Site) FilePathPage(rel string) (pages.Document, bool) { + // This looks wasteful. If it shows up as a hotspot, you know what to do. for _, p := range s.Routes { - if p.SourcePath() == relpath { + if p.SourcePath() != "" && rel == utils.MustRel(s.SourceDir(), p.SourcePath()) { return p, true } } diff --git a/site/write.go b/site/write.go index ca5fae4..8810a2e 100644 --- a/site/write.go +++ b/site/write.go @@ -35,7 +35,7 @@ func (s *Site) WritePages(options BuildOptions) (count int, err error) { // SavePage writes a document to the destination directory. // It attends to options.dry_run. func (s *Site) SavePage(p pages.Document, options BuildOptions) error { - from := filepath.Join(s.SourceDir(), filepath.ToSlash(p.SourcePath())) + from := p.SourcePath() to := filepath.Join(s.DestDir(), p.Permalink()) if !p.Static() && filepath.Ext(to) == "" { to = filepath.Join(to, "index.html")