Модул:phrase

Аз Wiktionary

Documentation for this module may be created at Модул:phrase/doc

local p = {}

--[[
text
 
Эта функция составляет фразу из слов и разделителей переданных в шаблоне.
 
Использование:
{{#invoke:phrase|text}}
]]

function p.text(frame)
	-- разбор параметров
    local pframe = frame:getParent()
    mw.logObject(pframe)
    local words = {};
    local param_names = {
    	['лемма'] = true, 
    	['калима'] = true, 
    	['аломат']  = true
    }
    for key, value in pairs(pframe.args) do
        if type(key) == 'string' and key ~= '' then
        	key = mw.ustring.lower(key)
        	local name, num = mw.ustring.match(key, '^(%D+)(%d+)$', 1)
        	num = tonumber(num)
        	if name ~= nil and num ~= nil and param_names[name] ~= nil then 
        		local word = words[num]
        		if word == nil then
        			words[num] = {[name] = value}
        		else
        			word[name] = value
        		end
        	end
        end
    end	
    -- составление строки
    result = ''
    for i, word in ipairs(words) do
    	local link = word['лемма']
    	local text = word['калима']
    	local sep = word['аломат']
    	if result ~= '' then
    		result = result .. ' '
    	end
    	mw.logObject(word)
    	if link ~= nil and link ~= '' and text ~= nil and text ~= '' then
    		result = result .. '[[' ..link .. '|' .. text  .. ']]' 
    	elseif text ~= nil and text ~= '' then
    		result = result .. text
    	end
    	if sep ~= nil then
    		if sep == '—' then
    			sep = ' ' .. sep
    		end
    		result = result .. sep
    	end
    end
    return result
end

return p;