Jump to content

Editing Module:Card

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
local p = {}
local p = {}


Line 6: Line 5:
local TemplateData = require('Module:TemplateData')
local TemplateData = require('Module:TemplateData')
local array = require('Module:Array')
local array = require('Module:Array')
local CentralData = require('Module:CentralData')


local allData = CentralData.dataSheets
local allData = {
-- {'Neighbor', mw.loadData('Module:CentralData/Neighbors')},
{'Creature', mw.loadData('Module:CentralData/Creatures')},
-- {'Furniture', mw.loadData('Module:CentralData/Furniture')},
{'Item', mw.loadData('Module:CentralData/Items')},
}


-- Minimal TemplateData args.
-- This does not need to be huge unless you want full documentation for every parameter.
local CARD_ARGS = {
local CARD_ARGS = {
{
{
Line 28: Line 33:
name = 'quality',
name = 'quality',
alias = 'rarity',
alias = 'rarity',
default = 'Common',
default = 'Gray',
},
},
{
{
name = 'type',
name = 'type',
},
default = 'Item',
{
name = 'split',
},
},
{
{
Line 45: Line 48:
{
{
name = 'image_size',
name = 'image_size',
alias = {'imageSize', 'size'},
alias = 'imageSize',
},
},
{
{
name = 'image_ext',
name = 'image_ext',
alias = {'imageExt', 'ext', 'extension'},
alias = {'imageExt', 'extension'},
default = 'png',
default = 'png',
},
},
Line 60: Line 63:
},
},
{
{
name = 'no_link',
name = 'nolink',
type = '1',
type = '1',
},
},
Line 86: Line 89:
alias = 'UP',
alias = 'UP',
type = '1',
type = '1',
},
{
name = 'galaxy',
},
},
{
{
Line 94: Line 100:
function p.main(frame)
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
local args = require('Module:Arguments').getArgs(frame, {
wrapper = {'Template:Card'},
wrapper = { 'Template:Card' },
parentOnly = false
parentOnly = false
})
})


return p._main(args)
return p._main(args)
end
local function getDataByName(dataType, dataTable, name)
local data = dataTable[name]
if data then
return data
end
local normalizedType = tostring(dataType or ''):lower()
if normalizedType == 'neighbor' or normalizedType == 'neighbors' then
return nil
end
for quality, names in pairs(dataTable) do
if type(names) == 'table' then
for _, itemName in ipairs(names) do
if itemName == name then
return {
quality = quality
}
end
end
end
end
return nil
end
end


Line 137: Line 116:


if lib.isEmpty(requestedType) or dataType == requestedType then
if lib.isEmpty(requestedType) or dataType == requestedType then
local data = getDataByName(dataType, dataTable, name)
local data = dataTable[name]


if data then
if data then
Line 172: Line 151:


if lib.isEmpty(quality) then
if lib.isEmpty(quality) then
quality = 'Common'
quality = 'Gray'
end
end


return normalizeClassName(quality)
return normalizeClassName(quality)
end
local function stripLinkPrefix(value)
value = tostring(value or '')
-- Converts "Luca: Myriad Meadows" to "Myriad Meadows".
return mw.text.trim(value:gsub('^.-:%s*', '', 1))
end
end


Line 191: Line 163:
node:css('width', width)
node:css('width', width)
end
end
end
-- Generates the second icon for a split card.
local function makeSplitIcon(name, mini)
name = tostring(name or '')
local size = mini and 74 or 56
return '[[File:' ..
lib.sanitizeFileName(name) ..
' Icon.png|' ..
size .. 'x' .. size ..
'px|link=' ..
name ..
']]'
end
end


Line 213: Line 170:
-- Basic identity
-- Basic identity
out.name = out.name or 'Unknown'
out.name = out.name or 'Unknown'
out.split = out.split or ''
-- Use only a manually supplied type for lookup.
local requestedType = out.nonDefaults
and out.nonDefaults.type
or nil


out.type, out.data = p.getTypeAndData(
-- Data lookup
out.name,
out.type, out.data = p.getTypeAndData(out.name, out.type)
requestedType
)


-- Base name for assets.
-- Boolean-ish params
-- Removes a trailing disambiguation such as "(Currency)".
out.image_name = out.name--:gsub('%s*%([^()]*%)$', '')
 
-- Boolean parameters
out.mini = normalizeBool(out.mini)
out.mini = normalizeBool(out.mini)
out.first = normalizeBool(out.first)
out.first = normalizeBool(out.first)
out.up = normalizeBool(out.up)
out.up = normalizeBool(out.up)
out.no_link = normalizeBool(out.no_link)
out.nolink = normalizeBool(out.nolink)
out.show_label = normalizeBool(out.show_label)
out.show_label = normalizeBool(out.show_label)
out.show_caption = normalizeBool(out.show_caption)
out.show_caption = normalizeBool(out.show_caption)
Line 249: Line 194:
end
end


-- Data-driven defaults
-- Data-driven name
if out.data then
out.data_name = out.data and out.data.name or nil
if out.data.name then
out.defaults.label = out.data.name
out.defaults.caption = out.data.name
end


if out.data.title then
-- Quality / rarity
out.defaults.caption = out.data.title
if lib.isEmpty(out.quality) and out.data and out.data.quality then
end
out.quality = out.data.quality
end


if out.data.quality then
out.quality = normalizeQuality(out.quality)
out.defaults.quality = out.data.quality


if not (out.nonDefaults and out.nonDefaults.quality) then
-- Galaxy
out.quality = out.data.quality
if lib.isEmpty(out.galaxy) and out.data and out.data.galaxy then
end
out.galaxy = out.data.galaxy
end
end
end


-- Quality / rarity
-- Label/text handling
out.quality = normalizeQuality(out.quality)
 
-- Label handling
if out.show_label then
if out.show_label then
out.label = out.data
out.label = out.data_name or out.name
and (out.data.name or out.data.title)
or out.name
end
end


Line 295: Line 230:
end
end


-- Account for transparent borders on neighbor icons
if tonumber(out.image_size) then
if tonumber(out.image_size) then
out.image_size = tonumber(out.image_size) + 2
out.image_size = tonumber(out.image_size) + 2
Line 300: Line 236:
end
end


if lcType == 'redirect' then
-- Caption handling
out.defaults.link = stripLinkPrefix(out.name)
 
if not (out.nonDefaults and out.nonDefaults.link) then
out.link = stripLinkPrefix(out.name)
end
end
 
-- Caption defaults
if out.show_caption and lib.isEmpty(out.caption) then
if out.show_caption and lib.isEmpty(out.caption) then
if out.data then
out.caption = out.data_name or out.name
out.caption = out.data.title
or out.data.name
or out.name
else
out.caption = out.name
end
end
end


-- Link handling
-- Link handling
if out.link == nil then
if out.link == nil then
out.link = lib.ternary(
out.link = lib.ternary(out.name == 'Unknown', '', out.name)
out.name == 'Unknown',
'',
out.name
)
end
end


Line 331: Line 249:
out.final_link = out.link .. out.link_suffix
out.final_link = out.link .. out.link_suffix


if out.no_link then
if out.nolink then
out.final_link = ''
out.final_link = ''
end
end


-- Main image filename
-- File name
if ne(out.image) then
if ne(out.image) then
out.final_file = out.image
out.final_file = out.image
else
else
out.final_file =
out.final_file = lib.sanitizeFileName(out.name) .. out.suffix .. '.' .. out.image_ext
lib.sanitizeFileName(out.image_name) ..
out.suffix ..
'.' ..
out.image_ext
end
end


-- Main image wikitext
-- Main image wikitext
local linkPart = out.no_link
local linkPart = out.nolink and '|link=' or ('|link=' .. out.final_link)
and '|link='
or ('|link=' .. out.final_link)


out.image_wikitext = table.concat({
out.image_wikitext = table.concat({
Line 357: Line 269:
out.image_size,
out.image_size,
'px|alt=',
'px|alt=',
out.data and (out.data.name or out.data.title)
out.name,
or out.name,
linkPart,
linkPart,
']]'
']]'
Line 368: Line 279:
function p._main(args)
function p._main(args)
local a = p._processArgs(args)
local a = p._processArgs(args)
local isSplit = ne(a.split)


-- Outer container
local node_container = mw.html.create('div')
local node_container = mw.html.create('div')
:addClass('card-container')
:addClass('card-container')
if isSplit then
node_container:addClass('card-split-card')
end


if a.mini then
if a.mini then
Line 382: Line 287:
end
end


-- Wrapper and body
-- Old-style wrapper/body structure.
-- This is usually important if your existing CSS was written for the old Card module.
local node_card = node_container:tag('span')
local node_card = node_container:tag('span')
:addClass('card-wrapper')
:addClass('card-wrapper')
:tag('span')
:tag('span')
:addClass('card-body')
:addClass('card-body')
-- Galaxy class on body
if not a.mini and ne(a.galaxy) then
node_card:addClass(
'card-galaxy-' .. normalizeClassName(a.galaxy)
)
end


-- Main image container
-- Main image container
local imageContainer = node_card:tag('span')
node_card:tag('span')
:addClass('card-image-container')
:addClass('card-image-container')
:addClass('card-quality-' .. a.quality)
:addClass('card-quality-' .. a.quality)
:wikitext(a.image_wikitext)


-- Split or regular image
-- Galaxy icon
if isSplit then
if not a.mini and ne(a.galaxy) then
imageContainer:addClass('card-split-image')
node_card:addClass('card-galaxy-' .. normalizeClassName(a.galaxy))


-- First item: top-left half
node_card:tag('span')
imageContainer:tag('span')
:addClass('card-icon-galaxy')
:addClass('card-split-half')
:wikitext(
:addClass('card-split-half-a')
'[[File:Icon ' ..
:wikitext(a.image_wikitext)
a.galaxy ..
 
'.png|alt=' ..
-- Second item: bottom-right half
a.galaxy ..
imageContainer:tag('span')
'|link=' ..
:addClass('card-split-half')
a.galaxy ..
:addClass('card-split-half-b')
'|20px]]'
:wikitext(makeSplitIcon(a.split, a.mini))
)
else
imageContainer:wikitext(a.image_wikitext)
end
end


Line 431: Line 329:
:addClass('card-modifier')
:addClass('card-modifier')
:addClass('card-modifier-first')
:addClass('card-modifier-first')
:wikitext(
:wikitext('[[File:Icon Item First.png|x25px|link=|alt=First]]')
'[[File:Icon Item First.png|x25px|link=|alt=First]]'
)
end
end


Line 441: Line 337:
:addClass('card-modifier')
:addClass('card-modifier')
:addClass('card-modifier-up')
:addClass('card-modifier-up')
:wikitext(
:wikitext('[[File:Icon Item UP.png|x25px|link=|alt=UP]]')
'[[File:Icon Item UP.png|x25px|link=|alt=UP]]'
)
end
end


-- Label below the image
-- Label/text below card image
if ne(a.label) then
if ne(a.label) then
node_container:tag('span')
node_container:tag('span')
Line 453: Line 347:
end
end


-- Caption
-- Caption below card
-- Split captions are opt-in and use spans instead of list items.
if a.show_caption and ne(a.caption) then
if a.show_caption and isSplit then
local node_caption = node_container:tag('span')
local node_caption = node_container:tag('span')
:addClass('card-caption')
:addClass('card-caption')
:addClass('card-split-caption')


local list = node_caption:tag('span')
if a.nolink or lib.isEmpty(a.final_link) then
:addClass('card-split-caption-list')
 
list:tag('span')
:addClass('card-split-caption-item')
:wikitext('[[' .. a.name .. ']]')
 
list:tag('span')
:addClass('card-split-caption-item')
:wikitext('[[' .. a.split .. ']]')
 
setCaptionWidth(node_caption, a.caption_width)
 
elseif a.show_caption and ne(a.caption) then
local node_caption = node_container:tag('span')
:addClass('card-caption')
 
if a.no_link or lib.isEmpty(a.final_link) then
node_caption:wikitext(a.caption)
node_caption:wikitext(a.caption)
else
else
node_caption:wikitext(
node_caption:wikitext('[[', a.final_link, '|', a.caption, ']]')
'[[' .. a.final_link .. '|' .. a.caption .. ']]'
)
end
end


Line 493: Line 366:
function p.templateData(frame)
function p.templateData(frame)
return TemplateData.templateData(CARD_ARGS, {
return TemplateData.templateData(CARD_ARGS, {
description = 'Creates an item-style card with image, quality, label, split images, modifiers, and optional data-driven icons.',
description = 'Creates an item-style card with image, quality, label, caption, modifiers, and optional data-driven icons.',
format = 'inline'
format = 'inline'
}, frame)
}, frame)
Please note that all contributions to The Petit Planet Wiki are considered to be released under the Creative Commons Attribution-ShareAlike (see Petit Planet Wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)
Preview page with this template
Below are commonly used wikitext from MediaWiki:Edittools. Click on any to insert them in the edit box.
File Pages==Summary====Licensing=={{Fairuse}}{{File|}}
Dynamic Page List¦²«»²{¦}²«»«/»«!----»
Array in DPL¹^2{}^2^2{¹}^2
Parser Functions{{#if:||}}
{{#ifeq:|||}}
{{#expr:}}
{{#switch:||#default=}}
{{#replace:||}}
{{#titleparts:}}
{{#tag:|}}
Markup{{}}|[][[]][[Category:]]#REDIRECT [[]] [[Category:Redirect Pages]]<code></code><includeonly></includeonly><noinclude></noinclude><nowiki></nowiki><!----><br>§
Notices{{Stub|}}{{Stub Dialogue}}{{Upcoming}}{{Under Construction}}{{Placeholder|}}
Magic Words{{!}}{{formatnum:}}{{lc:}}{{uc:}}{{PAGENAME}}{{FULLPAGENAME}}{{ROOTPAGENAME}}{{BASEPAGENAME}}{{SUBPAGENAME}}{{DISPLAYTITLE:|noreplace}}__TOC____NOTOC____EXPECTUNUSEDCATEGORY____HIDDENCAT__
HTML Entities&mdash;&nbsp;&comma;&shy;