Модуль:Клавиатура компьютера
Из Википедии, бесплатной энциклопедии
Это модуль для шаблона {{Клавиатура компьютера}}
local p = {} local function getLayout(frame) local pframe = frame:getParent() local layoutPage = pframe.args[1] local layout = pframe.args['раскладка'] if layout == nil or layout == '' then if layoutPage == nil or layoutPage == '' then layoutPage='PC ISO' elseif layoutPage=='Apple' or layoutPage=='apple' then layoutPage='Apple ISO' end local layoutSubpage = mw.title.new('Клавиатура компьютера/'..layoutPage,'шаблон') if layoutSubpage==nil then error('не найдена страница '..'Шаблон:Клавиатура компьютера/' .. layoutPage) end layout=layoutSubpage:getContent() end local labelsPage = pframe.args[2] local labels = pframe.args['обозначения'] if layoutPage ~= nil and layoutPage ~='' and (labels ==nil or labels == '') then if labelsPage == nil or labelsPage == '' then labelsPage = 'RU' end local labelsSubpage=mw.title.new('Клавиатура компьютера/'..layoutPage..'/'..labelsPage..'.json','шаблон') if labelsSubpage==nil then error('не найден пакет обозначений '..labelsPage) end labels = labelsSubpage:getContent() end local labelsArr = mw.text.jsonDecode(labels) return {layout,labelsArr} end function p.draw(frame) local gl=getLayout(frame) local layout=gl[1] local labelsArr=gl[2] local keyPage='Клавиатура компьютера/Клавиша' local pframe = frame:getParent() if pframe.args['тип клавиши']=='круглая' then keyPage='Клавиатура компьютера/Круглая клавиша' end local result = '' local x=0; local y=0; local nrow = 1; local ncol = 1; for row in mw.text.gsplit(layout,'\n',true) do if row=='--' then break end for itm in mw.text.gsplit(row,';',true) do local keyCaption='' local keyLink='' local curLabel = labelsArr[nrow][ncol] if type(curLabel)=='table' then keyCaption=curLabel['c'] keyLink=curLabel['l'] else keyCaption=curLabel or '' end keyCaption=frame:preprocess(keyCaption) keyCaption=string.gsub(keyCaption,'\n','<br>') local width=string.match(itm,'[0-9.]+') local height=1 if string.sub(itm,1,1)=='R' then y=y-1+width nrow=nrow-1 break end if string.sub(itm,1,1)=='V' then height=width width=1 end if itm=='ISOENTER' then width=1 result=result..frame:expandTemplate{ title=keyPage, args={'',(x+0.25)*40,(y+1)*40-4,36,30} } result=result..frame:expandTemplate{ title=keyPage, args={'',x*40,y*40,46} } result=result..frame:expandTemplate{ title=keyPage, args={keyCaption,(x+0.25)*40+2,y*40,36,66,keyLink, ['дополнительный стиль']='border-left:none;border-top-left-radius:0;'} } x=x+1.5 ncol=ncol+1 elseif itm=='STEPCAPS' then result=result..frame:expandTemplate{ title=keyPage, args={'',x*40,y*40,56} } result=result..frame:expandTemplate{ title=keyPage, args={keyCaption,x*40,y*40,36,26,keyLink} } x=x+1.75 ncol=ncol+1 elseif width~=nil then if string.sub(itm,1,1) ~='S' then result=result..frame:expandTemplate{ title=keyPage, args={keyCaption,x*40,y*40,width*40-14,height*40-14,keyLink} } ncol=ncol+1 end x=x+width end end x=0 ncol=1 nrow=nrow+1 y=y+1 end return '<div style="position:relative">'..result..'</div>' end function p.getWidth(frame) local gl=getLayout(frame) local layout=gl[1] maxWidth=0; for row in mw.text.gsplit(layout,'\n',true) do local curWidth=0 if row=='--' then break end for itm in mw.text.gsplit(row,';',true) do local width=string.match(itm,'[0-9.]+') if string.sub(itm,1,1)=='V' then width=1 elseif itm=='ISOENTER' then width=1.5 elseif itm=='STEPCAPS' then width=1.75 end if width~=nil then curWidth=curWidth+width end end if maxWidth<curWidth then maxWidth=curWidth end end return (maxWidth*40+5) end function p.getHeight(frame) local gl=getLayout(frame) local layout=gl[1] local height=0 for row in mw.text.gsplit(layout,'\n',true) do local curHeight=1 if string.sub(row,1,1)=='R' then curHeight=string.match(row,'[0-9.]+') end height=height+curHeight end return (height*40) end return p