From ae83873f26ff39059d4ec5853acf4efbc4ed75db Mon Sep 17 00:00:00 2001 From: shaoying Date: Mon, 10 Sep 2018 18:53:16 +0800 Subject: [PATCH] mac add wiki.menu --- src/contexts/lex/lex.go | 9 +- src/contexts/web/web.go | 28 +++---- src/contexts/yac/yac.go | 1 + usr/library/base.js | 59 +++++++++++++ usr/library/wiki.js | 154 ++++++++++++++++++++++++++++++++++ usr/template/common/base.html | 64 +------------- usr/template/common/wiki.html | 121 +++++--------------------- usr/wiki/zsh_tmux_vim.md | 20 ++--- 8 files changed, 267 insertions(+), 189 deletions(-) create mode 100644 usr/library/base.js create mode 100644 usr/library/wiki.js diff --git a/src/contexts/lex/lex.go b/src/contexts/lex/lex.go index 6d09372c..404ee290 100644 --- a/src/contexts/lex/lex.go +++ b/src/contexts/lex/lex.go @@ -341,9 +341,16 @@ func (lex *LEX) Close(m *ctx.Message, arg ...string) bool { // {{{ // }}} var Index = &ctx.Context{Name: "lex", Help: "词法中心", - Caches: map[string]*ctx.Cache{}, + Caches: map[string]*ctx.Cache{ + "nmat": &ctx.Cache{Name: "nmat", Value: "0", Help: "nmat"}, + }, Configs: map[string]*ctx.Config{}, Commands: map[string]*ctx.Command{ + "spawn": &ctx.Command{Name: "spawn", Help: "添加词法规则", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + if _, ok := m.Target().Server.(*LEX); m.Assert(ok) { // {{{ + m.Start(fmt.Sprintf("matrix%d", m.Capi("nmat", 1)), "matrix") + } // }}} + }}, "train": &ctx.Command{Name: "train seed [hash [page]", Help: "添加词法规则", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if lex, ok := m.Target().Server.(*LEX); m.Assert(ok) { // {{{ page, hash := 1, 1 diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index bcc13006..1db98234 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -280,20 +280,20 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{ web.Configs["logheaders"] = &ctx.Config{Name: "日志输出报文头(yes/no)", Value: "no", Help: "日志输出报文头"} m.Capi("nserve", 1) - yac := m.Sess("tags", m.Sess("yac").Cmd("scan")) - yac.Cmd("train", "void", "void", "[\t ]+") - yac.Cmd("train", "other", "other", "[^\n]+") - yac.Cmd("train", "key", "key", "[A-Za-z_][A-Za-z_0-9]*") - yac.Cmd("train", "code", "def", "def", "key", "(", "other") - yac.Cmd("train", "code", "def", "class", "key", "other") - yac.Cmd("train", "code", "struct", "struct", "key", "\\{") - yac.Cmd("train", "code", "struct", "\\}", "key", ";") - yac.Cmd("train", "code", "struct", "typedef", "struct", "key", "key", ";") - yac.Cmd("train", "code", "function", "key", "\\*", "key", "(", "other") - yac.Cmd("train", "code", "function", "key", "key", "(", "other") - yac.Cmd("train", "code", "variable", "struct", "key", "key", "other") - yac.Cmd("train", "code", "define", "#define", "key", "other") - + // yac := m.Sess("tags", m.Sess("yac").Cmd("scan")) + // yac.Cmd("train", "void", "void", "[\t ]+") + // yac.Cmd("train", "other", "other", "[^\n]+") + // yac.Cmd("train", "key", "key", "[A-Za-z_][A-Za-z_0-9]*") + // yac.Cmd("train", "code", "def", "def", "key", "(", "other") + // yac.Cmd("train", "code", "def", "class", "key", "other") + // yac.Cmd("train", "code", "struct", "struct", "key", "\\{") + // yac.Cmd("train", "code", "struct", "\\}", "key", ";") + // yac.Cmd("train", "code", "struct", "typedef", "struct", "key", "key", ";") + // yac.Cmd("train", "code", "function", "key", "\\*", "key", "(", "other") + // yac.Cmd("train", "code", "function", "key", "key", "(", "other") + // yac.Cmd("train", "code", "variable", "struct", "key", "key", "other") + // yac.Cmd("train", "code", "define", "#define", "key", "other") + // if m.Cap("protocol") == "https" { web.Caches["cert"] = &ctx.Cache{Name: "服务证书", Value: m.Conf("cert"), Help: "服务证书"} web.Caches["key"] = &ctx.Cache{Name: "服务密钥", Value: m.Conf("key"), Help: "服务密钥"} diff --git a/src/contexts/yac/yac.go b/src/contexts/yac/yac.go index 0c03836e..aed6655c 100644 --- a/src/contexts/yac/yac.go +++ b/src/contexts/yac/yac.go @@ -34,6 +34,7 @@ type YAC struct { mat []map[byte]*State state map[State]*State + lex *ctx.Message *ctx.Context } diff --git a/usr/library/base.js b/usr/library/base.js new file mode 100644 index 00000000..6ab18a33 --- /dev/null +++ b/usr/library/base.js @@ -0,0 +1,59 @@ + +function update(event, module, details, key) { + if (event) { + window[key+"timer"] = !window[key+"timer"]; + } + if (!window[key+"timer"]) { + return + } + console.log("update "+key) + setTimeout(function() { + action(event, module, details, key); + update(null, module, details, key); + }, refresh_time); +} + +function input(event, module, details, key) { + if (event.code == "Enter") { + action(event, module, details, key); + } +} +function action(event, module, details, key) { + var input = document.getElementsByClassName(key+"_input"); + for (var i = 0; i < input.length; i++ ){ + if (input[i].value != "") { + details.push(input[i].value) + } + } + ctx.POST("", {module:module, details:details}, function(msg) { + if (msg && msg.result) { + var result = document.getElementsByClassName(key+"_result")[0]; + result.innerHTML = msg.result; + } + if (!msg || !msg.append || msg.append.length < 1) { + return + } + + var append = document.getElementsByClassName(key+"_append")[0]; + if (append.rows.length == msg[msg.append[0]].length+1) { + return + } + append.innerHTML = ''; + var tr = append.insertRow(0); + for (var i in msg.append) { + var th = tr.appendChild(document.createElement("th")); + th.appendChild(document.createTextNode(msg.append[i])); + } + + for (var i = 0; i < msg[msg.append[0]].length; i++) { + var tr = append.insertRow(1); + for (var j in msg.append) { + var td = tr.appendChild(document.createElement("td")); + td.appendChild(document.createTextNode(msg[msg.append[j]][i])); + } + } + var div = append.parent; + + }) + return false; +} diff --git a/usr/library/wiki.js b/usr/library/wiki.js new file mode 100644 index 00000000..a0ac942e --- /dev/null +++ b/usr/library/wiki.js @@ -0,0 +1,154 @@ +function jumpto(url) { + if (url == "..") { + var ps = locaiton.href.split(); + } + location.href=url; +} +function keyup(event) { + console.log(event); + if (typeof window.control == "function") { + control(event); + } + if (event.key == "z") { + var input = document.getElementsByClassName("query_input")[0]; + if (!window.query_show) { + window.query_show = true; + var query = document.getElementsByClassName("query_menu")[0]; + var input = query.getElementsByTagName("input")[0]; + input.style.visibility = "visible"; + input.style.width = "80px"; + input.focus(); + } + return true + } + return true +} +document.onkeyup = keyup; +function toggle() { + window.list_hide = !window.list_hide; + var list = document.getElementsByClassName("list")[0]; + var content = document.getElementsByClassName("content")[0]; + if (list_hide) { + list.style.visibility = "hidden"; + list.style.width="0px"; + list.style.height="0px"; + list.style["min-width"]="0px"; + content.style.width="100%"; + } else { + list.style.visibility = "visible"; + list.style.width="15%"; + list.style.height="100%"; + list.style["min-width"]="180px"; + content.style.width="85%"; + } +} +function menu() { + var max = 0; + var min = 1000; + var list = []; + var hs = ["h2", "h3"]; + for (var i = 0; i < hs.length; i++) { + var head = document.getElementsByTagName(hs[i]); + for (var j = 0; j < head.length; j++) { + head[j].id = "head"+head[j].offsetTop; + list.push({"level": hs[i], "position": head[j].offsetTop, "title": head[j].innerText, "hash": head[j].id}) + if (head[j].offsetTop > max) { + max = head[j].offsetTop; + } + if (head[j].offsetTop < min) { + min = head[j].offsetTop; + } + } + } + max = max - min; + + for (var i = 0; i < list.length-1; i++) { + for (var j = i+1; j < list.length; j++) { + if (list[j].position < list[i].position) { + var a = list[i]; + list[i] = list[j]; + list[j] = a; + } + } + } + + var index2 = -1; + var index3 = 0; + for (var i = 0; i < list.length; i++) { + if (list[i].level == "h2") { + index2++; + index3=0; + } else { + index3++; + list[i].index3 = index3; + } + list[i].index2 = index2; + } + + var m = document.getElementsByClassName("menu"); + for (var i = 0; i < m.length; i++) { + for (var j = 0; j < list.length; j++) { + var text = list[j].index2+"." + if (list[j].level == "h3") { + text += list[j].index3 + } + text += " " + text += list[j].title; + + var h = document.getElementById(list[j].hash) + h.innerText = text + + var one = m[i].appendChild(document.createElement("div")); + var a = one.appendChild(document.createElement("a")); + a.href = "#"+list[j].hash; + a.innerText = text+" ("+parseInt((list[j].position-min)/max*100)+"%)"; + + one.className = list[j].level; + } + } +} +function query(event) { + if (event) { + if (event.code == "Enter") { + jumpto("/wiki/?query="+encodeURIComponent(event.target.value)); + } + console.log("what") + return true + } + window.query_show = !window.query_show; + var query = document.getElementsByClassName("query_menu")[0]; + var input = query.getElementsByTagName("input")[0]; + if (window.query_show) { + input.style.visibility = "visible"; + input.style.width = "80px"; + + } else { + input.style.visibility = "hidden"; + input.style.width = "0px"; + } +} +var tags_list = {}; +ctx.GET("/wiki/define.json", undefined, function(msg){ + tags_list = msg["define"]; +}) +function tags(event) { + console.log(event); + + if (event.srcElement.tagName == "CODE") { + var tag = document.getSelection().toString(); + console.log(tag); + if (tag && tag.length > 0 && tags_list[tag]) { + var position = tags_list[tag].position; + if (position.length == 1) { + jumpto("/wiki/src/"+position[0].file+"#hash_"+position[0].line); + } else { + jumpto("/wiki/?query="+encodeURIComponent(tag)); + } + } + } +} +document.onmouseup = tags; +window.onload = function() { + toggle(); + menu(); +} diff --git a/usr/template/common/base.html b/usr/template/common/base.html index 26466984..75b7224e 100644 --- a/usr/template/common/base.html +++ b/usr/template/common/base.html @@ -41,66 +41,7 @@ } - + {{end}} @@ -135,11 +76,10 @@ function action(event, module, details, key) { {{end}} diff --git a/usr/template/common/wiki.html b/usr/template/common/wiki.html index d136ba31..ae1a8c2f 100644 --- a/usr/template/common/wiki.html +++ b/usr/template/common/wiki.html @@ -5,35 +5,6 @@ {{end}} {{define "wiki_list"}} -
- {{if option . "modify_time"|meta}} -
上次修订时间: {{option . "modify_time"|meta}} 修订次数: {{option . "modify_count"|meta}}
- {{end}} -
上次阅读时间: {{option . "last_record_time"|meta}} 总访问量: {{option . "record_count"|meta}}
+ {{if option . "modify_time"|meta}} +
上次修订时间: {{option . "modify_time"|meta}} 修订次数: {{option . "modify_count"|meta}}
+ {{end}} +
上次阅读时间: {{option . "last_record_time"|meta}} 总访问量: {{option . "record_count"|meta}}
+
{{range option . "nline"|meta|list}}
{{.}}
@@ -254,12 +170,13 @@ window.onload = function() {
    {{end}} {{else if append . "code"|meta}} -

    {{append . "code"|meta}}

    - {{else}} -

    {{append . "body"|meta|unscaped}}

    +

    {{append . "code"|meta}}

    + {{else}} +

    {{append . "body"|meta|unscaped}}

    {{end}}
+ {{end}} diff --git a/usr/wiki/zsh_tmux_vim.md b/usr/wiki/zsh_tmux_vim.md index 8baee459..c5a8c742 100644 --- a/usr/wiki/zsh_tmux_vim.md +++ b/usr/wiki/zsh_tmux_vim.md @@ -1,4 +1,4 @@ -## 0. 简介 +## 简介 zsh 和bash一样,是一种终端的shell,但提供了更丰富的功能,更快捷的补全。 @@ -12,7 +12,7 @@ vim 是一款强大的编辑器,通过模式化快捷键提升编辑速度, - Mac包管理器: -### 0.1 zsh安装 +### zsh安装 Mac上自带zsh,不用安装,但Ubuntu上需要自己安装一下。 ``` $ sudo apt-get install zsh @@ -25,7 +25,7 @@ $ chsh -s /usr/bin/zsh ``` $ curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh ``` -### 0.2 tmux安装 +### tmux安装 Ubuntu上安装 ``` $ sudo apt-get install tmux @@ -42,7 +42,7 @@ $ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/maste ``` $ tmux ``` -### 0.3 vim安装 +### vim安装 Mac上自带vim,不需要安装,但Ubuntu需要自己安装一下。 ``` $ sudo apt-get install vim @@ -51,12 +51,12 @@ vim有很丰富的插件,可以下载一个插件管理器。 ``` $ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim ``` -## 1. 基本快捷键 -### 1.1 zsh使用 -### 1.2 tmux使用 -### 1.3 vim使用 -## 2. 个性化配置 -## 3. 源码解析 +## 基本快捷键 +### zsh使用 +### tmux使用 +### vim使用 +## 个性化配置 +## 源码解析 Mac上安装pip ``` $ sudo easy_install pip