Differenze tra le versioni di "Modulo:Css"

1 095 byte aggiunti ,  02:59, 7 gen 2017
nessun oggetto della modifica
(Added new functions *** sovrascritto il testo esistente ***)
local w = require('Modulo:Wikilib')
local c = mw.loadData('Modulo:Colore/data')
 
-- Prepends a comma to non-empty strings only
local prependComma = function(text)
return text ~= '' and text .. ', ' or text
end
 
--[[
vendorMappings.gradient = {
moz = {
[''] = '',
['to right'] = 'left'
},
 
webkit = {
[''] = '',
['to right'] = 'left'
}
}
 
-- All linear gradient function names
local linearGradientsFunctions = {
'-moz-linear-gradient',
'-webkit-linear-gradient',
'linear-gradient'
}
 
 
-- Holds all kinds of wikicode input processing
local processInput = {}
 
--[[
 
Returns a list of color hexes from
a table of couples: first one is the
color name, second one the shade
(defaulting to normal)
 
--]]
processInput.parseCouples = function(args)
local name = table.remove(args, 1)
local shade = table.remove(args, 1)
shade = (shade == '' or shade == nil)
and 'normale'
or shade
 
if #args == 0 then
return c[name][shade]
end
 
return c[name][shade], processInput.parseCouples(args)
end
 
--[[
requested color hexes.
 
Can take an arbitrary number of
First and third
arguments, areas colorlong names,as secondthey are
organized in couples: first the color
and fourth their respective variants.
name, then the shade (if the shade
Empty first variants default to 'normal',
is empty secondit variants defaultdefaults to first ones'normale').
 
If the first argument isn't a known
color, all arguments are assumed to
be hexes.
 
For two-color cases, there are special
If the first argument isn't a known color
rules: empty color name or shade default
it is assumed that first and second arguments
to their counterpart of the first color.
are hexes
 
--]]
processInput.gradient = function(args)
local p = w.trimAll(args, truefalse)
 
-- Colore/data indexing fails, assuming hexes
if not c[p[1]] then
return unpack(p[1], p[2])
end
 
-- Two-color case special rules
local from = {
if #p < 5 then
name = p[1],
p = w.emptyStringToNil(p, string.lower)
variant = string.lower(p[2] or 'normale')
}
 
local tofrom = {
name = p[31] or from.name,
variant shade = string.lower(p[42] or from.variant)'normale'
}
 
local to = {
return c[from.name][from.variant], c[to.name][to.variant]
name = p[3] or from.name,
shade = p[4] or from.shade
}
 
return c[from.name][from.shade], c[to.name][to.shade]
end
 
-- Standard behavior
return processInput.parseCouples(args)
end
 
 
-- Generates styles for linear gradients
styles.gradient.linear = function(conf, from, to...)
 
return string.interp('background-size: 100%; background-image: -moz-linear-gradient(${mozConf}#${from}, #${to}); background-image: -webkit-linear-gradient(${webkitConf}#${from}, #${to}); background-image: linear-gradient(${conf}#${from}, #${to});',
-- Grouping variadic and adding # to hexes
{
local colors = table.map({...}, function(hex)
conf = prependComma(conf),
return hex:find('#') and hex or '#' .. hex
mozConf = prependComma(vendorMappings.gradient.moz[conf]),
end)
webkitConf = prependComma(vendorMappings.gradient.webkit[conf]),
 
from = from,
-- Accumulator table
to = to
local css = {'background-size: 100%'}
})
 
for _, funct in pairs(linearGradientsFunctions) do
 
-- Cloning due to later table.insert
local gradientArgs = mw.clone(colors)
 
if conf then
local prefix = funct:match('^%-(%a+)%-')
local conf = prefix
and vendorMappings.gradient[prefix][conf]
or conf
table.insert(gradientArgs, 1, conf)
end
 
gradientArgs = table.concat(gradientArgs, ', ')
 
table.insert(css, table.concat{'background-image: ',
funct, '(', gradientArgs , ')'})
end
 
return table.concat(css, '; ') .. ';'
end
 
-- Generates horizontal linear gradients styles
css.horizGradLua = function(from, fromVariant, to, toVariant...)
local args = {from, fromVariant, to, toVariant}
return styles.gradient.linear('to right',
processInput.gradient(args{...}))
end
css.horiz_grad_lua = css.horizGradLua
 
-- Generates vertical linear gradients styles
css.vertGradLua = function(...)
return styles.gradient.linear(nil,
processInput.gradient({...}))
end
css.vert_grad_lua = css.vertGradLua
 
--[[
--]]
css['vert-grad'] = function(frame)
return styles.gradient.linear(''nil,
processInput.gradient(mw.clone(frame.args)))
end