Jump to content

Module:Sortkey/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ahecht (talk | contribs) at 18:16, 12 September 2024 (implement demo parameter). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local getArgs = require('Module:Arguments').getArgs

local p = {}

function p._encode(sortkey)
	-- Protect against sortkey nesting.
	-- Example: {{sort|{{dts|2013|07|07}}|{{dts|1990|12|01}}}}
	if string.find(sortkey, "sortkey") or string.find(sortkey, "data-sort-value") then
		return "";
	end
    return mw.text.encode(sortkey)
end

function p.encode(frame)
	local args = getArgs(frame);
	return p._encode(args[1] or "")
end

local function valid_number(num)
	-- Return true if num is a valid number.
	-- In Scribunto (different from some standard Lua), when expressed as a string,
	-- overflow or other problems are indicated with text like "inf" or "nan"
	-- which are regarded as invalid here (each contains "n").
	if type(num) == 'number' and tostring(num):find('n', 1, true) == nil then
		return true
	end
end

function p._sortKeyForNumber(value)
	local sortkey
	if not valid_number(value) then
		if value < 0 then
			sortkey = '1000000000000000000'
		else
			sortkey = '9000000000000000000'
		end
	elseif value == 0 then
		sortkey = '5000000000000000000'
	else
		local mag = math.floor(math.log10(math.abs(value)) + 1e-14)
		local prefix
		if value > 0 then
			prefix = 7000 + mag
		else
			prefix = 2999 - mag
			value = value + 10^(mag+1)
		end
		sortkey = string.format('%d', prefix) .. string.format('%015.0f', math.floor(value * 10^(math.min(28,14-mag))))
	end
	return sortkey;
end

function p.sortKeyForNumber(frame)
	local args = getArgs(frame)
	return p._sortKeyForNumber(args[1] or "")
end

function p._sortname(args)
	local encode = p._encode( ((args[4] or args.sort or '') ~= '')
		and (args[4] or args.sort)
		or ((args[2] or args.last or '{{{last}}}')..', '..(args[1] or args.first or '{{{first}}}')))
	local span = (args[1] or args.first or '{{{first}}}')..' '..(args[2] or args.last or '{{{last}}}')
	if ((args.nolink or '') == '') and ((args[1] or args.first or '')..(args[2] or args.last or '')..(args.dab or '') ~= '-') then
		if (args[3] or args.link or '') ~= '' then
			span = '[[' .. args[3] or args.link .. '|' .. span .. ']]'
		elseif (args.dab or '') ~= '' then
			span = '[[' .. span .. ' (' .. args.dab .. ')|' .. span .. ']]'
		else
			span = '[[' .. span .. ']]'
		end
	end
	local cat = (mw.title.getCurrentTitle().namespace == 0 and not args.demo)
		and '[[Category:Articles with hCards]]' or ''
	span = '<span data-sort-value="' .. encode ..
		'"><span class="vcard"><span class="fn">' .. span .. 
		'</span></span></span>'
	if args.demo then
		span = '&lt;' .. string.sub(span,2,-8) .. '&lt;/span>'
	end
	return span .. cat
end

function p.sortname(frame)
	local args = getArgs(frame)
	return p._sortname(args)
end

return p