Модул:Воситаҳои мизи

Documentation for this module may be created at Модул:Воситаҳои мизи/doc

local export = {}
 
local function manipulate_entry(entries, f)
	entries = mw.text.split(mw.ustring.gsub(entries, "^%s*(.-)%s*$", "%1"), "%s*,%s*")
 
	local sep = ""
	local ret = ""
 
	for _, entry in ipairs(entries) do
		ret = ret .. sep .. (entry == "-" and "—" or entry == "" and "" or f (entry))
		sep = ", "
	end
 
	return ret
end
 
local function gather_args(frame)
	local args = frame.args
	local lang = args["lang"]
 
	if not args[1] then
		args = frame:getParent().args
	end
 
	if not lang then
		lang = args[1]
		local n = 1
		while args[n] do
			args[n] = args[n + 1]
			n = n + 1
		end
	end
 
	return lang, args
end
 
local function get_notes(entry)
	local notes
	entry, notes = mw.ustring.match(entry, "^(.-)([%*%~%@%#%$%%%^%&0-9_ ]*)$")
 
	if notes ~= "" then
		notes = "<sup>" .. mw.ustring.gsub(notes, "_", " ") .. "</sup>"
	end
 
	return entry, notes
end
 
function export.linkify_entry(frame)
	local m_link = require("Module:links")
 
	local lang, args = gather_args(frame)
	lang = require("Module:languages").getByCode(lang)
	local entries = args[1]
	local allowSelfLink = (args["allowSelfLink"] or "") ~= ""
 
	local function f(entry)
		local e, notes = get_notes(entry)
		return m_link.language_link(e, nil, lang, nil, allowSelfLink) .. notes
	end
 
	return manipulate_entry(m_link.remove_links(entries), f)
end
 
function export.translit_entry(frame)
	local m_link = require("Module:links")
 
	local lang, args = gather_args(frame)
	lang = require("Module:languages").getByCode(lang)
	local entries = args[1]
 
	local function f(entry)
		local e, notes = get_notes(entry)
		return lang:transliterate(e) .. notes
	end
 
	return manipulate_entry(m_link.remove_links(entries), f)
end
 
function export.format_entry(frame)
	local m_link = require("Module:links")
 
	local lang, args = gather_args(frame)
	local entries = args[1]
 
	return manipulate_entry(m_link.remove_links(entries), function(entry) local e, n = get_notes(entry); return e .. n end)
end
 
function export.first_entry(frame)
	local m_link = require("Module:links")
 
	local lang, args = gather_args(frame)
	local entries = args[1]
	local entry = mw.text.split(mw.ustring.gsub(entries, "^%s*(.-)%s*$", "%1"), "%s*,%s*")[1]
 
	local e, notes = get_notes(entry)
	return e .. notes
end
 
return export