1
0
mirror of https://github.com/lxsang/antd-web-apps synced 2025-02-22 18:22:47 +01:00

Merge pull request #9 from lxsang/master

support for local media
This commit is contained in:
Xuan Sang LE 2020-09-13 17:16:01 +02:00 committed by GitHub
commit f684005fe4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 131 additions and 54 deletions

View File

@ -297,3 +297,9 @@ td.hljs-ln-numbers {
.hljs-ln td.hljs-ln-code {
padding-left: 10px;
}
model-viewer{
/*border: 1px solid #333f67;*/
width: 100%;
height: 350px;
}

View File

@ -1,9 +1,44 @@
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:[^\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 .. ")")
end
end
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,
"<model-viewer src=\"" .. HTTP_ROOT .. "/" ..
obj.name .. "/asset/" .. apath ..
"\" auto-rotate camera-controls></model-viewer>")
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}
@ -38,7 +73,8 @@ function DocController:loadTOC()
local spath = getpath(vs.path, self) .. "/meta.json"
local smeta = JSON.decodeFile(spath)
if smeta then
local section = {
local section =
{
name = smeta.name,
path = vs.path .. "/INTRO.md",
tpath = vs.path,
@ -55,8 +91,10 @@ function DocController:loadTOC()
local line = io.read()
io.close()
if line then
local file = {
name = std.trim(std.trim(line, "#"), " "),
local file =
{
name = std.trim(
std.trim(line, "#"), " "),
path = vf.path,
tpath = vf.path,
parent = section,
@ -104,16 +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
self.template:setView("index", "index")
self.template:set("data", content)
self.template:set("has_3d", has_3d)
else
self.template:setView("notfound", "index")
end
@ -150,7 +191,9 @@ function DocController:search(...)
end
local content = line:gsub("^[^:]*:", ""):lower()
for k, p in ipairs(patterns) do
content = content:gsub(p, "<span class='pattern'>"..p.."</span>")
content = content:gsub(p,
"<span class='pattern'>" .. p ..
"</span>")
end
table.insert(result[file], content)
end
@ -170,15 +213,32 @@ 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))
end
local rpath = "index.html"
if #args ~= 0 then
rpath = implode(args,"/")
end
if #args ~= 0 then rpath = implode(args, "/") end
local path = self.path_map.api_path .. "/" .. rpath
if ulib.exists(path) then

View File

@ -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

View File

@ -1,6 +1,7 @@
<?lua
local tocdata = __main__:get("toc")
local elinks = __main__:get("elinks")
local has_3d = __main__:get("has_3d")
?>
<!DOCTYPE html>
<html>
@ -13,10 +14,14 @@ local elinks = __main__:get("elinks")
rel="stylesheet"
type="text/css"
href="<?=HTTP_ROOT?>/rst/katex/katex.min.css" />
<!--script
type="text/javascript"
src="<?=HTTP_ROOT?>/rst/gscripts/showdown.min.js"
></script-->
<?lua
if has_3d then
?>
<script type="module" src="https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js"></script>
<script nomodule src="https://unpkg.com/@google/model-viewer/dist/model-viewer-legacy.js"></script>
<?lua
end
?>
<script
src="<?=HTTP_ROOT?>/rst/hljs/highlight.pack.js"
></script>

View File

@ -19,5 +19,9 @@
"wasm": {
"mime": "application/wasm",
"binary": true
},
"glb": {
"mime": "model/gltf-binary",
"binary": true
}
}