Модуль:Glossary

Материал из свободной русской энциклопедии «Традиция»
Перейти к навигации Перейти к поиску

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

local trim = mw.text.trim
local function append (tbl, item)
	if item then
		local str = trim (item)
		if str ~= '' then
			tbl [#tbl + 1] = str
		end
	end
end	-- local function append (tbl, item)

local so = mw.smw.subobject
local function file_term (term, definition, page)
	if term and trim (term) ~= '' then
		so {
			['Glossary-Term']		= term
		  , ['Glossary-Definition']	= definition
		  , ['Glossary-Link']		= page
		}
	end
end	-- local function file_term (term, definition, page)

local function file_terms (page, definition, terms)
	for _, term in ipairs (terms) do
		file_term (term, definition, page)
	end
end	-- local function file_terms (page, definition, terms)

local function terms (frame)
	local args = mw.clone (frame.args)
	local page = args.page or mw.title.getCurrentTitle ().fullText
	local definition = args.def or page
	local match, split = mw.ustring.gmatch, mw.text.split
	local terms = {}
	for _, val in ipairs (args) do
		if mw.ustring.find (val, '[[', 1, true) then
			for item in match (val, '%[%[(.-)%]%]') do
				append (terms, item)
			end
		else
			for _, item in ipairs (split (val, '%s*;%s*')) do
				append (terms, item)
			end
		end	-- if mw.ustring.find (val, '[[', 1, true)
	end	-- 	for _, val in ipairs (args)
	file_terms (page, definition, terms)
end	-- local function terms (frame)

return {
	terms = terms
}