From e3c4301baf340b2fb37a7af8be87176ef81ed36f Mon Sep 17 00:00:00 2001 From: shaoying Date: Sun, 17 Nov 2019 01:50:00 +0800 Subject: [PATCH] add wiki.index --- etc/conf/auto.sh | 2 +- src/contexts/ctx/cgi.go | 31 +++++++--- src/examples/wiki/wiki.go | 116 +++++++++++++++++++++++++---------- src/plugin/context/index.shy | 1 + usr/librarys/example.js | 2 +- usr/template/wiki/wiki.tmpl | 14 ++--- 6 files changed, 115 insertions(+), 51 deletions(-) diff --git a/etc/conf/auto.sh b/etc/conf/auto.sh index 74e02241..cce24361 100644 --- a/etc/conf/auto.sh +++ b/etc/conf/auto.sh @@ -91,7 +91,7 @@ Shy() { } ShyLogout() { - echo ${ctx_goodbye} && sleep 1 && [ "$ctx_sid" != "" ] && Shy logout + echo ${ctx_goodbye} && [ "$ctx_sid" != "" ] && Shy logout } ShyLogin() { HOST=`hostname` ctx_sid=`ShyPost cmd login pid "$$" pane "${TMUX_PANE}" hostname "${HOST}" username "${USER}"` diff --git a/src/contexts/ctx/cgi.go b/src/contexts/ctx/cgi.go index fd87f1d0..72f84a2f 100644 --- a/src/contexts/ctx/cgi.go +++ b/src/contexts/ctx/cgi.go @@ -28,7 +28,11 @@ func index(name string, arg ...interface{}) interface{} { } case string: if len(arg) == 2 { - return m.Optionv(value) + if name == "option" { + return m.Optionv(value) + } else { + return m.Append(value) + } } switch val := arg[2].(type) { @@ -114,6 +118,15 @@ var CGI = template.FuncMap{ } return nil }, + "appends": func(arg ...interface{}) interface{} { + switch value := index("append", arg...).(type) { + case string: + return value + case []string: + return strings.Join(value, "") + } + return "" + }, "append": func(arg ...interface{}) interface{} { switch m := arg[0].(type) { case *Message: @@ -184,14 +197,16 @@ func LocalCGI(m *Message, c *Context) *template.FuncMap { continue } func(k string, v *Command) { - cgi[k] = func(arg ...interface{}) interface{} { - msg := m.Spawn() - v.Hand(msg, c, k, kit.Trans(arg)...) + cgi[k] = func(arg ...interface{}) (res interface{}) { + m.TryCatch(m.Spawn(), true, func(msg *Message) { + 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()) + buffer := bytes.NewBuffer([]byte{}) + m.Assert(m.Optionv("tmpl").(*template.Template).ExecuteTemplate(buffer, + kit.Select(kit.Select("code", "table", len(msg.Meta["append"]) > 0), msg.Option("render")), msg)) + res = string(buffer.Bytes()) + }) + return } }(k, v) } diff --git a/src/examples/wiki/wiki.go b/src/examples/wiki/wiki.go index 3db9aa13..13335189 100644 --- a/src/examples/wiki/wiki.go +++ b/src/examples/wiki/wiki.go @@ -10,6 +10,7 @@ import ( "bytes" "encoding/json" "path" + "strconv" "strings" "text/template" ) @@ -57,6 +58,16 @@ var Index = &ctx.Context{Name: "wiki", Help: "文档中心", "node": map[string]interface{}{}, "head": map[string]interface{}{}, }, Help: "故事会"}, + + "template": {Name: "template", Value: map[string]interface{}{ + "list": []interface{}{ + `{{define "raw"}}{{.|results}}{{end}}`, + `{{define "title"}}{{.|results}}{{end}}`, + `{{define "chapter"}}{{.|results}}{{end}}`, + `{{define "section"}}{{.|results}}{{end}}`, + `{{define "block"}}
{{.|results}}
{{end}}`, + }, + }, 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) { @@ -67,19 +78,30 @@ var Index = &ctx.Context{Name: "wiki", Help: "文档中心", "text": {Name: "text", Help: "文章", Form: map[string]int{"level": 1, "class": 1, "favor": 1}, Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { which := m.Cmdx("nfs.path", path.Join(m.Confx("level"), m.Confx("class", arg, 1), m.Confx("favor", arg, 0))) - buffer := bytes.NewBuffer([]byte{}) tmpl := template.New("render").Funcs(*ctx.LocalCGI(m, c)) - + m.Confm("template", "list", func(index int, value string) { tmpl = template.Must(tmpl.Parse(value)) }) 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)) - + for i, v := range tmpl.Templates() { + m.Log("fuck", "%v, %v", i, v.Name()) + } + m.Optionv("title", map[string]int{}) m.Optionv("tmpl", tmpl) + m.Option("render", "") + + buffer := bytes.NewBuffer([]byte{}) m.Assert(tmpl.ExecuteTemplate(buffer, m.Option("filename", path.Base(which)), m)) - m.Echo(string(markdown.ToHTML(buffer.Bytes(), nil, nil))) + data := markdown.ToHTML(buffer.Bytes(), nil, nil) + m.Echo(string(data)) + if f, p, e := kit.Create(path.Join("var/tmp/file", which)); e == nil { + defer f.Close() + f.Write(data) + m.Log("info", "save %v", p) + } return }}, - "note": {Name: "note file", Help: "便签", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + "note": {Name: "note file|favor|commit", Help: "笔记", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { if len(arg) == 0 { m.Cmd("tree") return @@ -93,7 +115,8 @@ var Index = &ctx.Context{Name: "wiki", Help: "文档中心", } return }}, - "story": {Name: "story commit story scene enjoy happy", Help: "故事会", Hand: func(m *ctx.Message, c *ctx.Context, cmd string, arg ...string) (e error) { + + "story": {Name: "story favor|commit|branch|remote story scene enjoy happy", Help: "故事会", Hand: func(m *ctx.Message, c *ctx.Context, cmd string, arg ...string) (e error) { switch arg[0] { case "favor": if len(arg) < 4 { @@ -159,34 +182,31 @@ var Index = &ctx.Context{Name: "wiki", Help: "文档中心", } return }}, - "table": {Name: "table", Help: "表格", Hand: func(m *ctx.Message, c *ctx.Context, cmd string, arg ...string) (e error) { - switch len(arg) { - case 0: - return - case 2: - if arg[1] != "head" { - break - } - fallthrough - case 1: - arg = []string{arg[0], "head", kit.Hashs(m.Option("filename"), arg[1])} - fallthrough - default: - switch arg[1] { - case "name": - arg = []string{arg[0], "head", kit.Hashs(m.Option("filename"), arg[2])} - fallthrough - case "head": - arg = []string{arg[0], "node", m.Conf("story", []string{"head", arg[2], "node"})} - fallthrough - case "node": - arg = []string{arg[0], "data", m.Conf("story", []string{"node", arg[2], "data"})} - fallthrough - case "data": - arg = []string{arg[0], m.Conf("story", []string{"data", arg[2]})} - } + "index": {Name: "index name|hash", Help: "索引", Hand: func(m *ctx.Message, c *ctx.Context, cmd string, arg ...string) (e error) { + scene := "" + if hash := m.Conf("story", []string{"head", kit.Hashs(m.Option("filename"), arg[0]), "node"}); hash != "" { + arg[0] = hash + } else if hash := m.Conf("story", []string{"head", arg[0], "node"}); hash != "" { + arg[0] = hash + } + if hash := m.Conf("story", []string{"node", arg[0], "data"}); hash != "" { + scene = m.Conf("story", []string{"node", arg[0], "scene"}) + arg[0] = hash + } + if data := m.Conf("story", []string{"data", arg[0]}); data != "" { + arg[0] = data + } + if scene != "" { + m.Cmdy(scene, "", arg[0]) + } else { + m.Echo(arg[0]) + } + return + }}, + "table": {Name: "table name data", Help: "表格", Hand: func(m *ctx.Message, c *ctx.Context, cmd string, arg ...string) (e error) { + if len(arg) < 2 { + return } - m.Option("scene", cmd) m.Option("enjoy", arg[0]) m.Option("happy", arg[1]) @@ -203,6 +223,35 @@ var Index = &ctx.Context{Name: "wiki", Help: "文档中心", } return }}, + "refer": {Name: "refer", Help: "链接地址", Hand: func(m *ctx.Message, c *ctx.Context, cmd string, arg ...string) (e error) { + m.Set("option", "render", "raw").Echo("%s: %s", arg[0], arg[1]) + return + }}, + + "title": {Name: "title text", Help: "一级标题", Hand: func(m *ctx.Message, c *ctx.Context, cmd string, arg ...string) (e error) { + m.Set("option", "render", cmd).Echo(kit.Select("", arg, 0)) + return + }}, + "chapter": {Name: "chaper text", Help: "二级标题", Hand: func(m *ctx.Message, c *ctx.Context, cmd string, arg ...string) (e error) { + prefix := "" + if title, ok := m.Optionv("title").(map[string]int); ok { + title["chapter"]++ + title["section"] = 0 + prefix = strconv.Itoa(title["chapter"]) + " " + } + m.Set("option", "render", cmd).Echo(prefix + kit.Select("", arg, 0)) + return + }}, + "section": {Name: "section text", Help: "三级标题", Hand: func(m *ctx.Message, c *ctx.Context, cmd string, arg ...string) (e error) { + prefix := "" + if title, ok := m.Optionv("title").(map[string]int); ok { + title["section"]++ + prefix = strconv.Itoa(title["chapter"]) + "." + strconv.Itoa(title["section"]) + " " + } + m.Set("option", "render", cmd).Echo(prefix + kit.Select("", arg, 0)) + 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 @@ -220,7 +269,6 @@ var Index = &ctx.Context{Name: "wiki", Help: "文档中心", m.Echo(arg[0]) return }}, - "xls": {Name: "xls", Help: "表格", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { switch len(arg) { case 0: diff --git a/src/plugin/context/index.shy b/src/plugin/context/index.shy index d840c1d9..f588dff1 100644 --- a/src/plugin/context/index.shy +++ b/src/plugin/context/index.shy @@ -4,6 +4,7 @@ kit project "项目" private \ text "" name see imports plugin_see action auto \ button "执行" \ button "返回" cb Last \ + feature detail "加载" "复制" "下载" \ exports see name see kit compile "编译" private \ diff --git a/usr/librarys/example.js b/usr/librarys/example.js index adc12244..0077c68d 100644 --- a/usr/librarys/example.js +++ b/usr/librarys/example.js @@ -1533,7 +1533,7 @@ function Output(plugin, type, msg, cb, target, option) { target.innerHTML = "", plugin.onfigure.meta.max(target) output.onimport.meta._table(msg, msg.append) || kit.OrderCode(kit.ModifyNode(target, msg.result.join(""))) kit._call(cb, [msg]) - kit.Selector(target, ".commit", function(item) { + kit.Selector(target, "table.story", function(item) { var data = item.dataset item.oncontextmenu = function(event) { plugin.oncarte(event, shy("", { diff --git a/usr/template/wiki/wiki.tmpl b/usr/template/wiki/wiki.tmpl index 00ed7226..fb1ea499 100644 --- a/usr/template/wiki/wiki.tmpl +++ b/usr/template/wiki/wiki.tmpl @@ -1,11 +1,11 @@ -{{define "table"}}{{range $i, $line := .|trans}} -{{if eq $i 0}} -{{range $i, $v := $line}}{{end}} -{{else}} -{{range $i, $v := $line}}{{end}} -{{end}} +{{define "table"}}
{{$v}}
{{$v}}
{{range $i, $line := .|trans}} + {{if eq $i 0}} + {{range $i, $v := $line}}{{end}} + {{else}} + {{range $i, $v := $line}}{{end}} + {{end}} {{end}}
{{$v}}
{{$v}}
{{end}} {{define "code"}}
{{results .}}
{{end}}