summaryrefslogtreecommitdiff
path: root/luaextra-url.lua
diff options
context:
space:
mode:
Diffstat (limited to 'luaextra-url.lua')
-rw-r--r--luaextra-url.lua40
1 files changed, 34 insertions, 6 deletions
diff --git a/luaextra-url.lua b/luaextra-url.lua
index 1d282a1..e3e6f81 100644
--- a/luaextra-url.lua
+++ b/luaextra-url.lua
@@ -32,7 +32,9 @@ local hexdigit = lpeg.R("09","AF","af")
local plus = lpeg.P("+")
local escaped = (plus / " ") + (percent * lpeg.C(hexdigit * hexdigit) / tochar)
-local scheme = lpeg.Cs((escaped+(1-colon-slash-qmark-hash))^0) * colon + lpeg.Cc("")
+-- we assume schemes with more than 1 character (in order to avoid problems with windows disks)
+
+local scheme = lpeg.Cs((escaped+(1-colon-slash-qmark-hash))^2) * colon + lpeg.Cc("")
local authority = slash * slash * lpeg.Cs((escaped+(1- slash-qmark-hash))^0) + lpeg.Cc("")
local path = slash * lpeg.Cs((escaped+(1- qmark-hash))^0) + lpeg.Cc("")
local query = qmark * lpeg.Cs((escaped+(1- hash))^0) + lpeg.Cc("")
@@ -40,22 +42,48 @@ local fragment = hash * lpeg.Cs((escaped+(1- endofstring))^0
local parser = lpeg.Ct(scheme * authority * path * query * fragment)
+-- todo: reconsider Ct as we can as well have five return values (saves a table)
+-- so we can have two parsers, one with and one without
+
function url.split(str)
return (type(str) == "string" and lpegmatch(parser,str)) or str
end
+-- todo: cache them
+
function url.hashed(str)
local s = url.split(str)
+ local somescheme = s[1] ~= ""
return {
- scheme = (s[1] ~= "" and s[1]) or "file",
+ scheme = (somescheme and s[1]) or "file",
authority = s[2],
- path = s[3],
- query = s[4],
- fragment = s[5],
- original = str
+ path = s[3],
+ query = s[4],
+ fragment = s[5],
+ original = str,
+ noscheme = not somescheme,
}
end
+function url.hasscheme(str)
+ return url.split(str)[1] ~= ""
+end
+
+function url.addscheme(str,scheme)
+ return (url.hasscheme(str) and str) or ((scheme or "file:///") .. str)
+end
+
+function url.construct(hash)
+ local fullurl = hash.sheme .. "://".. hash.authority .. hash.path
+ if hash.query then
+ fullurl = fullurl .. "?".. hash.query
+ end
+ if hash.fragment then
+ fullurl = fullurl .. "?".. hash.fragment
+ end
+ return fullurl
+end
+
function url.filename(filename)
local t = url.hashed(filename)
return (t.scheme == "file" and (gsub(t.path,"^/([a-zA-Z])([:|])/)","%1:"))) or filename