]> Nishi Git Mirror - aya.git/commitdiff
Add support for .zsignore (Fixes #3) 0.1.0
authorJames Mills <1290234+prologic@users.noreply.github.com>
Sun, 12 Mar 2023 11:52:14 +0000 (21:52 +1000)
committerJames Mills <1290234+prologic@users.noreply.github.com>
Sun, 12 Mar 2023 11:52:14 +0000 (21:52 +1000)
go.mod
go.sum
main.go
testdata/ignore/.test/index.html [new file with mode: 0644]
testdata/ignore/.zs/layout.html [new file with mode: 0644]
testdata/ignore/.zsignore [new file with mode: 0644]
testdata/ignore/ignored.txt [new file with mode: 0644]
testdata/ignore/index.md [new file with mode: 0644]

diff --git a/go.mod b/go.mod
index f46bd2931ae639446209c47987ad48126c1c6132..e908abe808b30b2f8bfe061d0cfa36f73b7b27fd 100644 (file)
--- 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 420f1cdf48ca540d7f0b03290f537df51a92a8d1..8894b326e1a81c25edc709c0b2c4879fd8a79d32 100644 (file)
--- 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 57b46e83327bc750705f6df384c79d4fa3ffac49..93571503b1091e1798ee8cb9e2d1e896e618b4cf 100644 (file)
--- 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 (file)
index 0000000..69131fa
--- /dev/null
@@ -0,0 +1,6 @@
+<html>
+       <body><h1 id="simple">Simple</h1>
+
+<p>Simple page</p>
+</body>
+</html>
diff --git a/testdata/ignore/.zs/layout.html b/testdata/ignore/.zs/layout.html
new file mode 100644 (file)
index 0000000..5744e79
--- /dev/null
@@ -0,0 +1,3 @@
+<html>
+       <body>{{ content }}</body>
+</html>
diff --git a/testdata/ignore/.zsignore b/testdata/ignore/.zsignore
new file mode 100644 (file)
index 0000000..5721082
--- /dev/null
@@ -0,0 +1 @@
+/ignored.txt
diff --git a/testdata/ignore/ignored.txt b/testdata/ignore/ignored.txt
new file mode 100644 (file)
index 0000000..7209bc4
--- /dev/null
@@ -0,0 +1 @@
+This file is ignored
diff --git a/testdata/ignore/index.md b/testdata/ignore/index.md
new file mode 100644 (file)
index 0000000..4331622
--- /dev/null
@@ -0,0 +1,3 @@
+# Simple
+
+Simple page