From 0565a6b07a9ba8e288f11e6bda98e56355347289 Mon Sep 17 00:00:00 2001 From: "Serge A. Zaitsev" Date: Wed, 2 Sep 2015 16:54:16 +0200 Subject: [PATCH] dead end with template functions --- zs.go | 43 +++++++++++------------------ zs_ext.go | 79 +++++++++++++++++++++++++++++++++++++++++++++++++----- zs_util.go | 57 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 142 insertions(+), 37 deletions(-) diff --git a/zs.go b/zs.go index 9ff0778..4ae7e2e 100644 --- a/zs.go +++ b/zs.go @@ -67,24 +67,7 @@ func md(path string, globals Vars) (Vars, string, error) { // Use standard Go templates func render(s string, funcs Funcs, vars Vars) (string, error) { - f := Funcs{} - for k, v := range funcs { - f[k] = v - } - for k, v := range vars { - f[k] = varFunc(v) - } - // Plugin functions - files, _ := ioutil.ReadDir(ZSDIR) - for _, file := range files { - if !file.IsDir() { - name := file.Name() - if !strings.HasSuffix(name, ".html") && !strings.HasSuffix(name, ".amber") { - f[strings.TrimSuffix(name, filepath.Ext(name))] = pluginFunc(name, vars) - } - } - } - + f := makeFuncs(funcs, vars) tmpl, err := template.New("").Funcs(template.FuncMap(f)).Parse(s) if err != nil { return "", err @@ -106,7 +89,7 @@ func buildMarkdown(path string, w io.Writer, funcs Funcs, vars Vars) error { if err != nil { return err } - v["content"] = string(blackfriday.MarkdownBasic([]byte(content))) + v["content"] = string(blackfriday.MarkdownCommon([]byte(content))) if w == nil { out, err := os.Create(filepath.Join(PUBDIR, renameExt(path, "", ".html"))) if err != nil { @@ -156,7 +139,7 @@ func buildAmber(path string, w io.Writer, funcs Funcs, vars Vars) error { for k, v := range vars { data[k] = v } - for k, v := range funcs { + for k, v := range makeFuncs(funcs, Vars{}) { data[k] = v } @@ -302,17 +285,23 @@ func main() { case "watch": buildAll(true) case "var": - fmt.Println(Var(args)) + fmt.Println(Var(args...)) case "lorem": - fmt.Println(Lorem(args)) + fmt.Println(Lorem(args...)) case "dateparse": - fmt.Println(DateParse(args)) + fmt.Println(DateParse(args...)) case "datefmt": - fmt.Println(DateFmt(args)) + fmt.Println(DateFmt(args...)) case "wc": - fmt.Println(WordCount(args)) - case "timetoread": - fmt.Println(TimeToRead(args)) + fmt.Println(WordCount(args...)) + case "ttr": + fmt.Println(TimeToRead(args...)) + case "ls": + fmt.Println(strings.Join(List(args...), "\n")) + case "sort": + fmt.Println(strings.Join(Sort(args...), "\n")) + case "exec": + // TODO default: err := run(path.Join(ZSDIR, cmd), args, globals(), os.Stdout) if err != nil { diff --git a/zs_ext.go b/zs_ext.go index b22ec88..7789401 100644 --- a/zs_ext.go +++ b/zs_ext.go @@ -2,7 +2,10 @@ package main import ( "bytes" + "log" "os" + "path/filepath" + "sort" "strconv" "strings" "time" @@ -15,7 +18,7 @@ import ( // zs var -- returns list of variables and their values // zs var -- returns list of variable values -func Var(args []string) string { +func Var(args ...string) string { if len(args) == 0 { return "var: filename expected" } else { @@ -38,7 +41,7 @@ func Var(args []string) string { } // zs lorem -- returns random lorem ipsum sentences -func Lorem(args []string) string { +func Lorem(args ...string) string { if len(args) > 1 { return "lorem: invalid usage" } @@ -53,7 +56,7 @@ func Lorem(args []string) string { } // zs datefmt -- returns formatted date from unix time -func DateFmt(args []string) string { +func DateFmt(args ...string) string { if len(args) == 0 || len(args) > 2 { return "datefmt: invalid usage" } @@ -65,7 +68,7 @@ func DateFmt(args []string) string { } // zs dateparse -- returns unix time from the formatted date -func DateParse(args []string) string { +func DateParse(args ...string) string { if len(args) == 0 || len(args) > 2 { return "dateparse: invalid usage" } @@ -77,7 +80,7 @@ func DateParse(args []string) string { } // zs wc -- returns word count in the file (markdown, html or amber) -func WordCount(args []string) int { +func WordCount(args ...string) int { if os.Getenv("ZS_RECURSION") != "" { return 0 } @@ -97,7 +100,69 @@ func WordCount(args []string) int { } // zs timetoread -- returns number of minutes required to read the text -func TimeToRead(args []string) int { - wc := WordCount(args) +func TimeToRead(args ...string) int { + wc := WordCount(args...) return int(math.Floor(float64(wc)/200.0 + .5)) } + +// zs ls +func List(args ...string) []string { + if len(args) != 2 { + return []string{} + } + + dir := args[0] + mask := args[1] + + res := []string{} + filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return nil + } + if !info.IsDir() { + if ok, err := filepath.Match(mask, info.Name()); ok && err == nil { + res = append(res, path) + } + } + return nil + }) + return res +} + +// zs sort +func Sort(args ...string) []string { + delim := -1 + for i, s := range args { + if s == "--" { + delim = i + } + } + cmd := []string{"var", "title"} + if delim != -1 { + cmd = args[:delim] + args = args[delim+1:] + } + + sorted := map[string][]string{} + sortedKeys := []string{} + for _, f := range args { + params := append(cmd, f) + out := bytes.NewBuffer(nil) + run(os.Args[0], params, globals(), out) + val := string(out.Bytes()) + sorted[val] = append(sorted[val], f) + sortedKeys = append(sortedKeys, val) + } + log.Println(sortedKeys) + sort.Strings(sortedKeys) + if !asc { + } + + list := []string{} + for _, k := range sortedKeys { + vals := sorted[k] + sort.Strings(vals) + list = append(list, vals...) + } + return list +} diff --git a/zs_util.go b/zs_util.go index 8b35caa..938a3d5 100644 --- a/zs_util.go +++ b/zs_util.go @@ -3,6 +3,7 @@ package main import ( "bytes" "io" + "io/ioutil" "log" "os" "os/exec" @@ -10,6 +11,29 @@ import ( "strings" ) +func makeFuncs(funcs Funcs, vars Vars) Funcs { + f := Funcs{} + for k, v := range funcs { + f[k] = v + } + for k, v := range vars { + f[k] = varFunc(v) + } + // Plugin functions + files, _ := ioutil.ReadDir(ZSDIR) + for _, file := range files { + if !file.IsDir() { + name := file.Name() + if !strings.HasSuffix(name, ".html") && !strings.HasSuffix(name, ".amber") { + f[renameExt(name, "", "")] = pluginFunc(name, vars) + } else { + f[renameExt(name, "", "")] = partialFunc(name, f, vars) + } + } + } + return f +} + func varFunc(s string) func() string { return func() string { return s @@ -27,6 +51,22 @@ func pluginFunc(cmd string, vars Vars) func(args ...string) string { } } +func partialFunc(name string, funcs Funcs, vars Vars) func() string { + return func() string { + var err error + w := bytes.NewBuffer(nil) + if strings.HasSuffix(name, ".amber") { + err = buildAmber(filepath.Join(ZSDIR, name), w, funcs, vars) + } else { + err = buildHTML(filepath.Join(ZSDIR, name), w, funcs, vars) + } + if err != nil { + return name + ":" + err.Error() + } + return string(w.Bytes()) + } +} + func builtins() Funcs { exec := func(cmd string, args ...string) string { out := bytes.NewBuffer(nil) @@ -38,9 +78,20 @@ func builtins() Funcs { return "" } return Funcs{ - "exec": exec, - "zs": func(args ...string) string { - return exec(os.Args[0], args...) + "exec": exec, + "var": Var, + "lorem": Lorem, + "dateparse": DateParse, + "datefmt": DateFmt, + "wc": WordCount, + "ttr": TimeToRead, + "ls": List, + "...": func(args ...string) []string { + return append([]string{"..."}, args...) + }, + "sort": func(args ...string) []string { + + return Sort(args...) }, } } -- 2.43.0