1
0
mirror of https://shylinux.com/x/icebergs synced 2025-04-26 01:24:05 +08:00
icebergs/core/wiki/title.go
2023-04-12 10:35:06 +08:00

68 lines
1.9 KiB
Go

package wiki
import (
"path"
ice "shylinux.com/x/icebergs"
"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 _title_parse(m *ice.Message, text string) string {
return m.Cmdx(lex.SPLIT, "", "name,link", kit.Dict(nfs.CAT_CONTENT, text), func(ls []string) []string {
kit.If(!kit.HasPrefix(ls[1], nfs.PS, web.HTTP), func() { ls[1] = path.Join(path.Dir(m.Option(ice.MSG_SCRIPT)), ls[1]) })
return ls
})
}
func _title_menu(m *ice.Message, name, text string, arg ...string) *ice.Message {
m.Options(mdb.DATA, _title_parse(m, text))
return _wiki_template(m, name, name, text, arg...)
}
func _title_show(m *ice.Message, name, text string, arg ...string) *ice.Message {
switch title, _ := m.Optionv(TITLE).(map[string]int); name {
case SECTION:
title[SECTION]++
m.Options(LEVEL, "h3", PREFIX, kit.Format("%d.%d ", title[CHAPTER], title[SECTION]))
case CHAPTER:
title[CHAPTER]++
title[SECTION] = 0
m.Options(LEVEL, "h2", PREFIX, kit.Format("%d ", title[CHAPTER]))
default:
m.Options(LEVEL, "h1", PREFIX, "")
}
return _wiki_template(m, "", name, text, arg...)
}
const (
LEVEL = "level"
PREFIX = "prefix"
)
const (
NAVMENU = "navmenu"
PREMENU = "premenu"
CHAPTER = "chapter"
SECTION = "section"
ENDMENU = "endmenu"
)
const TITLE = "title"
func init() {
Index.MergeCommands(ice.Commands{
TITLE: {Name: "title type=navmenu,premenu,chapter,section,endmenu text", Help: "标题", Hand: func(m *ice.Message, arg ...string) {
switch arg[0] {
case NAVMENU: // navmenu text arg...
_title_menu(m, arg[0], arg[1], arg[2:]...)
case PREMENU, ENDMENU: // premenu arg...
_title_menu(m, arg[0], "", arg[1:]...)
case CHAPTER, SECTION: // chapter text arg...
_title_show(m, arg[0], arg[1], arg[2:]...)
default: // title text arg...
_title_show(m, "", arg[0], arg[1:]...)
}
}},
})
}