-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikitrek-FunzioniGeneriche.lua
431 lines (388 loc) · 13.7 KB
/
wikitrek-FunzioniGeneriche.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
-- [P2G] Auto upload by PageToGitHub on 2023-12-10T22:03:49+01:00
-- [P2G] This code from page Modulo:wikitrek-FunzioniGeneriche
-- Keyword: wikitrek
local p = {} --p stands for package
function p.EsempioTemplate(frame)
local SubPageName
local SubPageTitle
local Title
local Content
local pre = mw.html.create('pre')
local DoubleLF = string.char(10) .. string.char(10)
if not frame.args[1] then
SubPageName='Esempio'
else
SubPageName=frame.args[1]
end
Title = mw.title.getCurrentTitle()
SubPageTitle = mw.title.makeTitle(Title.namespace, Title.text .. '/' .. SubPageName)
local Intro = 'Questo esempio è automaticamente generato tramite script LUA a partire dal codice di esempio presente in <code>[[' .. SubPageTitle.prefixedText .. ']]</code>'
local CodeString = '=== Il codice ==='
local ReturnString = '=== restituisce ==='
Content = SubPageTitle:getContent()
if not Content then
return "La pagina <code>[[" .. SubPageTitle.prefixedText .. "]]</code> non esiste: non è possibile estrarre il testo e generare l'esempio"
else
pre
:css( 'width', '65%' )
:wikitext(mw.text.nowiki(Content))
return Intro .. DoubleLF .. CodeString .. DoubleLF .. tostring(pre) .. DoubleLF .. ReturnString .. DoubleLF .. frame:expandTemplate{ title = SubPageTitle }
end
end
function p.EsempioBreve(frame)
--<code><nowiki>{{DTItem|Q11}}</nowiki></code> → {{DTItem|Q11}}
local ExampleText
if frame.args[1] == nil then
Return "Error"
else
ExampleText=frame.args[1]
end
return "<code><nowiki>" .. mw.text.nowiki(ExampleText) .. "</nowiki></code> → " .. frame:expandTemplate{ title = ExampleText }
end
function p.TableFromArray(AllRows)
local Table = mw.html.create('table')
local First
local Tr
local Cell
for _, Row in pairs(AllRows) do
Tr = mw.html.create('tr')
First = true
for _, Field in pairs(Row) do
if First then
First = false
Cell = mw.html.create('th')
if (type(Field) == "table") then
Cell
:attr('id', Field[1])
:attr('title', Field[1])
:wikitext(Field[2]..":")
else
Cell
:wikitext(Field)
end
else
Cell = mw.html.create('td')
if #Field > 1 then
List = mw.html.create('ul')
for _, Item in pairs(Field) do
LI = mw.html.create('li')
LI:wikitext(Item)
List:node(LI)
end
Cell:node(List)
else
if type(Field) == "table" then
Cell
:wikitext(Field[1])
else
Cell
:wikitext(Field)
end
end
end
Tr:node(Cell)
end
Table:node(Tr)
end
--[==[if mw.wikibase.getEntity() then
Text = "Modifica i dati nella [[:datatrek:Item:" .. mw.wikibase.getEntityIdForCurrentPage() .. "|pagina della entità]] su ''DataTrek''"
else
Text = "Impossibile trovare l'entità collegata"
end
Table
:css('font-size', 'smaller')
:css('text-align', 'right')
:css('margin', '1px')
]==]
--return tostring(Table)
return Table
end
function p.NoWiki(frame)
return mw.text.nowiki(frame.args[1])
end
--- Returns the color to use in CSS for the text depending on the luminance
-- of the background
-- @param BackColor The hex code of the background color
-- @return name of the color
function p.TextColor(frame)
local hex = string.sub(frame.args[1], 2)
local R, G, B
local L
if hex:len() == 3 then
--return (tonumber("0x"..hex:sub(1,1))*17)/255, (tonumber("0x"..hex:sub(2,2))*17)/255, (tonumber("0x"..hex:sub(3,3))*17)/255
R = tonumber("0x"..hex:sub(1,1))*17/255
G = tonumber("0x"..hex:sub(2,2))*17/255
B = tonumber("0x"..hex:sub(3,3))*17/255
else
R = tonumber("0x"..hex:sub(1,2))/255
G = tonumber("0x"..hex:sub(3,4))/255
B = tonumber("0x"..hex:sub(5,6))/255
end
L = (R * 0.299 + G * 0.587 + B * 0.114) --/ 256
if L < 0.6 then --0.5 then
return "white"
else
return "black"
end
end
--- Test function for array manipulation
--
-- @param frame The interface to the parameters passed to {{#invoke:}}
-- @return Processed string
function p.TestArray(frame)
local TestGroups = {"A", "B", "C", "B", "C"}
local TestValues = {"Alpha", "Beta"}
local FinalArray = {}
local FinalString = ""
for _, Group in pairs(TestGroups) do
for _, Value in pairs(TestValues) do
if FinalArray[Group] == nil then
FinalArray[Group] = {}
end
table.insert(FinalArray[Group], Value)
end
end
for ID, Group in pairs(FinalArray) do
FinalString = FinalString .. "* " .. ID .. ": " .. table.concat(Group, ", ") .. string.char(10)
end
--return FinalArray["A"][1]
return FinalString
end
function p.TestArray2(frame)
local FinalArray = {}
local FinalString = ""
--local Actor = "Annie Wersching"
local Actor = "Emily Coutts"
local QueryResult = mw.smw.getQueryResult('[[Interprete::' .. Actor .. ']]|?' .. Actor .. '|sort=Numero di produzione|order=asc')
if QueryResult == nil then
return "''Nessun risultato''"
end
if type(QueryResult) == "table" then
for k, v in pairs(QueryResult.results) do
-- v.fulltext represents EPISODE
-- v.printouts[Actor][1].fulltext represents CHARACTER
local Episode = v.fulltext
local Character
if v.printouts[Actor][1] == nil then
Character = "''Senza pagina''"
else
--[==[
Character = v.printouts[Actor][1].fulltext
if FinalArray[Character] == nil then
FinalArray[Character] = {}
end
table.insert(FinalArray[Character], Episode)
]==]
for _, CurrChar in pairs(v.printouts[Actor]) do
Character = CurrChar.fulltext
if FinalArray[Character] == nil then
FinalArray[Character] = {}
end
table.insert(FinalArray[Character], Episode)
end
end
end
else
return "''Il risultato non è una TABLE''"
end
for ID, Group in pairs(FinalArray) do
FinalString = FinalString .. "* [[" .. ID .. "]]: [[" .. table.concat(Group, "]], [[") .. "]]" .. string.char(10)
end
return FinalString
end
--- Test function for spaces identifiaction
-- using regex
-- @param frame The interface to the parameters passed to {{#invoke:}}
-- @return Processed string
function p.TestSpaces(frame)
local TestString = "Seven of Nine"
local Match = string.match(TestString, "[^%s]+$")
return Match .. " - " .. string.upper(string.sub(Match, 1, 1))
end
--- Extract the name of a ship from its full designation
--
-- @param frame The interface to the parameters passed to {{#invoke:}}
-- @return Bare name of the ship
function p.ShipName(frame)
local FullName = frame.args[1]
return p.ShipNameCore(FullName)
end
--- Extract the name of a ship from its full designation
--
-- @param designation The full designation to process
-- @return Bare name of the ship
function p.ShipNameCore(designation)
local FullName = designation
local Prefixes = {"USS ", "IKS ", "ECS ", "''", "<i>", "</i>", " %(reboot%)", " %(Kelvin Timeline%)"}
-- Removes prefix
for _, Prefix in ipairs(Prefixes) do
FullName = FullName:gsub((Prefix), "")
end
--Removes suffix year or number specification
FullName = FullName:gsub("%s%(%d+%)", "")
--Removes suffix registry number
FullName = FullName:gsub("%s[^%s]+$", "")
return FullName
end
--- Test function to check properties sorting
--
-- @param frame The interface to the parameters passed to {{#invoke:}}
-- @return Comma-separated list of properties
function p.SortedPropertiesList(frame)
local AllP
Item = mw.wikibase.getEntity('Q11160')
AllP = mw.wikibase.orderProperties(Item:getProperties())
return table.concat(AllP, ",")
end
--- Process the value assigned to parameter of the "old"-style template
-- (pre-DataTrek) to sanitize it and pass it as clean value to
-- SMW property using the #set function
--
-- @param frame The interface to the parameters passed to {{#invoke:}}
-- No return @return Sanitized string representing one or more property values
function p.ParameterToSemantic(frame)
local Separator = ";"
local SepDeclaration = "|+sep=" .. Separator
local ParaString
local FinalArray = {}
local PropName
local PropValue
local LIPattern
if frame.args[1] == nil then
PropName = "Error"
else
PropName = frame.args[1]
if frame.args[2] == nil then
PropValue = "Error"
else
ParaString = frame.args[2]
if string.find(ParaString, "<li>") == nil then
-- Add dummy tags to use a single process afterwards
ParaString = "<li>" .. ParaString .. "</li>"
end
if string.find(ParaString, "%[%[") == nil then
-- There is no wikilink, plain text
LIPattern = "<li>(.-)</li>"
else
-- A wikilink is present, discard surrounding text
LIPattern = "<li>.-%[%[(.-)%]%].-</li>"
end
-- Determine if property is Assignment, so process a string like:
-- [[Timeline 2267|2267]] <i>[[USS Enterprise NCC-1701|USS Enterprise]]</i>
if PropName == "Assegnazione" then
-- then remove italic
ParaString = string.gsub(ParaString, "<i>", "")
ParaString = string.gsub(ParaString, "</i>", "")
-- then remove Timeline
ParaString = string.gsub(ParaString, "(%[%[Timeline.-%]%])", "")
end
--Process UL or OL
for Item in string.gmatch(ParaString, LIPattern) do
Item = string.gsub(Item, "(|.*)", "")
table.insert(FinalArray, Item)
end
PropValue = table.concat(FinalArray, Separator) .. SepDeclaration
end
end
mw.smw.set(PropName .. " = " .. PropValue)
end
--- OLD simpler original version
-- Process the value assigned to "EpisodioPersonaggi" of the "old-style"
-- template (pre-DataTrek) to sanitize it and pass it as clean value to
-- SMW property using the #set function
--
-- @param frame The interface to the parameters passed to {{#invoke:}}
-- No return @return Sanitized string representing one or more property values
function p.PerformersToSemanticOriginal(frame)
local InputString
local Character
local Performer
local Pattern = "%*.-%[%[(.-)%]%].-:%s?%[%[(.-)%]%]"
--InputString = "* [[Vina]]: [[Melissa George]]* [[Spock]]: [[Ethan Peck]]* [[Leland]]: [[Alan van Sprang]]* [[Nhan]]: [[Rachael Ancheril]]* Un [[Talosiani|Talosiano]]: [[Dee Pelletier]]* Il ''Keeper'' [[Talosiani|Talosiano]]: [[Rob Brownstein]]* Lt. Cmdr. [[Airiam]]: [[Hannah Cheesman]]* Lt. [[Keyla Detmer]]: [[Emily Coutts]]* [[Talosiani|Talosiano]] n.3: [[Nicole Dickinson]]"
InputString = frame.args[1]
--_, _, Character, Performer = string.find(InputString, Pattern)
for Character, Performer in string.gmatch(InputString, Pattern) do
Character = string.gsub(Character, "|.*","")
--print("Character: " .. Character, "Performer: " .. Performer)
mw.smw.set("Personaggio=" .. Character)
mw.smw.set("Interprete=" .. Performer)
mw.smw.set(Performer .. " = " .. Character)
end
end
--- NEW improved version
-- Process the values assigned to "EpisodioPersonaggi" parameter of the
-- "old-style" template (pre-DataTrek) to sanitize it and pass it
-- as clean value to SMW property using the #set function
--
-- @param frame The interface to the parameters passed to {{#invoke:}}
-- No return @return Sanitized string representing one or more property values
function p.PerformersToSemantic(frame)
local InputString
local Pattern
InputString = frame.args[1] .. "\n"
for FullRow in string.gmatch(InputString, "%*.-\n") do
local Character
local Performer
--Remove italic
FullRow = string.gsub(FullRow, "<i>", "")
FullRow = string.gsub(FullRow, "</i>", "")
--print (FullRow)
local CountLiks = select(2, string.gsub(FullRow, "%[%[", ""))
--print (CountLiks)
if string.find(FullRow, ":") == nil then
--Character only, unknown performer
Pattern = "%*.-%[%[(.-)%]%].-"
_, _, Character = string.find(FullRow, Pattern)
Performer = "Interprete non accreditato"
else
if CountLiks == 2 then
--Two links in the string, process both
--print("Two links")
Pattern = "%*.-%[%[(.-)%]%].-:%s?%[%[(.-)%]%]"
elseif CountLiks == 1 then
--Character is not a linked entity
--print("One link")
Pattern = "%*%s?(.-)%s?:%s?%[%[(.-)%]%]"
else
--error no links on either side
Pattern = "%*%s?(.-)%s?:%s?(.-)%s?\n"
end
_, _, Character, Performer = string.find(FullRow, Pattern)
end
Character = string.gsub(Character, "|.*","")
--print(" Character: " .. Character, "Performer: " .. Performer)
--print(string.rep("-",100))
mw.smw.set("Personaggio=" .. Character)
mw.smw.set("Interprete=" .. Performer)
mw.smw.set(Performer .. " = " .. Character)
end
end
function p.ParameterToSemanticTest(frame)
local Separator = ";"
local SepDeclaration = "|+sep=" .. Separator
local TestString = "<ul><li>[[Michael Perricone]]</li><li>[[Greg Elliot]]</li></ul>"
local FinalArray = {}
for Item in string.gmatch(TestString, "<li>(.-)</li>") do
table.insert(FinalArray, Item)
end
return mw.text.nowiki(table.concat(FinalArray, Separator) .. SepDeclaration)
end
--- Check if a file is SVG and validate it
--
-- @param frame The interface to the parameters passed to {{#invoke:}}
-- @return Processed string
function p.SVGValidate(frame)
local ValidateURI = "http://validator.w3.org/check?uri="
local FileTitle
local MediaURI
FileTitle = mw.title.getCurrentTitle()
if string.lower(string.sub(FileTitle.fullText, -4)) == ".svg" then
--file is Scalable Vector Graphics
MediaURI = frame:callParserFunction('filepath:' .. string.sub(FileTitle.fullText, 6))
--return FileTitle.fullText .. "<br />" .. FileTitle:fullUrl() .. "<br />" .. FileTitle:localUrl() .. "<br />" .. FileTitle:canonicalUrl() .. "<br />" .. MediaURI
return "\n== Validazione ==\n" .. "[[File:Valid SVG 1.1.svg|88px|link=" .. ValidateURI .. mw.uri.encode(MediaURI, "PATH") .. "]]"
else
return nil
end
end
return p