İçeriğe atla

Modül:sütunlar

Vikisözlük sitesinden
Modül belgelemesi [Oluştur]


local cikart = {}

local m_baglantilar = require("Modül:bağlantılar")
local m_diller = require("Modül:diller")


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_baglantilar.tam_bag({dil = lang, sozcuk = 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 cikart.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_baglantilar.kaldir_baglantilari(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 cikart.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("Modül:parameters").process(frame.args, params)
	
	params = {
		[1] = {list = true},
		
		["title"] = {},
		["lang"] = not frame_args["lang"] and {required = true, default = "und"} or nil,
		["collapse"] = {},
	}
	
	local args = require("Modül:parameters").process(frame:getParent().args, params)
	
	local lang = frame_args["lang"] or args["lang"]
	lang = m_diller.getirKod(lang) or m_diller.err(lang, "lang")
	
	for i, val in ipairs(args[1]) do
		args[1][i] = mw.text.trim(val)
	end
	
	return cikart.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 cikart