Anonimo

Modulo:FunzioniGeneriche: differenze tra le versioni

Da Wikitrek.
m
nessun oggetto della modifica
Nessun oggetto della modifica
mNessun oggetto della modifica
 
(99 versioni intermedie di uno stesso utente non sono mostrate)
Riga 79: Riga 79:
Cell:node(List)
Cell:node(List)
else
else
Cell
if type(Field[1]) == "table" then
:wikitext(Field[1])
Cell
:wikitext(Field[1][1])
else
Cell
:wikitext(Field[1])
end
end
end
end
end
Riga 128: Riga 133:
      
      
     L = (R * 0.299 + G * 0.587 + B * 0.114) --/ 256
     L = (R * 0.299 + G * 0.587 + B * 0.114) --/ 256
     if L < 0.5 then
     if L < 0.6 then --0.5 then
     return "white"
     return "white"
     else
     else
Riga 135: Riga 140:
end
end
--- Returns the color to use in CSS for the text depending on the luminance
--- Test function for array manipulation
-- of the background
--  
-- @param BackColor The hex code of the background color
-- @param frame The interface to the parameters passed to {{#invoke:}}
-- @return name of the color
-- @return Processed string
function p.TestArray(frame)
function p.TestArray(frame)
local TestGroups = {"A", "B", "C", "B", "C"}
local TestGroups = {"A", "B", "C", "B", "C"}
Riga 205: Riga 210:
for ID, Group in pairs(FinalArray) do
for ID, Group in pairs(FinalArray) do
FinalString = FinalString .. "* " .. ID .. ": " .. table.concat(Group, ", ") .. string.char(10)
FinalString = FinalString .. "* [[" .. ID .. "]]: [[" .. table.concat(Group, "]], [[") .. "]]" .. string.char(10)
end
end
return FinalString
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
end
return p
return p