comparison zs_ext.go @ 31:b2f491299cee draft

dead end with template functions
author zaitsev.serge
date Wed, 02 Sep 2015 14:54:16 +0000
parents d966bcb229c0
children
comparison
equal deleted inserted replaced
30:526ba3c717ba 31:b2f491299cee
1 package main 1 package main
2 2
3 import ( 3 import (
4 "bytes" 4 "bytes"
5 "log"
5 "os" 6 "os"
7 "path/filepath"
8 "sort"
6 "strconv" 9 "strconv"
7 "strings" 10 "strings"
8 "time" 11 "time"
9 12
10 "math" 13 "math"
13 "github.com/jaytaylor/html2text" 16 "github.com/jaytaylor/html2text"
14 ) 17 )
15 18
16 // zs var <filename> -- returns list of variables and their values 19 // zs var <filename> -- returns list of variables and their values
17 // zs var <filename> <var...> -- returns list of variable values 20 // zs var <filename> <var...> -- returns list of variable values
18 func Var(args []string) string { 21 func Var(args ...string) string {
19 if len(args) == 0 { 22 if len(args) == 0 {
20 return "var: filename expected" 23 return "var: filename expected"
21 } else { 24 } else {
22 s := "" 25 s := ""
23 if vars, _, err := md(args[0], globals()); err != nil { 26 if vars, _, err := md(args[0], globals()); err != nil {
36 return strings.TrimSpace(s) 39 return strings.TrimSpace(s)
37 } 40 }
38 } 41 }
39 42
40 // zs lorem <n> -- returns <n> random lorem ipsum sentences 43 // zs lorem <n> -- returns <n> random lorem ipsum sentences
41 func Lorem(args []string) string { 44 func Lorem(args ...string) string {
42 if len(args) > 1 { 45 if len(args) > 1 {
43 return "lorem: invalid usage" 46 return "lorem: invalid usage"
44 } 47 }
45 if len(args) == 0 { 48 if len(args) == 0 {
46 return lorem.Paragraph(5, 5) 49 return lorem.Paragraph(5, 5)
51 return "lorem: " + err.Error() 54 return "lorem: " + err.Error()
52 } 55 }
53 } 56 }
54 57
55 // zs datefmt <fmt> <date> -- returns formatted date from unix time 58 // zs datefmt <fmt> <date> -- returns formatted date from unix time
56 func DateFmt(args []string) string { 59 func DateFmt(args ...string) string {
57 if len(args) == 0 || len(args) > 2 { 60 if len(args) == 0 || len(args) > 2 {
58 return "datefmt: invalid usage" 61 return "datefmt: invalid usage"
59 } 62 }
60 if n, err := strconv.ParseInt(args[1], 10, 64); err == nil { 63 if n, err := strconv.ParseInt(args[1], 10, 64); err == nil {
61 return time.Unix(n, 0).Format(args[0]) 64 return time.Unix(n, 0).Format(args[0])
63 return "datefmt: " + err.Error() 66 return "datefmt: " + err.Error()
64 } 67 }
65 } 68 }
66 69
67 // zs dateparse <fmt> <date> -- returns unix time from the formatted date 70 // zs dateparse <fmt> <date> -- returns unix time from the formatted date
68 func DateParse(args []string) string { 71 func DateParse(args ...string) string {
69 if len(args) == 0 || len(args) > 2 { 72 if len(args) == 0 || len(args) > 2 {
70 return "dateparse: invalid usage" 73 return "dateparse: invalid usage"
71 } 74 }
72 if d, err := time.Parse(args[0], args[1]); err != nil { 75 if d, err := time.Parse(args[0], args[1]); err != nil {
73 return "dateparse: " + err.Error() 76 return "dateparse: " + err.Error()
75 return strconv.FormatInt(d.Unix(), 10) 78 return strconv.FormatInt(d.Unix(), 10)
76 } 79 }
77 } 80 }
78 81
79 // zs wc <file> -- returns word count in the file (markdown, html or amber) 82 // zs wc <file> -- returns word count in the file (markdown, html or amber)
80 func WordCount(args []string) int { 83 func WordCount(args ...string) int {
81 if os.Getenv("ZS_RECURSION") != "" { 84 if os.Getenv("ZS_RECURSION") != "" {
82 return 0 85 return 0
83 } 86 }
84 if len(args) != 1 { 87 if len(args) != 1 {
85 return 0 88 return 0
95 return len(strings.Fields(s)) 98 return len(strings.Fields(s))
96 } 99 }
97 } 100 }
98 101
99 // zs timetoread <file> -- returns number of minutes required to read the text 102 // zs timetoread <file> -- returns number of minutes required to read the text
100 func TimeToRead(args []string) int { 103 func TimeToRead(args ...string) int {
101 wc := WordCount(args) 104 wc := WordCount(args...)
102 return int(math.Floor(float64(wc)/200.0 + .5)) 105 return int(math.Floor(float64(wc)/200.0 + .5))
103 } 106 }
107
108 // zs ls <dir> <regexp>
109 func List(args ...string) []string {
110 if len(args) != 2 {
111 return []string{}
112 }
113
114 dir := args[0]
115 mask := args[1]
116
117 res := []string{}
118 filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
119 if err != nil {
120 return nil
121 }
122 if !info.IsDir() {
123 if ok, err := filepath.Match(mask, info.Name()); ok && err == nil {
124 res = append(res, path)
125 }
126 }
127 return nil
128 })
129 return res
130 }
131
132 // zs sort <key> <files...>
133 func Sort(args ...string) []string {
134 delim := -1
135 for i, s := range args {
136 if s == "--" {
137 delim = i
138 }
139 }
140 cmd := []string{"var", "title"}
141 if delim != -1 {
142 cmd = args[:delim]
143 args = args[delim+1:]
144 }
145
146 sorted := map[string][]string{}
147 sortedKeys := []string{}
148 for _, f := range args {
149 params := append(cmd, f)
150 out := bytes.NewBuffer(nil)
151 run(os.Args[0], params, globals(), out)
152 val := string(out.Bytes())
153 sorted[val] = append(sorted[val], f)
154 sortedKeys = append(sortedKeys, val)
155 }
156 log.Println(sortedKeys)
157 sort.Strings(sortedKeys)
158 if !asc {
159 }
160
161 list := []string{}
162 for _, k := range sortedKeys {
163 vals := sorted[k]
164 sort.Strings(vals)
165 list = append(list, vals...)
166 }
167 return list
168 }