forked from pokemoncentral/wiki-lua-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBox.lua
341 lines (277 loc) · 12.6 KB
/
Box.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
--[[
This module contains utility code for boxes: these are a link in a rounded
box, with a left-to-right gradient from the dark shade of a color to the
normal shade of the same color.
Other than this basic styles, the best way to style a box is via predefinite
configurations. For example, 'thick' creates a box with lots of whitespace
above and below the text, while 'tiny' returns a small box, with small text
and little whitespace around it. For more predefinite styles, check the
MANUALE DI STILE INTERMEDIO.
The basic function is called box, and has the following arguments:
- 1: displayed text.
- 2: link target, Defaults to text.
- 3: gradient color, from dark to normal, right-to-left.
- 4: Space-separated string of predefined configurations names. Optional,
defaults to no predefined configurations.
- 5: Space-separated list of CSS classes. Optional, defaults to no CSS classes.
- 6: CSS styles, in the format of an HTML style attribute values. Optional,
defaults to no CSS styles.
- 7: Text color, defaults to #FFFFFF
Example call:
{{#invoke: Box | box | erba | erba (Gruppo Uova) | erba_uova | thin |
inline-block width-sm-60 | padding: 2ex; margin-top: 0.5ex; | FFFFFF }}
There is also a number of shorthands, with a rather similar call. They are
listed below:
- TypeBox: a box displaying a type, with its color as background. Arguments:
- 1: The type. Defaults to 'Sconosciuto'.
- 2: Space-separated string of predefined configurations names. Optional,
defaults to no predefined configurations.
- 3: Space-separated list of CSS classes. Optional, defaults to no CSS
classes.
- 4: CSS styles, in the format of an HTML style attribute values. Optional,
defaults to no CSS styles.
Example: {{#invoke: Box | typeBox | Elettro | thick | | margin: 3em; }}
- CatBox: a box displaying a move category, with its color as background. Arguments:
- 1: The category. Defaults to 'Stato'.
- 2: Space-separated string of predefined configurations names. Optional,
defaults to no predefined configurations.
- 3: Space-separated list of CSS classes. Optional, defaults to no CSS
classes.
- 4: CSS styles, in the format of an HTML style attribute values. Optional,
defaults to no CSS styles.
Example: {{#invoke: Box | catBox | Speciale | thin | | margin: 3em; }}
- EggBox: a box displaying an egg grup, in its color as a background. Arguments:
- 1: The egg group, without ' (Gruppo Uova)' suffix. Defaults to
'Sconosciuto (Gruppo Uova)'.
- 2: Space-separated string of predefined configurations names. Optional,
defaults to no predefined configurations.
- 3: Space-separated list of CSS classes. Optional, defaults to no CSS
classes.
- 4: CSS styles, in the format of an HTML style attribute values. Optional,
defaults to no CSS styles.
Example: {{#invoke: Box | eggBox | Erba | | inline-block | margin: 3em; }}
In addition to the single box versions, also multiple box version are provided
for the shorthands. Their name is the same as the shorthand, with 'box'
replaced by 'list'. They are invoked the same way, except that the first
argument is a comma-separated list of types, egg groups, etc. The other
arguments are applied to all generated boxes. Some example calls below:
- typeList: {{#invoke: Box | typeList | Elettro, Terra, Acciaio | |
inline-block | margin: 3em; }}
- eggList: {{#invoke: Box | eggList | Acqua 1, Campo, Magico | tiny }}
--]]
local b = {}
local mw = require('mw')
local txt = require('Wikilib-strings') -- luacheck: no unused
local tab = require('Wikilib-tables') -- luacheck: no unused
local css = require('Css')
local w = require('Wikilib')
--[[
This table holds predefined styles configurations for boxes. Names of
such configurations are the keys, while values are tables with 'classes'
and 'styles' keys. These hold classes and styles respectively, and have as
values the same structures as arseClasses and parseStyles return.
--]]
local predefs = {
thick = {
classes = {'roundy-5', 'text-center'},
styles = {['padding'] = '0.5ex'}
},
thin = {
classes = {'roundy-5', 'text-center'},
styles = {['font-size'] = '90%', ['padding'] = '0 0.5ex'}
},
tiny = {
classes = {'roundy-5', 'text-center', 'text-small'},
styles = {['padding'] = '0 0.3ex'}
}
}
--[[
Aliases for auto-generated shorthands.
Every top-level item relates to the shorthand with the same key, defined below
in b.shortHands.
Every alias has two subtables, one for box shorthands and the other one for
the corresponding list shorthand. These can contain an arbitrary number of
elements.
--]]
local aliases = {
type = {
box = {'boxTipo', 'box_tipo'},
list = {'listTipo', 'list_tipo'}
}
}
--[[
This function exports the given pair of lua-wikicode interface, taking care of
aliasing. Aliases are added for both lua and wikicode intefaces, in the
CamelCase and snake_case flavor for the former and CamelCase only for the
latter.
Arguments:
- name: the name of the shorthand, as top-level key in aliases.
- suffix: the second-level key in the aliases table, also used as a suffix
for the non-alias exports.
- luaFunction: the lua function to be exported.
- wikicodeFunction: the wikicode companion of luaFunction.
--]]
local export = function(name, suffix, luaFunction, wikicodeFunction)
suffix = suffix or 'box'
local nameAliases = aliases[name] and aliases[name][suffix]
-- Aliases-independent names (eg. typeBoxLua)
local luaNames = {table.concat{name, string.fu(suffix), 'Lua'},
table.concat{name, '_', suffix, '_lua'}}
local wikicodeNames = {name .. string.fu(suffix),
string.fu(name .. suffix)}
-- Aliases-dependent names (eg, boxTipoLua)
if nameAliases then
luaNames = table.merge(luaNames, table.flatMap(nameAliases,
function(alias) return {alias .. 'Lua', alias .. '_lua'} end))
wikicodeNames = table.merge(wikicodeNames, table.flatMap(nameAliases,
function(alias) return {alias, string.fu(alias)} end))
end
for _, luaName in pairs(luaNames) do
b[luaName] = luaFunction
end
for _, wikicodeName in pairs(wikicodeNames) do
b[wikicodeName] = wikicodeFunction
end
end
--[[
This function returns the wikicode interface for a given lua one.
Unlike Wikilib.stdWikicodeInterface, it does not map the empty string to nil:
in fact, the predefinite styles list can be empty, and it is in such cases
that classes and styles are more important. However, with empty string mapped
to nil, classes and styles wouldn't be unpacked and passed to the lua function,
meaning that the box would basically be unstyled.
--]]
local makeWikicodeIntreface = function(luaFunction)
return function(frame)
local p = w.trimAll(table.copy(frame.args), false)
return luaFunction(table.unpack(p))
end
end
--[[
Shorthands are wrapper around the main b.boxLua function. Generally, they
derive the arguments to the latter from a smaller set of arguments: for
example, a box displaying a type has the same displayed text, link target and
color, that is the name of the type itself.
In this table, every element is a function that from arbitrary arguments
returns the b.boxLua arguments.
--]]
b.shortHands = {
--[[
Box displaying a type. Arguments:
- tipo: The type, defaults to 'Sconosciuto'
- pdfs: Table or space-separated string of predefined
configurations names. Optional, defaults to {}.
- classes: Table/string of CSS classes, in the format parseClasses
and printClasses produce respectively. Optional, defaults to {}.
- styles: Table/string of CSS styles, in the format parseStyles and
printStyles produce respectively. Optional, defaults to {}.
--]]
type = function(tipo, pdfs, classes, styles)
tipo = string.fu(string.trim(tipo or 'Sconosciuto'))
if type(classes) == 'table' then
classes = table.copy(classes)
table.insert(classes, 'box-' .. tipo:lower())
else
classes = table.concat{ classes or "", ' box-', tipo:lower() }
end
return tipo, tipo, nil, pdfs, classes, styles, 'FFF'
end,
--[[
Box displaying a damage category. Arguments:
- cat: The category. Defaults to 'Stato'
- pdfs: Table or space-separated string of predefined
configurations names. Optional, defaults to {}.
- classes: Table/string of CSS classes, in the format parseClasses
and printClasses produce respectively. Optional, defaults to {}.
- styles: Table/string of CSS styles, in the format parseStyles and
printStyles produce respectively. Optional, defaults to {}.
--]]
cat = function(cat, pdfs, classes, styles)
cat = string.fu(string.trim(cat or 'Stato'))
return cat, 'Categoria danno#' .. cat, cat, pdfs,
classes, styles, 'FFF'
end,
--[[
Box displaying an egg group. Arguments:
- egg: The egg group, without ' (Gruppo Uova)' suffix. Defaults to
'Sconosciuto (Gruppo Uova)'
- pdfs: Table or space-separated string of predefined
configurations names. Optional, defaults to {}.
- classes: Table/string of CSS classes, in the format parseClasses
and printClasses produce respectively. Optional, defaults to {}.
- styles: Table/string of CSS styles, in the format parseStyles and
printStyles produce respectively. Optional, defaults to {}.
--]]
egg = function(egg, pdfs, classes, styles)
egg = string.fu(string.trim(egg or 'Sconosciuto (Gruppo Uova)'))
return egg, egg .. ' (Gruppo Uova)', egg .. '_uova', pdfs,
classes, styles, 'FFF'
end
}
--[[
Main function creating a box. Lua interface. Arguments:
- text: displayed text.
- link: link target, Defaults to text.
- color: gradient color, from dark to normal, right-to-left. If nil, doesn't
apply any background (leaving it to classes).
- pdfs: Table or space-separated string of predefined configurations names.
Optional, defaults to {}.
- classes: Table/string of CSS classes, in the format parseClasses and
printClasses produce respectively. Optional, defaults to {}.
- styles: Table/string of CSS styles, in the format parseStyles and
printStyles produce respectively. Optional, defaults to {}.
- textcolor: Text color, defaults to #FFFFFF
--]]
b.boxLua = function(text, link, color, pdfs, classes, styles, textcolor)
classes, styles = css.classesStyles(predefs, pdfs, classes, styles)
return string.interp([=[<div class="${class}" style="${bg}; ${style}">[[${link}|<span style="color:#${tc}">${text}</span>]]</div>]=], {
class = css.printClasses(classes),
bg = color and css.horizGradLua{color, 'dark', color, 'normale'} or '',
tc = textcolor or 'FFF',
link = link or text,
text = text,
style = css.printStyles(styles)
})
end
b.box_lua = b.boxLua
--[[
Wikicode interface to boxLua. More info (but you won't likely need it if you
read till here ;) ), in the top comment.
--]]
b.box = makeWikicodeIntreface(b.boxLua)
b.Box = b.box
--[[
Generating exports for the shorthands: both lua and wikicode interfaces,
both for list and single boxes.
--]]
for name, makeBoxArgs in pairs(b.shortHands) do
--[[
Lua interface: just calls b.boxLua with wathever the shorthand returns,
given arbitrary arguments.
--]]
local luaFunction = function(...)
return b.boxLua(makeBoxArgs(...))
end
local wikicodeFunction = makeWikicodeIntreface(luaFunction)
-- Exporting single box interfaces
export(name, 'box', luaFunction, wikicodeFunction)
--[[
List-of-boxes lua interface: the first argument is a sequence: from
each item a box is generted, passing along the resto fo the arguments,
that are therefore the same for every item in the sequence.
The sequence can be a table of strings or a comma-separated string.
--]]
local luaList = function(items, ...)
local args = {...}
if type(items) == 'string' then
items = items == '' and {} or mw.text.split(items, ',%s*')
end
return w.mapAndConcat(items, function(item)
return luaFunction(item, table.unpack(args))
end)
end
local wikicodeList = makeWikicodeIntreface(luaList)
-- Exporting list interfaces
export(name, 'list', luaList, wikicodeList)
end
return b