Module:For: Difference between revisions

From Wikitia
Jump to navigation Jump to search
enwiki>Wikitia moderator
m 1 revision imported
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:
local mHatlist = require('Module:Hatnote list')
local mHatlist = require('Module:Hatnote list')
local mHatnote = require('Module:Hatnote')
local mHatnote = require('Module:Hatnote')
local yesNo = require('Module:Yesno')
 
-- FIX: Import yesno function correctly
local yesNo
local success, result = pcall(require, 'Module:Yesno')
if success and type(result) == "table" then
    yesNo = result._yesno or result.yesno
    if type(yesNo) ~= "function" then
        yesNo = function(val, default)
            if val == nil then return default end
            if type(val) == "boolean" then return val end
            if type(val) == "string" then
                val = val:lower():match('^%s*(.-)%s*$') or ""
                if val == 'yes' or val == 'y' or val == 'true' or val == 't' or val == 'on' or val == '1' then
                    return true
                elseif val == 'no' or val == 'n' or val == 'false' or val == 'f' or val == 'off' or val == '0' then
                    return false
                end
            elseif type(val) == "number" then
                if val == 1 then return true
                elseif val == 0 then return false end
            end
            return default
        end
    end
else
    yesNo = function(val, default)
        if val == nil then return default end
        if type(val) == "boolean" then return val end
        if type(val) == "string" then
            val = val:lower():match('^%s*(.-)%s*$') or ""
            if val == 'yes' or val == 'y' or val == 'true' or val == 't' or val == 'on' or val == '1' then
                return true
            elseif val == 'no' or val == 'n' or val == 'false' or val == 'f' or val == 'off' or val == '0' then
                return false
            end
        elseif type(val) == "number" then
            if val == 1 then return true
            elseif val == 0 then return false end
        end
        return default
    end
end
 
local p = {}
local p = {}



Latest revision as of 12:02, 1 May 2026

Documentation for this module may be created at Module:For/doc

local mArguments --initialize lazily
local mHatlist = require('Module:Hatnote list')
local mHatnote = require('Module:Hatnote')

-- FIX: Import yesno function correctly
local yesNo
local success, result = pcall(require, 'Module:Yesno')
if success and type(result) == "table" then
    yesNo = result._yesno or result.yesno
    if type(yesNo) ~= "function" then
        yesNo = function(val, default)
            if val == nil then return default end
            if type(val) == "boolean" then return val end
            if type(val) == "string" then
                val = val:lower():match('^%s*(.-)%s*$') or ""
                if val == 'yes' or val == 'y' or val == 'true' or val == 't' or val == 'on' or val == '1' then
                    return true
                elseif val == 'no' or val == 'n' or val == 'false' or val == 'f' or val == 'off' or val == '0' then
                    return false
                end
            elseif type(val) == "number" then
                if val == 1 then return true
                elseif val == 0 then return false end
            end
            return default
        end
    end
else
    yesNo = function(val, default)
        if val == nil then return default end
        if type(val) == "boolean" then return val end
        if type(val) == "string" then
            val = val:lower():match('^%s*(.-)%s*$') or ""
            if val == 'yes' or val == 'y' or val == 'true' or val == 't' or val == 'on' or val == '1' then
                return true
            elseif val == 'no' or val == 'n' or val == 'false' or val == 'f' or val == 'off' or val == '0' then
                return false
            end
        elseif type(val) == "number" then
            if val == 1 then return true
            elseif val == 0 then return false end
        end
        return default
    end
end

local p = {}

--Implements {{For}} from the frame
--uses capitalized "For" to avoid collision with Lua reserved word "for"
function p.For (frame)
	mArguments = require('Module:Arguments')
	return p._For(mArguments.getArgs(frame))
end

--Implements {{For}} but takes a manual arguments table
function p._For (args)
	local use = args[1]
	if (not use) then
		return mHatnote.makeWikitextError(
			'no context parameter provided. Use {{other uses}} for "other uses" hatnotes.',
			'Template:For#Errors',
			args.category
		)
	end
	local pages = {}
	function two (a, b) return a, b, 1 end --lets us run ipairs from 2
	for k, v in two(ipairs(args)) do table.insert(pages, v) end
	local title = mw.title.getCurrentTitle()
	local skipCat = title.isTalkPage or title.namespace == 2 --don't categorise talk pages and userspace
	local oddCat = skipCat and '' or '[[Category:Hatnote templates using unusual parameters]]'
	local category = yesNo(args.category)
	return mHatnote._hatnote(
		mHatlist.forSeeTableToString({{use = use, pages = pages}}),
		{selfref = args.selfref}
	) .. (
			(use == 'other uses') and ((category == true) or (category == nil)) and
			oddCat or ''
		)
end

return p