Module:Lang-utils/parse

From Guild of Archivists

Documentation for this module may be created at Module:Lang-utils/parse/doc

local p = {}

local args = {}
local origArgs
local root

local function parseWord(input)

    local result = ''

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

    if (word == mw.ustring.lower(mw.title.getCurrentTitle().rootText)) then
         prefix = '\'\'\''
         suffix = prefix
    end 
    if (word == '') then
        result = ' '
    elseif (word == ' ') then
        result = ' '
    elseif (word == '.' or word == '?' or word == '!' or word == ',') then
        result = first
    elseif (first == '-') then
        result = prefix..'[[Dictionary:'..word..'|'..mw.ustring.sub(word,2,-1)..']]'..suffix
    elseif (last == '-') then
        result = prefix..'[[Dictionary:'..word..'|'..mw.ustring.sub(word,1,-2)..']]'..suffix
    elseif (first == '.' or first == '!' or first == '?' or first == ',') then
        result = first..prefix..'[[Dictionary:'..mw.ustring.sub(word,2,-1)..'|'..mw.ustring.sub(word,2,-1)..']]'..suffix
    elseif (last == '.' or last == '!' or last == '?' or last == ',') then
        result = prefix..'[[Dictionary:'..mw.ustring.sub(word,1,-2)..'|'..mw.ustring.sub(word,1,-2)..']]'..suffix..last
    else
        result = prefix..'[[Dictionary:'..word..'|'..word..']]'..suffix
    end
    
    return result
    
end

function p.parsed_text(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.args
    end

	local result = ''

    for word in ipairs(origArgs) do
		result = result..parseWord(origArgs[word],count)
    end

	return result

end

return p