Module:WP explained

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 if not title_obj then -- title object for non-existent valid <title> will have been created; nil else return nil, label .. ': invalid title: ' .. title; -- return nil when <title> is malformed (not a valid title) end</p> <p>content = title_obj:getContent; -- get content if not content then return nil, label .. ': page does not exist: ' .. title; -- return nil when <title> does not exist elseif <i> </i></p> <h2>content then return nil, label .. ': page is empty: ' .. title; -- return nil when <title> has no content end return content;end</h2> <p>--[=[-------------------------< 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 <a href="../link/">link</a> -> link</p> <p>]=]</p> <p>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 <a href="../link/">label</a> wikilink else wikilink = wikilink:match('%[%[(.-)%]%]'); -- extract link from simple <a href="../link/">link</a> 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</p> <p>return text;end</p> <p>--title= parameter value, if present, pagename else<p>Because we are evaluating the content of pagename, we set the frag_flag here when the pagenamehas an anchor fragment (<namespace>:<pagename>#<anchor>)</p> </p> <p>local function title_make (title_param, pagename) local frag_flag; local c; local namespace; pagename, c = pagename:gsub ('#.*$', <i>); -- remove section fragment if any if 0 < c then frag_flag = true; -- when fragment removed, set the flag end</i></p> <p>namespace = pagename:match ('^%a+'):upper; -- extract canonical namespace; convert to upper case for indexing pagename = pagename:gsub ('^%a+', namespaces[namespace]); -- replace with abbreviation</p> <p>if <i> </i></p> <h2>title_param then title_param = '"' .. pagename .. '"'; -- use name of shortcut's target page when |title= missing or empty end</h2> <p>return title_param, frag_flag;end</p> <p>----------------------------< T O O L T I P _ M A K E >------------------------------------------------------<p>assemble the tooltip (title= attribute value)</p> </p> <p>local function tooltip_make (title_param, nutshell, frag_flag) return table.concat ;end</p> <p>--</p> <p>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:<page title>#<anchor name>)</p> <p>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 <target title> (page name) from content of redirect page: (#Redirect <target title>) or nil if not target then return nil, 'shortcut: ' .. shortcut .. ' is not a redirect'; end content, msg = get_content (target, 'target'); -- get content of redirect target if msg then return nil, msg; end</p> <p>local templatePatterns =</p><div class="Footer"> <div class="robots-noindex"><p>This article is licensed under the <a href="http://www.gnu.org/copyleft/fdl.html" rel="nofollow">GNU Free Documentation License</a>. It uses material from the Wikipedia article "<a href="http://en.wikipedia.org/wiki/Module%3aWP" rel="nofollow">Module:WP</a>".</p></div> <p>Except where otherwise indicated, Everything.Explained.Today is © Copyright 2009-2024, A B Cryer, All Rights Reserved. <a href="http://explained.today/cookie_policy.htm">Cookie policy</a>.</p> </div> </div> </body> </html>