Changeset 26 in code


Ignore:
Timestamp:
Aug 30, 2015, 12:42:22 PM (10 years ago)
Author:
zaitsev.serge
Message:

added word count and time to read functions

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/zs.go

    r25 r26  
    305305        case "datefmt":
    306306                fmt.Println(DateFmt(args))
     307        case "wc":
     308                fmt.Println(WordCount(args))
     309        case "timetoread":
     310                fmt.Println(TimeToRead(args))
    307311        default:
    308312                err := run(path.Join(ZSDIR, cmd), args, Vars{}, os.Stdout)
  • trunk/zs_ext.go

    r24 r26  
    22
    33import (
     4        "bytes"
     5        "os"
    46        "strconv"
    57        "strings"
     
    79
    810        "github.com/drhodes/golorem"
     11        "github.com/google/gxui/math"
     12        "github.com/jaytaylor/html2text"
    913)
    1014
     
    7175        }
    7276}
     77
     78// zs wc <file> -- returns word count in the file (markdown, html or amber)
     79func 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
     99func TimeToRead(args []string) int {
     100        wc := WordCount(args)
     101        return int(math.Round(float64(wc) / float64(200)))
     102}
Note: See TracChangeset for help on using the changeset viewer.