Changeset 32 in code for trunk/zs_ext.go


Ignore:
Timestamp:
Sep 2, 2015, 2:54:16 PM (10 years ago)
Author:
zaitsev.serge
Message:

dead end with template functions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/zs_ext.go

    r27 r32  
    33import (
    44        "bytes"
     5        "log"
    56        "os"
     7        "path/filepath"
     8        "sort"
    69        "strconv"
    710        "strings"
     
    1619// zs var <filename> -- returns list of variables and their values
    1720// zs var <filename> <var...> -- returns list of variable values
    18 func Var(args []string) string {
     21func Var(args ...string) string {
    1922        if len(args) == 0 {
    2023                return "var: filename expected"
     
    3942
    4043// zs lorem <n> -- returns <n> random lorem ipsum sentences
    41 func Lorem(args []string) string {
     44func Lorem(args ...string) string {
    4245        if len(args) > 1 {
    4346                return "lorem: invalid usage"
     
    5457
    5558// zs datefmt <fmt> <date> -- returns formatted date from unix time
    56 func DateFmt(args []string) string {
     59func DateFmt(args ...string) string {
    5760        if len(args) == 0 || len(args) > 2 {
    5861                return "datefmt: invalid usage"
     
    6669
    6770// zs dateparse <fmt> <date> -- returns unix time from the formatted date
    68 func DateParse(args []string) string {
     71func DateParse(args ...string) string {
    6972        if len(args) == 0 || len(args) > 2 {
    7073                return "dateparse: invalid usage"
     
    7881
    7982// zs wc <file> -- returns word count in the file (markdown, html or amber)
    80 func WordCount(args []string) int {
     83func WordCount(args ...string) int {
    8184        if os.Getenv("ZS_RECURSION") != "" {
    8285                return 0
     
    98101
    99102// zs timetoread <file> -- returns number of minutes required to read the text
    100 func TimeToRead(args []string) int {
    101         wc := WordCount(args)
     103func TimeToRead(args ...string) int {
     104        wc := WordCount(args...)
    102105        return int(math.Floor(float64(wc)/200.0 + .5))
    103106}
     107
     108// zs ls <dir> <regexp>
     109func 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...>
     133func 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}
Note: See TracChangeset for help on using the changeset viewer.