Module:columns: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
No edit summary
Wyang (talk | contribs)
accommodate already-formatted links
Line 30: Line 30:
local item = items[count]
local item = items[count]
if lang then
if lang and not mw.ustring.find(item, "<span") then
item = m_links.full_link({lang = lang, term = item})
item = m_links.full_link({lang = lang, term = item})
end
end

Revision as of 23:06, 15 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}}

Lua error at line 136: Parameter "lang" is required.

  • {{#invoke:columns|display|sort=1|collapse=1|columns=2}} -> {{col2|nl|a|b|c|d|e|f|g}}

Lua error at line 136: Parameter "lang" is required.

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_languages = require("Module:languages")


local 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


local function set_columns(n_columns, items, line_start, lang)
	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 and not mw.ustring.find(item, "<span") 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


local 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)
    else
    	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)
	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
    		local function prepare(element)
    			element = m_links.remove_links(element)
    			element = mw.ustring.gsub(element, "[%p ]", "")
    			element = lang:makeSortKey(lang:makeEntryName(element))
    			return element
    		end
    		
    		local function comp(element1, element2)
    			element1, element2 = prepare(element1), prepare(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 (mw.ustring.sub(element2, 1, mw.ustring.len(element1)) == element1 and
    					element1 ~= element2)
    				or false)
			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)
	local columns = table.concat(columns_t, separator)
	
	return header .. columns .. final
end


function export.display(frame)
	local params = {
		["class"] = {default = "derivedterms"},
		["collapse"] = {type = "boolean"},
		["columns"] = {type = "number", default = 1},
		["lang"] = {},
		["sort"] = {type = "boolean"},
		["title"] = {default = ""},
	}
	
	local frame_args = require("Module:parameters").process(frame.args, params)
	
	params = {
		[1] = {list = true},
		
		["title"] = {},
		["lang"] = not frame_args["lang"] and {required = true, default = "und"} or nil,
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local lang = frame_args["lang"] or args["lang"]
	lang = m_languages.getByCode(lang) or m_languages.err(lang, "lang")
	
	for i, val in ipairs(args[1]) do
		args[1][i] = mw.text.trim(val)
	end
	
	return export.create_table(frame_args["columns"], args[1], frame_args["sort"], "#F8F8FF", frame_args["collapse"], frame_args["class"], args["title"] or frame_args["title"], math.floor(80 / frame_args["columns"]), "* ", lang)
end


return export