diff options
author | Manuel Pégourié-Gonnard <mpg@elzevir.fr> | 2010-03-28 14:34:44 +0200 |
---|---|---|
committer | Manuel Pégourié-Gonnard <mpg@elzevir.fr> | 2010-03-28 14:34:44 +0200 |
commit | 0bb6d1e8d28f914398d3b3de15cf5317f480d02a (patch) | |
tree | a222ca028b0f3bec9f4057424623323602174c29 /luaextra-url.lua | |
parent | 7047b46b6fe6dead388c8427a7510710f920b617 (diff) | |
parent | 22fa4cc9381179fdd43ca8a251cb49dd2703079d (diff) | |
download | lualibs-0bb6d1e8d28f914398d3b3de15cf5317f480d02a.tar.gz |
Merge remote branch 'github/master'
Diffstat (limited to 'luaextra-url.lua')
-rw-r--r-- | luaextra-url.lua | 40 |
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 |