Modul:URLutil: Unterschied zwischen den Versionen
2016-03-01 getNormalized
w>PerfektesChaos (2016-01-01) |
w>PerfektesChaos (2016-03-01 getNormalized) |
||
Zeile 1: | Zeile 1: | ||
local URLutil = { suite = "URLutil", | local URLutil = { suite = "URLutil", | ||
serial = "2016- | serial = "2016-03-01" }; | ||
--[=[ | --[=[ | ||
Utilities for URL etc. on www. | Utilities for URL etc. on www. | ||
Zeile 7: | Zeile 7: | ||
* getHost() | * getHost() | ||
* getLocation() | * getLocation() | ||
* getNormalized() | |||
* getPath() | * getPath() | ||
* getPort() | * getPort() | ||
Zeile 52: | Zeile 53: | ||
return false | return false | ||
end -- getURIScheme() | end -- getURIScheme() | ||
local decodeComponent = function ( ask, averse ) | |||
local j, k, m, n | |||
local i = 1 | |||
while ( i ) do | |||
i = ask:find( "%%[2-7]%x", i ) | |||
if i then | |||
s = false | |||
j = i + 1 | |||
k = j + 1 | |||
n = ask:byte( k, k ) | |||
k = k + 1 | |||
m = ( n > 96 ) | |||
if m then | |||
n = n - 32 | |||
m = n | |||
end | |||
if n > 57 then | |||
n = n - 55 | |||
else | |||
n = n - 48 | |||
end | |||
n = ( ask:byte( j, j ) - 48 ) * 16 + n | |||
if n == 20 or n == 127 or | |||
averse:find( string.char( n ), 1, true ) then | |||
if m then | |||
ask = string.format( "%s%c%s", | |||
ask:sub( 1, j ), | |||
m, | |||
ask:sub( k ) ) | |||
end | |||
elseif i == 1 then | |||
ask = string.format( "%c%s", n, ask:sub( k ) ) | |||
else | |||
ask = string.format( "%s%c%s", | |||
ask:sub( 1, i - 1 ), | |||
n, | |||
ask:sub( k ) ) | |||
end | |||
i = j | |||
end | |||
end -- while i | |||
return ask | |||
end -- decodeComponent() | |||
Zeile 71: | Zeile 117: | ||
return r | return r | ||
end -- getTopDomain() | end -- getTopDomain() | ||
local getHash = function ( url ) | |||
local r = url:find( "#", 1, true ) | |||
if r then | |||
local i = url:find( "&#", 1, true ) | |||
if i then | |||
local s | |||
while ( i ) do | |||
s = url:sub( i + 2 ) | |||
if s:match( "^%d+;" ) or s:match( "^x%x+;" ) then | |||
r = url:find( "#", i + 4, true ) | |||
if r then | |||
i = url:find( "&#", i + 4, true ) | |||
else | |||
i = false | |||
end | |||
else | |||
r = i + 1 | |||
i = false | |||
end | |||
end -- while i | |||
end | |||
end | |||
return r | |||
end -- getHash() | |||
Zeile 108: | Zeile 181: | ||
local r | local r | ||
if type( url ) == "string" then | if type( url ) == "string" then | ||
local | local i = getHash( url ) | ||
if i then | if i then | ||
r = mw.text.trim( | r = mw.text.trim( url:sub( i ) ):sub( 2 ) | ||
if type( decode ) == "string" then | if type( decode ) == "string" then | ||
local encoding = mw.text.trim( decode ) | local encoding = mw.text.trim( decode ) | ||
Zeile 155: | Zeile 227: | ||
else | else | ||
local i | local i | ||
i = getHash( r ) | |||
if i then | if i then | ||
if i == 1 then | if i == 1 then | ||
Zeile 170: | Zeile 241: | ||
return r | return r | ||
end -- URLutil.getLocation() | end -- URLutil.getLocation() | ||
URLutil.getNormalized = function ( url ) | |||
local r | |||
if type( url ) == "string" then | |||
r = mw.text.trim( url ) | |||
if r == "" then | |||
r = false | |||
end | |||
else | |||
r = false | |||
end | |||
if r then | |||
local k = r:find( "//", 1, true ) | |||
if k then | |||
local j = r:find( "/", k + 2, true ) | |||
local sF, sP, sQ | |||
if r:find( "%%[2-7]%x" ) then | |||
local i = getHash( r ) | |||
if i then | |||
sF = r:sub( i + 1 ) | |||
r = r:sub( 1, i - 1 ) | |||
if sF == "" then | |||
sF = false | |||
else | |||
sF = decodeComponent( sF, "\"#%<>[\]^`{|}" ) | |||
end | |||
end | |||
i = r:find( "?", 1, true ) | |||
if i then | |||
sQ = r:sub( i ) | |||
r = r:sub( 1, i - 1 ) | |||
sQ = decodeComponent( sQ, "\"#%<>[\]^`{|}&=+;" ) | |||
end | |||
if j then | |||
if #r > j then | |||
sP = decodeComponent( r:sub( j + 1 ), | |||
"\"#%<>[\]^`{|}/?" ) | |||
end | |||
r = r:sub( 1, j - 1 ) | |||
end | |||
elseif j then | |||
local n = #r | |||
if r:byte( n, n ) == 35 then -- '#' | |||
n = n - 1 | |||
r = r:sub( 1, n ) | |||
end | |||
if n > j then | |||
sP = r:sub( j + 1 ) | |||
end | |||
r = r:sub( 1, j - 1 ) | |||
end | |||
r = mw.ustring.lower( r ) .. "/" | |||
if sP then | |||
r = r .. sP | |||
end | |||
if sQ then | |||
r = r .. sQ | |||
end | |||
if sF then | |||
r = string.format( "%s#%s", r, sF ) | |||
end | |||
end | |||
end | |||
return r | |||
end -- URLutil.getNormalized() | |||
Zeile 693: | Zeile 831: | ||
function p.getLocation( frame ) | function p.getLocation( frame ) | ||
return URLutil.getLocation( frame.args[ 1 ] ) or "" | return URLutil.getLocation( frame.args[ 1 ] ) or "" | ||
end | |||
function p.getNormalized( frame ) | |||
return URLutil.getNormalized( frame.args[ 1 ] ) or "" | |||
end | end | ||
function p.getPath( frame ) | function p.getPath( frame ) | ||
Zeile 790: | Zeile 931: | ||
function p.wikiEscapeURL( frame ) | function p.wikiEscapeURL( frame ) | ||
return URLutil.wikiEscapeURL( frame.args[ 1 ] ) | return URLutil.wikiEscapeURL( frame.args[ 1 ] ) | ||
end | |||
function p.failsafe() | |||
return URLutil.serial | |||
end | end | ||
function p.URLutil() | function p.URLutil() |