Module:columns: Difference between revisions

From Linguifex
Jump to navigation Jump to search
No edit summary
(Undo revision 225322 by Sware (talk))
Tag: Undo
Line 55: Line 55:
     if args.alphabetize then
     if args.alphabetize then
require("Module:collation").sort(args.content, args.lang)
require("Module:Collation").sort(args.content, args.lang)
end
end
table.insert(output, format_list_items(args.content, args.lang))
table.insert(output, format_list_items(args.content, args.lang))

Revision as of 10:44, 25 March 2021



local export = {}

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


local function format_list_items(items, lang)
	local result = {}
	
	for i, item in ipairs(items) do
		if lang and not string.find(item, "<span") then
			item = m_links.full_link({lang = lang, term = item})
		end
		
		result[i] = '\n* ' .. item
	end
	
	return table.concat(result)
end

local collapse_header =
	[[<div class="list-switcher" data-toggle-category="{{{toggle_category}}}">]]
local column_header = [[<div class="{{{class}}} term-list ul-column-count" ]]
	.. [[data-column-count="{{{column_count}}}" ]]
	.. [[style="background-color: {{{background_color}}};">]]
local button = [[<div class="list-switcher-element" ]]
	.. [[data-showtext="&nbsp;show more ▼&nbsp;" ]]
	.. [[data-hidetext="&nbsp;show less ▲&nbsp;" style="display: none;">&nbsp;</div>]]

function export.create_list(args)
	-- Fields in args that are used:
	-- args.column_count, args.content, args.alphabetize, args.background_color,
	-- args.collapse, args.toggle_category, args.class, args.lang
	-- Check for required fields?
	if type(args) ~= "table" then
		error("expected table, got " .. type(args))
	end
	
	args.class = args.class or "derivedterms"
	args.column_count = args.column_count or 1
	args.toggle_category = args.toggle_category or "derived terms"
	
	local output = {}
	
	if args.header then
		if args.format_header then
			args.header = '<div class="term-list-header">' .. args.header .. "</div>"
		end
		table.insert(output, args.header)
	end
	if args.collapse then
		table.insert(output, (collapse_header:gsub('{{{(.-)}}}', args)))
	end
	table.insert(output, (column_header:gsub('{{{(.-)}}}', args)))
	
    if args.alphabetize then
		require("Module:Collation").sort(args.content, args.lang)
	end
	table.insert(output, format_list_items(args.content, args.lang))
	
	table.insert(output, '</div>')
	if args.collapse then
		table.insert(output, button .. '</div>')
	end
	
	return table.concat(output)
end


-- This function is for compatibility with earlier version of [[Module:columns]]
-- (now found in [[Module:columns/old]]).
function export.create_table(...)
	-- Earlier arguments to create_table:
	-- n_columns, content, alphabetize, bg, collapse, class, title, column_width, line_start, lang
	local args = {}
	args.column_count, args.content, args.alphabetize, args.background_color,
		args.collapse, args.class, args.header, args.column_width,
		args.line_start, args.lang = ...
	
	args.format_header = true
	
	return export.create_list(args)
end


function export.display(frame)
	local iparams = {
		["class"] = {},
		-- Default for auto-collapse. Overridable by template |collapse= param.
		["collapse"] = {type = "boolean"},
		-- If specified, this specifies the number of columns, and no columns
		-- parameter is available on the template. Otherwise, the columns
		-- parameter is the first available numbered param after the language-code
		-- parameter.
		["columns"] = {type = "number"},
		-- If specified, this specifies the language code, and no language-code
		-- parameter is available on the template. Otherwise, the language-code
		-- parameter can be specified as either |lang= or |1=.
		["lang"] = {},
		-- Default for auto-sort. Overridable by template |sort= param.
		["sort"] = {type = "boolean"},
		-- The following is accepted but currently ignored, per an extended discussion in
		-- [[Wiktionary:Beer parlour/2018/November#Titles of morphological relations templates]].
		["title"] = {default = ""},
		["toggle_category"] = {},
	}
	
	local frame_args = require("Module:parameters").process(frame.args, iparams)
	local parent_args = frame:getParent().args
	
	local compat = frame_args["lang"] or parent_args["lang"]
	local lang_param = compat and "lang" or 1
	local columns_param = compat and 1 or 2
	local first_content_param = columns_param + (frame_args["columns"] and 0 or 1)

	local params = {
		[lang_param] = not frame_args["lang"] and {required = true, default = "und"} or nil,
		[columns_param] = not frame_args["columns"] and {required = true, default = 2} or nil,
		[first_content_param] = {list = true},
		
		["title"] = {},
		["collapse"] = {type = "boolean"},
		["sort"] = {type = "boolean"},
	}
	
	local args = require("Module:parameters").process(parent_args, params)
	
	local lang = frame_args["lang"] or args[lang_param]
	lang = m_languages.getByCode(lang) or m_languages.err(lang, lang_param)
	
	local sort = frame_args["sort"]
	if args["sort"] ~= nil then
		sort = args["sort"]
	end
	local collapse = frame_args["collapse"]
	if args["collapse"] ~= nil then
		collapse = args["collapse"]
	end
	
	return export.create_list { column_count = frame_args["columns"] or args[columns_param],
		content = args[first_content_param],
		alphabetize = sort,
		header = args["title"], background_color = "#F8F8FF",
		collapse = collapse,
		toggle_category = frame_args["toggle_category"],
		class = frame_args["class"], lang = lang, format_header = true }
end


return export