Module:D'ni Tools: Difference between revisions

From Guild of Archivists
mNo edit summary
No edit summary
Line 1: Line 1:
--
-- This implements several useful functions for dealing with D'ni text in the Wiki.
--
-- parsed_dni() returns a string in which every component is linked to its appropriate Dictionary namespace entry.
-- dnifont2ots() converts a Dnifont string to an ots one
-- dnifont2nts() converts a Dnifont string to an nts one
 
local p = {}
local p = {}


Line 7: Line 13:
local output = ''
local output = ''


local function parseArg(input)
-- Local Functions
 
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"] = "i",
                                ["A"] = "é",
                                ["U"] = "ú",
                                ["x"] = "c",
                                ["d"] = "ð",
                                ["D"] = "d",
                                ["k"] = "x",
                                ["K"] = "k",
                                ["I"] = "á",
                                ["å"] = "æ",
                                })
 
    return output
end
 
 
 
local function parseDniStr(input)


     local output = ''
     local output = ''
Line 46: Line 104:


     for word in ipairs(origArgs) do
     for word in ipairs(origArgs) do
         output = output..parseArg(origArgs[word])
         output = output..parseDniString(origArgs[word])
     end
     end


     return output
     return output
end
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

Revision as of 19:17, 24 July 2016

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

--
-- This implements several useful functions for dealing with D'ni text in the Wiki.
--
-- parsed_dni() returns a string in which every component is linked to its appropriate Dictionary namespace entry.
-- dnifont2ots() converts a Dnifont string to an ots one
-- dnifont2nts() converts a Dnifont string to an nts one

local p = {}

local args = {}
local origArgs

local output = ''

-- Local Functions

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"] = "i", 
                                 ["A"] = "é", 
                                 ["U"] = "ú",
                                 ["x"] = "c",
                                 ["d"] = "ð",
                                 ["D"] = "d",
                                 ["k"] = "x",
                                 ["K"] = "k",
                                 ["I"] = "á",
                                 ["å"] = "æ",
                                })

     return output
end



local function parseDniStr(input)

    local output = ''

    local word = mw.text.trim(input)
    local first = mw.ustring.sub(word,1,1)
    local last = mw.ustring.sub(word,-1,-1)

    if (word == '') then
        output = ' '
    elseif (word == ' ') then
        output = ' '
    elseif (word == '.' or word == '?' or word == '!' or word == ',') then
        output = first
    elseif (first == '-') then
        output = '[[Dictionary:'..word..'|'..mw.ustring.sub(word,2,-1)..']]'
    elseif (last == '-') then
        output = '[[Dictionary:'..word..'|'..mw.ustring.sub(word,1,-2)..']]'
    elseif (first == '.' or first == '!' or first == '?' or first == ',') then
        output = first..'[[Dictionary:'..mw.ustring.sub(word,2,-1)..'|'..mw.ustring.sub(word,2,-1)..']]'
    elseif (last == ',') then
        output = '[[Dictionary:'..mw.ustring.sub(word,1,-2)..'|'..mw.ustring.sub(word,1,-2)..']]'..last
    else
        output = '[[Dictionary:'..word..'|'..word..']]'
    end

    return output
end

function p.parsed_dni(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

    for word in ipairs(origArgs) do
        output = output..parseDniString(origArgs[word])
    end

    return 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