Documentation for this module may be created at Module:Sandbox/Ivan Štambuk/lang-ex-helper/doc

local p = {}
 
-- Processes "ipa=" parameter when specified inside the {{lang-ex}} template. Adds slashes for phonemic transcription,
-- unless the argument is already encapsulated within slashes (for phonemic transcription) or square brackets (for
-- phonetic transcription).
function p.process_ipa(frame)
    local ipa = frame:getParent().args["ipa"]
    if ipa == "" or ipa == nil then
        return ""
    end
    if mw.ustring.match(ipa, "^%/.*%/$") == nil and mw.ustring.match(ipa, "^%[.*%]$") == nil then
        return ("/" .. ipa .. "/")
    else
        return ipa
    end
end

-- Generate a wikilink to language name, followed by a whitespace, when the language code ends with "+".
-- Depends on language code being expanded to full language name via the {{ISO 639 name}} template, and
-- passed through "langname=" parameter, which is faster than expanding that template in Scribunto.
function p.process_langcode(frame)
    local langcode = frame:getParent().args["1"]
    if langcode == "" or langcode == nil then
        return ""
    end
    if mw.ustring.match(langcode, "%+$") then
        local langname = frame.args["langname"]
        return "[[" .. langname .. " language|" .. langname .. "]]: "
    else
        return ""
    end
end

-- If language code provided as the first argument ends with "+", it returns the code without the "+" suffix.
-- Otherwise, it returns the first argument.
function p.get_bare_langcode(frame)
    local langcode = frame:getParent().args["1"]
    if langcode == "" or langcode == nil then
        return ""
    end
    if mw.ustring.match(langcode, "%+$") then
        -- strip the "+" suffix
        local bare_langcode = mw.ustring.sub(langcode, 1, mw.ustring.len(langcode)-1);
        return (bare_langcode)
    else
        return ""
    end    
end

return p