require('strict');
local getArgs = require ('Module:Arguments').getArgs;local mRedirect = require ('Module:Redirect') local namespaces =
----------------------------< E S C A P E _ L U A _ M A G I C _ C H A R S >----------------------------------
Returns a string where all of lua's magic characters have been escaped. This is important because functions likestring.gsub treat their pattern and replace strings as patterns, not literal strings.
local function escape_lua_magic_chars (argument) argument = argument:gsub("%%", "%%%%"); -- replace % with %% argument = argument:gsub("([%^%$%(%)%.%[%]%*%+%-%?])", "%%%1"); -- replace all other lua magic pattern characters return argument;end
--
local function err (error_msg) return '
' .. error_msg .. ' (help)'; -- tamer, less strident error messagesend--
local function get_content (title, label) local content; local title_obj = mw.title.new (title); -- get title object for
content = title_obj:getContent; -- get content if not content then return nil, label .. ': page does not exist: ' .. title; -- return nil when
--[=[-------------------------< W I K I L I N K _ S T R I P >-------------------------------------------------- Wikilink markup does not belong in an anchor id and can / does confuse the code that parses apart citation and harvc templates so here we remove any wiki markup: [[link|label]] -> label link -> link
]=]
local function wikilink_strip(text) for wikilink in text:gmatch('%[%b[]%]') do -- get a wikilink text = text:gsub('%[%b[]%]', '__57r1P__', 1); -- install a marker if wikilink:match ('^%[%[%s*[Ff]ile:') or wikilink:match ('^%[%[%s*[Ii]mage:') then -- if this wikilink is an image wikilink = '[IMAGE]'; -- can't display it in a tooltip so use a word; TODO: parse out alt text or caption? worth the effort? elseif wikilink:match('%[%[.-|(.-)%]%]') then wikilink = wikilink:match('%[%[.-|(.-)%]%]'); -- extract label from complex label wikilink else wikilink = wikilink:match('%[%[(.-)%]%]'); -- extract link from simple link wikilinks end wikilink = escape_lua_magic_chars(wikilink); -- in case there are lua magic characters in wikilink text = text:gsub('__57r1P__', wikilink, 1); -- replace the marker with the appropriate text end
return text;end
--title= parameter value, if present, pagename else
Because we are evaluating the content of pagename, we set the frag_flag here when the pagenamehas an anchor fragment (
local function title_make (title_param, pagename) local frag_flag; local c; local namespace; pagename, c = pagename:gsub ('#.*$', ); -- remove section fragment if any if 0 < c then frag_flag = true; -- when fragment removed, set the flag end
namespace = pagename:match ('^%a+'):upper; -- extract canonical namespace; convert to upper case for indexing pagename = pagename:gsub ('^%a+', namespaces[namespace]); -- replace with abbreviation
if
return title_param, frag_flag;end
----------------------------< T O O L T I P _ M A K E >------------------------------------------------------
assemble the tooltip (title= attribute value)
local function tooltip_make (title_param, nutshell, frag_flag) return table.concat ;end
--
local function nutshell_text_get (shortcut, frame) local content; -- content of shortcut page then content of target local target; -- name of shortcut's redirect target local msg; -- error messages go here local c; -- general purpose var holds the tally of gsub replacements made when needed local title_param; -- |title= parameter contents and value used in tooltip rendering local frag_flag; -- boolean set true when normalized page name has a fragment (WP:
content, msg = get_content (shortcut, 'shortcut'); -- get content of shortcut redirect page if msg then return nil, msg; end target = mRedirect.getTargetFromText (content); -- get redirect
local templatePatterns =