]> Nishi Git Mirror - aya.git/commitdiff
added word count and time to read functions
authorSerge A. Zaitsev <zaitsev.serge@gmail.com>
Sun, 30 Aug 2015 12:42:22 +0000 (14:42 +0200)
committerSerge A. Zaitsev <zaitsev.serge@gmail.com>
Sun, 30 Aug 2015 12:42:22 +0000 (14:42 +0200)
zs.go
zs_ext.go

diff --git a/zs.go b/zs.go
index 6a3bc8f9bc1355fd7712e767b92df9c53bc3f270..f38dbf5ff9908ebcbc4f36831ad658a960d6a2c4 100644 (file)
--- a/zs.go
+++ b/zs.go
@@ -304,6 +304,10 @@ func main() {
                fmt.Println(DateParse(args))
        case "datefmt":
                fmt.Println(DateFmt(args))
+       case "wc":
+               fmt.Println(WordCount(args))
+       case "timetoread":
+               fmt.Println(TimeToRead(args))
        default:
                err := run(path.Join(ZSDIR, cmd), args, Vars{}, os.Stdout)
                if err != nil {
index 205942e6a3972bf583f5daa4624109afe36a5659..cedd88476659f037cb045a62e1e3c90c2e2d1a60 100644 (file)
--- a/zs_ext.go
+++ b/zs_ext.go
@@ -1,11 +1,15 @@
 package main
 
 import (
+       "bytes"
+       "os"
        "strconv"
        "strings"
        "time"
 
        "github.com/drhodes/golorem"
+       "github.com/google/gxui/math"
+       "github.com/jaytaylor/html2text"
 )
 
 // zs var <filename> -- returns list of variables and their values
@@ -70,3 +74,29 @@ func DateParse(args []string) string {
                return strconv.FormatInt(d.Unix(), 10)
        }
 }
+
+// zs wc <file> -- returns word count in the file (markdown, html or amber)
+func WordCount(args []string) int {
+       if os.Getenv("ZS_RECURSION") != "" {
+               return 0
+       }
+       if len(args) != 1 {
+               return 0
+       }
+       os.Setenv("ZS_RECURSION", "1")
+       out := &bytes.Buffer{}
+       if err := build(args[0], out, builtins(), globals()); err != nil {
+               return 0
+       }
+       if s, err := html2text.FromString(string(out.Bytes())); err != nil {
+               return 0
+       } else {
+               return len(strings.Fields(s))
+       }
+}
+
+// zs timetoread <file> -- returns number of minutes required to read the text
+func TimeToRead(args []string) int {
+       wc := WordCount(args)
+       return int(math.Round(float64(wc) / float64(200)))
+}