From 77d5267443165307884e0a5cac88c9ccf01389ed Mon Sep 17 00:00:00 2001 From: lxsang Date: Sun, 13 Sep 2020 15:41:39 +0200 Subject: [PATCH 1/2] add local image support --- doc/controllers/doccontroller.lua | 132 +++++++++++++++++++----------- 1 file changed, 86 insertions(+), 46 deletions(-) diff --git a/doc/controllers/doccontroller.lua b/doc/controllers/doccontroller.lua index a97cd78..d48237f 100644 --- a/doc/controllers/doccontroller.lua +++ b/doc/controllers/doccontroller.lua @@ -1,12 +1,27 @@ BaseController:subclass("DocController") local getpath = function(vfspath, controller) - return vfspath:gsub(controller.path_map.vfs_path, controller.path_map.local_path) + return vfspath:gsub(controller.path_map.vfs_path, + controller.path_map.local_path) +end + +local pre_process_md = function(str, obj) + local content = str + for capture in str:gmatch("(%[%[@book:image:.*%]%])") do + local apath = capture:match("%[%[@book:image:(.*)%]%]") + local pattern = capture:gsub("%[", "%%["):gsub("%]", "%%]") + if apath then + content = str:gsub(pattern, + "![](" .. HTTP_ROOT .. "/" .. obj.name .. + "/asset/" .. apath .. ")") + end + end + return content end function DocController:loadTOC() - local path = self.path_map.local_path.."/meta.json" - local result = { error = false} + local path = self.path_map.local_path .. "/meta.json" + local result = {error = false} if ulib.exists(path) then local bmeta = JSON.decodeFile(path) if bmeta == nil then @@ -15,39 +30,40 @@ function DocController:loadTOC() else result.data = { name = bmeta.name, - path = self.path_map.vfs_path.."/INTRO.md", + path = self.path_map.vfs_path .. "/INTRO.md", entries = {}, parent = nil } -- read all the entries - for kc,vc in pairs(bmeta.entries) do - local cpath = getpath(vc.path, self).."/meta.json" + for kc, vc in pairs(bmeta.entries) do + local cpath = getpath(vc.path, self) .. "/meta.json" if ulib.exists(cpath) then local cmeta = JSON.decodeFile(cpath) if cmeta then local chapter = { name = cmeta.name, - path = vc.path.."/INTRO.md", + path = vc.path .. "/INTRO.md", tpath = vc.path, entries = {}, parent = result.data, id = kc } -- read all sections - for ks,vs in pairs(cmeta.entries) do - local spath = getpath(vs.path, self).."/meta.json" + for ks, vs in pairs(cmeta.entries) do + local spath = getpath(vs.path, self) .. "/meta.json" local smeta = JSON.decodeFile(spath) if smeta then - local section = { - name = smeta.name, - path = vs.path.."/INTRO.md", - tpath = vs.path, - entries = {}, - parent = chapter, - id = ks - } + local section = + { + name = smeta.name, + path = vs.path .. "/INTRO.md", + tpath = vs.path, + entries = {}, + parent = chapter, + id = ks + } -- read all files - for kf,vf in pairs(smeta.entries) do + for kf, vf in pairs(smeta.entries) do local fpath = getpath(vf.path, self) if ulib.exists(fpath) then local file = io.open(fpath, "r") @@ -55,21 +71,23 @@ function DocController:loadTOC() local line = io.read() io.close() if line then - local file = { - name = std.trim(std.trim(line, "#"), " "), - path = vf.path, - tpath = vf.path, - parent = section, - id = kf - } - table.insert( section.entries, file) + local file = + { + name = std.trim( + std.trim(line, "#"), " "), + path = vf.path, + tpath = vf.path, + parent = section, + id = kf + } + table.insert(section.entries, file) end end end - table.insert( chapter.entries, section) + table.insert(chapter.entries, section) end end - table.insert( result.data.entries, chapter) + table.insert(result.data.entries, chapter) end end end @@ -101,12 +119,15 @@ function DocController:index(...) end else toc.cpath = self.path_map.vfs_path - path = self.path_map.local_path.."/INTRO.md" + path = self.path_map.local_path .. "/INTRO.md" end if path and ulib.exists(path) then local file = io.open(path, "r") local content = file:read("*a") file.close() + -- replace some display plugins + content = pre_process_md(content, self) + self.template:setView("index", "index") self.template:set("data", content) else @@ -122,11 +143,11 @@ function DocController:search(...) local cmd = "grep -ri --include=\\*.md " local arr = explode(query, " ") local patterns = {} - for k,v in ipairs(arr) do + for k, v in ipairs(arr) do local world = std.trim(v, " ") if v and v ~= "" then - cmd = cmd.." -e '"..v.."' " - table.insert( patterns,v:lower()) + cmd = cmd .. " -e '" .. v .. "' " + table.insert(patterns, v:lower()) end end if #patterns > 0 then @@ -134,7 +155,7 @@ function DocController:search(...) toc.controller = self.name self.template:set("toc", toc) self.template:setView("search", "index") - cmd = cmd..self.path_map.local_path + cmd = cmd .. self.path_map.local_path local handle = io.popen(cmd) local result = {} for line in handle:lines() do @@ -143,11 +164,13 @@ function DocController:search(...) if not result[file] then result[file] = {} end - local content = line:gsub("^[^:]*:",""):lower() - for k,p in ipairs(patterns) do - content = content:gsub(p, ""..p.."") + local content = line:gsub("^[^:]*:", ""):lower() + for k, p in ipairs(patterns) do + content = content:gsub(p, + "" .. p .. + "") end - table.insert(result[file],content) + table.insert(result[file], content) end end handle:close() @@ -165,26 +188,43 @@ function DocController:search(...) return true end +function DocController:asset(...) + local args = {...} + if #args == 0 then return self:actionnotfound(table.unpack(args)) end + + local path = self.path_map.local_path .. "/" .. implode(args, DIR_SEP) + + if self.registry.fileaccess and ulib.exists(path) then + local mime = std.mimeOf(path) + print(mime) + if POLICY.mimes[mime] then + std.sendFile(path) + else + self:error("Access forbidden: " .. path) + end + else + self:error("Asset file not found or access forbidden: " .. path) + end + return false +end function DocController:api(...) local args = {...} if not self.path_map.api_path then - return self:actionnotfound(table.unpack(args)) + return self:actionnotfound(table.unpack(args)) end local rpath = "index.html" - if #args ~= 0 then - rpath = implode(args,"/") - end - local path = self.path_map.api_path.."/"..rpath + if #args ~= 0 then rpath = implode(args, "/") end + local path = self.path_map.api_path .. "/" .. rpath if ulib.exists(path) then local mime = std.mimeOf(path) if POLICY.mimes[mime] then std.sendFile(path) - else - self:error("Access forbidden: "..path) + else + self:error("Access forbidden: " .. path) end else - self:error("File not found or access forbidden: "..path) + self:error("File not found or access forbidden: " .. path) end return false end @@ -192,4 +232,4 @@ end function DocController:actionnotfound(...) local args = {...} return self:index(table.unpack(args)) -end \ No newline at end of file +end From a45b1ef9f0d65cb12a515bbd2fbfda15c1c3d031 Mon Sep 17 00:00:00 2001 From: lxsang Date: Sun, 13 Sep 2020 17:14:14 +0200 Subject: [PATCH 2/2] Add support for - local media file - add jarvis book --- doc/assets/style.css | 6 ++++++ doc/controllers/doccontroller.lua | 34 ++++++++++++++++++++++++------- doc/router.lua | 2 ++ doc/views/default/layout.ls | 13 ++++++++---- mimes.json | 4 ++++ 5 files changed, 48 insertions(+), 11 deletions(-) diff --git a/doc/assets/style.css b/doc/assets/style.css index 1c7c920..82245c7 100644 --- a/doc/assets/style.css +++ b/doc/assets/style.css @@ -296,4 +296,10 @@ td.hljs-ln-numbers { /* for block of code */ .hljs-ln td.hljs-ln-code { padding-left: 10px; +} + +model-viewer{ + /*border: 1px solid #333f67;*/ + width: 100%; + height: 350px; } \ No newline at end of file diff --git a/doc/controllers/doccontroller.lua b/doc/controllers/doccontroller.lua index 211597a..54235de 100644 --- a/doc/controllers/doccontroller.lua +++ b/doc/controllers/doccontroller.lua @@ -7,10 +7,12 @@ end local pre_process_md = function(str, obj) local content = str - for capture in str:gmatch("(%[%[@book:image:.*%]%])") do - local apath = capture:match("%[%[@book:image:(.*)%]%]") + for capture in str:gmatch("(%[%[@book:image:[^\n%]]*%]%])") do + local apath = capture:match("%[%[@book:image:([^\n%]]*)%]%]") local pattern = capture:gsub("%[", "%%["):gsub("%]", "%%]") if apath then + apath = apath:gsub(" ", "%%%%20") + print(apath) content = str:gsub(pattern, "![](" .. HTTP_ROOT .. "/" .. obj.name .. "/asset/" .. apath .. ")") @@ -19,6 +21,24 @@ local pre_process_md = function(str, obj) return content end +local post_process_md = function(str, obj) + local content = str + local has_model = false + for capture in str:gmatch("(%[%[@book:3dmodel:[^\n%]]*%]%])") do + local apath = capture:match("%[%[@book:3dmodel:([^\n%]]*)%]%]") + local pattern = capture:gsub("%[", "%%["):gsub("%]", "%%]") + if apath then + --apath = utils.urlencode(apath):gsub("%%", "%%%%") + apath = apath:gsub(" ", "%%20") + content = str:gsub(pattern, + "") + has_model = true + end + end + return content, has_model +end function DocController:loadTOC() local path = self.path_map.local_path .. "/meta.json" local result = {error = false} @@ -122,19 +142,19 @@ function DocController:index(...) path = self.path_map.local_path .. "/INTRO.md" end if path and ulib.exists(path) then + local has_3d = false local file = io.open(path, "r") local content = "" local md = require("md") - local callback = function(s) - content = content..s - end - md.to_html(file:read("*a"), callback) + local callback = function(s) content = content .. s end + md.to_html(pre_process_md(file:read("*a"), self), callback) file.close() + content, has_3d = post_process_md(content, self) -- replace some display plugins - content = pre_process_md(content, self) self.template:setView("index", "index") self.template:set("data", content) + self.template:set("has_3d", has_3d) else self.template:setView("notfound", "index") end diff --git a/doc/router.lua b/doc/router.lua index 953a75e..ab256b8 100644 --- a/doc/router.lua +++ b/doc/router.lua @@ -22,6 +22,8 @@ POST_LIMIT = 10 -- require needed library require(BASE_FRW.."silk.api") +POLICY.mimes["model/gltf-binary"] = true + if REQUEST.r then REQUEST.r = REQUEST.r:gsub("%:", "/") end diff --git a/doc/views/default/layout.ls b/doc/views/default/layout.ls index e2abf21..66a3826 100644 --- a/doc/views/default/layout.ls +++ b/doc/views/default/layout.ls @@ -1,6 +1,7 @@ @@ -13,10 +14,14 @@ local elinks = __main__:get("elinks") rel="stylesheet" type="text/css" href="/rst/katex/katex.min.css" /> - + + + + diff --git a/mimes.json b/mimes.json index 7cc007c..f4fd73f 100644 --- a/mimes.json +++ b/mimes.json @@ -19,5 +19,9 @@ "wasm": { "mime": "application/wasm", "binary": true + }, + "glb": { + "mime": "model/gltf-binary", + "binary": true } } \ No newline at end of file