Module:Portal-inline: திருத்தங்களுக்கு இடையிலான வேறுபாடு
உள்ளடக்கம் நீக்கப்பட்டது உள்ளடக்கம் சேர்க்கப்பட்டது
அடையாளம்: Undo |
No edit summary அடையாளம்: Reverted |
||
வரிசை 1: | வரிசை 1: | ||
-- determine whether we're being called from a sandbox |
|||
local getImageName = require( 'Module:Portal' ).image |
|||
local isSandbox = mw.getCurrentFrame():getTitle():find('sandbox', 1, true) |
|||
local sandbox = isSandbox and '/sandbox' or '' |
|||
local getArgs = require('Module:Arguments').getArgs |
|||
local yesno = require('Module:Yesno') |
|||
local portalModule = require('Module:Portal'..sandbox) |
|||
local getImageName = portalModule._image |
|||
local checkPortals = portalModule._checkPortals |
|||
local processPortalArgs = portalModule._processPortalArgs |
|||
local p = {} |
local p = {} |
||
-- Function to format error message and tracking category |
|||
local function checkPortalExists(portal) |
|||
-- Arguments: |
|||
return not (mw.title.makeTitle(100, portal).id == 0) |
|||
-- errMsg: string, or nil/false if no error |
|||
-- trackingCat: string for tracking category (or empty string) |
|||
local function formatError(errMsg, trackingCat) |
|||
local result = trackingCat or '' |
|||
if errMsg then |
|||
local errTag = mw.html.create('span') |
|||
errTag:addClass("error") |
|||
errTag:css("font-size",'100%') |
|||
errTag:wikitext("Error: "..errMsg) |
|||
result = tostring(errTag)..result |
|||
end |
|||
return result |
|||
end |
end |
||
local function image(portal, args) |
|||
local trackingEnabled = true |
|||
local size = args.size == "tiny" and "16x16px" or "32x28px" |
|||
return string.format('[[File:%s|class=noviewer|%s]]',getImageName(portal,true), size) |
|||
-- Check whether to do tracking in this namespace |
|||
-- Returns true unless the page is one of the banned namespaces |
|||
local function checkTrackingNamespace() |
|||
local thisPage = mw.title.getCurrentTitle() |
|||
if (thisPage.namespace == 1) -- Talk |
|||
or (thisPage.namespace == 2) -- User |
|||
or (thisPage.namespace == 3) -- User talk |
|||
or (thisPage.namespace == 5) -- Wikipedia talk |
|||
or (thisPage.namespace == 7) -- File talk |
|||
or (thisPage.namespace == 11) -- Template talk |
|||
or (thisPage.namespace == 15) -- Category talk |
|||
or (thisPage.namespace == 101) -- Portal talk |
|||
or (thisPage.namespace == 118) -- Draft |
|||
or (thisPage.namespace == 119) -- Draft talk |
|||
or (thisPage.namespace == 829) -- Module talk |
|||
then |
|||
return false |
|||
end |
|||
return true |
|||
end |
end |
||
local function link(portal, args) |
|||
-- Check whether to do tracking on this pagename |
|||
local displayName = "" |
|||
-- Returns false if the page title matches one of the banned strings |
|||
if not (args.text == "" or args.text == nil) then |
|||
-- Otherwise returns true |
|||
displayName = args.text |
|||
local function checkTrackingPagename() |
|||
elseif args.short then |
|||
local thisPage = mw.title.getCurrentTitle() |
|||
displayName = portal |
|||
local thisPageLC = mw.ustring.lower(thisPage.text) |
|||
else |
|||
if (string.match(thisPageLC, "/archive") ~= nil) then |
|||
displayName = portal .. " portal" |
|||
return false |
|||
end |
end |
||
return string.format('[[வலைவாசல்:%s|%s]]',portal,displayName) |
|||
if (string.match(thisPageLC, "/doc") ~= nil) then |
|||
return false |
|||
end |
|||
if (string.match(thisPageLC, "/test") ~= nil) then |
|||
return false |
|||
end |
|||
return true |
|||
end |
end |
||
function p._main(portals, args) |
function p._main(portals, args) |
||
local root = "" |
|||
mw.logObject(args) |
mw.logObject(args) |
||
-- ignore extra portals listed |
|||
-- If no portals have been specified, display an error and add the page to a tracking category. |
|||
-- Tracking is on by default. |
|||
-- It is disabled if any of the following is true |
|||
-- 1/ the parameter "tracking" is set to 'no, 'n', or 'false' |
|||
-- 2/ the current page fails the namespace tests in checkTrackingNamespace() |
|||
-- 3/ the current page fails the pagename tests in checkTrackingPagename() |
|||
if (args.tracking == 'no') or (args.tracking == 'n') or (args.tracking == 'false') then |
|||
trackingEnabled = false |
|||
end |
|||
if (checkTrackingNamespace() == false) then |
|||
trackingEnabled = false |
|||
end |
|||
if (checkTrackingPagename() == false) then |
|||
trackingEnabled = false |
|||
end |
|||
if not portals[1] then |
|||
root = '<span style="font-size:100%;" class="error">error: missing portal name.</span></strong>"' |
|||
if trackingEnabled then |
|||
root = root .. '[[Category:Portal templates without a parameter]][[Category:Portal-inline template without a parameter]]' |
|||
end |
|||
return tostring(root) |
|||
end |
|||
if portals[2] or portals[3] then |
|||
root = '<span style="font-size:100%;" class="error">error: Template:portal-inline accepts only one portal as a parameter</span></strong> "' |
|||
if trackingEnabled then |
|||
root = root .. '[[Category:Portal-inline template with more than one portal parameter]]' |
|||
end |
|||
end |
|||
-- Normalize all arguments |
|||
if not pcall(checkPortalExists, portals[1]) or not checkPortalExists(portals[1]) then |
|||
if args.redlinks == 'include' then args.redlinks = true end |
|||
-- Getting here means a redlinked portal has been found |
|||
for key, default in pairs({tracking=true,redlinks=false,short=false}) do |
|||
if not ((args.redlinks == 'yes') or (args.redlinks == 'y') or (args.redlinks == 'true') or (args.redlinks == 'include')) then |
|||
if args[key] == nil then args[key] = default end |
|||
-- just return if redlinks is not "yes" or similar |
|||
args[key] = yesno(args[key], default) |
|||
if trackingEnabled then |
|||
if portals[2] or portals[3] then |
|||
root = root .. '[[Category:Portal templates with redlinked portals]][[Category:Portal-inline template with redlinked portals]]' |
|||
else |
|||
root = root .. '[[Category:Portal templates with all redlinked portals]][[Category:Portal-inline template with redlinked portals]]' |
|||
end |
|||
end |
|||
return tostring(root) |
|||
end |
|||
if trackingEnabled then |
|||
root = '[[Category:Portal templates with redlinked portals]][[Category:Portal-inline template with redlinked portals]]' |
|||
end |
|||
end |
end |
||
local trackingCat = '' |
|||
if args['size'] == "tiny" then |
|||
local errMsg = nil |
|||
args['size'] = "16x16px" |
|||
else |
|||
args['size'] = "32x28px" |
|||
end |
|||
-- Check for existing categories, drop if not. |
|||
local displayName = "" |
|||
-- Possible generate tracking category & error message if needed |
|||
if not (args['text'] == "" or args['text'] == nil) then |
|||
args.minPortals = args.minPortals or 1 |
|||
displayName = args['text'] |
|||
args.maxPortals = args.maxPortals or 1 |
|||
portals, trackingCat, errMsg = checkPortals(portals,args) |
|||
displayName = portals[1] |
|||
-- use more specific tracking cat for inline portal |
|||
else |
|||
trackingCat = mw.ustring.gsub(trackingCat,"Portal templates","Portal-inline template") |
|||
displayName = portals[1] .. " வலைவாசல்" |
|||
-- either too many/few portals, or no portals left after filtering, then return |
|||
end |
|||
if errMsg or #portals == 0 then |
|||
-- display portal-inline content |
|||
return formatError(errMsg, trackingCat) |
|||
root = root .. string.format('[[File:%s|class=noviewer|%s]] [[வலைவாசல்:%s|%s]]', getImageName{ portals[1] }, args['size'], portals[1], displayName) |
|||
return tostring(root) |
|||
end |
|||
-- copied from [[Module:Portal]] |
|||
local function processPortalArgs(args) |
|||
-- This function processes a table of arguments and returns two tables: an array of portal names for processing by ipairs, and a table of |
|||
-- the named arguments that specify style options, etc. We need to use ipairs because we want to list all the portals in the order |
|||
-- they were passed to the template, but we also want to be able to deal with positional arguments passed explicitly, for example |
|||
-- {{portal-inline|Politics}}. The behaviour of ipairs is undefined if nil values are present, so we need to make sure they are all removed. |
|||
args = type(args) == 'table' and args or {} |
|||
local portals = {} |
|||
local namedArgs = {} |
|||
for k, v in pairs(args) do |
|||
if type(k) == 'number' and type(v) == 'string' then -- Make sure we have no non-string portal names. |
|||
table.insert(portals, k) |
|||
elseif type(k) ~= 'number' then |
|||
namedArgs[k] = v |
|||
end |
|||
end |
|||
table.sort(portals) |
|||
for i, v in ipairs(portals) do |
|||
portals[i] = args[v] |
|||
end |
|||
return portals, namedArgs |
|||
end |
|||
local function makeWrapper(funcName) |
|||
-- Processes external arguments and sends them to the other functions. |
|||
return function (frame) |
|||
-- If called via #invoke, use the args passed into the invoking |
|||
-- template, or the args passed to #invoke if any exist. Otherwise |
|||
-- assume args are being passed directly in from the debug console |
|||
-- or from another Lua module. |
|||
local origArgs |
|||
if type(frame.getParent) == 'function' then |
|||
origArgs = frame:getParent().args |
|||
for k, v in pairs(frame.args) do |
|||
origArgs = frame.args |
|||
break |
|||
end |
|||
else |
|||
origArgs = frame |
|||
end |
|||
-- Trim whitespace and remove blank arguments. |
|||
local args = {} |
|||
for k, v in pairs(origArgs) do |
|||
if type(v) == 'string' then |
|||
v = mw.text.trim(v) |
|||
end |
|||
if v ~= '' then |
|||
args[k] = v |
|||
end |
|||
end |
|||
return p[funcName](processPortalArgs(args)) -- passes two tables to func: an array of portal names, and a table of named arguments. |
|||
end |
end |
||
return image(portals[1],args)..' '..link(portals[1],args)..(trackingCat or '') |
|||
end |
end |
||
function p.main(frame) |
|||
for _, funcName in ipairs{'main'} do |
|||
local origArgs = getArgs(frame) |
|||
p[funcName] = makeWrapper('_' .. funcName) |
|||
local portals, args = processPortalArgs(origArgs) |
|||
return p._main(portals, args) |
|||
end |
end |
||
05:50, 11 மே 2024 இல் நிலவும் திருத்தம்
Documentation for this module may be created at Module:Portal-inline/doc
-- determine whether we're being called from a sandbox
local isSandbox = mw.getCurrentFrame():getTitle():find('sandbox', 1, true)
local sandbox = isSandbox and '/sandbox' or ''
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local portalModule = require('Module:Portal'..sandbox)
local getImageName = portalModule._image
local checkPortals = portalModule._checkPortals
local processPortalArgs = portalModule._processPortalArgs
local p = {}
-- Function to format error message and tracking category
-- Arguments:
-- errMsg: string, or nil/false if no error
-- trackingCat: string for tracking category (or empty string)
local function formatError(errMsg, trackingCat)
local result = trackingCat or ''
if errMsg then
local errTag = mw.html.create('span')
errTag:addClass("error")
errTag:css("font-size",'100%')
errTag:wikitext("Error: "..errMsg)
result = tostring(errTag)..result
end
return result
end
local function image(portal, args)
local size = args.size == "tiny" and "16x16px" or "32x28px"
return string.format('[[File:%s|class=noviewer|%s]]',getImageName(portal,true), size)
end
local function link(portal, args)
local displayName = ""
if not (args.text == "" or args.text == nil) then
displayName = args.text
elseif args.short then
displayName = portal
else
displayName = portal .. " portal"
end
return string.format('[[வலைவாசல்:%s|%s]]',portal,displayName)
end
function p._main(portals, args)
mw.logObject(args)
-- Normalize all arguments
if args.redlinks == 'include' then args.redlinks = true end
for key, default in pairs({tracking=true,redlinks=false,short=false}) do
if args[key] == nil then args[key] = default end
args[key] = yesno(args[key], default)
end
local trackingCat = ''
local errMsg = nil
-- Check for existing categories, drop if not.
-- Possible generate tracking category & error message if needed
args.minPortals = args.minPortals or 1
args.maxPortals = args.maxPortals or 1
portals, trackingCat, errMsg = checkPortals(portals,args)
-- use more specific tracking cat for inline portal
trackingCat = mw.ustring.gsub(trackingCat,"Portal templates","Portal-inline template")
-- either too many/few portals, or no portals left after filtering, then return
if errMsg or #portals == 0 then
return formatError(errMsg, trackingCat)
end
return image(portals[1],args)..' '..link(portals[1],args)..(trackingCat or '')
end
function p.main(frame)
local origArgs = getArgs(frame)
local portals, args = processPortalArgs(origArgs)
return p._main(portals, args)
end
return p