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

Материал из in.wiki
Перейти к навигации Перейти к поиску
(fix)
(например)
Строка 26: Строка 26:
 
function p.main(frame)
 
function p.main(frame)
 
local args = copy(frame.args)
 
local args = copy(frame.args)
tag =  args._tag or 'code'
+
local tag =  args._tag or 'code'
sep =  args._sep or '→'
+
local sep =  args._sep or '→'
nwt = mw.html.create(tag):tag(tag)
+
local nwt = mw.html.create(tag):tag(tag)
content = '{{'
+
local content = '{{'
 
+
local tname = ''
 +
 
if args._template then
 
if args._template then
 
tname = args._template
 
tname = args._template

Версия от 14:22, 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

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)
	local args = copy(frame.args)
	local tag =  args._tag or 'code'
	local sep =  args._sep or '→'
	local nwt = mw.html.create(tag):tag(tag)
	local content = '{{'
	local tname = ''
	
	if args._template then
		tname = args._template
	else
		tname = args[1]
		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