forked from x/icebergs
114 lines
4.3 KiB
Go
114 lines
4.3 KiB
Go
package wiki
|
|
|
|
import (
|
|
"html"
|
|
"path"
|
|
"strings"
|
|
|
|
ice "shylinux.com/x/icebergs"
|
|
"shylinux.com/x/icebergs/base/ctx"
|
|
"shylinux.com/x/icebergs/base/lex"
|
|
"shylinux.com/x/icebergs/base/mdb"
|
|
"shylinux.com/x/icebergs/base/nfs"
|
|
"shylinux.com/x/icebergs/base/web"
|
|
kit "shylinux.com/x/toolkits"
|
|
)
|
|
|
|
func _name(m *ice.Message, arg []string) []string {
|
|
kit.If(len(arg) == 1, func() { arg = []string{"", arg[0]} })
|
|
return arg
|
|
}
|
|
func _option(m *ice.Message, kind, name, text string, arg ...string) *ice.Message {
|
|
extra := kit.Dict()
|
|
kit.For(arg, func(k, v string) {
|
|
kit.If(k == "fg", func() { k = "style.color" })
|
|
kit.If(k == "bg", func() { k = "style.background" })
|
|
kit.Value(extra, k, kit.Format(kit.Parse(nil, "", kit.Split(v)...)))
|
|
})
|
|
return m.Options(mdb.TYPE, kind, mdb.NAME, name, mdb.TEXT, text, mdb.EXTRA, kit.Format(extra))
|
|
}
|
|
func _wiki_path(m *ice.Message, arg ...string) string {
|
|
return path.Join(mdb.Config(m, nfs.PATH), path.Join(arg...))
|
|
}
|
|
func _wiki_link(m *ice.Message, text string) string {
|
|
kit.If(!kit.HasPrefix(text, nfs.PS, ice.HTTP), func() { text = path.Join(web.SHARE_LOCAL, _wiki_path(m, text)) })
|
|
return text
|
|
}
|
|
func _wiki_list(m *ice.Message, arg ...string) bool {
|
|
if m.Option(nfs.DIR_ROOT, _wiki_path(m)); len(arg) == 0 || kit.HasSuffix(arg[0], nfs.PS) {
|
|
kit.If(m.Option(nfs.DIR_DEEP) != ice.TRUE, func() { 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, mdb.Config(m, lex.REGEXP)))
|
|
m.SortStrR(mdb.TIME).StatusTimeCount()
|
|
return true
|
|
} else {
|
|
ctx.DisplayLocal(m, path.Join(kit.PathName(2), kit.Keys(kit.FileName(2), nfs.JS)))
|
|
return false
|
|
}
|
|
}
|
|
func _wiki_show(m *ice.Message, name string, arg ...string) {
|
|
m.Cmdy(nfs.CAT, name, kit.Dict(nfs.DIR_ROOT, _wiki_path(m)))
|
|
}
|
|
func _wiki_save(m *ice.Message, name, text string, arg ...string) {
|
|
m.Cmd(nfs.SAVE, name, text, kit.Dict(nfs.DIR_ROOT, _wiki_path(m)))
|
|
}
|
|
func _wiki_upload(m *ice.Message, dir string) {
|
|
m.Cmdy(web.CACHE, web.WATCH, m.Option(ice.MSG_UPLOAD), _wiki_path(m, dir, m.Option(mdb.NAME)))
|
|
}
|
|
func _wiki_template(m *ice.Message, file, name, text string, arg ...string) *ice.Message {
|
|
return m.Echo(nfs.Template(&Message{_option(m, m.CommandKey(), name, strings.TrimSpace(text), arg...)}, kit.Keys(kit.Select(m.CommandKey(), file), nfs.HTML)))
|
|
}
|
|
|
|
const WIKI = "wiki"
|
|
|
|
var Index = &ice.Context{Name: WIKI, Help: "文档中心"}
|
|
|
|
func init() {
|
|
web.Index.Register(Index, &web.Frame{},
|
|
TITLE, BRIEF, REFER, SPARK, PARSE, FIELD,
|
|
ORDER, TABLE, CHART, IMAGE, VIDEO, AUDIO,
|
|
WORD, DATA, DRAW, FEEL,
|
|
)
|
|
}
|
|
func Prefix(arg ...string) string { return web.Prefix(WIKI, kit.Keys(arg)) }
|
|
|
|
func WikiAction(dir string, ext ...string) ice.Actions {
|
|
return ice.Actions{ice.CTX_INIT: mdb.AutoConfig(nfs.PATH, dir, lex.REGEXP, kit.ExtReg(ext...)),
|
|
web.UPLOAD: {Hand: func(m *ice.Message, arg ...string) { _wiki_upload(m, m.Option(nfs.PATH)) }},
|
|
nfs.TRASH: {Hand: func(m *ice.Message, arg ...string) {
|
|
nfs.Trash(m, _wiki_path(m, kit.Select("some", kit.Select(m.Option(nfs.PATH), arg, 0))))
|
|
}},
|
|
nfs.SAVE: {Hand: func(m *ice.Message, arg ...string) {
|
|
_wiki_save(m, m.Option(nfs.PATH), kit.Select(m.Option(mdb.TEXT), arg, 1))
|
|
}},
|
|
mdb.INPUTS: {Hand: func(m *ice.Message, arg ...string) {
|
|
switch arg[0] {
|
|
case nfs.PATH:
|
|
m.Option(nfs.DIR_REG, mdb.Config(m, lex.REGEXP))
|
|
m.Cmdy(nfs.DIR, path.Join(mdb.Config(m, nfs.PATH), kit.Select("", arg, 1)))
|
|
case ctx.INDEX:
|
|
m.Cmdy(ctx.COMMAND, mdb.SEARCH, ctx.COMMAND, ice.OptionFields(ctx.INDEX))
|
|
}
|
|
}},
|
|
}
|
|
}
|
|
|
|
type Message struct{ *ice.Message }
|
|
|
|
func (m *Message) OptionTemplate() string {
|
|
res := []string{`class="story"`}
|
|
add := func(pre, key string) {
|
|
kit.If(m.Option(key), func() { res = append(res, kit.Format(`%s%s=%q`, pre, key, html.EscapeString(m.Option(key)))) })
|
|
}
|
|
kit.For(kit.Split("type,name,text"), func(k string) { add("data-", k) })
|
|
kit.For(m.Optionv(mdb.EXTRA), func(k string, v string) { kit.If(!strings.Contains(k, "-"), func() { add("data-", k) }) })
|
|
kit.For(kit.Split(ctx.STYLE), func(k string) { add("", k) })
|
|
return kit.Join(res, lex.SP)
|
|
}
|
|
func (m *Message) OptionKV(key ...string) string {
|
|
res := []string{}
|
|
kit.For(kit.Split(kit.Join(key)), func(k string) {
|
|
kit.If(m.Option(k), func() { res = append(res, kit.Format("%s='%s'", k, m.Option(k))) })
|
|
})
|
|
return kit.Join(res, lex.SP)
|
|
}
|