From: James Mills <1290234+prologic@users.noreply.github.com> Date: Sun, 12 Mar 2023 11:52:14 +0000 (+1000) Subject: Add support for .zsignore (Fixes #3) X-Git-Tag: 0.1.0 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=dc9995175175d48dadca297ceff523232e43b900;p=aya.git Add support for .zsignore (Fixes #3) --- diff --git a/go.mod b/go.mod index f46bd29..e908abe 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.18 require ( github.com/russross/blackfriday/v2 v2.1.0 + github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 github.com/sirupsen/logrus v1.9.0 github.com/spf13/cobra v1.6.1 go.mills.io/static v0.0.0-20230312034046-6dff09caed3b diff --git a/go.sum b/go.sum index 420f1cd..8894b32 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,8 @@ github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBO github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI= +github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA= @@ -34,6 +36,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= diff --git a/main.go b/main.go index 57b46e8..9357150 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ import ( "time" "github.com/russross/blackfriday/v2" + ignore "github.com/sabhiram/go-gitignore" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "go.mills.io/static" @@ -28,10 +29,16 @@ const ( // ZSDIR is the default directory for storing layouts and extensions ZSDIR = ".zs" + // ZSIGNORE is the default ignore file + ZSIGNORE = ".zsignore" + // PUBDIR is the default directory for publishing final built content PUBDIR = ".pub" ) +// Ignore holds a set of patterns to ignore from parsing a .zsignore file +var Ignore *ignore.GitIgnore + // Vars holds a map of global variables type Vars map[string]string @@ -263,6 +270,10 @@ func run(vars Vars, cmd string, args ...string) (string, error) { // content by an empty line. Header can be either YAML or JSON. // If no empty newline is found - file is treated as content-only. func getVars(path string, globals Vars) (Vars, string, error) { + if Ignore.MatchesPath(path) { + return nil, "", nil + } + b, err := ioutil.ReadFile(path) if err != nil { return nil, "", err @@ -421,6 +432,10 @@ func buildRaw(path string, w io.Writer) error { } func build(path string, w io.Writer, vars Vars) error { + if Ignore.MatchesPath(path) { + return nil + } + ext := filepath.Ext(path) if ext == ".md" || ext == ".mkd" { return buildMarkdown(path, w, vars) @@ -446,10 +461,11 @@ func buildAll(ctx context.Context, watch bool) error { case <-ticker.C: os.Mkdir(PUBDIR, 0755) filepath.Walk(".", func(path string, info os.FileInfo, err error) error { - // ignore hidden files and directories - if filepath.Base(path)[0] == '.' || strings.HasPrefix(path, ".") { + // ignore hidden files and directories and ignored patterns + if filepath.Base(path)[0] == '.' || strings.HasPrefix(path, ".") || Ignore.MatchesPath(path) { return nil } + // inform user about fs walk errors, but continue iteration if err != nil { log.WithError(err).Warn("error walking directory") @@ -545,6 +561,13 @@ func main() { w, _ := os.Getwd() ensureFirstPath(filepath.Join(w, ZSDIR)) + // initialise Ignore (.zsignore) patterns + if ignoreObject, err := ignore.CompileIgnoreFile(ZSIGNORE); err == nil { + Ignore = ignoreObject + } else { + Ignore = ignore.CompileIgnoreLines("") + } + if err := RootCmd.Execute(); err != nil { log.WithError(err).Error("error executing command") os.Exit(1) diff --git a/testdata/ignore/.test/index.html b/testdata/ignore/.test/index.html new file mode 100644 index 0000000..69131fa --- /dev/null +++ b/testdata/ignore/.test/index.html @@ -0,0 +1,6 @@ + +

Simple

+ +

Simple page

+ + diff --git a/testdata/ignore/.zs/layout.html b/testdata/ignore/.zs/layout.html new file mode 100644 index 0000000..5744e79 --- /dev/null +++ b/testdata/ignore/.zs/layout.html @@ -0,0 +1,3 @@ + + {{ content }} + diff --git a/testdata/ignore/.zsignore b/testdata/ignore/.zsignore new file mode 100644 index 0000000..5721082 --- /dev/null +++ b/testdata/ignore/.zsignore @@ -0,0 +1 @@ +/ignored.txt diff --git a/testdata/ignore/ignored.txt b/testdata/ignore/ignored.txt new file mode 100644 index 0000000..7209bc4 --- /dev/null +++ b/testdata/ignore/ignored.txt @@ -0,0 +1 @@ +This file is ignored diff --git a/testdata/ignore/index.md b/testdata/ignore/index.md new file mode 100644 index 0000000..4331622 --- /dev/null +++ b/testdata/ignore/index.md @@ -0,0 +1,3 @@ +# Simple + +Simple page