Module:WP
Appearance
![]() | This module depends on the following other modules: |
Implements {{WP}}
local p = {}
require('Module:No globals');
local getArgs = require ('Module:Arguments').getArgs;
local mRedirect = require ('Module:Redirect')
--[[--------------------------< 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 like
string.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
-- Return blank text, or an error message if requested
--local function err(text)
-- if text then error(text, 2) end
-- return ""
--end
-- Get a redirect target (or nil if not a redirect) without using the expensive title object property .isRedirect
local function getRedirectTarget(titleObject)
local content = titleObject:getContent()
if not content then return nil end
return mRedirect.getTargetFromText(content)
end
-- Get a page's content, following redirects, and processing file description pages for files.
-- Also returns the page name, or the target page name if a redirect was followed, or false if no page found
local function getContent(page, frame)
local title = mw.title.new(page) -- Read description page (for :File:Foo rather than File:Foo)
if not title then return false, false end
local redir = getRedirectTarget(title)
if redir then title = mw.title.new(redir) end
return title:getContent(), redir or title.prefixedText
end
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('%[%[.-|(.-)%]%]') then
wikilink = wikilink:match('%[%[.-|(.-)%]%]') -- extract label from complex [[link|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
-- Main function returns a string value: nutshell text from the {{nutshell}} template on the given page.
--local function _main(pagename, frame)
local function nutshell_text_get (frame)
local pagename = frame.args[1]; -- template takes only one argument
pagename = mw.text.trim (pagename);
if not pagename or '' == pagename then
return nil, 'No page name given'
end
pagename = 'WP:' .. pagename; -- add namespace; TODO: better way to do this?
local content, normalisedPagename = getContent(pagename)
if not normalisedPagename then
return nil, 'No title for page name ' .. pagename;
end
if content then
local isStub = mw.ustring.find(content, "%s*{{[^{|}]*%-[Ss]tub%s*}}")
if isStub then content = nil end
end
if not content then
return nil, 'Cannot read a valid page: page name is ' .. pagename;
end
local templatePatterns = {
"{{%s*[Nn]utshell%s*|",
"{{%s*[Pp]olicy in a nutshell%s*|",
"{{%s*[Pp]olicy proposal in a nutshell%s*|",
"{{%s*[Ii]n a nutshell%s*|",
"{{%s*[Ii]nanutshell%s*|",
"{{%s*[Gg]uideline in a nutshell%s*|",
"{{%s*[Gg]uideline one liner%s*|",
"{{%s*[Nn]aming convention in a nutshell%s*|",
"{{%s*[Nn]utshell2%s*|",
"{{%s*[Pp]roposal in a nutshell%s*|",
"{{%s*[Ee]ssay in a nutshell%s*|"
}
local nutshell
for i, pattern in ipairs (templatePatterns) do
local pos = mw.ustring.find (content, pattern)
if pos then
nutshell = mw.ustring.match (content, '%b{}', pos)
break
end
end
if not nutshell then
-- return nil, 'Page has no nutshell text: ' .. pagename;
return; -- this is not an error
end
nutshell = nutshell:gsub ('|%s*title%s*=%s*[^|]*', ''); -- remove title parameter and value
-- Remove outer template.
nutshell = nutshell:gsub ("{{[%w ]*|(.*)}}", "%1")
for t in nutshell:gmatch('%b{}') do -- get an embedded template wikilink
nutshell = nutshell:gsub('%b{}', '__57r1P__', 1) -- install a marker
local replacement = frame:preprocess (t); -- get the template's rendering
replacement = escape_lua_magic_chars(replacement); -- in case there are lua magic characters in replacement
nutshell = nutshell:gsub('__57r1P__', replacement, 1) -- replace the marker with the appropriate text
end
-- Remove wikilinks.
nutshell = wikilink_strip (nutshell)
-- Replace `|` with `. `, for templates like {{nutshell|point one|point two|point three}}.
--nutshell, _ = string.gsub(nutshell, "|", ". ")
-- Change double quotes to single quotes, so that tooltips work properly.
nutshell = nutshell:gsub ('"', "'"):gsub ('%b<>', ''); -- convert quotes then remove html-like tags
return nutshell
end
--function p.main(pagename) return _main(pagename) end
--function p.invoke(frame) return frame:preprocess(_main(frame.args[1], frame)) end
function p.invoke(frame)
local out = {}
local pagename = mw.text.trim (frame.args[1]); -- TODO: error check this; no point in continuing without properly formed pagename
local nutshell, error_msg = nutshell_text_get (frame);
if error_msg then
return '<span class="error" style="font-size:100%">' .. error_msg .. '</span>'; --
end
table.insert (out, '[[WP:'); -- open wikilink
table.insert (out, pagename); -- add pagename
if nutshell then
table.insert (out, '|<span title="'); -- pipe, then start the opening span
table.insert (out, nutshell); -- add nutshell text
table.insert (out, '" class="rt-commentedText" style="border-bottom:1px dotted">'); --finish the opening span
table.insert (out, 'WP:'); -- begin pagename
table.insert (out, pagename); -- finish pagename
table.insert (out, '</span>'); -- close the span
end
table.insert (out, ']]'); -- close the wikilink
return table.concat (out); -- concatenate and done
end
return p