From 87bb0d8dbcd1b3ba6ec127e51235853c606b4e27 Mon Sep 17 00:00:00 2001 From: harveyshao Date: Sat, 5 Nov 2022 14:46:07 +0800 Subject: [PATCH] opt wiki --- base/lex/split.go | 2 ++ core/wiki/audio.go | 7 ++++-- core/wiki/brief.go | 2 +- core/wiki/chart.go | 55 +++++++++++----------------------------------- core/wiki/data.go | 5 +++-- core/wiki/draw.go | 5 ++--- core/wiki/field.go | 16 +++++--------- core/wiki/image.go | 2 +- core/wiki/order.go | 4 +--- core/wiki/parse.go | 2 +- core/wiki/spark.go | 4 ++-- core/wiki/table.go | 52 +++++++++++++++++++++++-------------------- core/wiki/title.go | 20 ++++++++--------- core/wiki/video.go | 4 ++-- core/wiki/wiki.go | 1 + core/wiki/wiki.shy | 10 ++++----- core/wiki/word.go | 4 ++-- 17 files changed, 83 insertions(+), 112 deletions(-) diff --git a/base/lex/split.go b/base/lex/split.go index 8b27aa10..6416186a 100644 --- a/base/lex/split.go +++ b/base/lex/split.go @@ -66,6 +66,8 @@ func _split_list(m *ice.Message, file string, arg ...string) ice.Map { ls = cb(ls, data) case func([]string): cb(ls) + case func([]string) []string: + ls = cb(ls) case nil: default: m.ErrorNotImplement(cb) diff --git a/core/wiki/audio.go b/core/wiki/audio.go index 96ab287c..f86349df 100644 --- a/core/wiki/audio.go +++ b/core/wiki/audio.go @@ -5,12 +5,15 @@ import ( "shylinux.com/x/icebergs/base/nfs" ) +const ( + M4A = "m4a" +) const AUDIO = "audio" func init() { Index.MergeCommands(ice.Commands{ - AUDIO: {Name: "audio url run", Help: "音频", Actions: WordAction( - ``, nfs.PATH, ice.USR_LOCAL_IMAGE, + AUDIO: {Name: "audio url", Help: "音频", Actions: WordAction( + ``, nfs.PATH, ice.USR_LOCAL_IMAGE, ), Hand: func(m *ice.Message, arg ...string) { _image_show(m, arg[0], arg[1:]...) }}, }) } diff --git a/core/wiki/brief.go b/core/wiki/brief.go index 8ff386f7..52acaaa3 100644 --- a/core/wiki/brief.go +++ b/core/wiki/brief.go @@ -8,7 +8,7 @@ const BRIEF = "brief" func init() { Index.MergeCommands(ice.Commands{ - BRIEF: {Name: "brief text run", Help: "摘要", Actions: WordAction( + BRIEF: {Name: "brief text", Help: "摘要", Actions: WordAction( `

{{.Option "text"}}

`, ), Hand: func(m *ice.Message, arg ...string) { _wiki_template(m, "", arg[0], arg[1:]...) }}, }) diff --git a/core/wiki/chart.go b/core/wiki/chart.go index 32d4765d..9ad19310 100644 --- a/core/wiki/chart.go +++ b/core/wiki/chart.go @@ -56,16 +56,6 @@ func (g *Group) Option(group string, key string, arg ...ice.Any) string { } func (g *Group) Get(group string) *ice.Message { return g.list[group] } -func (g *Group) Join(arg ...string) string { - res := []string{} - for i := 0; i < len(arg)-1; i += 2 { - if arg[i] == "" { - continue - } - res = append(res, kit.Format(`%s="%s"`, arg[i], arg[i+1])) - } - return kit.Join(res, ice.SP) -} func (g *Group) Echo(group string, str string, arg ...ice.Any) *ice.Message { return g.Get(group).Echo(str, arg...) } @@ -76,57 +66,42 @@ func (g *Group) EchoLine(group string, x1, y1, x2, y2 int) *ice.Message { return g.Echo(group, "", x1, y1, x2, y2) } func (g *Group) EchoRect(group string, height, width, x, y int, arg ...string) *ice.Message { // rx ry - return g.Echo(group, ``, height, width, kit.Select("4", arg, 0), kit.Select("4", arg, 1), x, y, g.Join(kit.Slice(arg, 2)...)) + return g.Echo(group, ``, + height, width, kit.Select("4", arg, 0), kit.Select("4", arg, 1), x, y, kit.JoinKV(ice.EQ, ice.SP, kit.Slice(arg, 2)...)) } func (g *Group) EchoText(group string, x, y int, text string, arg ...string) *ice.Message { - if text == "" { - return g.Get(group) - } - return g.Echo(group, "%s", x, y, g.Join(arg...), text) + return g.Echo(group, "%s", x, y, kit.JoinKV(ice.EQ, ice.SP, arg...), text) } func (g *Group) EchoTexts(group string, x, y int, text string, arg ...string) *ice.Message { m := g.Get(group) - float := kit.Int(kit.Select("2", "7", kit.Contains(m.Option(ice.MSG_USERUA), "iPhone"))) - if strings.Contains(m.Option(ice.MSG_USERUA), "Chrome") || strings.Contains(m.Option(ice.MSG_USERUA), "Mobile") { - } else { - float += 4 - } + float := kit.Int(kit.Select("6", "2", strings.Contains(m.Option(ice.MSG_USERUA), "Chrome") || (strings.Contains(m.Option(ice.MSG_USERUA), "Mobile") && !kit.Contains(m.Option(ice.MSG_USERUA), "iPhone")))) return g.EchoText(group, x, y+float, text, arg...) } +func (g *Group) EchoArrowLine(group string, x1, y1, x2, y2 int, arg ...string) *ice.Message { // marker-end + return g.Echo(group, "", x1, y1, x2, y2, kit.Select("arrowhead", arg, 0)) +} func (g *Group) DefsArrow(group string, height, width int, arg ...string) *ice.Message { // name return g.Echo(group, ` `, kit.Select("arrowhead", arg, 0), height, width, height/2, width, height/2, height) } -func (g *Group) EchoArrowLine(group string, x1, y1, x2, y2 int, arg ...string) *ice.Message { // marker-end - return g.Echo(group, "", x1, y1, x2, y2, kit.Select("arrowhead", arg, 0)) -} func (g *Group) Dump(m *ice.Message, group string, arg ...string) *Group { item := NewItem("").Dump(m).Copy(g.Get(group)).Echo("") return g } -func (g *Group) DumpAll(m *ice.Message, group ...string) *Group { +func (g *Group) DumpAll(m *ice.Message, group ...string) { for _, grp := range group { g.Dump(m, grp) } - return g } type Chart interface { Init(*ice.Message, ...string) Chart Draw(*ice.Message, int, int) Chart - Data(*ice.Message, ice.Any) Chart GetHeight(...string) int GetWidth(...string) int } @@ -159,12 +134,12 @@ const ( FG = "fg" BG = "bg" + FONT_SIZE = "font-size" + FONT_FAMILY = "font-family" STROKE_DASHARRAY = "stroke-dasharray" STROKE_WIDTH = "stroke-width" STROKE = "stroke" FILL = "fill" - FONT_SIZE = "font-size" - FONT_FAMILY = "font-family" PADDING = "padding" MARGINX = "marginx" @@ -181,14 +156,10 @@ const CHART = "chart" func init() { Index.MergeCommands(ice.Commands{ - CHART: {Name: "chart type=label,chain,sequence auto text", Help: "图表", Actions: WordAction( + CHART: {Name: "chart type=label,chain,sequence run text", Help: "图表", Actions: WordAction( ``, - ), Hand: func(m *ice.Message, arg ...string) { - if len(arg) > 1 { - _chart_show(m, arg[0], strings.TrimSpace(arg[1]), arg[2:]...) - } - }}, + ), Hand: func(m *ice.Message, arg ...string) { _chart_show(m, arg[0], strings.TrimSpace(arg[1]), arg[2:]...) }}, }) } diff --git a/core/wiki/data.go b/core/wiki/data.go index c89ebcf5..e8815c62 100644 --- a/core/wiki/data.go +++ b/core/wiki/data.go @@ -23,14 +23,15 @@ func init() { } }}, mdb.CREATE: {Name: "create path fields value", Hand: func(m *ice.Message, arg ...string) { - m.Cmd("", nfs.SAVE, m.Option(nfs.PATH), kit.Join(kit.Split(m.Option("fields")), ice.FS)+ice.NL+kit.Join(kit.Split(m.Option(mdb.VALUE)))+ice.NL) + m.Cmd("", nfs.SAVE, m.Option(nfs.PATH), kit.Join(kit.Split(m.Option("fields")))+ice.NL+kit.Join(kit.Split(m.Option(mdb.VALUE)))+ice.NL) }}, nfs.PUSH: {Name: "push path record", Help: "添加", Hand: func(m *ice.Message, arg ...string) { m.Cmd(nfs.PUSH, path.Join(m.Config(nfs.PATH), arg[0]), kit.Join(arg[1:], ice.FS)+ice.NL) }}, + "draw": {Name: "draw", Help: "绘图"}, }, WikiAction(ice.USR_LOCAL_EXPORT, nfs.CSV)), Hand: func(m *ice.Message, arg ...string) { if !_wiki_list(m, arg...) { - CSV(m, m.Cmd(nfs.CAT, arg[0]).Result()).StatusTimeCount() + CSV(m, m.Cmdx(nfs.CAT, arg[0])).StatusTimeCount() } }}, }) diff --git a/core/wiki/draw.go b/core/wiki/draw.go index b7312297..dc2f763e 100644 --- a/core/wiki/draw.go +++ b/core/wiki/draw.go @@ -14,13 +14,12 @@ const DRAW = "draw" func init() { Index.MergeCommands(ice.Commands{ - DRAW: {Name: "draw path=src/main.svg pid refresh:button=auto save edit actions", Help: "思维导图", Actions: ice.MergeActions(ice.Actions{ + DRAW: {Name: "draw path=src/main.svg pid:text refresh:button=auto save edit actions", Help: "思维导图", Actions: ice.MergeActions(ice.Actions{ ice.CTX_INIT: {Hand: func(m *ice.Message, arg ...string) { m.Cmd(mdb.RENDER, mdb.CREATE, mdb.TYPE, nfs.SVG, mdb.NAME, m.PrefixKey()) }}, mdb.RENDER: {Hand: func(m *ice.Message, arg ...string) { - m.Echo("") - defer m.Echo("") + defer m.Echo("").Echo("") m.Cmdy(nfs.CAT, path.Join(arg[2], arg[1])) }}, }, WikiAction("", nfs.SVG), ctx.CmdAction()), Hand: func(m *ice.Message, arg ...string) { diff --git a/core/wiki/field.go b/core/wiki/field.go index 1c819999..3124ff13 100644 --- a/core/wiki/field.go +++ b/core/wiki/field.go @@ -15,14 +15,10 @@ import ( func Parse(m *ice.Message, meta string, key string, arg ...string) (data ice.Any) { list := []string{} for _, line := range kit.SplitLine(strings.Join(arg, ice.SP)) { - ls := kit.Split(line) - for i := 0; i < len(ls); i++ { - if strings.HasPrefix(ls[i], "# ") { - ls = ls[:i] - break - } + if strings.HasPrefix(strings.TrimSpace(line), "# ") { + continue } - list = append(list, ls...) + list = append(list, kit.SplitWord(line)...) } switch data = kit.Parse(nil, "", list...); meta { case ice.MSG_OPTION: @@ -48,12 +44,10 @@ func _field_show(m *ice.Message, name, text string, arg ...string) { msg := m.Spawn() for i := 0; i < len(arg)-1; i += 2 { - if strings.HasPrefix(arg[i], "opts.") { - kit.Value(meta, arg[i], m.Option(arg[i], strings.TrimSpace(arg[i+1]))) + if strings.HasPrefix(arg[i], ARGS) { + kit.Value(meta, arg[i], m.Optionv(arg[i], kit.Split(strings.TrimSuffix(strings.TrimPrefix(arg[i+1], "["), "]")))) } else if strings.HasPrefix(arg[i], "args.") { kit.Value(meta, arg[i], m.Option(arg[i], strings.TrimSpace(arg[i+1]))) - } else if strings.HasPrefix(arg[i], ARGS) { - kit.Value(meta, arg[i], m.Optionv(arg[i], kit.Split(strings.TrimSuffix(strings.TrimPrefix(arg[i+1], "["), "]")))) } else { kit.Value(meta, arg[i], Parse(m, ice.MSG_OPTION, arg[i], arg[i+1])) } diff --git a/core/wiki/image.go b/core/wiki/image.go index 0a3941f5..0cfe60cc 100644 --- a/core/wiki/image.go +++ b/core/wiki/image.go @@ -19,7 +19,7 @@ const IMAGE = "image" func init() { Index.MergeCommands(ice.Commands{ - IMAGE: {Name: "image url", Help: "图片", Actions: WordAction( + IMAGE: {Name: "image url run", Help: "图片", Actions: WordAction( ``, nfs.PATH, ice.USR_LOCAL_IMAGE, ), Hand: func(m *ice.Message, arg ...string) { _image_show(m, arg[0], arg[1:]...) }}, }) diff --git a/core/wiki/order.go b/core/wiki/order.go index a5047d1e..ae1b8e27 100644 --- a/core/wiki/order.go +++ b/core/wiki/order.go @@ -1,15 +1,13 @@ package wiki import ( - "strings" - ice "shylinux.com/x/icebergs" "shylinux.com/x/icebergs/base/mdb" kit "shylinux.com/x/toolkits" ) func _order_show(m *ice.Message, text string, arg ...string) { - m.Optionv(mdb.LIST, kit.Split(strings.TrimSpace(text), ice.NL)) + m.Optionv(mdb.LIST, kit.SplitLine(text)) _wiki_template(m, "", text, arg...) } diff --git a/core/wiki/parse.go b/core/wiki/parse.go index 448afc61..56c4f067 100644 --- a/core/wiki/parse.go +++ b/core/wiki/parse.go @@ -31,7 +31,7 @@ func init() { arg[0] = nfs.JSON } else if strings.HasPrefix(arg[1], web.HTTP) { arg[0] = web.HTTP - } else if strings.Contains(arg[1], "=") { + } else if strings.Contains(arg[1], ice.EQ) { arg[0] = web.FORM } else if _, e := strconv.ParseInt(arg[1], 10, 64); e == nil { arg[0] = mdb.TIME diff --git a/core/wiki/spark.go b/core/wiki/spark.go index 47e2af6e..08bd9b2f 100644 --- a/core/wiki/spark.go +++ b/core/wiki/spark.go @@ -18,7 +18,7 @@ func _spark_show(m *ice.Message, name, text string, arg ...string) *ice.Message defer m.Echo("") switch name { - case "inner", "field": + case "inner", FIELD: return m.Echo(text) } @@ -54,7 +54,7 @@ func init() { } list := []string{kit.Format(`
`, arg[0])} for _, l := range kit.SplitLine(strings.Join(arg[1:], ice.NL)) { - list = append(list, Format("div", Format("label", kit.Select("> ", "$ ", arg[0] == SHELL)), Format("span", l))) + list = append(list, Format("div", Format("label", kit.Select("> ", "$ ", arg[0] == SHELL)), Format("span", l))) } return strings.Join(append(list, "
"), "") }) diff --git a/core/wiki/table.go b/core/wiki/table.go index de15d258..e0d01453 100644 --- a/core/wiki/table.go +++ b/core/wiki/table.go @@ -9,7 +9,6 @@ import ( func _table_run(m *ice.Message, arg ...string) { msg := m.Cmd(arg) - list := [][]string{} msg.Table(func(index int, value ice.Maps, head []string) { if index == 0 { @@ -26,40 +25,45 @@ func _table_run(m *ice.Message, arg ...string) { } func _table_show(m *ice.Message, text string, arg ...string) { head, list := []string{}, [][]string{} - for i, v := range kit.SplitLine(text) { - if v = strings.ReplaceAll(v, "%", "%%"); i == 0 { - head = kit.SplitWord(v) - } else { - line := kit.SplitWord(v) - for i, v := range line { - if ls := kit.SplitWord(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) + for i, line := range kit.SplitLine(text) { + if line = strings.ReplaceAll(line, "%", "%%"); i == 0 { + head = kit.SplitWord(line) + continue } + list = append(list, transList(kit.SplitWord(line), func(value string) string { + if ls := kit.SplitWord(value); len(ls) > 1 { + return kit.Format(`%s`, kit.JoinKV(":", ";", transArgKey(ls[1:])...), ls[0]) + } + return value + })) } m.Optionv("head", head) m.Optionv("list", list) _wiki_template(m, "", text, arg...) } - +func transList(arg []string, cb func(string) string) []string { + for i, v := range arg { + arg[i] = cb(v) + } + return arg +} +func transArgKey(arg []string) []string { + for i := 0; i < len(arg)-1; i += 2 { + switch arg[i] { + case BG: + arg[i] = "background-color" + case FG: + arg[i] = "color" + } + } + return arg +} const TABLE = "table" func init() { Index.MergeCommands(ice.Commands{ TABLE: {Name: "table text", Help: "表格", Actions: ice.MergeActions(ice.Actions{ - ice.RUN: {Name: "run", Hand: func(m *ice.Message, arg ...string) { _table_run(m, arg...) }}, + ice.RUN: {Hand: func(m *ice.Message, arg ...string) { _table_run(m, arg...) }}, }, WordAction(`{{range $i, $v := .Optionv "head"}}{{end}} {{range $index, $value := .Optionv "list"}}{{range $i, $v := $value}}{{end}}{{end}} diff --git a/core/wiki/title.go b/core/wiki/title.go index 400ef4ef..4e132858 100644 --- a/core/wiki/title.go +++ b/core/wiki/title.go @@ -11,23 +11,21 @@ import ( ) func _title_parse(m *ice.Message, dir string, text string) string { - return m.Cmdx(lex.SPLIT, "", "name,link", kit.Dict(nfs.CAT_CONTENT, text), func(ls []string, data ice.Map) []string { + return m.Cmdx(lex.SPLIT, "", "name,link", kit.Dict(nfs.CAT_CONTENT, text), func(ls []string) []string { if len(ls) > 1 { ls[1] = path.Join(dir, ls[1]) } return ls }) } - +func _title_menu(m *ice.Message, kind, text string, arg ...string) *ice.Message { + if kind == NAVMENU { + m.Option(mdb.DATA, _title_parse(m, path.Dir(m.Option(ice.MSG_SCRIPT)), text)) + } + return _option(m, kind, "", text, arg...).RenderTemplate(m.Config(kind), &Message{m}) +} func _title_show(m *ice.Message, kind, text string, arg ...string) *ice.Message { switch title, _ := m.Optionv(TITLE).(map[string]int); kind { - case NAVMENU: - m.Option(mdb.DATA, _title_parse(m, path.Dir(m.Option(ice.MSG_SCRIPT)), text)) - return _option(m, kind, "", text, arg...).RenderTemplate(m.Config(kind), &Message{m}) - case PREMENU: - return _option(m, kind, "", "", arg...).RenderTemplate(m.Config(kind), &Message{m}) - case ENDMENU: - return _option(m, kind, "", "", arg...).RenderTemplate(m.Config(kind), &Message{m}) case SECTION: title[SECTION]++ m.Option(LEVEL, "h3") @@ -72,9 +70,9 @@ func init() { `), Help: "标题", Hand: func(m *ice.Message, arg ...string) { switch arg[0] { case NAVMENU: - _title_show(m, arg[0], arg[1], arg[2:]...) + _title_menu(m, arg[0], arg[1], arg[2:]...) case PREMENU, ENDMENU: - _title_show(m, arg[0], "", arg[1:]...) + _title_menu(m, arg[0], "", arg[1:]...) case CHAPTER, SECTION: _title_show(m, arg[0], arg[1], arg[2:]...) default: diff --git a/core/wiki/video.go b/core/wiki/video.go index 7f0e346e..284119a1 100644 --- a/core/wiki/video.go +++ b/core/wiki/video.go @@ -6,15 +6,15 @@ import ( ) const ( - mp4 = "mp4" m4v = "m4v" + mp4 = "mp4" MOV = "mov" ) const VIDEO = "video" func init() { Index.MergeCommands(ice.Commands{ - VIDEO: {Name: "video url", Help: "视频", Actions: WordAction( + VIDEO: {Name: "video url run", Help: "视频", Actions: WordAction( ``, nfs.PATH, ice.USR_LOCAL_IMAGE, ), Hand: func(m *ice.Message, arg ...string) { _image_show(m, arg[0], arg[1:]...) }}, }) diff --git a/core/wiki/wiki.go b/core/wiki/wiki.go index f0bd2620..98ccd747 100644 --- a/core/wiki/wiki.go +++ b/core/wiki/wiki.go @@ -47,6 +47,7 @@ func _wiki_list(m *ice.Message, arg ...string) bool { m.Cmdy(nfs.DIR, kit.Slice(arg, 0, 1), kit.Dict(nfs.DIR_TYPE, nfs.DIR)) } m.Cmdy(nfs.DIR, kit.Slice(arg, 0, 1), kit.Dict(nfs.DIR_TYPE, nfs.CAT, nfs.DIR_REG, m.Config(lex.REGEXP))) + m.StatusTimeCount() return true } ctx.DisplayLocal(m, path.Join(kit.PathName(2), kit.Keys(kit.FileName(2), ice.JS))) diff --git a/core/wiki/wiki.shy b/core/wiki/wiki.shy index b478b794..7bc9667d 100644 --- a/core/wiki/wiki.shy +++ b/core/wiki/wiki.shy @@ -1,8 +1,8 @@ wiki.shy wiki.go -feel.go -draw.go data.go +draw.go +feel.go word.go title.go @@ -12,9 +12,9 @@ spark.go field.go parse.go -image.go -video.go -audio.go order.go table.go chart.go +image.go +video.go +audio.go diff --git a/core/wiki/word.go b/core/wiki/word.go index 383a0dc4..dbd80d84 100644 --- a/core/wiki/word.go +++ b/core/wiki/word.go @@ -33,10 +33,10 @@ func init() { WordAlias(m, CHAIN, CHART, CHAIN) WordAlias(m, SEQUENCE, CHART, SEQUENCE) }}, - mdb.INPUTS: {Name: "inputs", Help: "补全", Hand: func(m *ice.Message, arg ...string) { + mdb.INPUTS: {Hand: func(m *ice.Message, arg ...string) { m.Cmd(git.REPOS, ice.OptionFields(nfs.PATH)).Tables(func(value ice.Maps) { if m.Option(nfs.DIR_DEEP, ice.TRUE); kit.Path(value[nfs.PATH]) == kit.Path("") { - _wiki_list(m, value[nfs.PATH]+"/src/") + _wiki_list(m, "src/") } else { _wiki_list(m, value[nfs.PATH]) }
{{$v}}
{{$v}}