Módulo:Gadgets
Aparência
Módulo para processar definições de gadgets de MediaWiki:Gadgets-definition.
Uso
Destinado ao uso apenas a partir de outros módulos.
local gadgets = require('Module:Gadgets')
local registroDeGadget = gadgets.parse()
local p = {}
p.parse = function()
local text = mw.title.new('MediaWiki:Gadgets-definition'):getContent()
local lines = mw.text.split(text, '\n', false)
local repo = {}
for _, line in ipairs(lines) do
if line:sub(1, 1) == '*' then
local name, options, pages = p.parse_line(line)
if name and #pages ~= 0 then
repo[name] = { options = options, pages = pages }
end
end
end
return repo
end
p.parse_line = function(def)
local pattern_colchetes = "^%*%s*(.+)%s*(%b[])%s*(.-)$"
local pattern_sem_colchetes = "^%*%s*(.+)%s*(.-)%s*$"
local name, opts, pageList = string.match(def, pattern_colchetes)
if not name then
name, pageList = string.match(def, pattern_sem_colchetes)
opts = nil
end
name = mw.text.trim(name)
-- Process options string into a Lua table
local options = {}
if opts then
-- Extracting the options without square brackets and trimming spaces
opts = opts:sub(2, -2):gsub("%s+", "")
for pair in opts:gmatch("[^|]+") do
local key, value = pair:match("([^=]+)=([^=|]+)")
if key and value then
options[mw.text.trim(key)] = mw.text.trim(value)
else
key = mw.text.trim(pair)
options[key] = true
end
end
end
-- Process page list into an array
local pages = {}
if pageList then
for page in pageList:gmatch("[^|]+") do
table.insert(pages, mw.text.trim(page))
end
end
return name, options, pages
end
p.get_type = function(def)
if def.options.type == 'general' or def.options.type == 'styles' then
return def.options.type
end
if def.options.dependencies ~= nil then
return 'general'
end
for _, page in ipairs(def.pages) do
if not string.match(page, '%.css$') then
return 'general'
end
end
return 'styles'
end
p.get_usage = function(name)
-- escape name for use in pattern
name = name:gsub("[%-%.%+%[%]%(%)%$%^%%%?%*]", "%%%1"):gsub("_", " ")
-- rely on [[Wikipedia:GUS2Wiki]] until [[phab:T354890]] is implemented
local _, _, count = mw.title.new('Wikipedia:GUS2Wiki'):getContent():find('\n'..name..',(%d+)')
return tonumber(count) or -1
end
return p