Modul:WLink: Unterschied zwischen den Versionen
2018-09-23 Special:Diff/181178730/next
K (25 Versionen importiert: Lua) |
de>XenonX3 (2018-09-23 Special:Diff/181178730/next) |
||
Zeile 1: | Zeile 1: | ||
local WLink = { suite = "WLink", | local WLink = { suite = "WLink", | ||
serial = "2018- | serial = "2018-09-23", | ||
item = 19363224 }; | item = 19363224 }; | ||
--[=[ | --[=[ | ||
Zeile 21: | Zeile 21: | ||
getTitle() | getTitle() | ||
getWeblink() | getWeblink() | ||
getWikilink() | |||
isBracketedLink() | isBracketedLink() | ||
isBracketedURL() | isBracketedURL() | ||
Zeile 32: | Zeile 33: | ||
isValidLink() | isValidLink() | ||
isWikilink() | isWikilink() | ||
pageTarget() | |||
wikilink() | wikilink() | ||
failsafe() | failsafe() | ||
Zeile 62: | Zeile 64: | ||
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 212: | ||
r1 = false; | r1 = false; | ||
else | else | ||
r1 = | r1 = cleanWikilink( r1 ); | ||
end | end | ||
end | end | ||
Zeile 206: | Zeile 220: | ||
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 258: | ||
}; | }; | ||
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 448: | Zeile 456: | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns string, with suitable link title | -- Returns string, with suitable link title | ||
return | local s = mw.text.trim( attempt ); | ||
return s:gsub( "\n", " " ) | |||
:gsub( "%[", "[" ) | |||
:gsub( "%]", "]" ) | |||
:gsub( "|", "|" ); | |||
end -- WLink.getEscapedTitle() | end -- WLink.getEscapedTitle() | ||
Zeile 710: | Zeile 719: | ||
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 904: | Zeile 913: | ||
return r; | return r; | ||
end -- WLink.getWeblink() | end -- WLink.getWeblink() | ||
function WLink.getWikilink( attempt, appear ) | |||
-- Retrieve bracketed link from text | |||
-- Precondition: | |||
-- attempt -- string, with current target, or plain | |||
-- appear -- string, with link title, or nil | |||
-- Postcondition: | |||
-- Returns string, with first detected link target | |||
-- false if nothing found | |||
local r = WLink.pageTarget( attempt ); | |||
if r then | |||
if appear then | |||
local show = WLink.getEscapedTitle( appear ); | |||
if show ~= r and show ~= "" then | |||
r = string.format( "%s|%s", r, show ); | |||
end | |||
end | |||
r = string.format( "[[%s]]", r ); | |||
end | |||
return r; | |||
end -- WLink.getWikilink() | |||
Zeile 1.116: | Zeile 1.148: | ||
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.218: | ||
-- "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.237: | ||
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.264: | ||
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.270: | ||
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.322: | ||
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.255: | Zeile 1.361: | ||
s = mw.text.trim( v ); | s = mw.text.trim( v ); | ||
end | end | ||
elseif ( k == 2 | elseif ( k == 2 and | ||
( action == "getNamespaced" or | |||
action == "getWikilink" ) ) or | |||
( k == "space" and action == "ansiPercent" ) then | ( k == "space" and action == "ansiPercent" ) then | ||
if v ~= "" then | if v ~= "" then | ||
Zeile 1.355: | Zeile 1.463: | ||
p.getWeblink = function ( frame ) | p.getWeblink = function ( frame ) | ||
return Template( frame, "getWeblink" ); | return Template( frame, "getWeblink" ); | ||
end | |||
p.getWikilink = function ( frame ) | |||
return Template( frame, "getWikilink" ); | |||
end | end | ||
p.isBracketedLink = function ( frame ) | p.isBracketedLink = function ( frame ) |