Модуль:Навигация

Материал из Posmotrelisu
Перейти к навигации Перейти к поиску

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

-- формат данных
-- { head_v style_h list_v style_l n } - tr
--


local pos=1 -- итерируемая позиция в разбираемом шаблоне
local args = {} -- элементы шаблона

local style_l ='width:100%;'
local style_h ='white-space:nowrap;'

-- эти 2 переменные можно всунуть внутрь функций
local t = {} -- таблица данных для создания шаблона навигации
local row = {} -- разбираемая строка шаблона навигации

function list ()
if args[pos+1] == nil then return end
row["list_v"]=args[pos+1]
row["style_l"]=style_l
row["n"]=1
pos=pos+2
end  

function header ( )
if args[pos+1] == nil then return end
row["head_v"]=args[pos+1]
row["style_h"]=style_h
pos=pos+2
	if args[pos] == "list" then
		list ()
		row["n"]=2
	else 
		if args[pos] == "subh" then 
		subheader ( )
		end
	end

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

-- преобразует разобранное тело в 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></tr>'
	else 
		if t[i]["head_v"] ~= nil then
			result=result..'<tr><th colspan="2">'..t[i]["head_v"]..'</th></tr>'
		else 
			result=result..'<tr><td colspan="2">'..t[i]["list_v"]..'</td></tr>'
		end
	end

end

return result 

end 

-- разбирает тело нав.таблицы
function prepare ()
row={}

	while true do
		if args[pos] == nil then break end
		
		if args[pos] == "header" then 
		header()
		table.insert(t, row )
		row={}
		end
	
		if args[pos] == "list" then 
		list()
		table.insert(t, row )
		row={}
		end
	
	end

end

-- выдать заголовок таблицы
function nav_head () 
	local z="Пропущен заголовок"
	if args["заголовок"] ~= nil then z=args["заголовок"] end
    local redact='<font color="red">Нет имени шаблона для редактирования</font>'

if args["имя"] ~= nil then 

redact='['..tostring(mw.uri.fullUrl('Шаблон:'..redact, "action=edit"))..' [изменить]]'

end

return '<tr><th colspan="2" style="text-align:center;width:100%;"><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 ()

return ""
end 

function nav_subhead ()

return ""
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="autocollapse"
	if args["state"]~= nil then state= args["state"] 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)
	if args["стиль_заголовков"]~= nil then style_h=style_h..args["стиль_заголовков"] end
	if args["стиль_списков"]~= nil then style_l=style_l..args["стиль_списков"] end

prepare ()
style_upd (t)

return nav_table_start()..nav_head()..bilding(t)..nav_footer()..'</table>'

end

return p