Modul:WLink: Unterschied zwischen den Versionen
akt. gem. Special:Diff/181153700/next
w>Schniggendiller (c/p aus https://de.wikipedia.beta.wmflabs.org/w/index.php?title=Modul:WLink&oldid=22453 (gemäß Spezial:PermaLink/179296556/1× c&p TIA)) |
w>Mabschaaf (akt. gem. Special:Diff/181153700/next) |
||
| Zeile 1: | Zeile 1: | ||
local WLink = { suite = "WLink", | local WLink = { suite = "WLink", | ||
serial = "2018- | serial = "2018-09-22", | ||
item = 19363224 }; | item = 19363224 }; | ||
--[=[ | --[=[ | ||
| Zeile 32: | Zeile 32: | ||
isValidLink() | isValidLink() | ||
isWikilink() | isWikilink() | ||
pageTarget() | |||
wikilink() | wikilink() | ||
failsafe() | failsafe() | ||
| Zeile 62: | Zeile 63: | ||
return URLutil; | return URLutil; | ||
end -- utilURL() | end -- utilURL() | ||
local cleanWikilink = function ( access ) | |||
-- Refine wikilink spacing and decode | |||
-- Precondition: | |||
-- access -- string, with presumable link | |||
-- Postcondition: | |||
-- Returns string, with pretty target | |||
local r = access:gsub( "_", " " ) | |||
:gsub( " ", " " ) | |||
:gsub( " ", " " ) | |||
:gsub( " ", " " ) | |||
:gsub( " ", " " ) | |||
:gsub( "%s+", " " ); | |||
r = mw.text.decode( r ); | |||
return r; | |||
end -- cleanWikilink() | |||
| Zeile 192: | Zeile 211: | ||
r1 = false; | r1 = false; | ||
else | else | ||
r1 = | r1 = cleanWikilink( r1 ); | ||
end | end | ||
end | end | ||
| Zeile 206: | Zeile 219: | ||
local prefix = function ( ask | 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 | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns string,string or nil | -- Returns string,string or nil | ||
| Zeile 245: | Zeile 257: | ||
}; | }; | ||
local s = mw.text.trim( ask ); | local s = mw.text.trim( ask ); | ||
if s | if s ~= "" then | ||
local p; | local p; | ||
s = s:lower(); | s = s:lower(); | ||
| Zeile 710: | Zeile 717: | ||
break; -- while true | break; -- while true | ||
end | end | ||
end -- while true | end -- while true | ||
return r; | return r; | ||
end -- WLink.getPlain() | end -- WLink.getPlain() | ||
| Zeile 1.116: | Zeile 1.123: | ||
return ( m == 2 ); | return ( m == 2 ); | ||
end -- WLink.isWikilink() | end -- WLink.isWikilink() | ||
function WLink.pageTarget( attempt ) | |||
-- Create standardized wikilink target of a page | |||
-- Precondition: | |||
-- attempt -- string, with presumable link | |||
-- expected to be enclosed in "[[" "]]" | |||
-- else wikilink | |||
-- Postcondition: | |||
-- Returns string with link target | |||
local p = WLink.wikilink( attempt ); | |||
local r; | |||
if p then | |||
local lead; | |||
if p.ns then | |||
if p.lang or | |||
( p.project and ( p.project == "commons" or | |||
p.project == "d" or | |||
p.project == "meta" or | |||
p.project == "mw" ) ) then | |||
p.space = mw.site.namespaces[ p.ns ].canonicalName; | |||
end | |||
lead = ( p.ns == 6 or p.ns == 14 ); | |||
end | |||
if p.lang then | |||
lead = true; | |||
end | |||
if p.project then | |||
r = p.project .. ":"; | |||
elseif lead then | |||
r = ":"; | |||
else | |||
r = ""; | |||
end | |||
if p.lang then | |||
r = string.format( "%s%s:", r, p.lang ); | |||
end | |||
if p.space then | |||
r = string.format( "%s%s:", r, p.space ); | |||
end | |||
if p.title then | |||
r = r .. p.title; | |||
end | |||
if r == "" then | |||
r = false; | |||
end | |||
end | |||
if not r then | |||
r = string.format( ":%s:", | |||
mw.language.getContentLanguage():getCode() ); | |||
end | |||
return r; | |||
end -- WLink.pageTarget() | |||
| Zeile 1.132: | Zeile 1.193: | ||
-- "ns", "space", "title" | -- "ns", "space", "title" | ||
-- false if nothing found | -- false if nothing found | ||
local s = contentWikilink( attempt ); | local s = contentWikilink( attempt or "" ); | ||
local got, n, r; | local got, n, r; | ||
if not s then | if not s then | ||
s = attempt; | s = attempt; | ||
end | |||
if s:find( "%", 1, true ) then | |||
s = mw.uri.decode( s, "PATH" ); | |||
end | end | ||
i = s:find( "|", 1, true ); | i = s:find( "|", 1, true ); | ||
| Zeile 1.148: | Zeile 1.212: | ||
local j, k, o, v; | local j, k, o, v; | ||
r = { title = "" }; | r = { title = "" }; | ||
while ( got[ 1 ] == "" ) do | |||
r.lead = true; | |||
table.remove( got, 1 ); | |||
n = n - 1; | |||
end -- while got[ 1 ] == "" | |||
if n > 4 then | if n > 4 then | ||
k = 4; | k = 4; | ||
elseif n > 1 then | |||
k = n - 1; | |||
else | else | ||
k = | k = 1; | ||
end | end | ||
j = k; | j = k; | ||
| Zeile 1.168: | Zeile 1.239: | ||
end -- for i | end -- for i | ||
for i = 1, j do | for i = 1, j do | ||
o, v = prefix( got[ i ] | o, v = prefix( got[ i ] ); | ||
if o then | if o then | ||
if r[ o ] then | if r[ o ] then | ||
| Zeile 1.174: | Zeile 1.245: | ||
break; -- for i | break; -- for i | ||
else | else | ||
if i >= k then | |||
k = i + 1; | |||
end | |||
r[ o ] = v; | r[ o ] = v; | ||
end | end | ||
else | else | ||
if i == 1 and r.ns then | |||
r.ns = false; | |||
r.space = false; | |||
end | |||
k = i; | k = i; | ||
break; -- for i | break; -- for i | ||
end | end | ||
end -- for i | end -- for i | ||
for i = k, n do | if k > 0 then | ||
for i = k, n do | |||
r.title = r.title .. got[ i ]; | |||
if i < n then | |||
r.title = r.title .. ":"; | |||
end | |||
end -- for i | |||
end | |||
end | end | ||
r.title = cleanWikilink( r.title ); | |||
if r.lead and | if r.lead and | ||
( r.project | ( r.project 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.216: | Zeile 1.297: | ||
local vsn = ent:formatPropertyValues( "P348" ); | local vsn = ent:formatPropertyValues( "P348" ); | ||
if type( vsn ) == "table" and | if type( vsn ) == "table" and | ||
type( vsn.value) == "string" and | type( vsn.value ) == "string" and | ||
vsn.value ~= "" then | vsn.value ~= "" then | ||
r = vsn.value; | r = vsn.value; | ||
| Zeile 1.391: | Zeile 1.472: | ||
p.isWikilink = function ( frame ) | p.isWikilink = function ( frame ) | ||
return Template( frame, "isWikilink" ); | return Template( frame, "isWikilink" ); | ||
end | |||
p.pageTarget = function ( frame ) | |||
return Template( frame, "pageTarget" ); | |||
end | end | ||
p.failsafe = function ( frame ) | p.failsafe = function ( frame ) | ||