Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:C: Difference between revisions

From Teriock
Content deleted Content added
// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 1: Line 1:
local p = {}
local p = {}

local function makeBar(frame, title, content, link, flags, bottom)
if content == '' then return '' end
local barSpace = mw.html.create('span')
barSpace:wikitext(frame:preprocess(' ')):css('display', 'none')
local barTitle = mw.html.create('span')
local barTitleClass = "{{lc:" .. title .. "}}"
barTitleClass = barTitleClass:gsub(' ', '-')
barTitleClass = barTitleClass:gsub('\'', '')
barTitleClass = barTitleClass:gsub('\"', '')
local barText = "{{ucfirst:" .. title .. "}}"
if link then barText = "[[" .. link .. "|" .. barText .. "]]" end
barText = barText .. ":"
barTitle:addClass('ability-bar-title'):wikitext(frame:preprocess(barText))
:css('font-weight', 'bold'):css('margin-right', '0.5em')
local barContent = mw.html.create('span')
barContent:addClass('ability-bar-content'):wikitext(content)
local output = mw.html.create('div')
if bottom then output = mw.html.create('span') end
if not flags then flags = {} end
for k, v in pairs(flags) do
if type(v) == 'string' then
output:addClass('flag-' .. k .. '-' .. v)
end
end
output:addClass('ability-bar'):addClass('ability-bar-' .. barTitleClass)
:attr(flags):attr('test', 'out'):node(barSpace):node(barTitle):node(
barSpace):node(barContent):css('display', 'block')
-- if flags then
-- for k, v in ipairs(flags) do
-- output:attr(k, v)
-- end
-- end
return tostring(output)
end


function p.c(frame)
function p.c(frame)
local args = frame.args
local args = frame.args
local rawAbilities = args['a'] or ''
local rawAbilities = args['a'] or ''


-- === TAGS & CATEGORIES via TagBuilder ===
-- === TAGS & CATEGORIES via TagBuilder ===
local TagBuilder = require('Module:TagBuilder')
local TagBuilder = require('Module:TagBuilder')
local tRaw = args['t'] or ''
local tRaw = args['t'] or ''
local name = args['name'] or ''
local name = args['name'] or ''
local opts = {
local opts = {
name = name,
name = name,
ns = 'Creature', -- gate categories to the Creature namespace
ns = 'Creature', -- gate categories to the Creature namespace
transparent = args.o or args.transparent, -- optional passthrough
transparent = args.o or args.transparent -- optional passthrough
}
}
-- Use TagCreatures data and traits scope
-- Use TagCreatures data and traits scope
local tagOut, catOut = TagBuilder.build(tRaw, 'TagCreatures', 'traits', opts)
local tagOut, catOut =
TagBuilder.build(tRaw, 'TagCreatures', 'traits', opts)


-- === Parse abilities into a structured table ===
-- === Parse abilities into a structured table ===
local abilities = {}
local abilities = {}
for ability in mw.text.gsplit(rawAbilities, ";;", true) do
for ability in mw.text.gsplit(rawAbilities, ";;", true) do
ability = mw.text.trim(ability)
ability = mw.text.trim(ability)
if ability ~= "" then
if ability ~= "" then
local name = ability
local name = ability
local limited, improved = "", ""
local limited, improved = "", ""
local gifted, adept = false, false
local gifted, adept = false, false


-- flags
-- flags
if mw.ustring.find(ability, "%-g%-") then gifted = true end
if mw.ustring.find(ability, "%-g%-") then gifted = true end
if mw.ustring.find(ability, "%-a%-") then adept = true end
if mw.ustring.find(ability, "%-a%-") then adept = true end


-- notes (order-insensitive, tolerate any following marker)
-- notes (order-insensitive, tolerate any following marker)
local limitedMatch = mw.ustring.match(ability, "%-l%-(.-)(%-[liag]%-|$)")
local limitedMatch = mw.ustring.match(ability,
"%-l%-(.-)(%-[liag]%-|$)") or
or mw.ustring.match(ability, "%-l%-(.+)$")
mw.ustring.match(ability, "%-l%-(.+)$")
if limitedMatch then limited = mw.text.trim(limitedMatch) end
if limitedMatch then limited = mw.text.trim(limitedMatch) end


local improvedMatch = mw.ustring.match(ability, "%-i%-(.-)(%-[liag]%-|$)")
local improvedMatch = mw.ustring.match(ability,
"%-i%-(.-)(%-[liag]%-|$)") or
or mw.ustring.match(ability, "%-i%-(.+)$")
mw.ustring.match(ability, "%-i%-(.+)$")
if improvedMatch then improved = mw.text.trim(improvedMatch) end
if improvedMatch then
improved = mw.text.trim(improvedMatch)
end


-- clean name (strip markers + their payloads/flags)
-- clean name (strip markers + their payloads/flags)
name = mw.ustring.gsub(name, "%-l%-.+", "")
name = mw.ustring.gsub(name, "%-l%-.+", "")
name = mw.ustring.gsub(name, "%-i%-.+", "")
name = mw.ustring.gsub(name, "%-i%-.+", "")
name = mw.ustring.gsub(name, "%-g%-", "")
name = mw.ustring.gsub(name, "%-g%-", "")
name = mw.ustring.gsub(name, "%-a%-", "")
name = mw.ustring.gsub(name, "%-a%-", "")
name = mw.text.trim(name)
name = mw.text.trim(name)


table.insert(abilities, {
table.insert(abilities, {
name = name,
name = name,
limited = limited,
limited = limited,
improved = improved,
improved = improved,
gifted = gifted,
gifted = gifted,
adept = adept
adept = adept
})
})
end
end
end
end


-- Sort alphabetically by name (stable)
-- Sort alphabetically by name (stable)
table.sort(abilities, function(a, b) return a.name < b.name end)
table.sort(abilities, function(a, b) return a.name < b.name end)


-- Build the out string (NO visible separators between AE templates)
-- Build the out string (NO visible separators between AE templates)
local transclusions = {}
local transclusions = {}
for _, a in ipairs(abilities) do
for _, a in ipairs(abilities) do
local t = {"{{AE|", a.name}
local t = {"{{AE|", a.name}
if a.limited ~= "" then table.insert(t, "|limited=" .. a.limited) end
if a.limited ~= "" then table.insert(t, "|limited=" .. a.limited) end
if a.improved ~= "" then table.insert(t, "|improved=" .. a.improved) end
if a.improved ~= "" then
if a.gifted then table.insert(t, "|gifted=1") end
table.insert(t, "|improved=" .. a.improved)
end
if a.adept then table.insert(t, "|adept=1") end
table.insert(t, "}}")
if a.gifted then table.insert(t, "|gifted=1") end
if a.adept then table.insert(t, "|adept=1") end
table.insert(transclusions, table.concat(t))
table.insert(t, "}}")
end
table.insert(transclusions, table.concat(t))
end


-- Use "" (or "\n") so no semicolons are emitted
-- Use "" (or "\n") so no semicolons are emitted
local inner = table.concat(transclusions, "") -- <- no separators
local inner = table.concat(transclusions, "") -- <- no separators


-- Assemble output: tags (visible), abilities, then categories (gated by name/ns)
-- Assemble output: tags (visible), abilities, then categories (gated by name/ns)
local out = (tagOut or '')
local out = makeBar(frame, "Traits", (tagOut or ''), nul, nul, false)
out = out .. '<div class="expandable-table"><div>' .. frame:preprocess(inner) .. '</div></div>'
out = out .. '<div class="expandable-table"><div>' ..
frame:preprocess(inner) .. '</div></div>'
out = out .. (catOut or '')
out = out .. (catOut or '')


-- Preprocess the whole thing so {{Tag}} expands
-- Preprocess the whole thing so {{Tag}} expands
return frame:preprocess(out)
return frame:preprocess(out)
end
end



Revision as of 19:31, 12 August 2025

Documentation for this module may be created at Module:C/doc

local p = {}

local function makeBar(frame, title, content, link, flags, bottom)
    if content == '' then return '' end
    local barSpace = mw.html.create('span')
    barSpace:wikitext(frame:preprocess('&nbsp;')):css('display', 'none')
    local barTitle = mw.html.create('span')
    local barTitleClass = "{{lc:" .. title .. "}}"
    barTitleClass = barTitleClass:gsub(' ', '-')
    barTitleClass = barTitleClass:gsub('\'', '')
    barTitleClass = barTitleClass:gsub('\"', '')
    local barText = "{{ucfirst:" .. title .. "}}"
    if link then barText = "[[" .. link .. "|" .. barText .. "]]" end
    barText = barText .. ":"
    barTitle:addClass('ability-bar-title'):wikitext(frame:preprocess(barText))
        :css('font-weight', 'bold'):css('margin-right', '0.5em')
    local barContent = mw.html.create('span')
    barContent:addClass('ability-bar-content'):wikitext(content)
    local output = mw.html.create('div')
    if bottom then output = mw.html.create('span') end
    if not flags then flags = {} end
    for k, v in pairs(flags) do
        if type(v) == 'string' then
            output:addClass('flag-' .. k .. '-' .. v)
        end
    end
    output:addClass('ability-bar'):addClass('ability-bar-' .. barTitleClass)
        :attr(flags):attr('test', 'out'):node(barSpace):node(barTitle):node(
            barSpace):node(barContent):css('display', 'block')
    -- if flags then
    -- for k, v in ipairs(flags) do
    --     output:attr(k, v)
    -- end
    -- end
    return tostring(output)
end

function p.c(frame)
    local args = frame.args
    local rawAbilities = args['a'] or ''

    -- === TAGS & CATEGORIES via TagBuilder ===
    local TagBuilder = require('Module:TagBuilder')
    local tRaw = args['t'] or ''
    local name = args['name'] or ''
    local opts = {
        name = name,
        ns = 'Creature', -- gate categories to the Creature namespace
        transparent = args.o or args.transparent -- optional passthrough
    }
    -- Use TagCreatures data and traits scope
    local tagOut, catOut =
        TagBuilder.build(tRaw, 'TagCreatures', 'traits', opts)

    -- === Parse abilities into a structured table ===
    local abilities = {}
    for ability in mw.text.gsplit(rawAbilities, ";;", true) do
        ability = mw.text.trim(ability)
        if ability ~= "" then
            local name = ability
            local limited, improved = "", ""
            local gifted, adept = false, false

            -- flags
            if mw.ustring.find(ability, "%-g%-") then gifted = true end
            if mw.ustring.find(ability, "%-a%-") then adept = true end

            -- notes (order-insensitive, tolerate any following marker)
            local limitedMatch = mw.ustring.match(ability,
                                                  "%-l%-(.-)(%-[liag]%-|$)") or
                                     mw.ustring.match(ability, "%-l%-(.+)$")
            if limitedMatch then limited = mw.text.trim(limitedMatch) end

            local improvedMatch = mw.ustring.match(ability,
                                                   "%-i%-(.-)(%-[liag]%-|$)") or
                                      mw.ustring.match(ability, "%-i%-(.+)$")
            if improvedMatch then
                improved = mw.text.trim(improvedMatch)
            end

            -- clean name (strip markers + their payloads/flags)
            name = mw.ustring.gsub(name, "%-l%-.+", "")
            name = mw.ustring.gsub(name, "%-i%-.+", "")
            name = mw.ustring.gsub(name, "%-g%-", "")
            name = mw.ustring.gsub(name, "%-a%-", "")
            name = mw.text.trim(name)

            table.insert(abilities, {
                name = name,
                limited = limited,
                improved = improved,
                gifted = gifted,
                adept = adept
            })
        end
    end

    -- Sort alphabetically by name (stable)
    table.sort(abilities, function(a, b) return a.name < b.name end)

    -- Build the out string (NO visible separators between AE templates)
    local transclusions = {}
    for _, a in ipairs(abilities) do
        local t = {"{{AE|", a.name}
        if a.limited ~= "" then table.insert(t, "|limited=" .. a.limited) end
        if a.improved ~= "" then
            table.insert(t, "|improved=" .. a.improved)
        end
        if a.gifted then table.insert(t, "|gifted=1") end
        if a.adept then table.insert(t, "|adept=1") end
        table.insert(t, "}}")
        table.insert(transclusions, table.concat(t))
    end

    -- Use "" (or "\n") so no semicolons are emitted
    local inner = table.concat(transclusions, "") -- <- no separators

    -- Assemble output: tags (visible), abilities, then categories (gated by name/ns)
    local out = makeBar(frame, "Traits", (tagOut or ''), nul, nul, false)
    out = out .. '<div class="expandable-table"><div>' ..
              frame:preprocess(inner) .. '</div></div>'
    out = out .. (catOut or '')

    -- Preprocess the whole thing so {{Tag}} expands
    return frame:preprocess(out)
end

return p