Модуль:Прыклад
Выгляд
Дакумэнтацыю да гэтага модуля можна стварыць у Модуль:Прыклад/Дакумэнтацыя
local p = {}
-- выклік шаблёну, пры памылцы вяртае пусты радок
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
end
--выдаляе з парамэтраў упісаныя праз HTML-існасьці "<nowiki>" і замяняе "{{=}}" на "=" дзеля выкліку шаблёну
local function process_nowiki_equals(str)
str = str:gsub('<nowiki>', ''):gsub('</nowiki>', '')
:gsub('<nowiki>', ''):gsub('</nowiki>', '')
:gsub('{{=}}', '=')
:gsub('{{=}}', '=')
:gsub('{{=}}', '=')
:gsub('{{=}}', '=')
:gsub('&', '&')
return str
end
function p.main(frame)
local getArgs = require('Модуль:Аргумэнты').getArgs
local yesno = require('Модуль:ТакНе')
local template_code = require('Модуль:Код выкліку шаблёну')._main
local copy = require('Модуль:TableTools').shallowClone
local args = copy(getArgs(frame, {trim = false, removeBlanks = false}))
local tag = args._tag
local sep = args._sep and args._sep .. ' '
local prefix = args._prefix or args['_pre-text'] or ''
local postfix = args._postfix or args['_post-text'] or ''
local nocat = yesno(args._nocat, false)
local style = args._style
if style == '' then
style = nil
end
-- перадае шаблён {{Стос прыкладаў}}, адзін разьдзяляльнік на ўсе прыклады
local comment_sep = args._comment_sep
local comment = args._comment
-- карысна ў шаблёне {{Стос прыкладаў}} (гэта проста тэкст у канцы)
local after = args._after or ''
-- карысна ў шаблёне {{Стос прыкладаў}} (гэта проста тэкст у канцы)
local before = args._before and args._before .. ' ' or ''
if style == 'pre' or style == '*pre' or style == 'pre↓' or style == '*pre↓' then
tag = tag or 'pre'
end
if style == 'pre' or style == '*pre' then
sep = sep or '\n'
elseif style == 'pre↓' or style == '*pre↓' then
sep = sep or expand(frame, 'sp-down', {'', '-0.5em'})
end
if style == '*pre' or style == '*pre↓' then
before = '<ul><li>' .. expand(frame, 'chrome bullet hack', {}) .. before
after = after .. '</li></ul>'
end
if style == 'wikitable' then
tag = tag or 'kbd'
sep = sep or '\n| '
comment_sep = '\n| '
end
tag = tag or 'code'
sep = sep or '→ '
comment_sep = comment_sep or ' '
if comment then
if not style then
comment = '<small>' .. comment .. '</small>'
end
after = comment_sep .. comment .. after
end
local _args = copy(args)
_args._style = args._codestyle
_args._comment = args._codecomment
_args._tag = tag
_args._prefix = prefix
_args._postfix = postfix
_args._nowiki = true
if args._template then
table.insert(_args, 1, args._template)
end
local nwt = template_code(_args, {withoutParams = false})
local tname = args._template or args[1]
if tname == nil then -- калі імя шаблёну зьмяшчае знак "=" (працуе, толькі як няма няйменных парамэтраў)
local nextfunc, static, cur = pairs(args)
local k, v = nextfunc(static, cur)
if k ~= nil and type(k) ~= 'number' and not k:find('^_') then -- іменныя парамэтры, за выняткам мадыфікатараў вонкавага выгляду
tname = k .. "=" .. v
args[k] = nil --больш гэты парамэтар нам не прыдасца
end
elseif not args._template then
-- Назва выкліканага шаблёну ў няйменным першым парамэтры (або ўзятае з назвы старонкі ці
-- зь менаванага парамэтру пры адсутнасьці няйменных — у наступным радку шкоды няма ў кожным разе),
-- больш яго апрацоўваць ня трэба
table.remove(args, 1)
end
if tname == '' or tname == nil then -- пры прапушчаным першым парамэтры бярэм імя шаблёну з назвы старонкі
tname = mw.title.getCurrentTitle().rootText
end
local targs, equals_pos, param, value, left_shift = {}, 0, '', '', 0
for k, v in pairs(args) do
if type(k) == 'number' then -- няйменныя парамэтры
equals_pos = v:find('=')
if equals_pos and v:find('{{=}}') == equals_pos-2 then
equals_pos = nil
end
if equals_pos then -- парамэтры з "=" пераўтвараем у іменныя
param = v:sub(1, equals_pos-1)
value = v:sub(equals_pos+1)
targs[param] = process_nowiki_equals(value)
left_shift = left_shift + 1 -- зьменная патрэбная, каб квазінумараваныя парамэтры, перададзеныя праз "{{=}}",
-- не псавалі парадку
else -- сапраўды няйменныя
targs[k - left_shift] = process_nowiki_equals(v)
end
elseif not k:find('^_') then -- іменныя парамэтры, за выняткам мадыфікатараў вонкавага выгляду
targs[k] = process_nowiki_equals(v)
end
end
if nocat then
targs['nocat'] = 1
end
local expand_result = tostring(expand(frame, tname, targs))
if expand_result:sub(1, 2) == '{|' then
sep = sep .. '\n'
end
return before .. tostring(nwt) .. ' ' .. sep .. prefix .. expand_result .. postfix .. after
end
return p