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

Distinguish better between site-relative and collection-relative paths

This commit is contained in:
Oliver Steele 2017-07-11 19:23:42 -04:00
parent fab24342be
commit 5d646b47e5
8 changed files with 40 additions and 30 deletions

View File

@ -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() {

View File

@ -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)
}

View File

@ -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:

View File

@ -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) {

View File

@ -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.
//

View File

@ -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

View File

@ -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
}
}

View File

@ -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")