From 028da6be6b518487a2b8c4dbfc8120305560100f Mon Sep 17 00:00:00 2001 From: shaoying Date: Mon, 26 Jul 2021 01:03:40 +0800 Subject: [PATCH] opt wiki --- core/wiki/brief.go | 23 +++++++++++ core/wiki/field.go | 98 ++++++++++++++++++++++++++++++++++++++++++++++ core/wiki/local.go | 32 +++++++++++++++ core/wiki/order.go | 35 +++++++++++++++++ core/wiki/other.go | 29 ++++++++++++++ core/wiki/parse.go | 60 ++++++++++++++++++++++++++++ core/wiki/refer.go | 45 +++++++++++++++++++++ core/wiki/shell.go | 35 +++++++++++++++++ core/wiki/spark.go | 49 +++++++++++++++++++++++ core/wiki/table.go | 65 ++++++++++++++++++++++++++++++ core/wiki/title.go | 82 ++++++++++++++++++++++++++++++++++++++ 11 files changed, 553 insertions(+) create mode 100644 core/wiki/brief.go create mode 100644 core/wiki/field.go create mode 100644 core/wiki/local.go create mode 100644 core/wiki/order.go create mode 100644 core/wiki/other.go create mode 100644 core/wiki/parse.go create mode 100644 core/wiki/refer.go create mode 100644 core/wiki/shell.go create mode 100644 core/wiki/spark.go create mode 100644 core/wiki/table.go create mode 100644 core/wiki/title.go diff --git a/core/wiki/brief.go b/core/wiki/brief.go new file mode 100644 index 00000000..5c719e78 --- /dev/null +++ b/core/wiki/brief.go @@ -0,0 +1,23 @@ +package wiki + +import ( + ice "github.com/shylinux/icebergs" + kit "github.com/shylinux/toolkits" +) + +const BRIEF = "brief" + +func init() { + Index.Merge(&ice.Context{ + Commands: map[string]*ice.Command{ + BRIEF: {Name: "brief [name] text", Help: "摘要", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + _word_template(m, cmd, arg...) + }}, + }, + Configs: map[string]*ice.Config{ + BRIEF: {Name: BRIEF, Help: "摘要", Value: kit.Data( + kit.MDB_TEMPLATE, `

{{.Option "text"}}

`, + )}, + }, + }) +} diff --git a/core/wiki/field.go b/core/wiki/field.go new file mode 100644 index 00000000..0ce7a8ea --- /dev/null +++ b/core/wiki/field.go @@ -0,0 +1,98 @@ +package wiki + +import ( + "path" + "strings" + + ice "github.com/shylinux/icebergs" + "github.com/shylinux/icebergs/base/cli" + kit "github.com/shylinux/toolkits" +) + +func _field_show(m *ice.Message, name, text string, arg ...string) { + // 基本参数 + m.Option(kit.MDB_TYPE, FIELD) + m.Option(kit.MDB_NAME, name) + m.Option(kit.MDB_TEXT, text) + + // 命令参数 + data := kit.Dict(kit.MDB_NAME, name) + cmds := kit.Split(text) + m.Search(cmds[0], func(p *ice.Context, s *ice.Context, key string, cmd *ice.Command) { + data["feature"], data["inputs"] = cmd.Meta, cmd.List + }) + + // 扩展参数 + for i := 0; i < len(arg)-1; i += 2 { + if strings.HasPrefix(arg[i], "args.") { + m.Option(arg[i], strings.TrimSpace(arg[i+1])) + kit.Value(data, arg[i], m.Option(arg[i])) + } else if strings.HasPrefix(arg[i], "args") { + m.Option(arg[i], kit.Split(strings.TrimSuffix(strings.TrimPrefix(arg[i+1], "["), "]"))) + kit.Value(data, arg[i], m.Optionv(arg[i])) + } else { + m.Parse("option", arg[i], arg[i+1]) + kit.Value(data, arg[i], m.Optionv(arg[i])) + } + + switch arg[i] { + case "content": + data[arg[i]] = arg[i+1] + + case "args": + args := kit.Simple(m.Optionv(arg[i])) + + count := 0 + kit.Fetch(data["inputs"], func(index int, value map[string]interface{}) { + if value["_input"] != "button" && value["type"] != "button" { + count++ + } + }) + + if len(args) > count { + list := data["inputs"].([]interface{}) + for i := count; i < len(args); i++ { + list = append(list, kit.Dict( + "_input", "text", "name", "args", "value", args[i], + )) + } + data["inputs"] = list + } + } + } + + // 渲染引擎 + m.Option(kit.MDB_META, data) + m.RenderTemplate(m.Conf(FIELD, kit.Keym(kit.MDB_TEMPLATE))) +} + +const FIELD = "field" + +func init() { + Index.Merge(&ice.Context{ + Commands: map[string]*ice.Command{ + FIELD: {Name: "field [name] cmd", Help: "插件", Action: map[string]*ice.Action{ + cli.RUN: {Name: "run", Help: "执行", Hand: func(m *ice.Message, arg ...string) { + if !m.Warn(!m.Right(arg[1:]), ice.ErrNotRight, arg[1:]) { + m.Cmdy(arg[1:]) + } + }}, + }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + arg = _name(m, arg) + _field_show(m, strings.ReplaceAll(kit.Select(path.Base(arg[1]), arg[0]), " ", "_"), arg[1], arg[2:]...) + }}, + }, + Configs: map[string]*ice.Config{ + FIELD: {Name: FIELD, Help: "插件", Value: kit.Data( + kit.MDB_TEMPLATE, `
+{{.Option "name"}} +
+
+
+
+
`, + )}, + }, + }) +} diff --git a/core/wiki/local.go b/core/wiki/local.go new file mode 100644 index 00000000..34ca9fce --- /dev/null +++ b/core/wiki/local.go @@ -0,0 +1,32 @@ +package wiki + +import ( + ice "github.com/shylinux/icebergs" + "github.com/shylinux/icebergs/base/nfs" + kit "github.com/shylinux/toolkits" +) + +func _local_show(m *ice.Message, name, text string, arg ...string) { + m.Option("input", m.Cmdx(nfs.CAT, text)) + _option(m, LOCAL, name, text, arg...) + m.RenderTemplate(m.Conf(LOCAL, kit.Keym(kit.MDB_TEMPLATE))) +} + +const LOCAL = "local" + +func init() { + Index.Merge(&ice.Context{ + Commands: map[string]*ice.Command{ + LOCAL: {Name: "local [name] file", Help: "文件", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + arg = _name(m, arg) + _local_show(m, arg[0], kit.Select(arg[0], arg[1]), arg[2:]...) + }}, + }, + Configs: map[string]*ice.Config{ + LOCAL: {Name: LOCAL, Help: "文件", Value: kit.Data( + kit.MDB_TEMPLATE, `{{range $index, $value := .Optionv "input"}}{{$value}}{{end}}`, + )}, + }, + }) +} diff --git a/core/wiki/order.go b/core/wiki/order.go new file mode 100644 index 00000000..b8b75848 --- /dev/null +++ b/core/wiki/order.go @@ -0,0 +1,35 @@ +package wiki + +import ( + "strings" + + ice "github.com/shylinux/icebergs" + kit "github.com/shylinux/toolkits" +) + +func _order_show(m *ice.Message, name, text string, arg ...string) { + m.Optionv("list", kit.Split(strings.TrimSpace(text), "\n")) + _option(m, ORDER, name, text, arg...) + m.RenderTemplate(m.Conf(ORDER, kit.Keym(kit.MDB_TEMPLATE))) +} + +const ORDER = "order" + +func init() { + Index.Merge(&ice.Context{ + Commands: map[string]*ice.Command{ + ORDER: {Name: "order [name] `[item \n]...`", Help: "列表", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + arg = _name(m, arg) + _order_show(m, arg[0], kit.Select(arg[0], arg[1]), arg[2:]...) + }}, + }, + Configs: map[string]*ice.Config{ + ORDER: {Name: ORDER, Help: "列表", Value: kit.Data( + kit.MDB_TEMPLATE, ``, + )}, + }, + }) +} diff --git a/core/wiki/other.go b/core/wiki/other.go new file mode 100644 index 00000000..bdce9640 --- /dev/null +++ b/core/wiki/other.go @@ -0,0 +1,29 @@ +package wiki + +import ( + ice "github.com/shylinux/icebergs" + kit "github.com/shylinux/toolkits" +) + +func _other_show(m *ice.Message, name, text string, arg ...string) { + _option(m, OTHER, name, text, arg...) + m.RenderTemplate(m.Conf(OTHER, kit.Keym(kit.MDB_TEMPLATE))) +} + +const OTHER = "other" + +func init() { + Index.Merge(&ice.Context{ + Commands: map[string]*ice.Command{ + OTHER: {Name: "other [name] url", Help: "网页", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + arg = _name(m, arg) + _other_show(m, arg[0], kit.Select(arg[0], arg[1]), arg[2:]...) + }}, + }, + Configs: map[string]*ice.Config{ + OTHER: {Name: FIELD, Help: "网页", Value: kit.Data( + kit.MDB_TEMPLATE, ``, + )}, + }, + }) +} diff --git a/core/wiki/parse.go b/core/wiki/parse.go new file mode 100644 index 00000000..cc87df3b --- /dev/null +++ b/core/wiki/parse.go @@ -0,0 +1,60 @@ +package wiki + +import ( + "net/url" + "strings" + + ice "github.com/shylinux/icebergs" + "github.com/shylinux/icebergs/base/mdb" + kit "github.com/shylinux/toolkits" +) + +const PARSE = "parse" + +func init() { + Index.Merge(&ice.Context{ + Commands: map[string]*ice.Command{ + PARSE: {Name: "parse type=auto,json,http,form,list auto text:textarea", Help: "解析", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + if len(arg) < 2 { + return + } + if arg[0] == "auto" && (strings.HasPrefix(arg[1], "{") || strings.HasPrefix(arg[1], "[")) { + arg[0] = "json" + } else if strings.HasPrefix(arg[1], "http") { + arg[0] = "http" + } else if strings.Contains(arg[1], "=") { + arg[0] = "form" + } else { + arg[0] = "list" + } + + m.Option(mdb.FIELDS, mdb.DETAIL) + switch arg[0] { + case "json": + m.Echo(kit.Formats(kit.UnMarshal(arg[1]))) + case "http": + u, _ := url.Parse(arg[1]) + for k, v := range u.Query() { + for _, v := range v { + m.Push(k, v) + } + } + m.EchoQRCode(arg[1]) + + case "form": + for _, v := range kit.Split(arg[1], "&", "&", "&") { + ls := kit.Split(v, "=", "=", "=") + key, _ := url.QueryUnescape(ls[0]) + value, _ := url.QueryUnescape(kit.Select("", ls, 1)) + m.Push(key, value) + } + case "list": + for i, v := range kit.Split(arg[1]) { + m.Push(kit.Format(i), v) + } + } + }}, + }, + Configs: map[string]*ice.Config{}, + }) +} diff --git a/core/wiki/refer.go b/core/wiki/refer.go new file mode 100644 index 00000000..f8fb548c --- /dev/null +++ b/core/wiki/refer.go @@ -0,0 +1,45 @@ +package wiki + +import ( + "path" + "strings" + + ice "github.com/shylinux/icebergs" + kit "github.com/shylinux/toolkits" +) + +func _refer_show(m *ice.Message, name, text string, arg ...string) { + list := [][]string{} + for _, v := range kit.Split(strings.TrimSpace(text), "\n", "\n") { + if ls := kit.Split(v); len(ls) == 1 { + list = append(list, []string{path.Base(ls[0]), ls[0]}) + } else { + list = append(list, ls) + } + } + m.Optionv("list", list) + + _option(m, REFER, name, text, arg...) + m.RenderTemplate(m.Conf(REFER, kit.Keym(kit.MDB_TEMPLATE))) +} + +const REFER = "refer" + +func init() { + Index.Merge(&ice.Context{ + Commands: map[string]*ice.Command{ + REFER: {Name: "refer [name] `[name url]...`", Help: "参考", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + arg = _name(m, arg) + _refer_show(m, arg[0], arg[1], arg[2:]...) + }}, + }, + Configs: map[string]*ice.Config{ + REFER: {Name: REFER, Help: "参考", Value: kit.Data( + kit.MDB_TEMPLATE, ``, + )}, + }, + }) +} diff --git a/core/wiki/shell.go b/core/wiki/shell.go new file mode 100644 index 00000000..017ca366 --- /dev/null +++ b/core/wiki/shell.go @@ -0,0 +1,35 @@ +package wiki + +import ( + ice "github.com/shylinux/icebergs" + "github.com/shylinux/icebergs/base/cli" + kit "github.com/shylinux/toolkits" +) + +func _shell_show(m *ice.Message, name, text string, arg ...string) { + m.Option("output", m.Cmdx(cli.SYSTEM, "sh", "-c", m.Option("input", text))) + _option(m, SHELL, name, text, arg...) + m.RenderTemplate(m.Conf(SHELL, kit.Keym(kit.MDB_TEMPLATE))) +} + +const SHELL = "shell" + +func init() { + Index.Merge(&ice.Context{ + Commands: map[string]*ice.Command{ + SHELL: {Name: "shell [name] cmd", Help: "命令", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + arg = _name(m, arg) + _shell_show(m, arg[0], kit.Select(arg[0], arg[1]), arg[2:]...) + }}, + }, + Configs: map[string]*ice.Config{ + SHELL: {Name: SHELL, Help: "命令", Value: kit.Data( + kit.MDB_TEMPLATE, `$ {{.Option "input"}} # {{.Option "name"}} +{{.Option "output"}}`, + )}, + }, + }) +} diff --git a/core/wiki/spark.go b/core/wiki/spark.go new file mode 100644 index 00000000..312ea2d4 --- /dev/null +++ b/core/wiki/spark.go @@ -0,0 +1,49 @@ +package wiki + +import ( + "strings" + + ice "github.com/shylinux/icebergs" + kit "github.com/shylinux/toolkits" +) + +func _spark_show(m *ice.Message, name, text string, arg ...string) { + if name == "" { + _wiki_template(m, SPARK, name, text, arg...) + return + } + + prompt := kit.Select(name+"> ", m.Conf(SPARK, kit.Keym("prompt", name))) + m.Echo(`
`, name) + for _, l := range strings.Split(text, "\n") { + m.Echo("
") + m.Echo("") + m.Echo("").Echo(l).Echo("") + m.Echo("
") + } + m.Echo("
") +} + +const SPARK = "spark" + +func init() { + Index.Merge(&ice.Context{ + Commands: map[string]*ice.Command{ + SPARK: {Name: "spark [name] text", Help: "段落", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + if len(arg) == 0 { + m.Echo(`
`) + return + } + + arg = _name(m, arg) + _spark_show(m, arg[0], strings.TrimSpace(arg[1]), arg[2:]...) + }}, + }, + Configs: map[string]*ice.Config{ + SPARK: {Name: SPARK, Help: "段落", Value: kit.Data( + kit.MDB_TEMPLATE, `

{{.Option "text"}}

`, + "prompt", kit.Dict("shell", "$ "), + )}, + }, + }) +} diff --git a/core/wiki/table.go b/core/wiki/table.go new file mode 100644 index 00000000..93d07dce --- /dev/null +++ b/core/wiki/table.go @@ -0,0 +1,65 @@ +package wiki + +import ( + "strings" + + ice "github.com/shylinux/icebergs" + kit "github.com/shylinux/toolkits" +) + +func _table_show(m *ice.Message, name, text string, arg ...string) { + head, list := []string{}, [][]string{} + for i, v := range kit.Split(strings.TrimSpace(text), "\n") { + if v = strings.ReplaceAll(v, "%", "%%"); i == 0 { + head = kit.Split(v) + } else { + line := kit.Split(v) + for i, v := range line { + if ls := kit.Split(v); len(ls) > 1 { + style := []string{} + for i := 1; i < len(ls)-1; i += 2 { + switch ls[i] { + case "bg": + ls[i] = "background-color" + case "fg": + ls[i] = "color" + } + style = append(style, ls[i]+":"+ls[i+1]) + } + line[i] = kit.Format(`%s`, strings.Join(style, ";"), ls[0]) + } + } + list = append(list, line) + } + } + m.Optionv("head", head) + m.Optionv("list", list) + + _option(m, TABLE, name, text, arg...) + m.RenderTemplate(m.Conf(TABLE, kit.Keym(kit.MDB_TEMPLATE))) +} + +const TABLE = "table" + +func init() { + Index.Merge(&ice.Context{ + Commands: map[string]*ice.Command{ + TABLE: {Name: "table [name] `[item item\n]...`", Help: "表格", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + arg = _name(m, arg) + _table_show(m, arg[0], kit.Select(arg[0], arg[1]), arg[2:]...) + }}, + }, + Configs: map[string]*ice.Config{ + TABLE: {Name: TABLE, Help: "表格", Value: kit.Data( + kit.MDB_TEMPLATE, ` +{{range $i, $v := .Optionv "head"}}{{end}} +{{range $index, $value := .Optionv "list"}} +{{range $i, $v := $value}}{{end}} +{{end}} +
{{$v}}
{{$v}}
`, + )}, + }, + }) +} diff --git a/core/wiki/title.go b/core/wiki/title.go new file mode 100644 index 00000000..0b1c7775 --- /dev/null +++ b/core/wiki/title.go @@ -0,0 +1,82 @@ +package wiki + +import ( + "fmt" + "strings" + + ice "github.com/shylinux/icebergs" + kit "github.com/shylinux/toolkits" +) + +func _title_show(m *ice.Message, kind, text string, arg ...string) { + title, _ := m.Optionv(TITLE).(map[string]int) + switch kind { + case PREMENU: // 前置目录 + m.RenderTemplate(premenu) + return + + case ENDMENU: // 后置目录 + m.RenderTemplate(endmenu) + return + + case SECTION: // 分节标题 + title[SECTION]++ + m.Option("level", "h3") + m.Option("prefix", fmt.Sprintf("%d.%d ", title[CHAPTER], title[SECTION])) + + case CHAPTER: // 章节标题 + title[CHAPTER]++ + title[SECTION] = 0 + m.Option("level", "h2") + m.Option("prefix", fmt.Sprintf("%d ", title[CHAPTER])) + + default: // 文章标题 + m.Option("level", "h1") + m.Option("prefix", "") + } + + // 添加目录 + menu, _ := m.Optionv("menu").(map[string]interface{}) + menu["list"] = append(menu["list"].([]interface{}), map[string]interface{}{ + "level": m.Option("level"), "prefix": m.Option("prefix"), "content": m.Option("content", text), + }) + + // 渲染引擎 + _wiki_template(m, TITLE, "", text, arg...) +} + +const ( + PREMENU = "premenu" + CHAPTER = "chapter" + SECTION = "section" + ENDMENU = "endmenu" +) + +const TITLE = "title" + +func init() { + Index.Merge(&ice.Context{ + Commands: map[string]*ice.Command{ + TITLE: {Name: "title [premenu|chapter|section|endmenu] text", Help: "标题", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + if len(arg) == 0 { + ns := strings.Split(ice.Info.NodeName, "-") + arg = append(arg, kit.Select(ns[len(ns)-1], "")) + } + if len(arg) == 1 { + arg = append(arg, "") + } + _title_show(m, arg[0], kit.Select(arg[0], arg[1]), arg[2:]...) + }}, + }, + Configs: map[string]*ice.Config{ + TITLE: {Name: TITLE, Help: "标题", Value: kit.Data( + kit.MDB_TEMPLATE, `<{{.Option "level"}} {{.OptionTemplate}}>{{.Option "prefix"}} {{.Option "content"}}`, + )}, + }, + }) +} + +var premenu = `` +var endmenu = ``