Modul:WLink: Unterschied zwischen den Versionen
updates
wp>PerfektesChaos (+) |
wp>PerfektesChaos (updates) |
||
Zeile 1: | Zeile 1: | ||
--[=[ 2014- | --[=[ 2014-10-08 | ||
WLink | WLink | ||
]=] | ]=] | ||
Zeile 174: | Zeile 174: | ||
function target( attempt ) | function target( attempt, lonely ) | ||
-- Retrieve first target (wikilink or URL), or entire string | -- Retrieve first target (wikilink or URL), or entire string | ||
-- Precondition: | -- Precondition: | ||
-- attempt -- string, with presumable link somewhere | -- attempt -- string, with presumable link somewhere | ||
-- lonely -- remove fragment, if true | |||
-- Postcondition: | -- Postcondition: | ||
-- Returns string, number | -- Returns string, number | ||
-- string, with detected link target, or entire | -- string, with detected link target, or entire | ||
-- number, with number of brackets, if found, or 2 | -- number, with number of brackets, if found, or 2 | ||
local r1, r2 = WLink.getTarget( attempt ); | local r1, r2 = WLink.getTarget( attempt ); | ||
if not r1 then | if not r1 then | ||
r1 = mw.text.trim( attempt ); | r1 = mw.text.trim( attempt ); | ||
r2 = 2; | r2 = 2; | ||
end | |||
if lonely then | |||
local i = r1:find( "#", 1, true ) | |||
if i == 1 then | |||
r1 = ""; | |||
elseif i then | |||
r1 = r1:sub( 1, i - 1 ); | |||
end | |||
end | end | ||
return r1, r2; | return r1, r2; | ||
Zeile 227: | Zeile 235: | ||
return r; | return r; | ||
end -- WLink.ansiPercent() | end -- WLink.ansiPercent() | ||
function WLink.getArticleBase( attempt ) | |||
-- Retrieve generic article title, no fragment nor brackets | |||
-- Precondition: | |||
-- attempt -- string, with wikilink or page title | |||
-- current page title, if missing | |||
-- Postcondition: | |||
-- Returns string, with identified lemma, or all | |||
-- false on invalid format | |||
local r; | |||
if attempt then | |||
local m; | |||
r, m = target( attempt, true ); | |||
if m ~= 2 then | |||
r = false; | |||
end | |||
else | |||
r = mw.title.getCurrentTitle().text; | |||
end | |||
if r then | |||
local sub = r:match( "^(.*%S) *%(.+%)$" ); | |||
if sub then | |||
r = sub; | |||
end | |||
end | |||
return r; | |||
end -- WLink.getArticleBase() | |||
function WLink.getBaseTitle( attempt ) | function WLink.getBaseTitle( attempt ) | ||
-- Retrieve last segment in subpage, no | -- Retrieve last segment in subpage, no fragment | ||
-- Precondition: | -- Precondition: | ||
-- attempt -- string, with wikilink or page title | -- attempt -- string, with wikilink or page title | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns string, with identified segment, or all | -- Returns string, with identified segment, or all | ||
local r | local r; | ||
local s, m = target( attempt ); | local s, m = target( attempt, true ); | ||
if m == 2 then | if m == 2 then | ||
local sub = s:match( "/([^/]+)$" ); | local sub = s:match( "/([^/]+)$" ); | ||
Zeile 245: | Zeile 282: | ||
r = s; | r = s; | ||
end | end | ||
else | |||
r = false; | |||
end | end | ||
return r; | return r; | ||
Zeile 301: | Zeile 340: | ||
r = find( trsl.name ); | r = find( trsl.name ); | ||
if not r then | if not r then | ||
trsl = trsl.aliases; | |||
for k, v in pairs( trsl ) do | |||
r = find( v ); | |||
if r then | |||
break; -- for k, v | |||
end | |||
end -- for k, v | |||
end | |||
end | end | ||
end | end | ||
Zeile 680: | Zeile 719: | ||
local function Template( frame, action, leave ) | local function Template( frame, action, leave, lone ) | ||
-- Run actual code from template transclusion | -- Run actual code from template transclusion | ||
-- Precondition: | -- Precondition: | ||
Zeile 686: | Zeile 725: | ||
-- action -- string, with function name | -- action -- string, with function name | ||
-- leave -- true: keep whitespace around | -- leave -- true: keep whitespace around | ||
-- lone -- true: permit call without parameters | |||
-- Postcondition: | -- Postcondition: | ||
-- Return string; might be error message | -- Return string; might be error message | ||
Zeile 710: | Zeile 750: | ||
end -- for k, v | end -- for k, v | ||
if lucky then | if lucky then | ||
if s then | if s or lone then | ||
lucky, r = pcall( WLink[ action ], s ); | lucky, r = pcall( WLink[ action ], s ); | ||
else | else | ||
Zeile 732: | Zeile 772: | ||
p.ansiPercent = function ( frame ) | p.ansiPercent = function ( frame ) | ||
return Template( frame, "ansiPercent" ); | return Template( frame, "ansiPercent" ); | ||
end | |||
p.getArticleBase = function ( frame ) | |||
return Template( frame, "getArticleBase", false, true ); | |||
end | end | ||
p.getBaseTitle = function ( frame ) | p.getBaseTitle = function ( frame ) |