Difference between revisions 22374 and 34824 on mkwiktionary

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)
    return "<" .. (tag or "span") .. (class and (" class=\"" .. class .. "\"") or "")
           .. " lang=\"" .. lang .. "\">" .. text .. "</" .. (tag or "span") .. ">"
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"] = "ۈۇې",        
        -- 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)
    text = export.tag_text(text, lang, tag, class)
 
    local annotations = {}
 
    if translit then
        translit = translit:gsub('%[%[', ''):gsub('%]%]', '') -- remove wikilinks
        table.insert(annotations, "<span lang=\"\"" .. (class_tr and (" class=\"" .. class_tr .. "\"") or "") .. ">" .. 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 lit then
        table.insert(annotations, "literally <span class=\"mention-gloss-double-quote\">“</span><span class='mention-gloss'>" .. lit .. "</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_list(gender)
        end
    end
 
    return text-- Module:utilities
-- Imported from en.wiktionary
-- 2016-06-22 -- V2 -- last modified by DenisWasRight
-- Extended version for mk.wiktionary

local export = {}

local notneeded = {
	["und"] = true,
	["cmn"] = true,
	["ja"] = true,
	["zu"] = true,
	["nan"] = true,
	["yue"] = true,
	["ko"] = true,
}

local neededhassubpage = {
	["ga"] = true,
	["gv"] = true,
	["nv"] = true,
	["roa-jer"] = true,
	["fr"] = true,
	["rm"] = true,
	["prg"] = true,
	["gd"] = true,
	["twf"] = true,
	["en"] = true,
	["ro"] = true,
	["egl"] = true,
	["roa-tar"] = true,
	["gl"] = true,
	["ast"] = true,
	["br"] = true,
}

-- A helper function to escape magic characters in a string
-- Magic characters: ^$()%.[]*+-?
function export.pattern_escape(text)
	text = (type(text) == "table" and text.args[1] or text)
	text = mw.ustring.gsub(text, "([%^$()%%.%[%]*+%-?|])", "%%%1")
	return text
end

-- Format the categories with the appropriate sort key
function export.format_categories(categories, lang, sort_key, sort_base)
	NAMESPACE = NAMESPACE or mw.title.getCurrentTitle().nsText
	
	if NAMESPACE == "" or NAMESPACE == "Помош" or NAMESPACE == "Викиречник" then
		PAGENAME = PAGENAME or mw.title.getCurrentTitle().text
		SUBPAGENAME = SUBPAGENAME or mw.title.getCurrentTitle().subpageText
		
		if not lang then
			lang = require("Модул:languages").getByCode("und")
		end
		
		-- Generate a default sort key
		sort_base = lang:makeSortKey(sort_base or SUBPAGENAME)
		
		if sort_key then
			-- Gather some statistics regarding sort keys
			if mw.ustring.lower(sort_key) == sort_base then
				table.insert(categories, "Sort key tracking/redundant")
			end
		else
			sort_key = sort_base
		end
		
		-- If the resulting key is the same as the wiki software's default, remove it
		if sort_key == PAGENAME then
			sort_key = nil
		end
		
		for key, cat in ipairs(categories) do
			categories[key] = "[[Категорија:" .. cat .. (sort_key and "|" .. sort_key or "") .. "]]"
		end
		
		return table.concat(categories, "")
	else
		return ""
	end
end

-- Used by {{categorize}}
function export.template_categorize(frame)
	NAMESPACE = NAMESPACE or mw.title.getCurrentTitle().nsText
	local format = frame.args["format"]
	local args = frame:getParent().args
	
	local langcode = args[1]; if langcode == "" then langcode = nil end
	local sort_key = args["sort"]; if sort_key == "" then sort_key = nil end
	local categories = {}
	
	if not langcode then
		if NAMESPACE == "Шаблон" then return "" end
		error("Language code has not been specified. Please pass parameter 1 to the template.")
	end
	
	local lang = require("Модул:languages").getByCode(langcode)
	
	if not lang then
		if NAMESPACE == "Шаблон" then return "" end
		error("The language code \"" .. langcode .. "\" is not valid.")
	end
	
	local prefix = ""
	
	if format == "pos" then
		prefix = lang:getCanonicalName() .. " "
	elseif format == "topic" then
		prefix = lang:getCode() .. ":"
	end
	
	local i = 2
	local cat = args[i]
	
	while cat do
		if cat ~= "" then
			table.insert(categories, prefix .. cat)
		end
		
		i = i + 1
		cat = args[i]
	end
	
	return export.format_categories(categories, lang, sort_key)
end
  
return export