Difference between revisions of "Module:Infobox"
m |
m |
||
Line 5: | Line 5: | ||
local animateImages = require("Module:Animated")._images | local animateImages = require("Module:Animated")._images | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
+ | local dataPrefix = "Module:Infobox/" | ||
function p.block(frame) | function p.block(frame) | ||
Line 36: | Line 37: | ||
end | end | ||
− | local function | + | -- Get data (IDs and names) for infobox headers. |
− | local data = mw.loadData( | + | -- |
+ | -- @param type Infobox type (e.g. block) | ||
+ | -- @param langCode Language code, defaults to current language | ||
+ | local function getHeadersData(type, langCode) | ||
+ | langCode = langCode or getLanguageCode() | ||
+ | local data = mw.loadData(dataPrefix .. langCode) | ||
+ | return data and data[type] or nil | ||
+ | end | ||
+ | |||
+ | -- Create an infobox. | ||
+ | -- | ||
+ | -- @param args Infobox arguments | ||
+ | -- @param type Infobox type (e.g. block) | ||
+ | local function infobox(args, type) | ||
+ | local headersData = getHeadersData(type) or getHeadersData(type, "en") | ||
local name = args.name | local name = args.name | ||
local width = args.width or "200px" | local width = args.width or "200px" | ||
Line 91: | Line 106: | ||
:wikitext(description) | :wikitext(description) | ||
− | for _, | + | for _, headerData in ipairs(headersData) do |
infobox | infobox | ||
:addRow() | :addRow() | ||
:addData() | :addData() | ||
− | :wikitext("'''" .. | + | :wikitext("'''" .. headerData.name .. "'''") |
:done() | :done() | ||
:addData() | :addData() | ||
− | :wikitext(args[ | + | :wikitext(args[headerData.id]) |
end | end | ||
Line 106: | Line 121: | ||
function p._block(args) | function p._block(args) | ||
args.imagesize = args.imagesize or "150px" | args.imagesize = args.imagesize or "150px" | ||
− | + | return tostring(infobox(args, "block")) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
function p._item(args) | function p._item(args) | ||
args.imagesize = args.imagesize or "160px" | args.imagesize = args.imagesize or "160px" | ||
− | + | return tostring(infobox(args, "item")) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
function p._foodItem(args) | function p._foodItem(args) | ||
args.imagesize = args.imagesize or "160px" | args.imagesize = args.imagesize or "160px" | ||
− | + | return tostring(infobox(args, "foodItem")) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
function p._mob(args) | function p._mob(args) | ||
args.imagesize = args.imagesize or "150px" | args.imagesize = args.imagesize or "150px" | ||
− | + | return tostring(infobox(args, "mob")) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
function p._game(args) | function p._game(args) | ||
args.imagesize = args.imagesize or "150px" | args.imagesize = args.imagesize or "150px" | ||
− | + | return tostring(infobox(args, "game")) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
function p._mod(args) | function p._mod(args) | ||
args.imagesize = args.imagesize or "150px" | args.imagesize = args.imagesize or "150px" | ||
− | + | return tostring(infobox(args, "mod")) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
return p | return p |
Revision as of 18:07, 13 April 2017
local p = {}
local TableBuilder = require("Module:TableBuilder") local getLanguageCode = require("Module:Languages").getLanguageCode local animateImages = require("Module:Animated")._images local getArgs = require('Module:Arguments').getArgs local dataPrefix = "Module:Infobox/"
function p.block(frame)
local args = getArgs(frame, {inherited = true}) return p._block(args)
end
function p.item(frame)
local args = getArgs(frame, {inherited = true}) return p._item(args)
end
function p.foodItem(frame)
local args = getArgs(frame, {inherited = true}) return p._foodItem(args)
end
function p.mob(frame)
local args = getArgs(frame, {inherited = true}) return p._mob(args)
end
function p.game(frame)
local args = getArgs(frame, {inherited = true}) return p._game(args)
end
function p.mod(frame)
local args = getArgs(frame, {inherited = true}) return p._mod(args)
end
-- Get data (IDs and names) for infobox headers. -- -- @param type Infobox type (e.g. block) -- @param langCode Language code, defaults to current language local function getHeadersData(type, langCode)
langCode = langCode or getLanguageCode() local data = mw.loadData(dataPrefix .. langCode) return data and data[type] or nil
end
-- Create an infobox. -- -- @param args Infobox arguments -- @param type Infobox type (e.g. block) local function infobox(args, type)
local headersData = getHeadersData(type) or getHeadersData(type, "en") local name = args.name local width = args.width or "200px" local description = args.description local image = args.image or "Blank.png" local imagesize = args.imagesize local imageText if string.match(image, ",") then imageText = animateImages({image, imagesize}) else if imagesize then imageText = "" .. imagesize .. "" else imageText = "File:" .. image .. "" end end
local infobox = TableBuilder.create()
infobox :addClass("wikitable") :css("float", "right") :css("text-align", "left") :css("margin", "0 0 0.5em 1em") :css("padding", "5px") :css("font-size", "90%") :css("position", "relative") :css("clear", "right") :css("overflow", "auto") :css("z-index", "1") :attr("width", width) :addRow() :addHeader() :attr("colspan", 2) :css("font-size", "110%") :css("text-align", "center") :wikitext(name) :done() :done() :addRow() :addHeader() :attr("colspan", 2) :tag("div") :addClass("center") :wikitext(imageText) :done() :done() :done() :addRow() :addHeader() :attr("colspan", 2) :attr("align", "center") :wikitext(description)
for _, headerData in ipairs(headersData) do infobox :addRow() :addData() :wikitext("" .. headerData.name .. "") :done() :addData() :wikitext(args[headerData.id]) end
return infobox
end
function p._block(args)
args.imagesize = args.imagesize or "150px" return tostring(infobox(args, "block"))
end
function p._item(args)
args.imagesize = args.imagesize or "160px" return tostring(infobox(args, "item"))
end
function p._foodItem(args)
args.imagesize = args.imagesize or "160px" return tostring(infobox(args, "foodItem"))
end
function p._mob(args)
args.imagesize = args.imagesize or "150px" return tostring(infobox(args, "mob"))
end
function p._game(args)
args.imagesize = args.imagesize or "150px" return tostring(infobox(args, "game"))
end
function p._mod(args)
args.imagesize = args.imagesize or "150px" return tostring(infobox(args, "mod"))
end
return p