モジュール:gender and number

このモジュールについての説明文ページを モジュール:gender and number/doc に作成できます

--[=[
    This module creates standardised displays for gender and number.
    It converts a gender specification into Wiki/HTML format.
    
    A gender specification is a list of one of the elements listed below,
    separated by hyphens. Examples are: "c", "n", "f-p", "m-an-p"
]=]--

local export = {}

local codes = {}

-- A list of all possible "parts" that a specification can be made out of.

codes["?"] = '?'

-- Genders
codes["m"] = '男性'
codes["f"] = '女性'
codes["n"] = '中性'
codes["c"] = '通性'

-- Additional qualifiers
codes["an"] = '有生'
codes["in"] = '非有生'
codes["anml"] = '有生'
codes["pr"] = '人称'
codes["np"] = '非人称'
codes["vr"] = '男性人間'
codes["nv"] = '非男性人間'

-- Numbers
codes["s"] = '単数'
codes["d"] = '双数'
codes["p"] = '複数'

-- Verb qualifiers
codes["impf"] = '不完了体'
codes["pf"] = '完了体'

-- Version of format_list that can be invoked from a template.
function export.show_list(frame)
    local args = frame.args
    local list = {}
    local i = 1
    
    while args[i] and args[i] ~= "" do
        table.insert(list, args[i])
        i = i + 1
    end
    
    return export.format_list(list)
end

-- Format one or more gender specifications, in the form of a table of specifications.
function export.format_list(list)
    local is_nounclass = nil
    local templatestyles = mw.getCurrentFrame():extensionTag( 'templatestyles', '', { src = "モジュール:gender and number/styles.css" } );
    
    -- Iterate over each specification and format it
    for key, spec in ipairs(list) do
        local nc
        list[key], nc = export.format_specification(spec)
        
        -- Ensure that the specifications are either all noun classes, or none are.
        if is_nounclass == nil then
            is_nounclass = nc
        elseif is_nounclass ~= nc then
            error("名詞クラスと性を同時に指定することはできません。どちらか一方のみを指定してください。")
        end
    end
    
    if is_nounclass then
        -- Add the processed codes together with slashes
        return templatestyles .. "<span class=\"gender\">クラス " .. table.concat(list, "/") .. "</span>"
    else
        -- Add the processed codes together with commas
        return templatestyles .. "<span class=\"gender\">" .. table.concat(list, ", ") .. "</span>"
    end
end

-- Format the sub-parts of a single gender specification.
function export.format_specification(spec)
    local separator = " "
    
    -- If the specification starts with cX, then it is a noun class specification.
    if spec:find("^[1-9]") or spec:find("^c[^-]") then
        code = spec:gsub("^c", "")
        
        if code == "?" then
            return "<abbr class=\"noun-class\" title=\"名詞クラス不明\">?</abbr>", true
        else
            return "<abbr class=\"noun-class\" title=\"名詞クラス" .. code .. "\">" .. code .. "</abbr>", true
        end
    else
        -- Split the parts and iterate over each part, converting it into its display form
        local parts = mw.text.split(spec, "-")
        
        for key, code in ipairs(parts) do
            -- Is this code valid?
            if codes[code] then
                parts[key] = codes[code]
            else
                error("性コードが無効です: \"" .. code .. "\"")
            end
        end
        
        -- Add the processed codes together with non-breaking spaces
        return table.concat(parts, "&nbsp;"), false
    end
end

return export