Bearbeiten von „Modul:WLink“
Zur Navigation springen
Zur Suche springen
Die Bearbeitung kann rückgängig gemacht werden. Bitte prüfe den Vergleich unten, um sicherzustellen, dass du dies tun möchtest, und veröffentliche dann unten deine Änderungen, um die Bearbeitung rückgängig zu machen.
Aktuelle Version | Dein Text | ||
Zeile 1: | Zeile 1: | ||
local WLink = { suite = "WLink", | local WLink = { suite = "WLink", | ||
serial = " | serial = "2018-07-19", | ||
item = 19363224 }; | item = 19363224 }; | ||
--[=[ | --[=[ | ||
Zeile 21: | Zeile 21: | ||
getTitle() | getTitle() | ||
getWeblink() | getWeblink() | ||
isBracketedLink() | isBracketedLink() | ||
isBracketedURL() | isBracketedURL() | ||
Zeile 32: | Zeile 31: | ||
isTitledLink() | isTitledLink() | ||
isValidLink() | isValidLink() | ||
isWikilink() | isWikilink() | ||
wikilink() | wikilink() | ||
failsafe() | failsafe() | ||
Zeile 44: | Zeile 40: | ||
-- local globals | -- local globals | ||
local URLutil = false; | local URLutil = false; | ||
Zeile 84: | Zeile 62: | ||
return URLutil; | return URLutil; | ||
end -- utilURL() | end -- utilURL() | ||
Zeile 232: | Zeile 192: | ||
r1 = false; | r1 = false; | ||
else | else | ||
r1 = | r1 = r1:gsub( "_", " " ) | ||
:gsub( " ", " " ) | |||
:gsub( " ", " " ) | |||
:gsub( " ", " " ) | |||
:gsub( " ", " " ) | |||
:gsub( " +", " " ); | |||
r1 = mw.text.decode( r1 ); | |||
end | end | ||
end | end | ||
Zeile 240: | Zeile 206: | ||
local prefix = function ( ask, ahead ) | |||
local prefix = function ( ask ) | |||
-- Interprete prefix of language or project type | -- Interprete prefix of language or project type | ||
-- Precondition: | -- Precondition: | ||
-- ask -- string, with presumable prefix | -- ask -- string, with presumable prefix | ||
-- ahead -- true, if first segment | |||
-- Postcondition: | -- Postcondition: | ||
-- Returns string,string or nil | -- Returns string,string or nil | ||
Zeile 326: | Zeile 245: | ||
}; | }; | ||
local s = mw.text.trim( ask ); | local s = mw.text.trim( ask ); | ||
if s | if s == "" then | ||
if ahead then | |||
r1 = "lead"; | |||
r2 = true; | |||
end | |||
else | |||
local p; | local p; | ||
s = s:lower(); | s = s:lower(); | ||
Zeile 405: | Zeile 329: | ||
end | end | ||
else | else | ||
s = string.format( "%%% | s = string.format( "%%%2X", k ); | ||
end | end | ||
r = string.format( "%s%s%s", | r = string.format( "%s%s%s", | ||
Zeile 422: | Zeile 346: | ||
-- Precondition: | -- Precondition: | ||
-- adjust -- string, with URL or domain/path or bracketed link | -- adjust -- string, with URL or domain/path or bracketed link | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns string, with bracketed link | -- Returns string, with bracketed link | ||
Zeile 436: | Zeile 359: | ||
host = URLutil.getHost( adjust ); | host = URLutil.getHost( adjust ); | ||
if not host then | if not host then | ||
url = "://" .. adjust | url = "http://" .. adjust; | ||
host = URLutil.getHost( url ); | host = URLutil.getHost( url ); | ||
end | end | ||
Zeile 529: | Zeile 448: | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns string, with suitable link title | -- Returns string, with suitable link title | ||
return attempt:gsub( "\n", " " ) | |||
:gsub( "%[", "[" ) | |||
:gsub( "%]", "]" ) | |||
:gsub( "|", "|" ); | |||
end -- WLink.getEscapedTitle() | end -- WLink.getEscapedTitle() | ||
Zeile 585: | Zeile 503: | ||
r = find( "file" ); | r = find( "file" ); | ||
if not r then | if not r then | ||
local trsl = mw.site.namespaces[ 6 ]; | local trsl = mw.site.namespaces[6]; | ||
r = find( trsl.name ); | r = find( trsl.name ); | ||
if not r then | if not r then | ||
Zeile 744: | Zeile 662: | ||
if k > 1 then | if k > 1 then | ||
n = n - k; | n = n - k; | ||
i = j + k | i = j + k; | ||
j = i - 1; | j = i - 1; | ||
suffix = r:sub( j ); | suffix = r:sub( j ); | ||
Zeile 792: | Zeile 710: | ||
break; -- while true | break; -- while true | ||
end | end | ||
end | end -- while true | ||
return r; | return r; | ||
end -- WLink.getPlain() | end -- WLink.getPlain() | ||
Zeile 989: | Zeile 907: | ||
function WLink.isBracketedLink( attempt ) | |||
-- Does attempt match a bracketed link? | |||
function WLink.isBracketedLink( attempt ) | |||
-- Does attempt match a bracketed link? | |||
-- Precondition: | -- Precondition: | ||
-- attempt -- string, with presumable link somewhere | -- attempt -- string, with presumable link somewhere | ||
Zeile 1.203: | Zeile 1.098: | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns boolean | -- Returns boolean | ||
local | local s, r = WLink.getTarget( attempt ); | ||
if r then | if r then | ||
r = true; | |||
end | end | ||
return r; | return r; | ||
Zeile 1.220: | Zeile 1.107: | ||
function WLink. | function WLink.isWikilink( attempt ) | ||
-- | -- Does attempt match a wikilink? | ||
-- Precondition: | -- Precondition: | ||
-- attempt -- string, with presumable | -- attempt -- string, with presumable link somewhere | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns boolean | -- Returns boolean | ||
local | local s, m = WLink.getTarget( attempt ); | ||
return ( m == 2 ); | |||
end -- WLink.isWikilink() | |||
end -- WLink. | |||
function WLink. | function WLink.wikilink( attempt ) | ||
-- Retrieve wikilink components | |||
-- | |||
-- Precondition: | -- Precondition: | ||
-- attempt -- string, with presumable link | -- attempt -- string, with presumable link | ||
-- expected to be enclosed in "[[" "]]" | -- expected to be enclosed in "[[" "]]" | ||
-- else wikilink | -- else wikilink | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns | -- Returns table or false | ||
local | -- table of assignments with { type, value } | ||
local s = | -- type is one of "lead", | ||
-- "project", "lang", | |||
if | -- "ns", "space", "title" | ||
-- false if nothing found | |||
local s = contentWikilink( attempt ); | |||
local got, n, r; | |||
if not s then | |||
s = attempt; | |||
end | |||
i = s:find( "|", 1, true ); | |||
if i then | |||
s = s:sub( 1, i - 1 ); | |||
end | |||
got = mw.text.split( s, ":" ); | |||
n = table.maxn( got ); | |||
if n == 1 then | |||
r = { title = mw.text.trim( s ) }; | |||
else | else | ||
local j, k, o, v; | |||
r = { title = "" }; | |||
if n > 4 then | |||
k = 4; | |||
else | |||
k = n - 1; | |||
end | end | ||
j = k; | |||
if | for i = 1, j do | ||
s = mw.text.trim( got[ i ] ); | |||
if s ~= "" then | |||
o = mw.site.namespaces[ mw.text.trim( got[ i ] ) ]; | |||
if o then | |||
r.ns = o.id; | |||
r.space = o.name; | |||
k = i + 1; | |||
j = i - 1; | |||
break; -- for i | |||
end | |||
end | end | ||
end -- for i | |||
for i = 1, j do | |||
o, v = prefix( got[ i ], ( i == 1 ) ); | |||
if o then | |||
end | if r[ o ] then | ||
k = i; | |||
break; -- for i | |||
else | |||
r[ o ] = v; | |||
end | |||
else | |||
k = i; | |||
break; -- for i | |||
end | end | ||
end -- for i | end -- for i | ||
for i = | for i = k, n do | ||
r.title = r.title .. got[ i ]; | |||
if | if i < n then | ||
r.title = r.title .. ":"; | |||
end | end | ||
end -- for i | end -- for i | ||
end | end | ||
if r.lead and | if r.lead and | ||
( r.project or | ( r.project or not r.title or | ||
( not r.lang and r.ns ~= 6 and r.ns ~= 14 ) ) then | ( not r.lang and r.ns ~= 6 and r.ns ~= 14 ) ) then | ||
r.lead = false; | r.lead = false; | ||
Zeile 1.527: | Zeile 1.216: | ||
local vsn = ent:formatPropertyValues( "P348" ); | local vsn = ent:formatPropertyValues( "P348" ); | ||
if type( vsn ) == "table" and | if type( vsn ) == "table" and | ||
type( vsn.value ) == "string" | type( vsn.value) == "string" and | ||
vsn.value ~= "" then | vsn.value ~= "" then | ||
r = vsn.value; | r = vsn.value; | ||
Zeile 1.558: | Zeile 1.247: | ||
local s = false; | local s = false; | ||
local r = false; | local r = false; | ||
local | local space; | ||
for k, v in pairs( frame.args ) do | for k, v in pairs( frame.args ) do | ||
if k == 1 then | if k == 1 then | ||
Zeile 1.566: | Zeile 1.255: | ||
s = mw.text.trim( v ); | s = mw.text.trim( v ); | ||
end | end | ||
elseif ( k == 2 | elseif ( k == 2 and action == "getNamespaced" ) or | ||
( k == "space" and action == "ansiPercent" ) then | ( k == "space" and action == "ansiPercent" ) then | ||
if v ~= "" then | if v ~= "" then | ||
space = v; | space = v; | ||
end | end | ||
elseif k ~= "template" then | elseif k ~= "template" then | ||
lucky = false; | lucky = false; | ||
Zeile 1.594: | Zeile 1.272: | ||
if lucky then | if lucky then | ||
if s or lone then | if s or lone then | ||
lucky, r = pcall( WLink[ action ], s, space | lucky, r = pcall( WLink[ action ], s, space ); | ||
else | else | ||
r = "Parameter missing"; | r = "Parameter missing"; | ||
Zeile 1.677: | Zeile 1.355: | ||
p.getWeblink = function ( frame ) | p.getWeblink = function ( frame ) | ||
return Template( frame, "getWeblink" ); | return Template( frame, "getWeblink" ); | ||
end | end | ||
p.isBracketedLink = function ( frame ) | p.isBracketedLink = function ( frame ) | ||
Zeile 1.710: | Zeile 1.385: | ||
p.isValidLink = function ( frame ) | p.isValidLink = function ( frame ) | ||
return Template( frame, "isValidLink" ); | return Template( frame, "isValidLink" ); | ||
end | end | ||
p.isWeblink = function ( frame ) | p.isWeblink = function ( frame ) | ||
Zeile 1.719: | Zeile 1.391: | ||
p.isWikilink = function ( frame ) | p.isWikilink = function ( frame ) | ||
return Template( frame, "isWikilink" ); | return Template( frame, "isWikilink" ); | ||
end | end | ||
p.failsafe = function ( frame ) | p.failsafe = function ( frame ) |