Модуль:ExampleList
Реализация шаблонов {{стопка примеров}} и {{таблица примеров}}, см. их документацию. См. также
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
yesno = require('Module:Yesno')
local trim = not (yesno(frame:getParent().args.trim or frame:getParent().args._trim, false) == false) -- по умолчанию true
local args = copy(getArgs(frame, {trim = false, removeBlanks = false})) -- copy(frame.args)
for k, v in pairs(args) do
if type(k) == 'number' then
v = v
:gsub('^ *\n+', '')
:gsub('\n+ *$', '')
if trim then
v = v
:gsub('^ +', '')
:gsub(' +$', '')
end
args[k] = v
end
end
local g_alias = args._alias
local tname = args.t or args._t or args.template or args._template
local ucFirst = yesno(args.u or args._u, false)
local opener = args.opener or args._opener
local sep = args.sep or args._sep
local prefix = args.prefix or args._prefix
local postfix = args.postfix or args._postfix
local style = args.style or args._style
local nobr = args.nobr or args._nobr
local nocat = args.nocat or args._nocat
local spaced = args.spaced or args._spaced
local comment_sep = args.comment_sep or args._comment_sep
local between = args.between or args._between
local inthemiddle = yesno(args.inthemiddle or args._inthemiddle, false)
local wide = yesno(args.wide or args._wide, false)
local class = args.class or args._class
if not opener then
if style == 'wikitable' then
opener = '|-\n| '
elseif style == 'pre' or style == '*pre' or style == 'pre↓' or style == '*pre↓' then
opener = ''
if between == nil and (style == '*pre' or style == '*pre↓') then
if mw.isSubsting() then
between = '{{^|3em}}'
else
between = '<div style="margin-top:3em"></div>'
end
end
else
opener = '* '
end
end
if between == nil and (style == 'pre' or style == '*pre' or style == 'pre↓' or style == '*pre↓') then
if mw.isSubsting() then
between = '{{^|2em}}'
else
between = '<div style="margin-top:2em"></div>'
end
end
if tname == '' or tname == nil then -- при опущенном первом параметре берём имя шаблона из названия страницы
local ru = mw.language.new('ru')
local currentTitle = mw.title.getCurrentTitle().rootText
if not ucFirst and
((ru:uc(currentTitle) ~= currentTitle and -- названия со всеми заглавными буквами
not mw.ustring.match(currentTitle, '^[А-Яа-яA-Za-z]+:?[А-ЯA-Z]') -- Книга:Литературное наследство, TranslateDate
) or
#currentTitle == 1
)
then
tname = ru:lcfirst(currentTitle)
else
tname = currentTitle
end
end
-- Узнаем, заполнено ли где-то поле комментария
local are_comments = false
for k, v in pairs(args) do
local pre_targs = {}
if type(k) == 'number' then
if v ~= '' then
pre_targs = mw.text.split(v, '\\')
for k2, v2 in pairs(pre_targs) do
local equals_pos
if v2:sub(1, 1) == '_' and v2:find('=') then -- параметры настроек шаблона {{пример}}
equals_pos = v2:find('=')
end
if equals_pos then
local param = v2:sub(1, equals_pos - 1)
if param == '_comment' then are_comments = true end
end
end
end
end
end
local content, i = '', 0
for k, v in pairs(args) do
local pre_targs, targs = {}, {}
if type(k) == 'number' then
i = i + 1
if are_comments then
targs._comment = '' -- значение по умолчанию
end
if v ~= '' then
pre_targs = mw.text.split(v, '\\')
for k2, v2 in pairs(pre_targs) do
local equals_pos
if v2:sub(1, 1) == '_' and v2:find('=') then -- параметры настроек шаблона {{пример}}
equals_pos = v2:find('=')
end
if equals_pos then
local param = v2:sub(1, equals_pos - 1)
local value = v2:sub(equals_pos + 1)
targs[param] = value
else
if v2:sub(1, 5) == '_' then
targs[k2] = v2:gsub('_', '_')
else
targs[k2] = v2
end
end
end
end
-- если задан локальный алиас, то он используется вместо _template и глобального alias (консенсусная реализация, не меняю)
-- иначе используется _template = tname и _alias = g_alias, если последний задан
-- пример: {{стопка примеров|t=Википедия:Часто_используемые_источники/Статус|_alias=/Статус|...}} даст
-- <code>{{/Статус|...}}</code> → {{Википедия:Часто_используемые_источники/Статус|...}}
local targs_tname = targs._alias
local local_alias = nil
if not targs_tname then
targs_tname = tname
local_alias = g_alias
end
targs._sep, targs._style, targs._nobr, targs._nocat, targs._spaced, targs._comment_sep, targs._template, targs._alias = sep, style, nobr,
nocat, spaced, comment_sep, targs_tname, local_alias
if not targs._prefix then
targs._prefix = prefix
end
if not targs._postfix then
targs._postfix = postfix
end
local adjusted_opener
if style == 'pre' and opener == '*' and targs._before == nil then
adjusted_opener = ''
targs._style = '*pre'
else
if style == 'pre' and opener == '*' then
if mw.isSubsting() then
targs._before = targs._before .. '{{^|-0.5em}}'
else
targs._before = targs._before .. '<div style="margin-top:-0.5em"></div>'
end
end
adjusted_opener = opener == '*' and '* ' or opener
end
--[[if not targs._style then
targs._style = style
end]]
local exampleCode
if mw.isSubsting() then
exampleCode = '{{пример'
for name, value in pairs(targs) do
if type(name) == 'number' then
exampleCode = exampleCode .. '|' .. value
else
exampleCode = exampleCode .. '|' .. name .. '=' .. value
end
end
exampleCode = exampleCode .. '}}'
else
exampleCode = tostring(expand(frame, 'пример', targs))
end
content = content .. (i ~= 1 and (between and between .. '\n' or '\n') or '') .. adjusted_opener .. exampleCode
end
end
if style == 'wikitable' then
local table_content = ''
if not inthemiddle then
table_content = '{| class="wikitable '
if wide then table_content = table_content .. 'wide' end
if class then table_content = table_content .. class end
table_content = table_content .. '"\n! scope="col" | Код !! scope="col" | Результат'
if are_comments then table_content = table_content .. ' !! Комментарий' end
end
content = table_content .. '\n' .. content
if not inthemiddle then
content = content .. '\n|}'
end
end
return content
end
return p
|
Index:
pl ar de en es fr it arz nl ja pt ceb sv uk vi war zh ru af ast az bg zh-min-nan bn be ca cs cy da et el eo eu fa gl ko hi hr id he ka la lv lt hu mk ms min no nn ce uz kk ro simple sk sl sr sh fi ta tt th tg azb tr ur zh-yue hy my ace als am an hyw ban bjn map-bms ba be-tarask bcl bpy bar bs br cv nv eml hif fo fy ga gd gu hak ha hsb io ig ilo ia ie os is jv kn ht ku ckb ky mrj lb lij li lmo mai mg ml zh-classical mr xmf mzn cdo mn nap new ne frr oc mhr or as pa pnb ps pms nds crh qu sa sah sco sq scn si sd szl su sw tl shn te bug vec vo wa wuu yi yo diq bat-smg zu lad kbd ang smn ab roa-rup frp arc gn av ay bh bi bo bxr cbk-zam co za dag ary se pdc dv dsb myv ext fur gv gag inh ki glk gan guw xal haw rw kbp pam csb kw km kv koi kg gom ks gcr lo lbe ltg lez nia ln jbo lg mt mi tw mwl mdf mnw nqo fj nah na nds-nl nrm nov om pi pag pap pfl pcd krc kaa ksh rm rue sm sat sc trv stq nso sn cu so srn kab roa-tara tet tpi to chr tum tk tyv udm ug vep fiu-vro vls wo xh zea ty ak bm ch ny ee ff got iu ik kl mad cr pih ami pwn pnt dz rmy rn sg st tn ss ti din chy ts kcg ve
Portal di Ensiklopedia Dunia