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

Module:Defaultsort builder

From Ross Surname Project

This module generates a properly formatted and standardized `DEFAULTSORT` value for use in MediaWiki pages.

It constructs a default sort key based on:

  • |given_name=
  • |middle_name=
  • |surname=
  • |title_ordinal=
  • |title_role=
  • |title_object=

Features

Usage

Use in a template with `#invoke` like this:

{{#invoke:Defaultsort builder|buildDefaultSort
 |given_name=John
 |middle_name=Alexander
 |surname=MacLeod
 |title_ordinal=5th
 |title_role=Chief
 |title_object=Clan MacLeod
}}

Example Output

{{Surname sort standardization switch|MacLeod}}, John Alexander, Clan MacLeod Chief {{Ordinal numbers switch|5th}}

→ Would expand to:

MacLeod, John Alexander, Clan MacLeod Chief 5

See Also

Acknowledgements

This module includes code generated by ChatGPT.


local p = {}

local function trim(s)
    return mw.text.trim(s or "")
end

function p.buildDefaultSort(frame)
    local args = frame:getParent().args
    if not next(args) then
        args = frame.args
    end

    local given_name = trim(args.given_name)
    local middle_name = trim(args.middle_name)
    local surname = trim(args.surname)
    local title_ordinal = trim(args.title_ordinal)
    local title_role = trim(args.title_role)
    local title_object = trim(args.title_object)
    local birth_year = trim(args.birth_year)
    local death_year = trim(args.death_year)

    -- If everything is blank, bail
    if given_name == "" and middle_name == "" and surname == "" 
       and title_ordinal == "" and title_role == "" and title_object == ""
       and birth_year == "" and death_year == "" then
        return "⚠️ No input provided."
    end

    -- Expand surname switch if provided
    local standardized_surname = ""
    if surname ~= "" then
        standardized_surname = frame:expandTemplate{
            title = 'Surname sort standardization switch',
            args = { surname }
        }
    end

    -- Expand ordinal switch if needed
    local standardized_ordinal = ""
    if title_ordinal ~= "" then
        standardized_ordinal = frame:expandTemplate{
            title = 'Ordinal numbers switch',
            args = { title_ordinal }
        }
    end

    -- Handle laird exclusion (case-insensitive)
    local include_role = mw.ustring.lower(title_role) ~= "laird" and title_role or ""

    -- Build name section
    local name_parts = {}
    if given_name ~= "" then table.insert(name_parts, given_name) end
    if middle_name ~= "" then table.insert(name_parts, middle_name) end
    local name_str = table.concat(name_parts, " ")

    -- Build title section
    local title_parts = {}
    if title_object ~= "" then table.insert(title_parts, title_object) end
    if include_role ~= "" then table.insert(title_parts, include_role) end
    if standardized_ordinal ~= "" then table.insert(title_parts, standardized_ordinal) end
    local title_str = table.concat(title_parts, " ")

    -- Build main parts
    local result_parts = {}
    if standardized_surname ~= "" then table.insert(result_parts, standardized_surname) end
    if name_str ~= "" then table.insert(result_parts, name_str) end
    if title_str ~= "" then table.insert(result_parts, title_str) end

    local final_sort = table.concat(result_parts, ", ")

    -- Add birth–death years if provided
    if birth_year ~= "" or death_year ~= "" then
        local years = ""
        if birth_year ~= "" and death_year ~= "" then
            years = birth_year .. "–" .. death_year
        elseif birth_year ~= "" then
            years = birth_year .. "–"
        elseif death_year ~= "" then
            years = "0–" .. death_year
        end
        final_sort = final_sort .. " " .. years
    end

    return final_sort
end

return p