Модуль:Тест: различия между версиями
Перейти к навигации
Перейти к поиску
ЗаяцВолк (обсуждение | вклад) |
ЗаяцВолк (обсуждение | вклад) |
||
Строка 1: | Строка 1: | ||
-- формат данных | |||
-- { head_v style_h list_v style_l n } - tr | |||
-- | |||
local header_text="заголовок" | |||
local list_text="список" | |||
local pos=1 -- итерируемая позиция в разбираемом шаблоне | |||
local args = {} -- элементы шаблона | |||
local span_im = 2 -- корректировка для вставки изображения | |||
local i= | local style_l ='width:100%;' | ||
local style_h ='white-space:nowrap;' | |||
-- эти 2 переменные можно всунуть внутрь функций | |||
local t = {} -- таблица данных для создания шаблона навигации | |||
local row = {} -- разбираемая строка шаблона навигации | |||
function list () | |||
if args[pos+1] == nil then pos=pos+1 return end | |||
row["list_v"]=args[pos+1] | |||
row["style_l"]=style_l | |||
row["n"]=1 | |||
pos=pos+2 | |||
table.insert (t, row) | |||
row={} | |||
end | |||
function header ( ) | |||
if args[pos+1] == nil then pos=pos+1 return end | |||
row["head_v"]=args[pos+1] | |||
row["style_h"]=style_h | |||
pos=pos+2 | |||
if args[pos] == list_text then | |||
list () | |||
t[#t]["n"]=2 | |||
return | |||
end | |||
row["n"]=1 | |||
table.insert (t, row) | |||
row={} | |||
end | |||
function style_upd ( t ) | |||
local s1="" | |||
local s2="" | |||
if args["стиль_нечетных"]== nil and args["стиль_четных"]== nil then return end | |||
if args["стиль_нечетных"]~= nil then s1=args["стиль_нечетных"] end | |||
if args["стиль_четных"]~= nil then s2=args["стиль_четных"] end | |||
for i in ipairs (t) do | |||
if t[i]["list_v"] ~= nil then | |||
if i%2 == 0 then | |||
t[i]["style_l"]=t[i]["style_l"]..s2 | |||
else | |||
t[i]["style_l"]=t[i]["style_l"]..s1 | |||
end | |||
end | |||
end | |||
end | |||
function image () | |||
if args["изображение"]== nil then return "" end | |||
local style_im="" | |||
if args['imagestyle'] ~= nil then style_im=args['imagestyle'] end | |||
return '<td rowspan="'..tostring(#t)..'" style="vertical-align:middle; padding-left:7px; width:0%;'..style_im..'">'..args["изображение"]..'</td>' | |||
end | |||
-- преобразует разобранное тело в html-код | |||
function bilding ( t ) | |||
local result="" | |||
for i in ipairs (t) do | |||
if t[i]["n"]==2 then | |||
-- result=result..'<tr><th style="'..t[i]["style_h"]..'" >'..t[i]["head_v"]..'</th><td style="'..t[i]["style_l"]..'" >'..t[i]["list_v"]..'</td>' | |||
result=table.concat( | |||
{ | |||
result, | |||
'<tr><th style="', | |||
t[i]["style_h"], | |||
'" >', | |||
t[i]["head_v"], | |||
'</th><td style="', | |||
t[i]["style_l"], | |||
'" >', | |||
t[i]["list_v"], | |||
'</td>' | |||
} | |||
) | |||
else | |||
if t[i]["head_v"] ~= nil then | |||
result=result..'<tr><th colspan="2" style="text-align:center;'..t[i]["style_h"]..'">'..t[i]["head_v"]..'</th>' | |||
else | |||
result=result..'<tr><td colspan="2">'..t[i]["list_v"]..'</td>' | |||
end | |||
end | |||
if i==1 then result=result..image () end -- колхоз | |||
result=result..'</tr>' | |||
end | |||
return result | |||
end | |||
-- совместимость со старым кодом | |||
function prepareN () | |||
local i=1 | |||
local n=1 | |||
local rown = {} | |||
while i<25 do | |||
if | |||
args['заголовок'..tostring(i)] ~= nil | |||
and | |||
args['список'..tostring(i)] ~= nil | |||
then | |||
n=2 | |||
end | |||
if | |||
args['заголовок'..tostring(i)] ~= nil | |||
or | |||
args['список'..tostring(i)] ~= nil | |||
then | |||
rown["head_v"]=args['заголовок'..tostring(i)] | |||
rown["style_h"]=style_h | |||
rown["list_v"]=args['список'..tostring(i)] | |||
rown["style_l"]=style_l | |||
rown["n"]=n | |||
table.insert(t, rown ) | |||
end | |||
rown={} | |||
n=1 | |||
i=i+1 | |||
end | |||
end | |||
function ignor_empty () | |||
pos=pos+1 | |||
end | |||
-- разбирает тело нав.таблицы | |||
function prepare () | |||
row={} | |||
while true do | |||
if args[pos] == nil then break end | |||
if args[pos] == header_text then | |||
header() | |||
end | |||
if args[pos] == list_text then | |||
list() | |||
end | |||
if | |||
args[pos] ~= list_text | |||
and | |||
args[pos] ~= header_text | |||
then | |||
ignor_empty () | |||
end | |||
end | |||
end | |||
-- выдать заголовок таблицы | |||
function nav_head () | |||
local z="Пропущен заголовок" | |||
if args["заголовок"] ~= nil then z=args["заголовок"] end | |||
local redact='<font color="red">Нет имени шаблона для редактирования</font>' | |||
local style_s = "" | |||
if args["стиль_основного_заголовка"] ~= nil then style_s=args["стиль_основного_заголовка"] end | |||
if args["имя"] ~= nil then | |||
redact=tostring(mw.uri.fullUrl('Шаблон:'..args["имя"], "action=edit")) | |||
redact='['..redact..' [изменить]]' | |||
end | end | ||
return '<tr><th colspan="'..span_im..'" style="text-align:center;width:100%;'..style_s..'"><div style="float:left; width:6em; text-align:left; font-size: smaller; font-weight: normal;" class="plainlinks">'..redact..'</div><span style="font-size:110%;">'..z..'</span></th></tr>' | |||
end | end | ||
function nav_footer () | |||
if args["внизу"] == nil then return "" end | |||
local style_s = "" | |||
if args["стиль_внизу"] ~= nil then style_s=args["стиль_внизу"] end | |||
return table.concat( | |||
{'<tr><td style="text-align:center; ', | |||
style_s, | |||
'" colspan="', | |||
span_im, | |||
'" >', | |||
args["внизу"], | |||
'</td></tr>' | |||
} | |||
) | |||
end | |||
function nav_subhead () | |||
if args["вверху"] == nil then return "" end | |||
local style_s = "" | |||
if args["стиль_вверху"] ~= nil then style_s=args["стиль_вверху"] end | |||
return '<tr><td style="text-align:center; '..style_s..'" colspan="'..span_im..'" >'..args["вверху"]..'</td></tr>' | |||
end | |||
function nav_table_start () | |||
-- общие стили нав. таблицы | |||
local style="" | |||
if args["стиль"]~= nil then style=style..args["стиль"] end | |||
if args["стиль_тела"]~= nil then style=style..args["стиль_тела"] end | |||
local state="" | |||
if args["state"] =='' | |||
then | |||
state="yes" | |||
else | |||
if args["state"] == nil then state="autocollapse" else state= args["state"] end | |||
end | |||
return | |||
return '<table class="navbox collapsible '..state..' nowraplinks" style="margin:auto; '..style..' " >' | |||
end | end | ||
local p = {} | |||
p.nav = function ( frame) | |||
args = require('Module:Arguments').getArgs(frame, {trim = false, removeBlanks = false}) | |||
if args["стиль_заголовков"]~= nil then style_h=style_h..args["стиль_заголовков"] end | |||
if args["стиль_списков"]~= nil then style_l=style_l..args["стиль_списков"] end | |||
if args["изображение"]~= nil then span_im=3 end | |||
if args["заголовок1"] ~= nil or args["список1"] ~= nil then prepareN() else prepare() end | |||
style_upd (t) | |||
return table.concat({nav_table_start(),nav_head(),nav_subhead(),bilding(t),nav_footer(),'</table>'}) | |||
end | |||
p. | p.subnav = function (frame) | ||
args = require('Module:Arguments').getArgs(frame, {trim = false, removeBlanks = false}) | |||
prepare() | |||
return ' | return '<table>'..bilding ( t )..'</table></th>' | ||
end | end | ||
return p | return p |
Версия 17:17, 25 августа 2024
Для документации этого модуля может быть создана страница Модуль:Тест/doc
-- формат данных -- { head_v style_h list_v style_l n } - tr -- local header_text="заголовок" local list_text="список" local pos=1 -- итерируемая позиция в разбираемом шаблоне local args = {} -- элементы шаблона local span_im = 2 -- корректировка для вставки изображения local style_l ='width:100%;' local style_h ='white-space:nowrap;' -- эти 2 переменные можно всунуть внутрь функций local t = {} -- таблица данных для создания шаблона навигации local row = {} -- разбираемая строка шаблона навигации function list () if args[pos+1] == nil then pos=pos+1 return end row["list_v"]=args[pos+1] row["style_l"]=style_l row["n"]=1 pos=pos+2 table.insert (t, row) row={} end function header ( ) if args[pos+1] == nil then pos=pos+1 return end row["head_v"]=args[pos+1] row["style_h"]=style_h pos=pos+2 if args[pos] == list_text then list () t[#t]["n"]=2 return end row["n"]=1 table.insert (t, row) row={} end function style_upd ( t ) local s1="" local s2="" if args["стиль_нечетных"]== nil and args["стиль_четных"]== nil then return end if args["стиль_нечетных"]~= nil then s1=args["стиль_нечетных"] end if args["стиль_четных"]~= nil then s2=args["стиль_четных"] end for i in ipairs (t) do if t[i]["list_v"] ~= nil then if i%2 == 0 then t[i]["style_l"]=t[i]["style_l"]..s2 else t[i]["style_l"]=t[i]["style_l"]..s1 end end end end function image () if args["изображение"]== nil then return "" end local style_im="" if args['imagestyle'] ~= nil then style_im=args['imagestyle'] end return '<td rowspan="'..tostring(#t)..'" style="vertical-align:middle; padding-left:7px; width:0%;'..style_im..'">'..args["изображение"]..'</td>' end -- преобразует разобранное тело в html-код function bilding ( t ) local result="" for i in ipairs (t) do if t[i]["n"]==2 then -- result=result..'<tr><th style="'..t[i]["style_h"]..'" >'..t[i]["head_v"]..'</th><td style="'..t[i]["style_l"]..'" >'..t[i]["list_v"]..'</td>' result=table.concat( { result, '<tr><th style="', t[i]["style_h"], '" >', t[i]["head_v"], '</th><td style="', t[i]["style_l"], '" >', t[i]["list_v"], '</td>' } ) else if t[i]["head_v"] ~= nil then result=result..'<tr><th colspan="2" style="text-align:center;'..t[i]["style_h"]..'">'..t[i]["head_v"]..'</th>' else result=result..'<tr><td colspan="2">'..t[i]["list_v"]..'</td>' end end if i==1 then result=result..image () end -- колхоз result=result..'</tr>' end return result end -- совместимость со старым кодом function prepareN () local i=1 local n=1 local rown = {} while i<25 do if args['заголовок'..tostring(i)] ~= nil and args['список'..tostring(i)] ~= nil then n=2 end if args['заголовок'..tostring(i)] ~= nil or args['список'..tostring(i)] ~= nil then rown["head_v"]=args['заголовок'..tostring(i)] rown["style_h"]=style_h rown["list_v"]=args['список'..tostring(i)] rown["style_l"]=style_l rown["n"]=n table.insert(t, rown ) end rown={} n=1 i=i+1 end end function ignor_empty () pos=pos+1 end -- разбирает тело нав.таблицы function prepare () row={} while true do if args[pos] == nil then break end if args[pos] == header_text then header() end if args[pos] == list_text then list() end if args[pos] ~= list_text and args[pos] ~= header_text then ignor_empty () end end end -- выдать заголовок таблицы function nav_head () local z="Пропущен заголовок" if args["заголовок"] ~= nil then z=args["заголовок"] end local redact='<font color="red">Нет имени шаблона для редактирования</font>' local style_s = "" if args["стиль_основного_заголовка"] ~= nil then style_s=args["стиль_основного_заголовка"] end if args["имя"] ~= nil then redact=tostring(mw.uri.fullUrl('Шаблон:'..args["имя"], "action=edit")) redact='['..redact..' [изменить]]' end return '<tr><th colspan="'..span_im..'" style="text-align:center;width:100%;'..style_s..'"><div style="float:left; width:6em; text-align:left; font-size: smaller; font-weight: normal;" class="plainlinks">'..redact..'</div><span style="font-size:110%;">'..z..'</span></th></tr>' end function nav_footer () if args["внизу"] == nil then return "" end local style_s = "" if args["стиль_внизу"] ~= nil then style_s=args["стиль_внизу"] end return table.concat( {'<tr><td style="text-align:center; ', style_s, '" colspan="', span_im, '" >', args["внизу"], '</td></tr>' } ) end function nav_subhead () if args["вверху"] == nil then return "" end local style_s = "" if args["стиль_вверху"] ~= nil then style_s=args["стиль_вверху"] end return '<tr><td style="text-align:center; '..style_s..'" colspan="'..span_im..'" >'..args["вверху"]..'</td></tr>' end function nav_table_start () -- общие стили нав. таблицы local style="" if args["стиль"]~= nil then style=style..args["стиль"] end if args["стиль_тела"]~= nil then style=style..args["стиль_тела"] end local state="" if args["state"] =='' then state="yes" else if args["state"] == nil then state="autocollapse" else state= args["state"] end end return '<table class="navbox collapsible '..state..' nowraplinks" style="margin:auto; '..style..' " >' end local p = {} p.nav = function ( frame) args = require('Module:Arguments').getArgs(frame, {trim = false, removeBlanks = false}) if args["стиль_заголовков"]~= nil then style_h=style_h..args["стиль_заголовков"] end if args["стиль_списков"]~= nil then style_l=style_l..args["стиль_списков"] end if args["изображение"]~= nil then span_im=3 end if args["заголовок1"] ~= nil or args["список1"] ~= nil then prepareN() else prepare() end style_upd (t) return table.concat({nav_table_start(),nav_head(),nav_subhead(),bilding(t),nav_footer(),'</table>'}) end p.subnav = function (frame) args = require('Module:Arguments').getArgs(frame, {trim = false, removeBlanks = false}) prepare() return '<table>'..bilding ( t )..'</table></th>' end return p