Editing Module:AbilitySection

Warning: You are not logged in. Once you make an edit, a temporary account will be created for you. Learn more. Log in or create an account to continue receiving notifications after this account expires, and to access other features.
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:
-- See /doc for docs
-- see /doc for docs


local p = {}
local p = {}
local utils          = require "Module:Abilities/utils"
local lang            = require "Module:Lang"
local AbilityHints    = require "Module:AbilityHints"
local resource_lookup = mw.loadJsonData("Data:ResourceLookup.json")
local dictionary_data = mw.loadJsonData("Data:Dictionary")


-- Tab definitions — order here determines display order.
-- Tab definitions — order here determines display order.
-- label_dict_key names the [[Data:Dictionary]] entry holding the tab label; the Notes
-- tab reuses the existing 'Notes' entry rather than adding a duplicate.
local TABS = {
local TABS = {
    { key = 'notes',        label_dict_key = 'Notes'                     },
{ key = 'notes',        label = 'Notes'       },
    { key = 'tips',         label_dict_key = 'AbilityCardTabTips'         },
{ key = 'interactions', label = 'Interactions' },
    { key = 'interactions', label_dict_key = 'AbilityCardTabInteractions' },
{ key = 'tips',         label = 'Tips'         },
}
}
local SHOW_ALL_DICT_KEY = 'AbilityCardTabShowAll'
-- Tab labels are read from the dictionary directly instead of going through
-- Module:Dictionary, which appends Template:NeedsTranslationTooltip markup to
-- anything untranslated. mw.ext.tabber.render HTML-escapes a label and reuses it as a
-- DOM id, so that markup would show up as literal text in the tab and mangle the
-- anchor. Untranslated labels therefore fall back to English silently.
local function tab_label(dict_key)
    local entry = dictionary_data[dict_key]
    if entry == nil then return dict_key end
    return entry[lang.get_lang_code()] or entry.en or dict_key
end
local function get_hero_key(hero_name)
    if hero_name == nil or hero_name == '' then return nil end
    if string.sub(hero_name, 1, 5) == "hero_" then return hero_name end
    local entry = resource_lookup[hero_name:lower()]
    if entry == nil then return nil end
    return entry.key
end


local function trim(s)
local function trim(s)
    return s:match('^%s*(.-)%s*$')
return s:match('^%s*(.-)%s*$')
end
end


-- Process content in the Interactions tab to build list of icons to draw in ability hints beside ability card
local function build_tabber(wikitext_map)
local function process_interactions(html, ability_num)
local filled = {}
    local manual, idx, seen = {}, 0, {}
for _, tab in ipairs(TABS) do
    local out = html:gsub('(<div)([^>]*data%-iconbox%-icon="[^"]*"[^>]*)(>)', function(pre, attrs, post)
if wikitext_map[tab.key] ~= '' then
        local file = attrs:match('data%-iconbox%-icon="([^"]*)"')
table.insert(filled, { key = tab.key, label = tab.label, wikitext = wikitext_map[tab.key] })
        if file and file ~= '' then
end
            local base  = mw.ustring.lower((file:gsub('%.%w+$', '')))
end
            local entry = resource_lookup[base]
            if entry ~= nil and entry.type == 'item' and not seen[base] then
                seen[base] = true
                idx = idx + 1
                local box_id = 'ai-int-' .. ability_num .. '-' .. idx
                table.insert(manual, {
                    file  = file,
                    title  = attrs:match('data%-iconbox%-title="([^"]*)"') or '',
                    state  = attrs:match('data%-iconbox%-state="([^"]*)"') or 'normal',
                    anchor = box_id,
                })
                return pre .. attrs .. ' id="' .. box_id .. '"' .. post
            end
        end
        return pre .. attrs .. post
    end)
    return out, manual
end


local function build_tabber(wikitext_map, tab_overrides)
if #filled == 0 then
    tab_overrides = tab_overrides or {}
return ''
    local filled = {}
end
    for _, tab in ipairs(TABS) do
        if wikitext_map[tab.key] ~= '' then
            local tab_wt = tab_overrides[tab.key]
            if tab_wt == nil or tab_wt == '' then tab_wt = wikitext_map[tab.key] end
            table.insert(filled, {
                key    = tab.key,
                label  = tab_label(tab.label_dict_key),
                tab_wt = tab_wt,
                all_wt = wikitext_map[tab.key],
            })
        end
    end


    if #filled == 0 then
local tabs = {}
        return ''
for _, section in ipairs(filled) do
    end
table.insert(tabs, {
label  = section.label,
content = '<div class="ability-details-panel">\n' .. section.wikitext .. '\n</div>',
})
end


    local tabs = {}
-- "Show All" tab wraps each section's wikitext in a <div> to give the parser
    for _, section in ipairs(filled) do
-- a fresh block context per section, preventing html mangling.
        table.insert(tabs, {
if #filled >= 2 then
            label  = section.label,
local all_parts = {}
            content = '<div class="ability-details-panel">\n' .. section.tab_wt .. '\n</div>',
for _, section in ipairs(filled) do
        })
table.insert(all_parts,
    end
'<div class="ability-details-section-header">' .. section.label .. '</div>'
.. '<div class="ability-details-section-content">\n' .. section.wikitext .. '\n</div>'
)
end
table.insert(tabs, {
label  = 'Show All',
content = '<div class="ability-details-panel">' .. table.concat(all_parts) .. '</div>',
})
end


    if #filled >= 2 then
return mw.ext.tabber.render(tabs)
        local all_parts = {}
        for _, section in ipairs(filled) do
            table.insert(all_parts,
                '<div class="ability-details-section-header">' .. section.label .. '</div>'
                .. '<div class="ability-details-section-content">\n' .. section.all_wt .. '\n</div>'
            )
        end
        table.insert(tabs, {
            label  = tab_label(SHOW_ALL_DICT_KEY),
            content = '<div class="ability-details-panel">' .. table.concat(all_parts) .. '</div>',
        })
    end
 
    local rendered = mw.ext.tabber.render(tabs)
    if #filled == 1 then
        return '<div class="single-tabbed">' .. rendered .. '</div>'
    end
    return rendered
end
end


-- Entry point. Called via {{#invoke:AbilitySection|render}}.
-- Entry point. Called via {{#invoke:AbilitySection|render}}.
function p.render(frame)
function p.render(frame)
    local args       = frame.args
local args         = frame.args
    local hero_name   = trim(args.hero_name or '')
local hero_name   = trim(args.hero_name or '')
    local ability_num = trim(args.ability_num or '')
local ability_num = trim(args.ability_num or '')
    local notes        = trim(args.notes or '')
local notes        = trim(args.notes or '')
    local tips        = trim(args.tips or '')
local interactions = trim(args.interactions or '')
    local interactions = trim(args.interactions or '')
local tips         = trim(args.tips or '')
 
    local hero_key    = get_hero_key(hero_name)
    local heading    = ''
    local ability_key = nil
    if hero_key ~= nil then
         local ability = utils.get_ability_card_data(hero_key, ability_num)
        if ability ~= nil then
            ability_key = ability.Key
            local ability_name = lang.get_string(ability.Key)
            -- Create a visually hidden H3 heading so the Table of Contents can parse
            -- the ability name without displaying a redundant title above the card.
            heading = '<h3 class="ability-name-toc">' .. ability_name .. '</h3>'
        end
    end
 
    local card = frame:expandTemplate{
        title = 'Ability card v2',
        args  = { hero_name = hero_name, ability_num = ability_num, link_name = 'false' }
    }
 
    local interactions_ided, manual = interactions, {}
    if interactions ~= '' then
        interactions_ided, manual = process_interactions(interactions, ability_num)
    end
 
    local wikitext_map = {
        notes        = notes,
        tips         = tips,
        interactions = interactions,
    }
 
    local tabber = build_tabber(wikitext_map, { interactions = interactions_ided })
 
    local hints_col = ''
    if ability_key ~= nil or #manual > 0 then
        hints_col = AbilityHints.get_icons(ability_key, ability_num, manual)
    end


    local details_col = ''
local card = frame:expandTemplate{
    if tabber ~= '' then
title = 'Ability card v2',
        details_col = '<div class="ability-section-details">' .. tabber .. '</div>'
args  = { hero_name = hero_name, ability_num = ability_num, link_name = 'false' }
    end
}


    local card_block = '<div class="ability-section-card">' .. card .. '</div>'
local wikitext_map = {
    local left_col
notes        = notes,
    if hints_col ~= '' then
interactions = interactions,
        left_col = '<div class="ability-section-main">' .. card_block .. hints_col .. '</div>'
tips         = tips,
    else
}
         left_col = card_block
    end


    local ability_section = '<div class="ability-section">'
local tabber = build_tabber(wikitext_map)
        .. left_col
local notes_col = ''
        .. details_col
if tabber ~= '' then
        .. '</div>'
notes_col = '<div class="ability-section-notes">' .. tabber .. '</div>'
end


    return '<div class="ability-section-wrapper">'
return '<div class="ability-section">'
        .. heading
.. '<div class="ability-section-card">' .. card .. '</div>'
        .. ability_section
.. notes_col
        .. '</div>'
.. '</div>'
end
end


return p
return p
Please note that all contributions to The Deadlock Wiki are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see Deadlock: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

Pages included on this page:

This page is a member of a hidden category: