Jump to content

Module:Sorted plain list

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Frietjes (talk | contribs) at 13:51, 25 March 2017. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- This module generates a sorted plain list
-- It was created as a modification of [[Module:Sort]]
local p = {}

local lang = mw.getContentLanguage()

-- This function was copied/modified from [[Module:Wikidata]]
local function getRawValue(frame, propertyID)
	local entity = mw.wikibase.getEntityObject()
	local claims
	if entity and entity.claims then claims = entity.claims[propertyID] end
	if claims then
		local result = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value

		-- if number type: remove thousand separators, bounds and units
		if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "quantity") then
			result = mw.ustring.gsub(result, "(%d),(%d)", "%1%2")
			result = mw.ustring.gsub(result, "(%d)±.*", "%1")
		end
		return result
	else
		return {""}
	end
end

function p.asc(frame)
    local items
    if frame.args.propertyID then
    	items = getRawValue(frame, frame.args.propertyID)
    else
    	items = mw.text.split( frame.args[1] or '', frame.args[2] or ',', true)
    end
    if (frame.args['type'] or '') == 'number' then
    	table.sort( items, function (a, b) return ((lang:parseFormattedNumber(a) or math.huge) < (lang:parseFormattedNumber(b) or math.huge)) end )
    else
	    table.sort( items )
    end
    return '<div class="plainlist"><ul><li>' .. table.concat( items, "</li><li>" ) .. '</li></ul></div>'
end

function p.desc(frame)
    if frame.args.propertyID then
    	items = getRawValue(frame, frame.args.propertyID)
    else
    	items = mw.text.split( frame.args[1] or '', frame.args[2] or ',', true)
    end
    if (frame.args['type'] or '') == 'number' then
    	table.sort( items, function (a, b) return ((lang:parseFormattedNumber(a) or math.huge) > (lang:parseFormattedNumber(b) or math.huge)) end )
    else
    	table.sort( items, function (a, b) return a > b end )
    end
    return '<div class="plainlist"><ul><li>' .. table.concat( items, "</li><li>" ) .. '</li></ul></div>'
end

return p