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