このモジュールについての説明文ページを モジュール:columns/doc に作成できます

local export = {}

local m_links = require("モジュール:links")
local m_lang = require("モジュール:languages")

local function unpack_content(args)
	local result = {}
	for _,val in ipairs(args) do
		table.insert(result, mw.text.trim(val))
	end
	return result
end

function get_col_lengths(n_columns, n_items)
	local r = math.mod(n_items, n_columns)

	local col_lengths = {}
	for i = 1, n_columns do
		table.insert(col_lengths, (n_items - r) / n_columns)
		if (i <= r) then
			col_lengths[i] = col_lengths[i] + 1
		end
	end

	return col_lengths
end

function set_columns(n_columns, items, line_start, lang, frame)
	local col_lengths = get_col_lengths(n_columns, #items)
	local result = {}
	local count = 1
	for i = 1, n_columns do
		local col = ""
		for j = 1, col_lengths[i] do
			local item = items[count]
			
			if lang then
				item = m_links.full_link({lang = lang, term = item})
			end
			
			col = col .. '\n' .. line_start .. item
			count = count + 1
		end
		table.insert(result, col)
	end
	return result
end

function get_col_header(bg, collapse, class, title, column_width)
	if (collapse == 1) then
		local result = {'<div class="NavFrame">\n<div class="NavHead">',
                        title,
                        '</div>\n<div class="NavContent">\n{| style="width:100%;" role="presentation" class="',
                        class,
                        '"\n|-\n| style="vertical-align: top; text-align: left; background-color: ',
                        bg,
                        ';  width: ',
                        column_width,
                        '%;" |'}
		return table.concat(result)
    end
    if (collapse == 0) then
    	local result = {'<div style="width:auto;margin:0;overflow:auto;">\n{| role="presentation" style="width:100%"\n|-\n| style="background:',
                        bg,
                        ';vertical-align:top;width:',
                        column_width,
                        '%" |'}
		return table.concat(result)
    end

end

function export.create_table(n_columns, content, alphabetize, bg, collapse, class, title, column_width, line_start, lang, frame)
	local separator = '\n| style=width:1% |\n| style="background:' .. bg .. ';vertical-align:top;text-align:left;width:' .. column_width .. '%;" |'
	
    local final = '\n|}</div></div>'

    if (alphabetize == 1) then
    	if lang then
    		function comp(element1, element2)
    			return lang:makeSortKey(lang:makeEntryName(element1)) < lang:makeSortKey(lang:makeEntryName(element2))
			end
    		table.sort(content, comp)
		else
			table.sort(content)
		end
	end

	local header = get_col_header(bg, collapse, class, title, column_width)

	local columns_t = set_columns(n_columns, content, line_start, lang, frame)
	local columns = table.concat(columns_t, separator)

	return header .. columns .. final
end

function export.display(frame)
	
	local frame_args = frame:getParent().args
	-- unpack args
	local bg = frame.args["bg"] or "#F8F8FF"
	local collapse = tonumber(frame.args["collapse"]) or 0
	local class = frame.args["class"] or "Derived terms"
	local title = frame.args["title"] or ""
	local n_columns = tonumber(frame.args["columns"]) or 1
	local column_width = frame.args[columns] or math.floor(80 / n_columns)
	local alphabetize = tonumber(frame.args["sort"]) or 0
	local line_start = frame.args["line_start"] or "* "
	local lang = frame.args["lang"] or frame_args["lang"]
	local content = unpack_content(frame_args)
	
	if lang then
		lang = m_lang.getByCode(lang) or error("言語コード\"" .. lang .. "\"は無効です。")
	end
	
	return export.create_table(n_columns, content, alphabetize, bg, collapse, class, title, column_width, line_start, lang, frame)

end

return export