Module:D'ni Tools: Difference between revisions

From Guild of Archivists
mNo edit summary
mNo edit summary
Line 17: Line 17:
     if (word == '') then
     if (word == '') then
         output = ' '
         output = ' '
    elseif (word == '.' or word == '?' or word == '!' or word == ',') then
        output = ' '..first
     elseif (first == '-') then
     elseif (first == '-') then
         output = '[[Dictionary:'..word..'|'..mw.ustring.sub(word,2,-1)..']]'
         output = '[[Dictionary:'..word..'|'..mw.ustring.sub(word,2,-1)..']]'
     elseif (last == '-') then
     elseif (last == '-') then
         output = '[[Dictionary:'..word..'|'..mw.ustring.sub(word,1,-2)..']]'
         output = '[[Dictionary:'..word..'|'..mw.ustring.sub(word,1,-2)..']]'
     elseif (first == '.') then
     elseif (first == '.' or first == '!' or first == '?' or first == ',') then
         output = '.[[Dictionary:'..mw.ustring.sub(word,2,-1)..'|'..mw.ustring.sub(word,2,-1)..']]'
         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
     else
         output = '[[Dictionary:'..word..'|'..word..']]'
         output = '[[Dictionary:'..word..'|'..word..']]'

Revision as of 23:54, 20 July 2016

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

 
local p = {}

local args = {}
local origArgs

local output = ''

local function parseArg(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 == '.' 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..parseArg(origArgs[word])
    end

    return output
end
 
return p