]> Nishi Git Mirror - aya.git/commitdiff
Add ignore functionality taken from prologic/zs 0.5.1
authorIzuru Yakumo <yakumo.izuru@chaotic.ninja>
Sun, 16 Apr 2023 01:02:59 +0000 (22:02 -0300)
committerIzuru Yakumo <yakumo.izuru@chaotic.ninja>
Sun, 16 Apr 2023 01:02:59 +0000 (22:02 -0300)
Signed-off-by: Izuru Yakumo <yakumo.izuru@chaotic.ninja>
cmd/aya/main.go
go.mod
go.sum

index 961e67400a2dbde585689735c24b96f654eda455..398d7043c986684fc8c17d9f29a637c503aabe2f 100644 (file)
@@ -5,26 +5,39 @@ import (
        "fmt"
        "io"
        "io/ioutil"
-       "log"
        "os"
        "os/exec"
        "path/filepath"
        "strings"
        "text/template"
        "time"
+       "errors"
 
        "github.com/russross/blackfriday/v2"
+       ignore "github.com/sabhiram/go-gitignore"
+       log "github.com/sirupsen/logrus"
        "gopkg.in/yaml.v2"
        "marisa.chaotic.ninja/aya"
 )
 
 const (
-       AYADIR  = ".aya"
-       PUBDIR = ".pub"
+       AYADIR        = ".aya"
+       AYAIGNORE     = ".ayaignore"
+       PUBDIR        = ".pub"
+       DefaultIgnore = `*~
+*.bak
+
+COPYING
+Dockerfile
+LICENSE
+Makefile
+README.md`
 )
 
 type Vars map[string]string
 
+var Ignore *ignore.GitIgnore
+
 // renameExt renames extension (if any) from oldext to newext
 // If oldext is an empty string - extension is extracted automatically.
 // If path has no extension - new extension is appended
@@ -88,6 +101,9 @@ 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
@@ -169,7 +185,7 @@ func render(s string, vars Vars) (string, error) {
                        }
                }
        }
-       
+
 }
 
 // Renders markdown with the given layout into html expanding all the macros
@@ -239,8 +255,10 @@ func buildRaw(path string, w io.Writer) error {
        _, err = io.Copy(w, in)
        return err
 }
-
 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)
@@ -259,6 +277,14 @@ func buildAll(watch bool) {
        for {
                os.Mkdir(PUBDIR, 0755)
                filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
+                       // rebuild if ignore file changes
+                       if filepath.Base(path) == AYAIGNORE || filepath.Dir(path) == AYADIR && info.ModTime().After(lastModified) {
+                               if filepath.Base(path) == AYAIGNORE {
+                                       Ignore = ParseIgnoreFile(path)
+                               }
+                               lastModified = time.Unix(0, 0)
+                               return nil
+                       }
                        // ignore hidden files and directories
                        if filepath.Base(path)[0] == '.' || strings.HasPrefix(path, ".") {
                                return nil
@@ -302,7 +328,17 @@ func init() {
        p = os.Getenv("PWD") + "/" + AYADIR + ":" + p
        os.Setenv("PATH", p)
 }
+func ParseIgnoreFile(fn string) *ignore.GitIgnore {
+       obj, err := ignore.CompileIgnoreFile(AYAIGNORE)
+       if err != nil {
+               if !errors.Is(err, os.ErrNotExist) {
+                       log.WithError(err).Warnf("error parsing .ayaignore: %s (using defaults)s", fn)
+               }
+               return ignore.CompileIgnoreLines(DefaultIgnore)
+       }
 
+       return obj
+}
 func main() {
        if len(os.Args) == 1 {
                fmt.Println(os.Args[0], "<command> [args]")
diff --git a/go.mod b/go.mod
index 74753188de973e49510d4dd962f0d3b850db4c90..6d8090f60346637e65fa65ff87165626951ab232 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -4,5 +4,9 @@ go 1.17
 
 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
        gopkg.in/yaml.v2 v2.4.0
 )
+
+require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
diff --git a/go.sum b/go.sum
index 6fc90070d83fe904734e35fb90ecd13c7f51040f..3d333532ed56e648c4cfcb7ac22acdf2ece29f6d 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -1,6 +1,23 @@
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
+github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
+golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
 gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=