Module:User:ZxxZxxZ/utilities

local languages = mw.loadData("Module:languages") 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 text then return "<" .. (tag or "span") .. (class and (" class=\"" .. class .. "\"") or "") .. " lang=\"" .. lang .. "\">" .. text .. ""   else return "" end end -- transliterate the text, if possible function export.translit(lang, text) -- TODO: the table's information should be moved to Module:languages local translit_modules = { ["ae"] = "Module:Avst-translit", ["el"] = "Module:el-translit", ["ru"] = "Module:ru-translit", ["ug"] = "Module:ug-translit", ["tg"] = "Module:tg-translit", ["ka"] = "Module:ka-translit", ["xcl"] = "Module:xcl-translit", ["axm"] = "Module:axm-translit", ["hy"] = "Module:hy-translit" }   if translit_modules[lang] then return require(translit_modules[lang]).tr(text) end end -- Detect the 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 = { ["Latn"] = "A-Za-z", ["Arab"] = "؀-ۯ", ["Armn"] = "Ա-֊", ["Beng"] = "ঁ-৺", ["Cyrl"] = "Ѐ-ӿ", ["Deva"] = "ँ-ॽ", ["fa-Arab"] = "پچژکگی؀-۹", ["Geor"] = "Ⴀ-ჼ", ["Goth"] = "𐌰-𐍊", ["Grek"] = "ʹ-Ͽ", ["Hebr"] = "א-ת", ["Khmr"] = "ក-៹", ["Laoo"] = "ກ-ໝ", ["Mong"] = "᠀-ᢪ", ["Mymr"] = "က-ၙ", ["Thai"] = "ก-ฺ", ["Sinh"] = "ං-෴", ["ug-Arab"] = "ۈۇې",        --FIXME -- TODO }   -- first try to detect the script based on the native scripts of the language local scripts = languages[lang].scripts or {} for i, script in ipairs(scripts) do       if chars_table[script] and 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, class_tr, translit, gloss, lit, gender, frame, pos) text = export.tag_text(text, lang, tag, class)

local annotations = {} local paren_class = {} local mention = class_tr:find("mention") and true

if translit then translit = translit:gsub('%[%[', ):gsub('%]%]', ) -- remove wikilinks

table.insert(annotations, "" .. translit .. " ") if mention then table.insert(paren_class, "mention-tr-paren") if pos or gloss or lit then table.insert(paren_class, "mention-tr-gloss-paren") else table.insert(paren_class, "ib-brac") class_tr = "ib-content " .. class_tr end end else if mention and (pos or gloss or lit) then table.insert(paren_class, "mention-gloss-paren") end end

if pos then table.insert(annotations,                    mw.title.new("pos " .. pos, "Template").exists                    and frame:expandTemplate{title = "pos " .. pos}                     or pos) end

if gloss then table.insert(annotations, "“ " .. gloss .. " ” ") end

if lit then table.insert(annotations, "literally “ " .. lit .. " ” ") end

if #annotations > 0 then text = text .. " "

if #paren_class > 0 then text = text .. "( "       else            text = text .. "(" end

text = text .. table.concat(annotations, ", ")

if #paren_class > 0 then text = text .. ") "       else            text = text .. ")" end end

if gender then if gender[2] or gender[3] then --            text = text .. " " .. frame:expandTemplate{title = gender[1], args = {gender[2], gender[3]}} elseif gender[1] then local gen = require("Module:gender and number") text = text .. " " .. gen.format_list(gender) end end

return text end

return export