Module:columns: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
Wyang (talk | contribs)
reduce it to a "[Term?]" error first
match returns an error when one of the list items contains square brackets; percent-escaped with function in Module:utilities
Line 94: Line 94:
and mw.ustring.codepoint(mw.ustring.sub(element1, i, i)) <
and mw.ustring.codepoint(mw.ustring.sub(element1, i, i)) <
mw.ustring.codepoint(mw.ustring.sub(element2, i, i))
mw.ustring.codepoint(mw.ustring.sub(element2, i, i))
or type(mw.ustring.match(element2, element1)) == "string")
or type(mw.ustring.match(element2, require("Module:utilities").pattern_escape(element1))) == "string")
end
end
table.sort(content, comp)
table.sort(content, comp)

Revision as of 10:01, 8 April 2017

This module creates a list with automatically balanced columns. It should not be used directly in entries, but in templates such as {{col2}} or {{col3}}. List entries are given as parameters to the template.

Examples

  • {{#invoke:columns|display|sort=1|collapse=1|columns=3}} -> {{col3|en|z|y|x|w|v|u|t}}
  • {{#invoke:columns|display|sort=1|collapse=1|columns=2}} -> {{col2|nl|a|b|c|d|e|f|g}}

Parameters

create_list

export.create_list {
	column_count = number,
	content = list, alphabetize = boolean,
	background_color = string, collapse = boolean,
	toggle_category = string,
	class = string, lang = language_object,
}
content
A list of terms: { "term1", "term2", "term3", ... }.
lang
The language of the terms in the list. (Must be a language object from Module:languages.)
collapse
If true, table will be collapsed if it has enough items.
column_count
Number of columns in the table. Defaults to 1.
sort
Toggle sorting of the entries in the table. Defaults to false.
toggle_category
Determines the text for the "Show <toggle_category>" or "Hide <toggle category>" button in the "visibility" part of the toolbar. The default is "derived terms".
class
HTML class to add to the div tag that contains the list. Defaults to derivedterms.
background_color
A HTML color value for the list.

create_table

The old name for the main function. It is now just a wrapper for create_list.

display

The template-invokable function.


local export = {}

local m_links = require("Module:links")
local m_lang = require("Module: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 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 not collapse 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 then
    	if lang then
    		function comp(element1, element2)
    			if not element1 or not element2 then return false end
    			element1 = lang:makeSortKey(lang:makeEntryName(element1))
    			element2 = lang:makeSortKey(lang:makeEntryName(element2))
    			min_length = math.min(mw.ustring.len(element1), mw.ustring.len(element2))
    			i, resolved = 0, false
    			while i < min_length and not resolved do
    				i = i + 1
    				if mw.ustring.codepoint(mw.ustring.sub(element1, i, i)) ~= 
    					mw.ustring.codepoint(mw.ustring.sub(element2, i, i)) then
    					resolved = true
    				end
    			end
    			return (resolved 
    				and mw.ustring.codepoint(mw.ustring.sub(element1, i, i)) < 
    					mw.ustring.codepoint(mw.ustring.sub(element2, i, i)) 
    				or type(mw.ustring.match(element2, require("Module:utilities").pattern_escape(element1))) == "string")
			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.args
	local parent_args = frame:getParent().args
	-- unpack args
	local bg = frame_args["bg"] or "#F8F8FF"
	local collapse = require("Module:yesno")(frame_args["collapse"])
	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 = require("Module:yesno")(frame_args["sort"])
	local line_start = frame_args["line_start"] or "* "
	local lang = frame_args["lang"] or parent_args["lang"]
	local content = unpack_content(parent_args)
	
	if lang then
		lang = m_lang.getByCode(lang) or m_lang.err(lang, "lang")
	end
	
	return export.create_table(n_columns, content, alphabetize, bg, collapse, class, title, column_width, line_start, lang, frame)

end

return export