Add support for .zsignore (Fixes #3)

This commit is contained in:
James Mills 2023-03-12 21:52:14 +10:00
parent 26c0bfad0b
commit 239d26a15a
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6
8 changed files with 43 additions and 2 deletions

1
go.mod
View File

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

3
go.sum
View File

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

27
main.go
View File

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

6
testdata/ignore/.test/index.html vendored Normal file
View File

@ -0,0 +1,6 @@
<html>
<body><h1 id="simple">Simple</h1>
<p>Simple page</p>
</body>
</html>

3
testdata/ignore/.zs/layout.html vendored Normal file
View File

@ -0,0 +1,3 @@
<html>
<body>{{ content }}</body>
</html>

1
testdata/ignore/.zsignore vendored Normal file
View File

@ -0,0 +1 @@
/ignored.txt

1
testdata/ignore/ignored.txt vendored Normal file
View File

@ -0,0 +1 @@
This file is ignored

3
testdata/ignore/index.md vendored Normal file
View File

@ -0,0 +1,3 @@
# Simple
Simple page