Mercurial > yakumo_izuru > aya
annotate 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 |
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" |
5 "os" | |
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
|
6 "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
|
7 "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
|
8 "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
|
9 |
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 "github.com/drhodes/golorem" |
25 | 11 "github.com/google/gxui/math" |
12 "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
|
13 ) |
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
|
14 |
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 // 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
|
16 // zs var <filename> <var...> -- returns list of variable 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
|
17 func Var(args []string) string { |
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 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
|
19 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
|
20 } 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
|
21 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
|
22 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
|
23 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
|
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 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
|
26 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
|
27 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
|
28 } |
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 } 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
|
30 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
|
31 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
|
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 } |
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 } |
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 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
|
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 // zs lorem <n> -- returns <n> random lorem ipsum sentences |
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 func Lorem(args []string) string { |
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 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
|
42 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
|
43 } |
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
|
44 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
|
45 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
|
46 } |
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 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
|
48 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
|
49 } 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
|
50 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
|
51 } |
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 } |
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 |
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 // zs datefmt <fmt> <date> -- returns formatted date from unix 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
|
55 func DateFmt(args []string) string { |
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 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
|
57 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
|
58 } |
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
|
59 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
|
60 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
|
61 } 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
|
62 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
|
63 } |
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 } |
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 |
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 // zs dateparse <fmt> <date> -- returns unix time from the formatted date |
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 func DateParse(args []string) string { |
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 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
|
69 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
|
70 } |
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
|
71 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
|
72 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
|
73 } 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
|
74 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
|
75 } |
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 } |
25 | 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 } |