ሞድዩል:List

ካብ ዊኪፐድያ፣ ናጻ ኢንሳይክሎፐድያ

Documentation for this module may be created at ሞድዩል:List/doc

local list = {}
local categoryExists = require('Module:Page').categoryExists

-- Function that returns the elements of the list.
-- Only reported items are retrieved.
function list.GetListItems(frame)
	local Arguments = frame.args;
	local parent    = frame:getParent();

	if not Arguments[1] then
		Arguments = parent.args;
	end

	local category = Arguments['category'] or ''
	local order    = Arguments['order']   or ''

	separator   = frame.args['separator'] or Arguments['separator']
	conjunction = frame.args['conjunction'] or Arguments['conjunction']

	local ListItems = {}

	local i = 1;
	local j = 0;
	while Arguments[i] do
		local TextToShow
		if Arguments[i] and not Arguments[i]:match('^%s*$') then
			j = j + 1

			-- Add the link if it doesn't already include one
			local linkup = frame.args.linkup or parent.args.linkup

			if mw.ustring.find(Arguments[i], '%[%[') then
				linkup = 'no'
			end

			if linkup == 'yes' then
				TextToShow = Arguments[i]:match('^(.*)%s%(.*%)$') or Arguments[i]

				--Get the namespace of the arguments
				local namespace = frame.args.nspace or parent.args.nspace or frame.args['namespace'] or parent.args['namespace'] or mw.title.getCurrentTitle().nsText

				if namespace == '' then
					--The function is accessed with the namespace argument but it is unreported
					if Arguments[i] == TextToShow then
						ListItems[j] = mw.ustring.format('[[%s]]', TextToShow)
					else
						ListItems[j] = mw.ustring.format('[[%s|%s]]', Arguments[i], TextToShow)
					end
				else
					--Accessed without the namespace (in which case the namespace is taken from global) or reported.

					--Get the name of the page (removing the namespace if it has one). Similar to {{PAGENAME:Arguments[i]}}
					--For example, if arguments[i] are equal to "Template:A", pagename is reported with "A".
					local pagename

					local noError, pageObject = pcall(mw.title.new, Arguments[i])

					if noError then
						pagename = pageObject.text
					else
						pagename = Arguments[i]
					end

					-- Link taking as namespace the one received in the argument or the global.
					ListItems[j] = mw.ustring.format('[[:%s:%s|%s]]', namespace, pagename, TextToShow)
				end
			else
				TextToShow     = Arguments[i]
				ListItems[j] = mw.text.trim( TextToShow )
			end

			-- Add the delimiter
			local delimiter = frame.args.delimiter or parent.args.delimiter or frame.args.delim or parent.args.delim

			if delimiter then
				ListItems[j] = delimiter .. ListItems[j] .. delimiter
			end

			--Add category
			if category ~= '' then
				-- Use arguments[i] if the corresponding category exists
				local ItemCategory = categoryExists(category:gsub('$1',Arguments[i]))

				-- If it does not exist, see if the category exists by removing the text in parentheses
				if not ItemCategory and TextToShow ~= Arguments[i] then
					ItemCategory = categoryExists(category:gsub('$1',TextToShow))
				end
				if ItemCategory then
					if order == '' then
						ListItems[j] = ListItems[j] .. '[[' .. ItemCategory .. ']]'
					else
						ListItems[j] = ListItems[j] .. '[[' .. ItemCategory .. '|' .. order .. ']]'
					end
				end
			end
		end

		i = i + 1;
	end

	return ListItems
end

-- list with items separated by commas
function list.list(frame)
	-- Get the reported list items.
	local ListItems= list.GetListItems(frame)

	if not ListItems[1] then
		return
	end

	if not separator and not conjunction then
		separator  = '፣'
		conjunction = 'ከምኡ’ውን'
	elseif separator and not conjunction then
		conjunction = separator
	end
	-- Correct the separator
	if separator == '፣' then
		separator= '፣ '
	end
	-- Correct the conjunction
	if conjunction == 'ወይ' then
		conjunction = ' ወይ '
	end
	return mw.text.listToText(ListItems, separator, conjunction)
end

function list.htmllist(frame)
	local parent = frame:getParent();

	-- Get the reported list items by delimiting them with 
	local ListItems= list.GetListItems(frame)
	if not ListItems[1] then
		return
	end

	-- Add to list items <li> and </li>
	local i = 1;
	while ListItems[i] do
		ListItems[i] = '<li>' .. ListItems[i] .. '</li>'
		i = i + 1;
	end
	-- Add the class where appropriate
	local class = frame.args.class or parent.args.class
	local final = '<ul>' .. table.concat( ListItems ) .. '</ul>'
	if class then
		final = '<div class="' .. class ..'">' .. final .. '</div>'
	end
	return final
end

function list.first(frame)
	local ListItems= list.GetListItems(frame)
	if not ListItems[1] then
		return
	end
	table.sort( ListItems )
	return ListItems[1]
end

return list