comparison zs_ext.go @ 25:42b0a9fa5883 draft

added word count and time to read functions
author zaitsev.serge
date Sun, 30 Aug 2015 12:42:22 +0000
parents 40f55059fbfa
children d966bcb229c0
comparison
equal deleted inserted replaced
24:d052f3a44195 25:42b0a9fa5883
1 package main 1 package main
2 2
3 import ( 3 import (
4 "bytes"
5 "os"
4 "strconv" 6 "strconv"
5 "strings" 7 "strings"
6 "time" 8 "time"
7 9
8 "github.com/drhodes/golorem" 10 "github.com/drhodes/golorem"
11 "github.com/google/gxui/math"
12 "github.com/jaytaylor/html2text"
9 ) 13 )
10 14
11 // zs var <filename> -- returns list of variables and their values 15 // zs var <filename> -- returns list of variables and their values
12 // zs var <filename> <var...> -- returns list of variable values 16 // zs var <filename> <var...> -- returns list of variable values
13 func Var(args []string) string { 17 func Var(args []string) string {
68 return "dateparse: " + err.Error() 72 return "dateparse: " + err.Error()
69 } else { 73 } else {
70 return strconv.FormatInt(d.Unix(), 10) 74 return strconv.FormatInt(d.Unix(), 10)
71 } 75 }
72 } 76 }
77
78 // zs wc <file> -- returns word count in the file (markdown, html or amber)
79 func WordCount(args []string) int {
80 if os.Getenv("ZS_RECURSION") != "" {
81 return 0
82 }
83 if len(args) != 1 {
84 return 0
85 }
86 os.Setenv("ZS_RECURSION", "1")
87 out := &bytes.Buffer{}
88 if err := build(args[0], out, builtins(), globals()); err != nil {
89 return 0
90 }
91 if s, err := html2text.FromString(string(out.Bytes())); err != nil {
92 return 0
93 } else {
94 return len(strings.Fields(s))
95 }
96 }
97
98 // zs timetoread <file> -- returns number of minutes required to read the text
99 func TimeToRead(args []string) int {
100 wc := WordCount(args)
101 return int(math.Round(float64(wc) / float64(200)))
102 }