Модуль:labels

Материал из Викисловаря

Для документации этого модуля может быть создана страница Модуль:labels/Документация

-- Модуль:labels v1.9
-- 2015-06-25

local export = {}

local function process_category_string(frame, category, lang)
	if category:find('{lang|') ~= nil then
		if lang == '' or lang == nil then
			lang = '-'
		end
		category = frame:preprocess(category:gsub('%{lang%|(%w+)%}', '{{language|' .. lang .. '|%1}}'))
	else
		if category ~= '' and lang ~= '' and lang ~= 'int' and lang ~= 'INT' then
			category = category .. '/' .. lang
		end
	end
	return category
end

function export.convert(frame, text, lang, ignore_category)
	local data = mw.loadData("Module:labels/data");
	local lang = lang or ''
	for key, info in pairs(data) do
		if type(info) == 'string' then
			title = info
			info = data[title]
		else
			title = key
		end
		key = key:gsub('%.', '.?')  -- чтобы точки были необязательны в [сокр]
		hint = info['hint'] or 'Нет подсказки'
		category = ''
		category2 = ''
		if not ignore_category and info['category2'] then
			category2 = process_category_string(frame, info['category2'], lang)
		end
		if not ignore_category and info['category']then
			category = info['category']
			if type(category) == 'string' then
				category = process_category_string(frame, category, lang)
			elseif type(category) == 'table' then
				category_items = category
				for i, category_info in pairs(category_items) do
					category_key = category_info[1]
					category_value = process_category_string(frame, category_info[2], lang)
					local pattern = "%[".. key .."/".. category_key .."%]"
					local replace = "{{label|value=".. title .."|hint=".. hint .."|category=".. category_value .."|category2=".. category2 .."}}"
					text = text:gsub(pattern, replace)
					if category_key == '' then
						category = category_value
					end
				end
				if category == category_items then
					category = ''
				end
			end
		end
		if info['prep'] then
			prep = info['prep']
			local pattern = "%[".. key .."/([^]]+)%]"
			local replace = "{{label|value=".. title .."|hint=".. hint .."|category=".. category .."|category2=".. category2 .."|nocolor=1}} ".. prep .." {{ссылки|%1}}"
			text = text:gsub(pattern, replace)
		end
		local pattern = "%[".. key .."%]"
		local replace = "{{label|value=".. title .."|hint=".. hint .."|category=".. category .."|category2=".. category2 .."}}"
		text = text:gsub(pattern, replace)
	end
	text = text:gsub("%[%[", "{{откр.ссылка}}")
	text = text:gsub("%]%]", "{{закр.ссылка}}")
	text = text:gsub("%[([^][{}<>|=]+)=([^]]+)%]", "{{label-test|value=%1|hint=%2|nolink=1}}")
	text = text:gsub("%[([^]]+)%]", "{{label-test|value=%1|hint=|nolink=1}}")
	text = text:gsub("%{%{откр.ссылка%}%}", '[[')
	text = text:gsub("%{%{закр.ссылка%}%}", ']]')
	return text
end

return export