Differenze tra le versioni di "Modulo:Wikilib/sigle"

New interface for client modules needing arguments other than the abbreviations *** sovrascritto il testo esistente ***
m (Uuups)
(New interface for client modules needing arguments other than the abbreviations *** sovrascritto il testo esistente ***)
--[[
 
Utility function for modules using Sigle/data
Libreria di funzioni di utilità per
i moduli che usano Sigle/data
 
--]]
local sig = mw.loadData('Modulo:Sigle/data')
local wData = mw.loadData('Modulo:Wikilib/data')
 
--[[
 
Crea i link ai giochi contenuti in games,
visualizzandone le sigle come specificato
da singleDisplay.
 
Questa deve ritornare la visualizzazione di
una singola sigla, avendo come argomento una
coppia della subtable display degli elementi
di Sigle/data, ovvero:
- primo elemento: sigla del gioco
- secondo elemento: colore del gioco
 
--]]
 
q.abbrLinks = function(games, singleDisplay)
return table.concat(table.map(games, function(data)
return string.interp('[[Pokémon ${link}|${display}]]',
{
link = data.link,
display = table.concat(table.map(data.display,
singleDisplay))
})
end))
end
 
--[[
 
Crea i link ai giochi contenuti in games,
visualizzandone il testo usando display.
 
Questa deve ritornare la visualizzazione
del link, avendo come argomento il valore
dell'indice text, o link se il primo è
assente, degli elementi di Sigle/data
 
--]]
 
q.textLinks = function(games, display)
local links = table.map(games, function(data)
return string.interp('[[Pokémon ${link}|${display}]]',
{
link = data.link,
display = display(data.text or data.link)
})
end)
--[[
Se l'ultimo link ha ' e ' nel testo visualizzato
non serve aggiungerlo come separatore finale
--]]
return links[#links]:find('|.* e .*%]%]') and table.concat(links, ', ')
or mw.text.listToText(links, ', ', ' e ')
end
 
--[[
 
Crea la sigla del gioco a partire i suoi dati, con
un eventuale separatore a dividere i giochi e con
lettere colorate e in grassetto se specificato.
 
--]]
 
q.displayAbbr = function(abbrData, sep, coloredAndBold)
local makeAbbr
if coloredAndBold then
makeAbbr = function(abbr, color)
return string.interp([[<span style="color: #${c};">'''${a}'''</span>]],
{
c = c[color].normale,
a = abbr
})
end
else
makeAbbr = function(abbr) return abbr end
end
 
return table.concat(table.map(abbrData, function(gamesData)
return table.concat(table.map(gamesData.display, function(displayData)
return makeAbbr(displayData[1], displayData[2])
end), sep)
end), sep)
end
 
--[[
end), sep or '/')
end
 
 
--[[
 
This function returns the linksabbreviations toof all of the games in an abbreviation data,abbreviaton
data, displayed as abbreviations within a background of the color of the game.
 
Arguments:
 
Return:
- A list of strings, each one containing athe singletext link.for the games grouped
together in the abbreviation data.
 
--]]
q.backgroundAbbrevLinksbackgroundAbbrs = function(data, makeText, makeColors)
makeColors = makeColors or q.colorAndText
makeText = makeText or tostring
 
return q.makeLinksmapDisplay(data, function(text, color)
local background, textColor = makeColors(color)
return string.interp('<span style="padding: 0 0.3em; background: #${bg}; color: #${color};">${text}</span>', {
text = makeText(text)
})
end)
end
 
--[[
 
This function returns the links to all of the games in an abbreviation data,
displayed as abbreviations with a background of the color of the game.
 
Arguments:
- data: the data of an abbreviation, from module Sigle/data.
- makeText: the function creating the text to be displayed. It takes the
abbreviation of a single game, and returns the text to be displayed
in the link. Defaults to tostring.
- makeColors: the function returning the colors to be used. It takes the
color name as an argument, and returns the background and text color
to be used in the link. Defaults to colorAndText.
 
Return:
- A list of strings, each one containing a single link.
 
--]]
q.backgroundAbbrLinks = function(data, makeText, makeColors)
makeColors = makeColors or q.colorAndText
makeText = makeText or tostring
 
return q.makeLinks(data, function(abbrData)
return q.backgroundAbbrs(abbrData, makeText, makeColors)
end)
end
--[[
 
This function returns the linksabbreviations toof all of the games in an abbreviation data,abbreviaton
data, displayed asin abbreviationsthe coloredspecified incolor theirand gameshade. The color. can be passed as
an hexadecimal or as a named color; it defaults to each game's own color. The
shade default is 'normale'.
 
Arguments:
abbreviation of a single game, and returns the text to be displayed
in the link. Defaults to tostring.
- shadetextColor: theThe shadecolor ofthe gamesabbreviations colorshould tohave. Can be useda fornamed the abbreviations.color
or an hexadecimal Defaults to each game'normale's own color.
- shade: The shade of the color to use. Defaults to 'normale'.
 
Return:
- A list of strings, each one containing athe singletext link.for the games grouped
together in the abbreviation data.
 
--]]
q.coloredAbbrevLinkscoloredAbbrs = function(data, makeText, textColor, shade)
shade = shade or 'normale'
makeText = makeText or tostring
 
return q.makeLinksmapDisplay(data, function(text, colorgameColor)
local color = textColor or gameColor
return string.interp('<span style="color: #${color};">${text}</span>', {
-- This is ok also for when c[color][shade] evaluates to false
color = c[color] and c[color][shade] or color,
text = makeText(text)
})
end)
end
 
--[[
 
This function returns the links to all of the games in an abbreviation data,
displayed displayed in the specified color and shade. The color can be passed
as an hexadecimal or as a named color; it defaults to each game's own color.
The shade default is 'normale'.
 
Arguments:
- data: the data of an abbreviation, from module Sigle/data.
- makeText: the function creating the text to be displayed. It takes the
abbreviation of a single game, and returns the text to be displayed
in the link. Defaults to tostring.
- textColor: The color the abbreviations should have. Can be a named color
or an hexadecimal Defaults to each game's own color.
- shade: the shade of games color to be used for the abbreviations.
Defaults to 'normale'.
 
Return:
- A list of strings, each one containing a single link.
 
--]]
q.coloredAbbrLinks = function(data, makeText, textColor, shade)
shade = shade or 'normale'
makeText = makeText or tostring
 
return q.makeLinks(data, function(abbrData)
return q.coloredAbbrs(abbrData, makeText, textColor, shade)
end)
end
 
This function returns the normal shade of a color, and the color the text
should have is such color is used as a background. The color can be given
either as a named color or as an hexadecimal. In the latter case, however, the
text color will always be black, except for black hexadecimal.
 
Arguments:
- color: the name of the color or an hexadecimal.
 
--]]
q.colorAndText = function(color)
-- This is ok also for when c[color].normale evaluates to false
local background = c[color] and c[color].normale or color
local text = table.search(wData.whitetext, color:lower())
or '000'
return background, text
end
 
--[[
 
This function concatenates the string representation of the passed
abbreviations, as found in the given data module. Every non-found abbreviaton
will have the empty string as its string representation.
 
Arguments:
- abbrs: The abbreviations to be merged, as a space separated string or as
a table
- dataModule: The data module holding the content that will be
concatenated.
 
--]]
q.concatAbbrs = function(abbrs, dataModule)
abbrs = type(abbrs) == 'string' and mw.text.split(abbrs, ' ') or abbrs
 
return w.mapAndConcat(abbrs, function(abbr)
return dataModule[abbr] or ''
end)
end
 
--[[
 
This function returnsmaps over the linksdisplay to allkey of theevery gamesgame in an abbreviationthe data,passed
abbreviation data. The results of the mapping are concatenated for every
displayed as specified.
display property, so that the returned table is one-dimensioned.
 
Arguments:
- data: theThe data of ansource abbreviation, from module Sigle/data.
- makeText: theThe mapping function. creatingIt thetakes textas toparameters be displayed forthe atwo singleelements
game.of Itdisplay takeskey the abbreviation ofelements, that oneis gamethe abbreviation and itsthe color name.of a
game. Must return a string.
 
Return:
- A list of stringsstring, each one containingresulting afrom singlethe link.concatenation of the items
in the display key of a game, after mapping.
 
--]]
q.makeLinksmapDisplay = function(data, makeText)
return table.map(data, function(game)
local text =return w.mapAndConcat(game.display, function(pair)
return makeText(pair[1], pair[2])
end)
end)
end
 
--[[
 
This function returns a list of links from the passed abbreviation data. The
links target is the page in the link key of every game in the data, while the
displayed text is obtained by mapping with the specified function.
 
Arguments:
- data: The source abbreviation data.
- makeText: The mapping function that produces the text to be displayed
for every game in the abbreviation data. Takes such data as an
argument, and must return a table with the text for every game in the
data, in the same order as they are in the abbreviation data.
 
Return:
- A list of links, one for each game in the abbreviation data.
 
--]]
q.makeLinks = function(data, makeText)
local zipped = table.zip(data, makeText(data))
 
return table.map(zipped, function(pair)
local game, text = unpack(pair)
 
return string.interp('[[${link}|${text}]]', {
})
end)
end
 
--[[
 
This function returns a table containing interface functions, both for lua and
wikicode. Wikicode interfaces are named after abbreviations, while lua
interfaces have the 'Lua' and '_lua' suffixes.
 
The interfaces are created by means of makeInterfaces, that must return at
least the lua function: the wikicode interface defaults to the standard one
calling the lua interface as in Wikilib.stdWikicodeInterface.
 
Arguments:
- makeInterfaces: This is the functino used to generate the lua interfaces,
and optionally the wikicode ones. Its parameters are abbreviation data
and its abbreviation. Must return the lua function and optionally the
wikicode one.
 
--]]
q.makeLuaAndWikicode = function(makeInterfaces)
local a = {}
 
for abbr, data in pairs(sig) do
local lua, wikicode = makeInterfaces(data, abbr)
wikicode = wikicode or w.stdWikicodeInterface(lua)
 
a[abbr .. 'Lua'], a[abbr .. '_lua'], a[abbr] = lua, lua, wikicode
end
 
return a
end
 
--[[
 
This function generates thea WikiCode interface for the client modules. As explained
below, the interface takes no arguments other than optional abbreviations.
 
This function is meant to be givennamed the name ofafter an abbreviation, and can take an
an arbitrary number of otheradditional abbreviations as arguments. For this intended
use, an example call would be:
 
{{#invoke: ClientModule | abbr0 | abbr1 | abbr2 }}
 
The returned function processes every abbreviaton, including the one it is meant to be
meant to be named after, via makeGame, and then the resulting table is passed on to
on to postProcess for the final processing before returning the value to WikiCode.
WikiCode.
 
Arguments:
- abbr: the abbreviaton this function is meant to be bound to, that will
always be prepended to the passed arguementsarguments.
- makeAbbrev: this function is used to geenrategenerate data for a single
abbreviation. It takes the data of such abbreviation in SugleSigle/data,
and returns whatever postProcess is able to handle as its list elementsitems.
- postProcess: this function is used for processing the data generated from
the abbreviations before the final result is given back to WikiCode.
 
return function(frame)
local args = w.trimAll(mwtable.clonecopy(frame.args))
table.insert(args, 1, abbr)
 
end))
end
end
 
--[[
 
This function generates a lua interface for client modules. As explained
below, the interface takes a single table, containing both arbitrary arguments
and extra abbreviations.
 
This function is meant to be named after an abbreviation, and takes as
argument a single table. One of the keys is meant to store some extra
abbreviations, either as a table or as a space-separated list. The other items
are the other arguments that the function needs.
 
Some example calls:
 
myModule.abbr0{games = {'abbr1', 'abbr2'}, arg1, arg2, arg3)
myModule.abbr0{games = 'abbr1 abbr2', arg1, arg2, arg3)
myModule.abbr0{arg1, arg2, arg3}
 
The returned function processes every abbreviaton, including the one it is
meant to be named after, via makeGame, and then the resulting table is passed
on to postProcess for the final processing before returning the value to
WikiCode.
 
Arguments:
- abbr: the abbreviaton this function is meant to be bound to, that will
always be prepended to the passed arguments.
- abbrKey: the key of the table the abbreviations are stored at.
Defaults to 'games'
- makeAbbrev: this function is used to generate data for a single
abbreviation. It takes the data of such abbreviation in Sigle/data and
the fixed final arguments packed in a table. It returns whatever
postProcess is able to handle as its list elements.
- postProcess: this function is used for processing the data generated from
the abbreviations before the final result is given back to WikiCode.
It takes the fixed final arguments packed in a table and a list of
whatever makeAbbrev returns, and should return a string. Defaults to
a function concatenating the second argument.
 
--]]
q.onMergedAbbrsArgs = function(abbr, abbrKey, makeAbbrev, postProcess)
postProcess = postProcess or function(_, t) return table.concat(t) end
abbrKey = abbrKey or 'games'
 
local luaInterface = function(args)
local abbrs = args[abbrKey] or {}
 
if type(abbrs) == 'string' then
abbrs = abbrs == '' and {} or mw.text.split(abbrs, ' ')
end
table.insert(abbrs, 1, abbr)
 
args[abbrKey] = nil
 
return postProcess(args, table.map(abbrs, function(game)
return makeAbbrev(sig[game], args)
end))
end
 
return luaInterface
end
 
106 621

contributi