Modul:WLink: Unterschied zwischen den Versionen
updates
wp>PerfektesChaos (updates) |
wp>PerfektesChaos (updates) |
||
Zeile 1: | Zeile 1: | ||
--[=[ 2014-10- | --[=[ 2014-10-28 | ||
WLink | WLink | ||
ansiPercent() | |||
formatURL() | |||
getArticleBase() | |||
getBaseTitle() | |||
getExtension() | |||
getFile() | |||
getFragment() | |||
getLanguage() | |||
getNamespace() | |||
getPlain() | |||
getProject() | |||
getTarget() | |||
getTargetPage() | |||
getTitle() | |||
isBracketedLink() | |||
isBracketedURL() | |||
isExternalLink() | |||
isInterlanguage() | |||
isInterwiki() | |||
isTitledLink() | |||
isValidLink() | |||
isWikilink() | |||
]=] | ]=] | ||
Zeile 235: | Zeile 257: | ||
return r; | return r; | ||
end -- WLink.ansiPercent() | end -- WLink.ansiPercent() | ||
function WLink.formatURL( adjust ) | |||
-- Create bracketed link, if not yet | |||
-- Precondition: | |||
-- adjust -- string, with URL or domain/path or bracketed link | |||
-- Postcondition: | |||
-- Returns string, with bracketed link | |||
-- false on invalid format | |||
local r; | |||
if type( adjust ) == "string" then | |||
if WLink.isBracketedLink( adjust ) then | |||
r = adjust; | |||
else | |||
local url = mw.text.trim( adjust ); | |||
local host; | |||
utilURL(); | |||
host = URLutil.getHost( adjust ); | |||
if not host then | |||
url = "http://" .. adjust; | |||
host = URLutil.getHost( adjust ); | |||
end | |||
if host then | |||
local path = URLutil.getRelativePath( url ); | |||
local show; | |||
if path == "/" then | |||
if not url:match( "/$" ) then | |||
url = url .. "/"; | |||
end | |||
show = host; | |||
else | |||
show = host .. path; | |||
end | |||
r = string.format( "[%s %s]", url, show ); | |||
else | |||
r = false; | |||
end | |||
end | |||
else | |||
r = false; | |||
end | |||
return r; | |||
end -- WLink.formatURL() | |||
Zeile 591: | Zeile 657: | ||
-- Does attempt match a bracketed link? | -- Does attempt match a bracketed link? | ||
-- Precondition: | -- Precondition: | ||
-- attempt -- string, with | -- attempt -- string, with presumable link somewhere | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns boolean | -- Returns boolean | ||
Zeile 615: | Zeile 681: | ||
-- Does attempt match a bracketed URL? | -- Does attempt match a bracketed URL? | ||
-- Precondition: | -- Precondition: | ||
-- attempt -- string, with | -- attempt -- string, with presumable link somewhere | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns boolean | -- Returns boolean | ||
local r = | local s, r = WLink.getTarget( attempt ); | ||
return r; | return ( r == 1 ); | ||
end -- WLink.isBracketedURL() | end -- WLink.isBracketedURL() | ||
Zeile 627: | Zeile 693: | ||
-- Does attempt match an external link? | -- Does attempt match an external link? | ||
-- Precondition: | -- Precondition: | ||
-- attempt -- string, with | -- attempt -- string, with presumable link somewhere | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns boolean | -- Returns boolean | ||
Zeile 642: | Zeile 708: | ||
-- Does attempt match an interlanguage link? | -- Does attempt match an interlanguage link? | ||
-- Precondition: | -- Precondition: | ||
-- attempt -- string, with | -- attempt -- string, with presumable link somewhere | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns boolean | -- Returns boolean | ||
local r = false; | local r = false; | ||
-- if m == 2 and | |||
-- s:find( ":", 3, true ) and s:byte( 1, 1 ) ~= 58 then | |||
-- local s | |||
-- end | |||
return r; | return r; | ||
end -- WLink.isInterlanguage() | end -- WLink.isInterlanguage() | ||
Zeile 654: | Zeile 724: | ||
-- Does attempt match an interwiki link? | -- Does attempt match an interwiki link? | ||
-- Precondition: | -- Precondition: | ||
-- attempt -- string, with | -- attempt -- string, with presumable link somewhere | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns boolean | -- Returns boolean | ||
local r = false; | local r = false; | ||
local s, m = WLink.getTarget( attempt ); | |||
if m == 2 and | |||
s:find( ":", 3, true ) and s:byte( 1, 1 ) ~= 58 then | |||
local s | |||
end | |||
return r; | return r; | ||
end -- WLink.isInterwiki() | end -- WLink.isInterwiki() | ||
Zeile 666: | Zeile 741: | ||
-- Does attempt match a titled link? | -- Does attempt match a titled link? | ||
-- Precondition: | -- Precondition: | ||
-- attempt -- string, with | -- attempt -- string, with presumable link somewhere | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns boolean | -- Returns boolean | ||
Zeile 672: | Zeile 747: | ||
local i = attempt:find( "[", 1, true ); | local i = attempt:find( "[", 1, true ); | ||
if i then | if i then | ||
local c; | local c, n; | ||
local s = attempt:sub( i ); | local s = attempt:sub( i ); | ||
if s:byte( 2, 2 ) == 91 then | if s:byte( 2, 2 ) == 91 then | ||
s | n = s:find( "%]%]", 5 ); | ||
c = "|"; | c = "|"; | ||
else | else | ||
s | n = s:find( "%]", 8 ); | ||
c = "%s"; | c = "%s%S"; | ||
end | end | ||
if | if n then | ||
local m = s:find( c, 2 ); | |||
if m and m + 1 < n and WLink.getTarget( attempt ) then | |||
r = true; | r = true; | ||
end | end | ||
Zeile 695: | Zeile 771: | ||
-- Does attempt match a link? | -- Does attempt match a link? | ||
-- Precondition: | -- Precondition: | ||
-- attempt -- string, with | -- attempt -- string, with presumable link somewhere | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns boolean | -- Returns boolean | ||
Zeile 710: | Zeile 786: | ||
-- Does attempt match a wikilink? | -- Does attempt match a wikilink? | ||
-- Precondition: | -- Precondition: | ||
-- attempt -- string, with | -- attempt -- string, with presumable link somewhere | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns boolean | -- Returns boolean | ||
Zeile 758: | Zeile 834: | ||
end | end | ||
if lucky then | if lucky then | ||
r = r | if type( r ) == "boolean" then | ||
if r then | |||
r = "1"; | |||
else | |||
r = ""; | |||
end | |||
end | |||
else | else | ||
r = string.format( "<span class=\"error\">%s</span>", r ); | r = string.format( "<span class=\"error\">%s</span>", r ); | ||
Zeile 784: | Zeile 866: | ||
p.getFile = function ( frame ) | p.getFile = function ( frame ) | ||
return Template( frame, "getFile" ); | return Template( frame, "getFile" ); | ||
end | |||
p.formatURL = function ( frame ) | |||
return Template( frame, "formatURL" ); | |||
end | end | ||
p.getFragment = function ( frame ) | p.getFragment = function ( frame ) |