跳转到内容

模組:Term list

維基詞典,自由的多語言詞典


local export = {}

local m_lang = require("Module:languages")
local m_links = require("Module:links")

function export.display(frame)
	local title = frame.args.title or error("Missing argument \"title\".")
	local frame_args = frame:getParent().args
	local lang = frame_args.lang
	if not lang then
		error("Missing attribute \"lang\".")
	end
	lang = m_lang.getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	local item_count
	local items = {}
	local last_item
	local nested
	for _, item in ipairs(frame_args) do
		if string.byte(item) == 42 then
			if not last_item then
				error("List of terms begins with a nested item.")
			end
			table.insert(last_item.nested_terms, mw.text.trim(string.sub(item, 2)))
			nested = 1
		else
			last_item = {nested_terms = {}, term = mw.text.trim(item)}
			table.insert(items, last_item)
		end
		item_count = _
	end
	if item_count < 1 then
		error("No list items are specified.")
	end
	local get_sort_value = require("Module:fun").memoize(function (element)
		return lang:makeSortKey(lang:makeEntryName(element))
	end)
	local function compare_items(element1, element2)
		return get_sort_value(element1.term) < get_sort_value(element2.term)
	end
	local function compare_terms(element1, element2)
		return get_sort_value(element1) < get_sort_value(element2)
	end
	local column_size
	local column_width
	local limit
	local result
	local tail
	if nested or item_count > 8 then
		bullet = "\n*"
		local column_count = math.min(4, item_count)
		column_size = math.ceil(item_count / column_count)
		limit = column_size
		column_width = math.floor(80 / column_count) .. '%" |'
		result = {'<div class="NavFrame">\n<div class="NavHead">',
			frame_args.title or title,
			'</div>\n<div class="NavContent">\n{| style="width:100%;" role="presentation" class="',
			"Derived terms",
			'"\n|-\n| style="vertical-align: top; text-align: left; width: ', column_width}
		tail = "\n|}</div></div>"
	else
		limit = 8
		title = frame_args.title
		if title then
			result = {'<div class="horizontal">', title, ":"}
		else
			result = {'<div class="horizontal">'}
		end
		tail = "</div>"
	end
	table.sort(items, compare_items)
	local last_term
	for _, item in ipairs(items) do
		local term = item.term
		if term == last_term then
			error("Duplicate item \"" .. term .. "\".")
		end
		last_term = term
		if limit <= 0 then
			table.insert(result, '\n| style=width:1% |\n| style="text-align:left;vertical-align:top;width:' .. column_width)
			limit = column_size
		end
		table.insert(result, "\n*" .. m_links.full_link({term = term, lang = lang}))
		bullet = "\n*"
		limit = limit - 1
		local nested_terms = item.nested_terms
		if nested_terms[1] then
			table.sort(nested_terms, compare_terms)
			local last_term
			for _, term in ipairs(nested_terms) do
				if term == last_term then
					error("Duplicate item \"" .. term .. "\".")
				end
				last_term = term
				table.insert(result, "\n**" .. m_links.full_link({term = term, lang = lang}))
				limit = limit - 1
			end
		end
	end
	table.insert(result, tail)
	return table.concat(result)
end

return export