From 88bb4eaf27fec6668293b589dd90e40ae8982068 Mon Sep 17 00:00:00 2001 From: shaoying Date: Fri, 15 Nov 2019 21:26:25 +0800 Subject: [PATCH] add LocalCGI --- src/contexts/ctx/cgi.go | 130 ++++++++++++++++++++++++++++++++++---- src/contexts/ssh/ssh.go | 2 +- src/contexts/web/web.go | 2 +- src/examples/code/code.go | 6 +- src/examples/wiki/wiki.go | 53 ++++++++++------ src/plugin/vim/index.shy | 8 +++ usr/librarys/example.js | 4 ++ 7 files changed, 171 insertions(+), 34 deletions(-) diff --git a/src/contexts/ctx/cgi.go b/src/contexts/ctx/cgi.go index 96e79a98..886ad4fa 100644 --- a/src/contexts/ctx/cgi.go +++ b/src/contexts/ctx/cgi.go @@ -1,11 +1,13 @@ package ctx import ( - "html/template" + "text/template" + "bytes" "io" "path" "strings" + "toolkit" ) func index(name string, arg ...interface{}) interface{} { @@ -75,6 +77,18 @@ func index(name string, arg ...interface{}) interface{} { } var CGI = template.FuncMap{ + "options": func(arg ...interface{}) string { + switch value := index("option", arg...).(type) { + case string: + return value + case []string: + return strings.Join(value, "") + } + return "" + }, + "option": func(arg ...interface{}) interface{} { + return index("option", arg...) + }, "conf": func(arg ...interface{}) interface{} { switch m := arg[0].(type) { case *Message: @@ -90,20 +104,114 @@ var CGI = template.FuncMap{ } return nil }, - "option": func(arg ...interface{}) interface{} { - return index("option", arg...) - }, - "options": func(arg ...interface{}) string { - switch value := index("option", arg...).(type) { - case string: - return value - case []string: - return strings.Join(value, "") + "cmd": func(arg ...interface{}) interface{} { + switch m := arg[0].(type) { + case *Message: + switch c := arg[1].(type) { + case string: + return m.Cmd(c, arg[2:]) + } } - return "" + return nil + }, + "append": func(arg ...interface{}) interface{} { + switch m := arg[0].(type) { + case *Message: + if len(arg) == 1 { + return m.Meta["append"] + } + if len(arg) > 1 { + switch c := arg[1].(type) { + case string: + if len(arg) > 2 { + switch i := arg[2].(type) { + case int: + return kit.Select("", m.Meta[c], i) + } + } + return m.Meta[c] + } + } + } + return nil + }, + "result": func(arg ...interface{}) interface{} { + switch m := arg[0].(type) { + case *Message: + return m.Meta["result"] + } + return nil + }, + "results": func(arg ...interface{}) interface{} { + switch m := arg[0].(type) { + case *Message: + return strings.Join(m.Meta["result"], "") + } + return nil }, } +func LocalCGI(m *Message, c *Context) *template.FuncMap { + cgi := template.FuncMap{ + "table": func(arg ...interface{}) interface{} { + if len(arg) == 0 { + return "" + } + switch msg := arg[0].(type) { + case *Message: + res := []map[string]string{} + msg.Table(func(index int, value map[string]string) { + res = append(res, value) + }) + return res + case string: + sub := m.Spawn() + head := []string{} + for i, l := range strings.Split(strings.TrimSpace(msg), "\n") { + if i == 0 { + head = kit.Split(l, ' ', 100) + continue + } + for j, v := range strings.Split(l, " ") { + sub.Push(head[j], v) + } + } + return sub + } + return nil + }, + "format": func(arg ...interface{}) interface{} { + switch msg := arg[0].(type) { + case *Message: + buffer := bytes.NewBuffer([]byte{}) + tmpl := m.Optionv("tmpl").(*template.Template) + m.Assert(tmpl.ExecuteTemplate(buffer, kit.Select("table", arg, 1), msg)) + return string(buffer.Bytes()) + } + return nil + }, + } + for k, v := range c.Commands { + if strings.HasPrefix(k, "/") || strings.HasPrefix(k, "_") { + continue + } + func(k string, v *Command) { + cgi[k] = func(arg ...interface{}) interface{} { + msg := m.Spawn() + v.Hand(msg, c, k, kit.Trans(arg)...) + + buffer := bytes.NewBuffer([]byte{}) + tmpl := m.Optionv("tmpl").(*template.Template) + m.Assert(tmpl.ExecuteTemplate(buffer, kit.Select("code", "table", len(msg.Meta["append"]) > 0), msg)) + return string(buffer.Bytes()) + } + }(k, v) + } + for k, v := range CGI { + cgi[k] = v + } + return &cgi +} func ExecuteFile(m *Message, w io.Writer, p string) error { tmpl := template.New("render").Funcs(CGI) tmpl.ParseGlob(p) diff --git a/src/contexts/ssh/ssh.go b/src/contexts/ssh/ssh.go index c08e150e..ea3a762a 100644 --- a/src/contexts/ssh/ssh.go +++ b/src/contexts/ssh/ssh.go @@ -450,7 +450,7 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", }) default: // 记录值 - index := kit.Int(arg[2]) - 1 + index := kit.Int(arg[2]) - 1 - m.Confi("flow", []string{m.Option("river"), "data", arg[1], "meta", "offset"}) switch m.Option("format") { case "object": m.Confm("flow", []string{m.Option("river"), "data", arg[1], "list", kit.Format(index)}, func(key string, value string) { diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index 68a04689..23b2a31a 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -11,7 +11,6 @@ import ( "bytes" "encoding/json" "fmt" - "html/template" "io" "io/ioutil" "mime/multipart" @@ -22,6 +21,7 @@ import ( "path/filepath" "strconv" "strings" + "text/template" ) type MUX interface { diff --git a/src/examples/code/code.go b/src/examples/code/code.go index ec1ee78a..2f3ba9c5 100644 --- a/src/examples/code/code.go +++ b/src/examples/code/code.go @@ -232,7 +232,11 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", m.Cmdy("ssh._route", m.Option("dream"), "ssh.data", "update", m.Option("table"), arg[1], arg[3], arg[4]) arg = []string{"list", m.Option("dream"), m.Option("table")} } - m.Cmdy("ssh._route", arg[1], "ssh.data", "show", arg[2:]) + if len(arg) > 1 { + m.Cmdy("ssh._route", arg[1], "ssh.data", "show", arg[2:]) + break + } + m.Cmdy("ssh.data", "show") } return }}, diff --git a/src/examples/wiki/wiki.go b/src/examples/wiki/wiki.go index 955e2c17..9a309d7d 100644 --- a/src/examples/wiki/wiki.go +++ b/src/examples/wiki/wiki.go @@ -9,8 +9,9 @@ import ( "bytes" "encoding/json" - "html/template" "path" + "strings" + "text/template" ) var Index = &ctx.Context{Name: "wiki", Help: "文档中心", @@ -47,9 +48,9 @@ var Index = &ctx.Context{Name: "wiki", Help: "文档中心", }, }, Help: "组件列表"}, - "level": {Name: "level", Value: "local/wiki/自然/编程", Help: "路由数量"}, - "class": {Name: "class", Value: "", Help: "路由数量"}, - "favor": {Name: "favor", Value: "index.md", Help: "路由数量"}, + "level": {Name: "level", Value: "usr/local/wiki", Help: "文档路径"}, + "class": {Name: "class", Value: "", Help: "文档目录"}, + "favor": {Name: "favor", Value: "index.md", Help: "默认文档"}, }, Commands: map[string]*ctx.Command{ "tree": {Name: "tree", Help: "目录", Form: map[string]int{"level": 1, "class": 1}, Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { @@ -61,10 +62,37 @@ var Index = &ctx.Context{Name: "wiki", Help: "文档中心", which := m.Cmdx("nfs.path", path.Join(m.Confx("level"), m.Confx("class", arg, 1), m.Confx("favor", arg, 0))) buffer := bytes.NewBuffer([]byte{}) - template.Must(template.ParseFiles(which)).Funcs(ctx.CGI).Execute(buffer, m) + tmpl := template.New("render").Funcs(*ctx.LocalCGI(m, c)) + + tmpl = template.Must(tmpl.ParseGlob(path.Join(m.Conf("route", "template_dir"), "/*.tmpl"))) + tmpl = template.Must(tmpl.ParseGlob(path.Join(m.Conf("route", "template_dir"), m.Cap("route"), "/*.tmpl"))) + tmpl = template.Must(tmpl.ParseFiles(which)) + + m.Optionv("tmpl", tmpl) + m.Assert(tmpl.ExecuteTemplate(buffer, path.Base(which), m)) m.Echo(string(markdown.ToHTML(buffer.Bytes(), nil, nil))) return }}, + "note": {Name: "note file", Help: "便签", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + if len(arg) > 0 { + m.Cmd(kit.Select("tree", "text", strings.HasSuffix(arg[0], ".md")), arg[0]) + } else { + m.Cmd("tree") + } + return + }}, + "runs": {Name: "run", Help: "便签", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + m.Cmdy(arg).Set("append") + return + }}, + "run": {Name: "run", Help: "便签", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + m.Cmdy(arg) + return + }}, + "time": {Name: "time", Help: "便签", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + m.Cmdy("cli.time", "show").Set("append") + return + }}, "svg": {Name: "svg", Help: "绘图", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { m.Echo(arg[0]) @@ -119,21 +147,6 @@ var Index = &ctx.Context{Name: "wiki", Help: "文档中心", } return }}, - "tip": {Name: "tip action table index ...", Help: "便签", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { - switch arg[0] { - case "show": - if len(arg) == 1 || arg[2] == "" { - m.Cmdy("ssh.data", "show", arg[1]) - } else { - m.Cmdy("ssh.data", "show", arg[1], arg[2]) - } - case "insert": - m.Cmdy("ssh.data", "insert", arg[1], arg[3:]) - case "update": - m.Cmdy("ssh.data", "update", arg[1], arg[2], arg[3:]) - } - return - }}, }, } diff --git a/src/plugin/vim/index.shy b/src/plugin/vim/index.shy index e70b4415..7dd1f79f 100644 --- a/src/plugin/vim/index.shy +++ b/src/plugin/vim/index.shy @@ -1,3 +1,11 @@ +kit note "记事本" public "ssh._route" _ "web.wiki.note" \ + text "" name you imports plugin_you action auto \ + text "" name doc imports plugin_doc view long action auto \ + button "查看" action auto \ + button "返回" cb Last \ + exports doc file \ + feature display inner + kit tips "便签" private "web.code.favor" list \ text "" name dream imports plugin_you action auto \ text "tip" name table imports plugin_vim_table action auto \ diff --git a/usr/librarys/example.js b/usr/librarys/example.js index 49336e76..3d21e924 100644 --- a/usr/librarys/example.js +++ b/usr/librarys/example.js @@ -1493,6 +1493,10 @@ function Output(plugin, type, msg, cb, target, option) { kit.Selector(option, ".args", function(item) { msg.Option(item.name, item.value) }) + if (name == "value") { + name = line.key + id = option.index.value + } plugin.Run(event, [id, meta[item], name, event.target.value], function(msg) { td.innerHTML = event.target.value plugin.ontoast("修改成功")