Module:Lang-utils/parse

From Guild of Archivists
< Module:Lang-utils
Revision as of 01:22, 17 July 2024 by BladeLakem (talk | contribs) (Created page with "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 = ' ' e...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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 == '&nbsp;') then
        result = '&nbsp;'
    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