Jump to content

Module:useful stuff

ពីWiktionary

Documentation for this module may be created at Module:useful stuff/doc

--[=[
    If you want to define a common function and you dont know where to put it (under what title), define it here.
    Functions in this module should be eventually moved to other modules.
]=]--

local export = {}

-- Create a tagged text from a given text. This is equivalent of the script templates.
function export.tag_text(text, lang, tag, class)
    if not class then
        return "<" .. tag .. " lang=\"" .. lang .. "\">" .. text .. "</" .. tag .. ">"
    else
        return "<" .. tag ..  " class=\"" .. class .. "\" lang=\"" .. lang .. "\">" .. text .. "</" .. tag .. ">"
    end
end

-- transliterate text, if possible
function export.translit(lang, text)
    local translit
    local translit_modules = {
        ["ae"] = "Module:Avst-translit",
        ["el"] = "Module:el-translit",
        ["ru"] = "Module:ru-translit",
        ["sh"] = "Module:sh-translit",
        ["ug"] = "Module:ug-translit",
        ["tg"] = "Module:tg-translit",
        ["xcl"] = "Module:xcl-translit"
    }

    if translit_modules[lang] then
        translit = require(translit_modules[lang]).tr(text)
    end
    
    return translit
end

-- Detect script based on the first alphabetical characters of a string
function export.detect_script(text, lang)
    -- list of characters that may occur at the beginning of a word
    local chars_table = {
        ["Arab"] = "ءاآأإئؤبتثجچحخدذرزسشصضطظعغفقكلمنوهي",
        ["Armn"] = "Ա-֊",
        ["Beng"] = "ঁ-৺",
        ["Cyrl"] = "Ѐ-ӿ",
        ["Deva"] = "ँ-ॽ",
        ["fa-Arab"] = "کیٔ",
        ["Geor"] = "Ⴀ-ჼ",
        ["Grek"] = "ʹ-Ͽ",
        ["Hebr"] = "א-ת",
        ["Khmr"] = "ក-៹",
        ["Latn"] = "A-Za-z",
        ["Laoo"] = "ກ-ໝ",
        ["Mong"] = "᠀-ᢪ",
        ["Mymr"] = "က-ၙ", 
        ["Thai"] = "ก-ฺ",        
        ["Sinh"] = "ං-෴",        
        ["ug-Arab"] = "ۈۇې",        
        -- TODO
    }
    
    -- first try to detect script based on the native scripts of the language
    local scripts = languages[lang].scripts or {}
    for i, script in ipairs(scripts) do
        if mw.ustring.match(text, "[%[%d%p%s]-[" .. chars_table[script] .. "]") then
            return script
        end
    end
    
    -- not written in native scripts; check for all scripts
    for script, chars in ipairs(chars_table) do
        if mw.ustring.match(text, "[%[%d%p%s]-[" .. chars .. "]") then
            return script
        end
    end
end

function export.annotate(text, lang, tag, class, translit, gloss, gender, frame)
    text = export.tag_text(text, lang, tag, class)
 
    local annotations = {}
 
    if translit then
        table.insert(annotations, "<span lang=\"\">" .. translit .. "</span>")
    end
 
    if gloss then
        table.insert(annotations, "<span class=\"mention-gloss-double-quote\">“</span><span class='mention-gloss'>" .. gloss .. "</span><span class=\"mention-gloss-double-quote\">”</span>")
    end
 
    if #annotations > 0 then
        text = text .. "&nbsp;(" .. table.concat(annotations, ", ") .. ")"
    end
 
    if gender then
        if gender[2] or gender[3] then
            -- {{#if:{{{g|{{{g1|}}}}}}|&nbsp;{{{{{g|{{{g1|}}}}}}|{{{g2|}}}|{{{g3|}}}}}}}
            text = text .. "&nbsp;" .. frame:expandTemplate{title = gender[1], args = {gender[2], gender[3]}}
        elseif gender[1] then
            local gen = require("Module:gender and number")
            text = text .. "&nbsp;" .. gen.format(gender)
        end
    end
 
    return text
end

return export