Mercurial > yakumo_izuru > aya
annotate 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 |
rev | line source |
---|---|
23
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
1 package main |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
2 |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
3 import ( |
25 | 4 "bytes" |
31 | 5 "log" |
25 | 6 "os" |
31 | 7 "path/filepath" |
8 "sort" | |
23
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
9 "strconv" |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
10 "strings" |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
11 "time" |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
12 |
26 | 13 "math" |
14 | |
23
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
15 "github.com/drhodes/golorem" |
25 | 16 "github.com/jaytaylor/html2text" |
23
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
17 ) |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
18 |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
19 // zs var <filename> -- returns list of variables and their values |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
20 // zs var <filename> <var...> -- returns list of variable values |
31 | 21 func Var(args ...string) string { |
23
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
22 if len(args) == 0 { |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
23 return "var: filename expected" |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
24 } else { |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
25 s := "" |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
26 if vars, _, err := md(args[0], globals()); err != nil { |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
27 return "var: " + err.Error() |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
28 } else { |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
29 if len(args) > 1 { |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
30 for _, a := range args[1:] { |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
31 s = s + vars[a] + "\n" |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
32 } |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
33 } else { |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
34 for k, v := range vars { |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
35 s = s + k + ":" + v + "\n" |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
36 } |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
37 } |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
38 } |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
39 return strings.TrimSpace(s) |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
40 } |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
41 } |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
42 |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
43 // zs lorem <n> -- returns <n> random lorem ipsum sentences |
31 | 44 func Lorem(args ...string) string { |
23
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
45 if len(args) > 1 { |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
46 return "lorem: invalid usage" |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
47 } |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
48 if len(args) == 0 { |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
49 return lorem.Paragraph(5, 5) |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
50 } |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
51 if n, err := strconv.Atoi(args[0]); err == nil { |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
52 return lorem.Paragraph(n, n) |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
53 } else { |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
54 return "lorem: " + err.Error() |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
55 } |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
56 } |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
57 |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
58 // zs datefmt <fmt> <date> -- returns formatted date from unix time |
31 | 59 func DateFmt(args ...string) string { |
23
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
60 if len(args) == 0 || len(args) > 2 { |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
61 return "datefmt: invalid usage" |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
62 } |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
63 if n, err := strconv.ParseInt(args[1], 10, 64); err == nil { |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
64 return time.Unix(n, 0).Format(args[0]) |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
65 } else { |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
66 return "datefmt: " + err.Error() |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
67 } |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
68 } |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
69 |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
70 // zs dateparse <fmt> <date> -- returns unix time from the formatted date |
31 | 71 func DateParse(args ...string) string { |
23
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
72 if len(args) == 0 || len(args) > 2 { |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
73 return "dateparse: invalid usage" |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
74 } |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
75 if d, err := time.Parse(args[0], args[1]); err != nil { |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
76 return "dateparse: " + err.Error() |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
77 } else { |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
78 return strconv.FormatInt(d.Unix(), 10) |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
79 } |
40f55059fbfa
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff
changeset
|
80 } |
25 | 81 |
82 // zs wc <file> -- returns word count in the file (markdown, html or amber) | |
31 | 83 func WordCount(args ...string) int { |
25 | 84 if os.Getenv("ZS_RECURSION") != "" { |
85 return 0 | |
86 } | |
87 if len(args) != 1 { | |
88 return 0 | |
89 } | |
90 os.Setenv("ZS_RECURSION", "1") | |
91 out := &bytes.Buffer{} | |
92 if err := build(args[0], out, builtins(), globals()); err != nil { | |
93 return 0 | |
94 } | |
95 if s, err := html2text.FromString(string(out.Bytes())); err != nil { | |
96 return 0 | |
97 } else { | |
98 return len(strings.Fields(s)) | |
99 } | |
100 } | |
101 | |
102 // zs timetoread <file> -- returns number of minutes required to read the text | |
31 | 103 func TimeToRead(args ...string) int { |
104 wc := WordCount(args...) | |
26 | 105 return int(math.Floor(float64(wc)/200.0 + .5)) |
25 | 106 } |
31 | 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 } |