aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-04-03 04:36:37 +0700
committerShulhan <ms@kilabit.info>2021-04-03 04:40:28 +0700
commit158e76e2c1c6ae2d42ba141e87130f29da142560 (patch)
tree646d7024d0112d73145bd4cea9d916bba6baebb3
parentbabb6b16261ffa6a5931223c95316487048ce4d8 (diff)
downloadciigo-158e76e2c1c6ae2d42ba141e87130f29da142560.tar.xz
all: add option to exclude certain paths using regular expression
The ConvertOptions now has the Exclude field that can contains regular expression. If the Exclude is not empty, it will be compiled and use in Convert, Generate, Watch, and Serve; to ignore specific paths being scanned.
-rw-r--r--ciigo.go78
-rw-r--r--cmd/ciigo/main.go17
-rw-r--r--convert_options.go30
-rw-r--r--generate_options.go9
-rw-r--r--serve_options.go9
-rw-r--r--server.go4
-rw-r--r--watcher.go6
-rw-r--r--watcher_test.go2
8 files changed, 121 insertions, 34 deletions
diff --git a/ciigo.go b/ciigo.go
index bad8117..d345658 100644
--- a/ciigo.go
+++ b/ciigo.go
@@ -15,6 +15,7 @@ import (
"fmt"
"os"
"path/filepath"
+ "regexp"
"strings"
"github.com/shuLhan/share/lib/memfs"
@@ -44,19 +45,26 @@ var (
// See template_index_html.go for template format.
//
func Convert(opts *ConvertOptions) (err error) {
- logp := "Convert"
+ var (
+ logp = "Convert"
+ htmlg *htmlGenerator
+ fileMarkups map[string]*fileMarkup
+ )
if opts == nil {
opts = &ConvertOptions{}
}
- opts.init()
+ err = opts.init()
+ if err != nil {
+ return fmt.Errorf("%s: %w", logp, err)
+ }
- htmlg, err := newHTMLGenerator(nil, opts.HtmlTemplate, true)
+ htmlg, err = newHTMLGenerator(nil, opts.HtmlTemplate, true)
if err != nil {
return fmt.Errorf("%s: %w", logp, err)
}
- fileMarkups, err := listFileMarkups(opts.Root)
+ fileMarkups, err = listFileMarkups(opts.Root, opts.excRE)
if err != nil {
return fmt.Errorf("%s: %w", logp, err)
}
@@ -77,19 +85,27 @@ func Convert(opts *ConvertOptions) (err error) {
// See template_index_html.go for template format.
//
func Generate(opts *GenerateOptions) (err error) {
- logp := "Generate"
+ var (
+ logp = "Generate"
+ htmlg *htmlGenerator
+ fileMarkups map[string]*fileMarkup
+ mfs *memfs.MemFS
+ )
if opts == nil {
opts = &GenerateOptions{}
}
- opts.init()
+ err = opts.init()
+ if err != nil {
+ return fmt.Errorf("%s: %w", logp, err)
+ }
- htmlg, err := newHTMLGenerator(nil, opts.HtmlTemplate, true)
+ htmlg, err = newHTMLGenerator(nil, opts.HtmlTemplate, true)
if err != nil {
return fmt.Errorf("%s: %w", logp, err)
}
- fileMarkups, err := listFileMarkups(opts.Root)
+ fileMarkups, err = listFileMarkups(opts.Root, opts.excRE)
if err != nil {
return fmt.Errorf("%s: %w", logp, err)
}
@@ -100,7 +116,7 @@ func Generate(opts *GenerateOptions) (err error) {
Root: opts.Root,
Excludes: defExcludes,
}
- mfs, err := memfs.New(memfsOpts)
+ mfs, err = memfs.New(memfsOpts)
if err != nil {
return fmt.Errorf("%s: %w", logp, err)
}
@@ -134,7 +150,10 @@ func Serve(opts *ServeOptions) (err error) {
if opts == nil {
opts = &ServeOptions{}
}
- opts.init()
+ err = opts.init()
+ if err != nil {
+ return fmt.Errorf("%s: %w", logp, err)
+ }
srv, err = newServer(opts)
if err != nil {
@@ -167,14 +186,17 @@ func Watch(opts *ConvertOptions) (err error) {
if opts == nil {
opts = &ConvertOptions{}
}
- opts.init()
+ err = opts.init()
+ if err != nil {
+ return fmt.Errorf("%s: %w", logp, err)
+ }
htmlg, err = newHTMLGenerator(nil, opts.HtmlTemplate, true)
if err != nil {
return fmt.Errorf("%s: %w", logp, err)
}
- w, err = newWatcher(htmlg, opts.Root)
+ w, err = newWatcher(htmlg, opts.Root, opts.Exclude)
if err != nil {
return fmt.Errorf("%s: %w", logp, err)
}
@@ -195,15 +217,21 @@ func isExtensionMarkup(ext string) bool {
// listFileMarkups find any markup files inside the content directory,
// recursively.
//
-func listFileMarkups(dir string) (fileMarkups map[string]*fileMarkup, err error) {
- logp := "listFileMarkups"
+func listFileMarkups(dir string, excRE []*regexp.Regexp) (
+ fileMarkups map[string]*fileMarkup, err error,
+) {
+ var (
+ logp = "listFileMarkups"
+ d *os.File
+ fis []os.FileInfo
+ )
- d, err := os.Open(dir)
+ d, err = os.Open(dir)
if err != nil {
return nil, fmt.Errorf("%s: %w", logp, err)
}
- fis, err := d.Readdir(0)
+ fis, err = d.Readdir(0)
if err != nil {
return nil, fmt.Errorf("%s: %w", logp, err)
}
@@ -215,7 +243,7 @@ func listFileMarkups(dir string) (fileMarkups map[string]*fileMarkup, err error)
if fi.IsDir() && name[0] != '.' {
newdir := filepath.Join(dir, fi.Name())
- fmarkups, err := listFileMarkups(newdir)
+ fmarkups, err := listFileMarkups(newdir, excRE)
if err != nil {
return nil, fmt.Errorf("%s: %w", logp, err)
}
@@ -235,6 +263,10 @@ func listFileMarkups(dir string) (fileMarkups map[string]*fileMarkup, err error)
filePath := filepath.Join(dir, name)
+ if isExcluded(filePath, excRE) {
+ continue
+ }
+
fmarkup := &fileMarkup{
path: filePath,
info: fi,
@@ -249,3 +281,15 @@ func listFileMarkups(dir string) (fileMarkups map[string]*fileMarkup, err error)
return fileMarkups, nil
}
+
+func isExcluded(path string, excs []*regexp.Regexp) bool {
+ if len(excs) == 0 {
+ return false
+ }
+ for _, re := range excs {
+ if re.MatchString(path) {
+ return true
+ }
+ }
+ return false
+}
diff --git a/cmd/ciigo/main.go b/cmd/ciigo/main.go
index 69c7231..5e3339c 100644
--- a/cmd/ciigo/main.go
+++ b/cmd/ciigo/main.go
@@ -10,20 +10,20 @@
//
// The following section describe how to use ciigo CLI.
//
-// ciigo [-template <file>] convert <dir>
+// ciigo [-template <file>] [-exclude <regex>] convert <dir>
//
// Scan the "dir" recursively to find markup files and convert them into HTML
// files.
// The template "file" is optional, default to embedded HTML template.
//
-// ciigo [-template <file>] [-out <file>] generate <dir>
+// ciigo [-template <file>] [-exclude <regex>] [-out <file>] generate <dir>
//
// Convert all the markup files inside directory "dir" recursively and then
// embed them into ".go" source file.
// The output file is optional, default to "ciigo_static.go" in current
// directory.
//
-// ciigo [-template <file>] [-address <ip:port>] serve <dir>
+// ciigo [-template <file>] [-exclude <regex>] [-address <ip:port>] serve <dir>
//
// Serve all files inside directory "dir" using HTTP server, watch changes on
// markup files and convert them to HTML files.
@@ -50,6 +50,8 @@ func main() {
"path to output of .go generated file")
address := flag.String("address", ":8080",
"the binding address for HTTP server")
+ exclude := flag.String("exclude", "",
+ "a regex to exclude certain paths from being scanned during covert, generate, watch, or serve")
flag.Parse()
@@ -77,6 +79,7 @@ func main() {
opts := ciigo.ConvertOptions{
Root: dir,
HtmlTemplate: *htmlTemplate,
+ Exclude: *exclude,
}
err = ciigo.Convert(&opts)
@@ -85,6 +88,7 @@ func main() {
ConvertOptions: ciigo.ConvertOptions{
Root: dir,
HtmlTemplate: *htmlTemplate,
+ Exclude: *exclude,
},
GenGoFileName: *outputFile,
}
@@ -96,6 +100,7 @@ func main() {
ConvertOptions: ciigo.ConvertOptions{
Root: dir,
HtmlTemplate: *htmlTemplate,
+ Exclude: *exclude,
},
Address: *address,
}
@@ -119,20 +124,20 @@ files, as HTML files.
== Usage
-ciigo [-template <file>] convert <dir>
+ciigo [-template <file>] [-exclude <regex>] convert <dir>
Scan the "dir" recursively to find markup files.
and convert them into HTML files.
The template "file" is optional, default to embedded HTML template.
-ciigo [-template <file>] [-out <file>] generate <dir>
+ciigo [-template <file>] [-exclude <regex>] [-out <file>] generate <dir>
Convert all markup files inside directory "dir" recursively and then
embed them into ".go" source file.
The output file is optional, default to "ciigo_static.go" in current
directory.
-ciigo [-template <file>] [-address <ip:port>] serve <dir>
+ciigo [-template <file>] [-exclude <regex>] [-address <ip:port>] serve <dir>
Serve all files inside directory "dir" using HTTP server, watch
changes on markup files and convert them to HTML files automatically.
diff --git a/convert_options.go b/convert_options.go
index eb179c5..9609299 100644
--- a/convert_options.go
+++ b/convert_options.go
@@ -4,6 +4,11 @@
package ciigo
+import (
+ "fmt"
+ "regexp"
+)
+
const (
// DefaultRoot define default Root value for GenerateOptions.
DefaultRoot = "."
@@ -18,15 +23,38 @@ type ConvertOptions struct {
// Default to DefaultRoot if its empty.
Root string
+ // Exclude define regular expresion to exclude certain paths from
+ // being scanned.
+ Exclude string
+
// HtmlTemplate the HTML template to be used when converting asciidoc
// file into HTML.
// If empty it will default to use embedded HTML template.
// See template_index_html.go for template format.
HtmlTemplate string
+
+ excRE []*regexp.Regexp
}
-func (opts *ConvertOptions) init() {
+func (opts *ConvertOptions) init() (err error) {
+ var (
+ logp = "ConvertOptions.init"
+ )
+
if len(opts.Root) == 0 {
opts.Root = DefaultRoot
}
+ if len(opts.Exclude) > 0 {
+ var re *regexp.Regexp
+
+ re, err = regexp.Compile(opts.Exclude)
+ if err != nil {
+ return fmt.Errorf("%s: %w", logp, err)
+ }
+
+ opts.excRE = append(opts.excRE, re)
+ defExcludes = append(defExcludes, opts.Exclude)
+ }
+ return nil
+
}
diff --git a/generate_options.go b/generate_options.go
index bf92075..e6217db 100644
--- a/generate_options.go
+++ b/generate_options.go
@@ -26,9 +26,11 @@ type GenerateOptions struct {
GenGoFileName string
}
-func (opts *GenerateOptions) init() {
- opts.ConvertOptions.init()
-
+func (opts *GenerateOptions) init() (err error) {
+ err = opts.ConvertOptions.init()
+ if err != nil {
+ return err
+ }
if len(opts.GenPackageName) == 0 {
opts.GenPackageName = memfs.DefaultGenPackageName
}
@@ -38,4 +40,5 @@ func (opts *GenerateOptions) init() {
if len(opts.GenGoFileName) == 0 {
opts.GenGoFileName = memfs.DefaultGenGoFileName
}
+ return nil
}
diff --git a/serve_options.go b/serve_options.go
index 953bb1f..d603e24 100644
--- a/serve_options.go
+++ b/serve_options.go
@@ -24,10 +24,13 @@ type ServeOptions struct {
Address string
}
-func (opts *ServeOptions) init() {
- opts.ConvertOptions.init()
-
+func (opts *ServeOptions) init() (err error) {
+ err = opts.ConvertOptions.init()
+ if err != nil {
+ return err
+ }
if len(opts.Address) == 0 {
opts.Address = defAddress
}
+ return nil
}
diff --git a/server.go b/server.go
index d225836..b65ebf4 100644
--- a/server.go
+++ b/server.go
@@ -71,12 +71,12 @@ func newServer(opts *ServeOptions) (srv *server, err error) {
}
if srv.opts.Development {
- srv.watcher, err = newWatcher(srv.htmlg, opts.Root)
+ srv.watcher, err = newWatcher(srv.htmlg, opts.Root, opts.Exclude)
if err != nil {
return nil, fmt.Errorf("%s: %w", logp, err)
}
- srv.watcher.fileMarkups, err = listFileMarkups(opts.Root)
+ srv.watcher.fileMarkups, err = listFileMarkups(opts.Root, opts.excRE)
if err != nil {
return nil, fmt.Errorf("%s: %w", logp, err)
}
diff --git a/watcher.go b/watcher.go
index 6a8b98d..71618e8 100644
--- a/watcher.go
+++ b/watcher.go
@@ -43,7 +43,7 @@ type watcher struct {
// |
// +--> UPDATE --> htmlGenerated.htmlTemplateReload()
//
-func newWatcher(htmlg *htmlGenerator, dir string) (w *watcher, err error) {
+func newWatcher(htmlg *htmlGenerator, dir, exclude string) (w *watcher, err error) {
w = &watcher{
dir: dir,
htmlg: htmlg,
@@ -66,6 +66,10 @@ func newWatcher(htmlg *htmlGenerator, dir string) (w *watcher, err error) {
Callback: w.onChangeFileMarkup,
}
+ if len(exclude) > 0 {
+ w.dw.Options.Excludes = append(w.dw.Options.Excludes, exclude)
+ }
+
return w, nil
}
diff --git a/watcher_test.go b/watcher_test.go
index e85e053..0bceff1 100644
--- a/watcher_test.go
+++ b/watcher_test.go
@@ -37,7 +37,7 @@ func TestWatcher(t *testing.T) {
t.Fatal(err)
}
- testWatcher, err = newWatcher(htmlg, testDir)
+ testWatcher, err = newWatcher(htmlg, testDir, "")
if err != nil {
t.Fatal(err)
}