Module:Emotes

From Dune: Awakening Community Wiki
Jump to navigation Jump to search

Module:Emotes

Renders a sortable wikitable of emotes defined in Module:Emotes/data with translatable display strings from MessageBundle:Emotes and other relevant MessageBundles.

This module provides the following function(s):

  • list - renders all emotes, or a subset filtered by source, as a wikitable.

Usage

List all emotes

By default, all emotes are shown, sorted by display name.

{{#invoke:Emotes|list}}

Filter by source

A source name can be provided to show only emotes matching that source. The Source column is omitted from the output, since it would be redundant once filtered.

{{#invoke:Emotes|list|Lost Harvest}}

Emotes that are available by default (Emotes:default in the dataset) are filtered the same way:

{{#invoke:Emotes|list|Default}}

The filter value is slugified before matching, so it does not need to exactly match the casing/formatting used in Module:Emotes/data.

Parameters

list

1 (optional)
string
Source name to filter by. Slugified before matching against the source token's slug half in Module:Emotes/data.
Returns an error message if no emotes match.

Notes

list

  • Display strings (name and description) are resolved via MessageBundle:Emotes using the page's current language, keyed by each emote's id (as <ID>-name and <ID>-description).
  • Column headers are also resolved via MessageBundle:Emotes (as label-icon, label-name, label-description, label-source).
  • Source values are rendered as wikilinks to their English-titled page, except Emotes:default, which is shown as plain text since it has no corresponding page.

local p = {}

local common = require("Module:Common")
local data = require("Module:Emotes/data")

-- ---------- Data ----------

local SORTED_EMOTES = {}
for _, emote in ipairs(data) do
    emote.sortName = common.translate("Emotes", emote.id .. "-name"):lower()
    SORTED_EMOTES[#SORTED_EMOTES + 1] = emote
end

table.sort(SORTED_EMOTES, function(a, b)
    return a.sortName < b.sortName
end)

-- ---------- Rendering ----------

local function renderSourceCell(source)
    local bundleName, slug = common.parseSourceToken(source)
    if not bundleName then return common.error("Malformed emote source: %s", source) end

    local key = (bundleName == "Emotes") and ("source-" .. slug) or (slug .. "-name")
    local displaySource = common.translate(bundleName, key)

    if bundleName == "Emotes" and slug == "default" then return displaySource end
    return common.translate(bundleName, key, nil, true)
end

local function renderTable(list, showSource)
    local table_ = common.newSortableTable(#list, "Emotes", "label-title")
    local headerRow = table_:tag("tr")

    headerRow:tag("th"):addClass("unsortable"):wikitext(common.translate("Common", "label-icon"))
    headerRow:tag("th"):wikitext(common.translate("Common", "label-name"))
    headerRow:tag("th"):addClass("unsortable"):wikitext(common.translate("Common", "label-description"))

    if showSource then
        headerRow:tag("th"):wikitext(common.translate("Common", "label-source"))
    end

    for _, emote in ipairs(list) do
        local displayName = common.translate("Emotes", emote.id .. "-name")
        local displayDescription = common.translate("Emotes", emote.id .. "-description")

        local row = table_:tag("tr")

        row:tag("td"):css("min-width", "128px"):wikitext(string.format("[[File:%s|128px]]", emote.image))
        row:tag("td"):css("white-space", "nowrap"):css("font-weight", "bold"):wikitext(displayName)
        row:tag("td"):css("text-align", "left"):wikitext(displayDescription)

        if showSource then
            row:tag("td"):css("white-space", "nowrap"):wikitext(renderSourceCell(emote.source))
        end
    end

    return table_
end

-- ---------- Interface ----------

function p.list(frame)
    local rawSource = common.trim(frame.args[1])
    local sourceFilter = rawSource ~= "" and common.slugify(rawSource) or ""

    local filtered = SORTED_EMOTES

    if sourceFilter ~= "" then
        filtered = {}
        for _, emote in ipairs(SORTED_EMOTES) do
            local _, slug = common.parseSourceToken(emote.source)
            if slug == sourceFilter then filtered[#filtered + 1] = emote end
        end

        if #filtered == 0 then return common.error("No emotes match source: %s", rawSource) end
    end

    local showSource = sourceFilter == ""
    return tostring(renderTable(filtered, showSource))
end

return p