url.lua:absolute_path(): fix issue #254, simplify, add more test cases

This commit is contained in:
E. Westbrook 2018-08-19 11:32:42 -06:00
parent 5813cd0505
commit 17a95c126a
2 changed files with 36 additions and 15 deletions

View file

@ -88,20 +88,18 @@ local function absolute_path(base_path, relative_path)
if string.sub(relative_path, 1, 1) == "/" then return relative_path end
local path = string.gsub(base_path, "[^/]*$", "")
path = path .. relative_path
path = string.gsub(path, "([^/]*%./)", function (s)
if s ~= "./" then return s else return "" end
end)
path = string.gsub(path, "/%.$", "/")
local reduced
while reduced ~= path do
reduced = path
path = string.gsub(reduced, "([^/]*/%.%./)", function (s)
if s ~= "../../" then return "" else return s end
end)
end
path = string.gsub(reduced, "([^/]*/%.%.)$", function (s)
if s ~= "../.." then return "" else return s end
end)
repeat
local was = path
path = path:gsub('/%./', '/')
until path == was
repeat
local was = path
path = path:gsub('[^/]+/%.%./([^/]+)', '%1')
until path == was
path = path:gsub('[^/]+/%.%./*$', '')
path = path:gsub('/%.%.$', '/')
path = path:gsub('/%.$', '/')
path = path:gsub('^/%.%.', '')
return path
end