Module:Dict D'ni: Difference between revisions

From Guild of Archivists
No edit summary
mNo edit summary
 
(40 intermediate revisions by 3 users not shown)
Line 9: Line 9:
local root
local root


local function getArgNums(prefix)
    -- Returns a table containing the numbers of the arguments that exist
    -- for the specified prefix. For example, if the prefix was 'data', and
    -- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
    local nums = {}
    for k, v in pairs(args) do
        local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
        if num then table.insert(nums, tonumber(num)) end
    end
    table.sort(nums)
    return nums
end


local function strCategories()
local function dnifont2ots(str)


        local output = ''
    local output = mw.text.trim(str)
 
   
        -- Determine 'sort' based off of first character in Dnifont
    output = mw.ustring.gsub(str,"%S", {["S"] = "sh",  
        local sort = mw.ustring.sub(args['dnifont'],1,1)
        sort = sort:gsub("%S+", {["S"] = "sh",  
                                 ["T"] = "th",  
                                 ["T"] = "th",  
                                 ["O"] = "oy",  
                                 ["O"] = "oy",  
Line 43: Line 29:
                                 ["K"] = "k",
                                 ["K"] = "k",
                                 ["I"] = "ai",
                                 ["I"] = "ai",
                                ["k"] = "kh",
                                 ["å"] = "a",
                                 ["å"] = "a",
                                 })
                                 })


        output = output..'[[Category:D\'ni Words beginning with '..sort..']]\n'
    return output
        output = output..'[[Category:D\'ni Words|'..sort..']]\n'
end
 
        return output
local function dnifont2nts(str)
 
    local output = mw.text.trim(str)
   
    output = mw.ustring.gsub(str,"%S",  {["S"] = "š",
                                ["T"] = "þ",
                                ["O"] = "ó",
                                ["c"] = "ç",
                                ["E"] = "í",
                                ["A"] = "é",
                                ["U"] = "ú",
                                ["x"] = "c",
                                ["d"] = "ð",
                                ["D"] = "d",
                                ["k"] = "x",
                                ["K"] = "k",
                                ["I"] = "á",
                                ["å"] = "æ",
                                })


    return output
end
end


local function _dni_word_entry()
local function getArgNums(prefix)
    -- Returns a table containing the numbers of the arguments that exist
    -- for the specified prefix. For example, if the prefix was 'data', and
    -- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
    local nums = {}
    for k, v in pairs(args) do
        local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
        if num then table.insert(nums, tonumber(num)) end
    end
    table.sort(nums)
    return nums
end
 
local function _dni_entry()


         local output = ''
         local output = ''
          
         local sort = dnifont2ots(mw.ustring.sub(args['dnifont'],1,1))
         output = output..'==D\'ni==\n'
        local pagename = string.upper(mw.title.getCurrentTitle().text)
         output = output..'{{huge|<d\'ni>'..args['dnifont']..'</d\'ni>}}\n'
        if (mw.ustring.sub(pagename,1,1) == '-') then
         output = output..'* [[wikipedia:International Phonetic Alphabet|IPA]]: ['..args['ipa']..']\n'
          pagename = mw.ustring.sub(pagename,2)
         output = output..'* [[Dnifont]]: '..args['dnifont']..'\n'
        end
         output = output..'* [[OTS]]: '..args['ots']..'\n'
 
         output = output..'* [[NTS]]: '..args['nts']..'\n'
         output = output..'{{#vardefine:language|D\'ni}}{{#set: Lang=D\'ni }}'
         output = outout..strCategories()
        output = output..'<h2>D\'ni</h2>\n'
         output = output..'{{huge|<dni>{{#vardefineecho:dnifont|'..args['dnifont']..'}}</dni>}}\n'
        output = output..'<h3>Romanization</h3>\n'
         output = output..'* [[Dnifont]]: [[Dnifont::'..args['dnifont']..']]\n'
         output = output..'* [[OTS]]: [[OTS::'..dnifont2ots(args['dnifont'])..']]\n'
         output = output..'* [[NTS]]: [[NTS::'..dnifont2nts(args['dnifont'])..']] '
         output = output..'[[Category:D\'ni words beginning with '..sort..']]'
        output = output..'[[Category:D\'ni words|'..sort..']]'
 
         output = output..'{{DEFAULTSORT:'..pagename..'}}'


     return output
     return output
Line 135: Line 161:
end
end
   
   
function p.dict_word_entry(frame)
function p.dict_entry(frame)
     -- If called via #invoke, use the args passed into the invoking template.
     -- If called via #invoke, use the args passed into the invoking template.
     -- Otherwise, for testing purposes, assume args are being passed directly in.
     -- Otherwise, for testing purposes, assume args are being passed directly in.
Line 145: Line 171:
      
      
     -- Parse the data parameters
     -- Parse the data parameters
    preprocessSingleArg('type')
     preprocessSingleArg('dnifont')
     preprocessSingleArg('dnifont')
    preprocessSingleArg('ipa')
 
    preprocessSingleArg('ots')
     return frame:preprocess(_dni_entry())
    preprocessSingleArg('nts')
 
     return frame:preprocess(_dni_word_entry())
end
end
   
   
function p.smw_format_gloss(frame)
    -- If called via #invoke, use the args passed into the invoking template.
    -- Otherwise, for testing purposes, assume args are being passed directly in.
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame
    end
   
    -- Parse the data parameters
    preprocessSingleArg(1)
    preprocessSingleArg(2)
    wordtype = string.lower(string.gsub(args[1],'.*#',''))
    if (string.find(args[2],"^",1,true) == nil) then
      glosses = args[2]
    elseif (args[2] == nil) then
      glosses = ""
    else
      glosses = '<ol class="hlist-ordered"><li>'..string.gsub(args[2],"%^","</li><li>").."</li></ol>"
    end
    glosses = string.gsub(string.gsub(glosses,'%[%[%s*[Cc]ategory%s*:.-%]%]', ''),"|","{{!}}")
    output = "''"..wordtype.."''.&nbsp;"..glosses
    return output  --frame:preprocess(_dni_entry())
end
function p.smw_format_dict_line(frame)
    -- If called via #invoke, use the args passed into the invoking template.
    -- Otherwise, for testing purposes, assume args are being passed directly in.
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame
    end
   
    -- Parse the data parameters
    preprocessSingleArg(1) --word
    preprocessSingleArg(2) --dnifont
    preprocessSingleArg(3) --glosses
    output = "'''''[[Dictionary:"..args[1].."|"..mw.ustring.lower(args[1]).."]]'''''&nbsp;-&nbsp;<span class='dni'>"..args[2].."</span>&nbsp;- "..args[3]
    return output --frame:preprocess(output)
end
function p.dnifont2ots(frame)
    -- If called via #invoke, use the args passed into the invoking template.
    -- Otherwise, for testing purposes, assume args are being passed directly in.
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame
    end
    preprocessSingleArg(1)
    return dnifont2ots(args[1])
end
function p.dnifont2nts(frame)
    -- If called via #invoke, use the args passed into the invoking template.
    -- Otherwise, for testing purposes, assume args are being passed directly in.
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame
    end
    preprocessSingleArg(1)
    return dnifont2nts(args[1])
end
return p
return p

Latest revision as of 19:20, 6 May 2023

Documentation for this module may be created at Module:Dict D'ni/doc

--
-- This module implements {{D'ni Word Entry}}
--
 
local p = {}

local args = {}
local origArgs
local root


local function dnifont2ots(str)

    local output = mw.text.trim(str)
    
    output = mw.ustring.gsub(str,"%S",  {["S"] = "sh", 
                                 ["T"] = "th", 
                                 ["O"] = "oy", 
                                 ["c"] = "ch", 
                                 ["a"] = "ah", 
                                 ["E"] = "ee", 
                                 ["A"] = "ay", 
                                 ["u"] = "uh",
                                 ["U"] = "oo",
                                 ["x"] = "ts",
                                 ["d"] = "dh",
                                 ["D"] = "d",
                                 ["k"] = "kh",
                                 ["K"] = "k",
                                 ["I"] = "ai",
                                 ["å"] = "a",
                                })

     return output
end
 
local function dnifont2nts(str)

    local output = mw.text.trim(str)
    
    output = mw.ustring.gsub(str,"%S",  {["S"] = "š", 
                                 ["T"] = "þ", 
                                 ["O"] = "ó", 
                                 ["c"] = "ç", 
                                 ["E"] = "í", 
                                 ["A"] = "é", 
                                 ["U"] = "ú",
                                 ["x"] = "c",
                                 ["d"] = "ð",
                                 ["D"] = "d",
                                 ["k"] = "x",
                                 ["K"] = "k",
                                 ["I"] = "á",
                                 ["å"] = "æ",
                                })

     return output
end

local function getArgNums(prefix)
    -- Returns a table containing the numbers of the arguments that exist
    -- for the specified prefix. For example, if the prefix was 'data', and
    -- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
    local nums = {}
    for k, v in pairs(args) do
        local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
        if num then table.insert(nums, tonumber(num)) end
    end
    table.sort(nums)
    return nums
end

local function _dni_entry()

        local output = ''
        local sort = dnifont2ots(mw.ustring.sub(args['dnifont'],1,1))
        local pagename = string.upper(mw.title.getCurrentTitle().text)
        if (mw.ustring.sub(pagename,1,1) == '-') then
           pagename = mw.ustring.sub(pagename,2)
        end

        output = output..'{{#vardefine:language|D\'ni}}{{#set: Lang=D\'ni }}'
        output = output..'<h2>D\'ni</h2>\n'
        output = output..'{{huge|<dni>{{#vardefineecho:dnifont|'..args['dnifont']..'}}</dni>}}\n'
        output = output..'<h3>Romanization</h3>\n'
        output = output..'* [[Dnifont]]: [[Dnifont::'..args['dnifont']..']]\n'
        output = output..'* [[OTS]]: [[OTS::'..dnifont2ots(args['dnifont'])..']]\n'
        output = output..'* [[NTS]]: [[NTS::'..dnifont2nts(args['dnifont'])..']] '
        output = output..'[[Category:D\'ni words beginning with '..sort..']]'
        output = output..'[[Category:D\'ni words|'..sort..']]'

        output = output..'{{DEFAULTSORT:'..pagename..'}}'

    return output
end

local function preprocessSingleArg(argName)
    -- If the argument exists and isn't blank, add it to the argument table.
    -- Blank arguments are treated as nil to match the behaviour of ParserFunctions.
    if origArgs[argName] and origArgs[argName] ~= '' then
        args[argName] = origArgs[argName]
    end
end

local function preprocessArgs(prefixTable, step)
    -- Assign the parameters with the given prefixes to the args table, in order, in batches
    -- of the step size specified. This is to prevent references etc. from appearing in the
    -- wrong order. The prefixTable should be an array containing tables, each of which has
    -- two possible fields, a "prefix" string and a "depend" table. The function always parses
    -- parameters containing the "prefix" string, but only parses parameters in the "depend"
    -- table if the prefix parameter is present and non-blank.
    if type(prefixTable) ~= 'table' then
        error("Non-table value detected for the prefix table", 2)
    end
    if type(step) ~= 'number' then
        error("Invalid step value detected", 2)
    end
    
    -- Get arguments without a number suffix, and check for bad input.
    for i,v in ipairs(prefixTable) do
        if type(v) ~= 'table' or type(v.prefix) ~= "string" or (v.depend and type(v.depend) ~= 'table') then
            error('Invalid input detected to preprocessArgs prefix table', 2)
        end
        preprocessSingleArg(v.prefix)
        -- Only parse the depend parameter if the prefix parameter is present and not blank.
        if args[v.prefix] and v.depend then
            for j, dependValue in ipairs(v.depend) do
                if type(dependValue) ~= 'string' then
                    error('Invalid "depend" parameter value detected in preprocessArgs')
                end
                preprocessSingleArg(dependValue)
            end
        end
    end

    -- Get arguments with number suffixes.
    local a = 1 -- Counter variable.
    local moreArgumentsExist = true
    while moreArgumentsExist == true do
        moreArgumentsExist = false
        for i = a, a + step - 1 do
            for j,v in ipairs(prefixTable) do
                local prefixArgName = v.prefix .. tostring(i)
                if origArgs[prefixArgName] then
                    moreArgumentsExist = true -- Do another loop if any arguments are found, even blank ones.
                    preprocessSingleArg(prefixArgName)
                end
                -- Process the depend table if the prefix argument is present and not blank, or
                -- we are processing "prefix1" and "prefix" is present and not blank, and
                -- if the depend table is present.
                if v.depend and (args[prefixArgName] or (i == 1 and args[v.prefix])) then
                    for j,dependValue in ipairs(v.depend) do
                        local dependArgName = dependValue .. tostring(i)
                        preprocessSingleArg(dependArgName)
                    end
                end
            end
        end
        a = a + step
    end
end
 
function p.dict_entry(frame)
    -- If called via #invoke, use the args passed into the invoking template.
    -- Otherwise, for testing purposes, assume args are being passed directly in.
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame
    end
    
    -- Parse the data parameters
    preprocessSingleArg('dnifont')

    return frame:preprocess(_dni_entry())
end

 
function p.smw_format_gloss(frame)
    -- If called via #invoke, use the args passed into the invoking template.
    -- Otherwise, for testing purposes, assume args are being passed directly in.
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame
    end
    
    -- Parse the data parameters
    preprocessSingleArg(1)
    preprocessSingleArg(2)

    wordtype = string.lower(string.gsub(args[1],'.*#',''))
    if (string.find(args[2],"^",1,true) == nil) then
       glosses = args[2]
    elseif (args[2] == nil) then
       glosses = ""
    else
       glosses = '<ol class="hlist-ordered"><li>'..string.gsub(args[2],"%^","</li><li>").."</li></ol>"
    end
    glosses = string.gsub(string.gsub(glosses,'%[%[%s*[Cc]ategory%s*:.-%]%]', ''),"|","{{!}}")

    output = "''"..wordtype.."''.&nbsp;"..glosses

    return output   --frame:preprocess(_dni_entry())
end

function p.smw_format_dict_line(frame)
    -- If called via #invoke, use the args passed into the invoking template.
    -- Otherwise, for testing purposes, assume args are being passed directly in.
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame
    end
    
    -- Parse the data parameters
    preprocessSingleArg(1) --word
    preprocessSingleArg(2) --dnifont
    preprocessSingleArg(3) --glosses


    output = "'''''[[Dictionary:"..args[1].."|"..mw.ustring.lower(args[1]).."]]'''''&nbsp;-&nbsp;<span class='dni'>"..args[2].."</span>&nbsp;- "..args[3]

    return output --frame:preprocess(output)
end

function p.dnifont2ots(frame)
    -- If called via #invoke, use the args passed into the invoking template.
    -- Otherwise, for testing purposes, assume args are being passed directly in.
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame
    end

    preprocessSingleArg(1)

    return dnifont2ots(args[1])
end

function p.dnifont2nts(frame)
    -- If called via #invoke, use the args passed into the invoking template.
    -- Otherwise, for testing purposes, assume args are being passed directly in.
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame
    end

    preprocessSingleArg(1)

    return dnifont2nts(args[1])
end

return p