Модуль:Example: различия между версиями

Материал из in.wiki
Перейти к навигации Перейти к поиску
(getArgs)
(оформление)
Строка 1: Строка 1:
 
local p = {}
 
local p = {}
  
 +
-- используется для того, чтобы можно было удалять элементы из таблицы
 
local function copy(other)
 
local function copy(other)
 
local res = {}
 
local res = {}
Строка 9: Строка 10:
 
end
 
end
  
function expand(frame, tname, targs)
+
-- вызов шаблона, при ошибке возвращает пустую строку
 +
local function expand(frame, tname, targs)
 
local success, result = pcall(
 
local success, result = pcall(
 
frame.expandTemplate,
 
frame.expandTemplate,
Строка 30: Строка 32:
 
local args = copy(getArgs(frame)) --copy(frame.args)
 
local args = copy(getArgs(frame)) --copy(frame.args)
 
local tag =  args._tag or 'code'
 
local tag =  args._tag or 'code'
local sep =  args._sep or '→'
+
local sep =  args._sep or '→' -- по умолчанию "→"
local nwt = mw.html.create(tag):tag(tag)
+
local nwt = mw.html.create(tag):tag(tag) --"no-wiki tag", внутри него шаблон не вызывается
local content = '{{'
+
local content = '{{' --для накопления содержимого тэга
local tname = args._template or args[1]
+
local tname = args._template or args[1]  
 
 
if args._template == nil then
+
if args._template == nil then --имя вызываемого шаблона в неименованном первом параметре, больше его обрабатывать не надо
 
table.remove(args,1)
 
table.remove(args,1)
 
end
 
end
 
 
content = content .. tname
 
content = content .. tname
 
local targs = {}
 
local targs = {}
 
for k, v in pairs(args) do
 
for k, v in pairs(args) do
if type(k) == 'number' then
+
if type(k) == 'number' then --неименованные параметры
 
targs[k] = v
 
targs[k] = v
 
content = content .. '|' .. v
 
content = content .. '|' .. v
elseif not k:find('^_') then
+
elseif not k:find('^_') then --именованные параметры, исключая модификаторы внешнего вида
 
targs[k] = v
 
targs[k] = v
 
content = content .. '|' .. k .. '=' .. v
 
content = content .. '|' .. k .. '=' .. v
 
end
 
end
 
end
 
end
 
 
content = content .. '}}'
 
content = content .. '}}'
 
nwt:wikitext(content):done()
 
nwt:wikitext(content):done()

Версия от 16:19, 16 января 2016

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

local p = {}

-- используется для того, чтобы можно было удалять элементы из таблицы
local function copy(other)
	local res = {}
	for k,v in pairs(other) do 
		res[k] = v
	end
	return res
end

-- вызов шаблона, при ошибке возвращает пустую строку
local function expand(frame, tname, targs)
	local success, result = pcall(
		frame.expandTemplate,
		frame,
		{title = tname, args = targs}
	)
	if success then
		return result
	else
		return ''
	end
	--return frame:expandTemplate({title = tname, args = args})
end	


function p.main(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end	
	local args = copy(getArgs(frame)) --copy(frame.args)
	local tag =  args._tag or 'code'
	local sep =  args._sep or '→' -- по умолчанию "→"
	local nwt = mw.html.create(tag):tag(tag) --"no-wiki tag", внутри него шаблон не вызывается
	local content = '{{' --для накопления содержимого тэга 
	local tname = args._template or args[1] 
	
	if args._template == nil then --имя вызываемого шаблона в неименованном первом параметре, больше его обрабатывать не надо
		table.remove(args,1)		
	end
	content = content .. tname	
	local targs = {}	
	for k, v in pairs(args) do
		if type(k) == 'number' then --неименованные параметры
			targs[k] = v
			content = content .. '|' .. v
		elseif not k:find('^_') then --именованные параметры, исключая модификаторы внешнего вида
			targs[k] = v
			content = content .. '|' .. k .. '=' .. v
		end
	end
	content = content .. '}}'
	nwt:wikitext(content):done()

	return tostring(nwt) .. ' ' .. sep .. ' ' .. tostring(expand(frame, tname, targs))
end

return p