Modul:Babel
Napohlad

Programowanje předłohi | Diskusije | Lua | Testy | Podstrony | ||||
---|---|---|---|---|---|---|---|---|
Modul | Hornjoserbsce | English | Deutsch
|
Modul: | Dokumentacija |
Jeli maš prašenje k tutemu modulu, da so móžeš na stronje Wikipedija:Lua/prašenja prašeć. |
![]() |
|
-- Babel -- 20150531
local p = {}
function p._specialist(babels,args)
-- this function builds the box used by the {{Babel-fachowy2}} template
args.header = "[[Project:Babel|Fachowe znajomosće]]" or args.header
args.footer = "[[:Kategorija:Wužiwarjo po předmjetach|Wužiwarjo po předmjetach]]" or args.footer
return p._babel(babels,args)
end
function p._babel(babels, args)
-- This function builds the babel box used by the {{babel}} template.
local ret = mw.html.create('table')
:addClass('userboxes')
:css( {
float = args.align or 'right',
['margin-left'] = (args.left or '1') .. 'em',
['margin-bottom'] = (args.bottom or '0') .. 'em',
width = (args.width or '248') .. 'px',
clear = args.align or 'right',
color = args.textcolor or '#000000',
border = (args.bordercolor or '#99B3FF') .. ' solid ' .. (args.solid or 1)..'px'
} )
if (args.shadow and string.lower(args.shadow) == 'yes') or (args['sćin'] and string.lower(args['sćin']) == 'haj') then
ret:css({ ['box-shadow'] = '0 2px 4px rgb(0,0,0,0.2)',
['-mox-box-shadow'] = '0 2px 4px rgb(0,0,0,0.2)',
['-webkit-box-shadow'] = '0 2px 4px rgb(0,0,0,0.2)' })
end
-- formats from {{Bordera radiuso}} and the old {{Babel}}
ret:css({ ['border-radius'] = '0.6em',
['-moz-border-radius:'] = '0.6em',
['-webkit-border-radius'] = '0.6em' })
ret:cssText( args['extra-css'] or '' )
local color = args.color or 'inherit'
local row1 = ret:tag('tr')
local row2 = ret:tag('tr')
local row3 = ret:tag('tr')
local body_cells = row2:tag('td')
:css('vertical-align', 'top !important')
local userboxes
local k = args.nocat or ''
local mf = args['mž'] or ''
local frame = mw.getCurrentFrame()
-- Special message for when first argument is blank; otherwise treat it as normal
if babels[1] and babels[1]:find('%S') then
if mw.ustring.sub(babels[1],1,2) == "[[" then
body_cells:wikitext(babels[1])
elseif mw.ustring.sub(babels[1],1,2) == "{{" and mw.ustring.sub(babels[1],1,3) ~= "{{{" then
body_cells.wikitext(babels[1])
elseif mw.ustring.sub(babels[1],1,1) == "<" then
body_cells.wikitext(babels[1])
else
userboxes = frame:preprocess('{{User|'..babels[1]..'|mž=' .. mf .. '|nocat=' .. k .. '}}')
end
else
userboxes = args.noboxestext or "''Tutón wužiwar njemóže čitać ani pisać někajku rěč. Pomoc je trjeba.''"
end
body_cells:wikitext(userboxes)
-- "remove" args[1] so it isn't looked at in the loop
-- table.remove(args,1) doesn't produce desired result
babels[1] = ''
-- Keep track of how many columns are in this table
local col_span = 1
for _, v in ipairs( babels ) do
-- ! indicates a new cell should be created
if v:find('%S') and v ~= '!' then
if mw.ustring.sub(v,1,2) == "[[" then
body_cells:wikitext(v)
elseif mw.ustring.sub(v,1,2) == "{{" and mw.ustring.sub(v,1,3) ~= "{{{" then
body_cells:wikitext(v)
elseif mw.ustring.sub(v,1,1) == "<" then
body_cells:wikitext(v)
else
body_cells:wikitext( frame:preprocess('{{User|'..v..'|mž=' .. mf .. '|nocat=' .. k .. '}}') )
end
-- Recycling body_cells for <td>
elseif v and v == '!' then
col_span = col_span + 1
body_cells:done()
body_cells = row2:tag('td')
end
end
if (args.header ~= "bjez") then
row1:tag('th')
:css({ ['background-color'] = color,
['text-align'] = 'center' })
:attr('colspan',col_span)
:wikitext( args.header or '[[Wikipedija:Babel|Rěčne kmanosće]]' )
:done()
end
if (args.footer ~= "bjez") then
row3:tag('td')
:css({ ['background-color'] = color,
['text-align'] = 'center' })
:attr('colspan',col_span)
:wikitext( args.footer or '[[:Kategorija:Wužiwarjo po rěčach|Wužiwarjo po rěčach]]' )
:done()
end
-- code for the template {{WužiwarskiKraj}}
if args.kraj ~= nil and args.kraj ~= "" then
local k = args.kraj
local m = args['městno'] or ""
local dm = args['družina městna'] or ""
local mj = args.mjeno or ""
local kon = args.kontinent or ""
local r = args.region or ""
local zk = args['zwjazkowy kraj'] or ""
local wok = args.wokrjes or ""
local woj = args['wojewódstwo'] or ""
body_cells:wikitext( frame:preprocess('{{WužiwarskiKraj|kraj=' .. k .. '|městno=' .. m
.. '|družina městna=' .. dm .. '|mjeno=' .. mj
.. '|kontinent=' .. kon .. '|region=' .. r .. '|zwjazkowy kraj=' .. zk
.. '|wokrjes=' .. wok .. '|wojewódstwo=' .. woj .. '|nocat=' .. k .. '}}') )
end
-- code for the template {{Narodniny}}
if args.narodniny ~= nil and args.narodniny ~= "" then
local n1 = args.narodniny
body_cells:wikitext( frame:preprocess('{{Narodniny|1='
.. n1 .. '|nocat=' .. k .. '}}') )
end
-- code for the template {{Wiki-narodniny}}
if args['wiki-narodniny'] ~= nil and args['wiki-narodniny'] ~= "" then
local wn1 = args['wiki-narodniny']
body_cells:wikitext( frame:preprocess('{{Wiki-narodniny|1='
.. wn1 .. '|nocat=' .. k .. '}}') )
end
-- code for the template {{User UTC}}
if args.utc ~= nil and args.utc ~= "" then
local u = args.utc
body_cells:wikitext( frame:preprocess('{{User UTC|utc=' .. u .. '|nocat=' .. k .. '}}') )
end
-- code for the template {{User UTC2}}
if args.utc1 ~= nil and args.utc1 ~= "" then
local u1 = args.utc1
local u2 = args.utc2
body_cells:wikitext( frame:preprocess('{{User UTC2|utc1=' .. u1 .. '|utc2=' .. u2 .. '|nocat=' .. k .. '}}') )
end
-- code for the template {{Kašćik wikidny}}
if args.wikidny ~= nil and args.wikidny ~= "" then
local wd = args.wikidny
local wm = args['wikiměsacy'] or ""
local wl = args['wikilěta'] or ""
body_cells:wikitext( frame:preprocess('{{Kašćik wikidny|1=' .. wd .. '|2=' .. wm
.. '|3=' .. wl .. '|nocat=' .. k .. '}}') )
end
if args['special-boxes'] then
body_cells:wikitext(args['special-boxes'])
end
body_cells:done()
return tostring(ret)
end
local function processBabelArgs(args)
-- This function processes a table of arguments and returns two tables: an array of portal names for processing by ipairs, and a table of
-- the named arguments that specify style options, etc. We need to use ipairs because we want to list all the portals in the order
-- they were passed to the template, but we also want to be able to deal with positional arguments passed explicitly, for example
-- {{portal|2=Politics}}. The behaviour of ipairs is undefined if nil values are present, so we need to make sure they are all removed.
args = type(args) == 'table' and args or {}
local babels = {}
local namedArgs = {}
for k, v in pairs(args) do
if type(k) == 'number' and type(v) == 'string' then -- Make sure we have no non-string babel names.
table.insert(babels, k)
elseif type(k) ~= 'number' then
namedArgs[k] = v
end
end
table.sort(babels)
for i, v in ipairs(babels) do
babels[i] = args[v]
end
return babels, namedArgs
end
local function makeWrapper(funcName)
-- Processes external arguments and sends them to the alia functions.
return function (frame)
-- If called via #invoke, use the args passed into the invoking
-- template, or the args passed to #invoke if any exist. Otherwise
-- assume args are being passed directly in from the debug console
-- or from another Lua modulo.
local origArgs
if frame == mw.getCurrentFrame() then
origArgs = frame:getParent().args
for k, v in pairs(frame.args) do
origArgs = frame.args
break
end
else
origArgs = frame
end
-- Trim whitespace and remove blank arguments.
local args = {}
for k, v in pairs(origArgs) do
if type(v) == 'string' then
v = mw.text.trim(v)
end
if v ~= '' then
args[k] = v
end
end
return p[funcName](processBabelArgs(args)) -- passes two tables to func: an array of portal names, and a table of named arguments.
end
end
local funcNames = {'babel', 'specialist'}
for _, funcName in ipairs(funcNames) do
p[funcName] = makeWrapper('_' .. funcName)
end
return p