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