सामग्री पर जाएँ

मॉड्यूल:Template:also/sandbox

विक्षनरी से

"इस मॉड्यूल हेतु प्रलेख मॉड्यूल:Template:also/sandbox/doc पर बनाया जा सकता है"

local export = {}

local yesno = require('Module:yesno')

local get_script = require("Module:scripts").charToScript

-- If there are characters in both scripts (the key and value), the second
-- should be used.
local overridden_by = {
	Latn = "Latinx",
	Grek = "polytonic",
	Cyrl = "Cyrs",
}

-- Join with serial "and" and serial comma
local function serial_comma_join(seq)
	if #seq == 0 then
		return ""
	elseif #seq == 1 then
		return seq[1] -- nothing to join
	elseif #seq == 2 then
		return seq[1] .. " ''and'' " .. seq[2]
	else
		return table.concat(seq, ", ", 1, #seq - 1) .. "<span class='serial-comma'>,</span>" ..
			"''<span class='serial-and'> and</span>'' " ..
			seq[#seq]
	end
end

function export.main(frame)
	local args = frame:getParent().args
	local sc_default = args["sc"]
	local uni_default = yesno((args["uni"] == "auto") or args["uni"]) and "auto" or nil
	
	local items = {}
	
	for i, arg in ipairs(args) do
		local uni = args["uni" .. i] or uni_default
		local sc = args["sc" .. i] or sc_default
		
		if not yesno(uni, uni) then
			uni = nil
		end
		
		local s = ""
		local has_piped_link, _, link_text = arg:find("%[%[[^%[%]|]+|(.+)]]")
		
		if has_piped_link then
			s = ("'''%s'''"):format(arg)
			arg = mw.text.decode(link_text)
		else
			local has_link, _, link_text = arg:find("%[%[([^%[%]|]+)]]")
			
			if has_link then
				s = ("'''%s'''"):format(arg)
				arg = mw.text.decode(link_text)
			else
				s = ("'''[[%s]]'''"):format(arg)
				arg = mw.text.decode(arg)
			end
		end
		
		local codepoint = nil
		
		if uni then
			require("Module:debug").track("also/uni")
			
			if uni == 'auto' then
				codepoint = (mw.ustring.len(arg) == 1) and mw.ustring.codepoint(arg, 1, 1)
			else
				codepoint = tonumber(uni)
				
				if mw.ustring.len(arg) ~= 1 or codepoint ~= mw.ustring.codepoint(arg, 1, 1) then
					require("Module:debug").track("also/uni/noauto")
				else
					require("Module:debug").track("also/uni/auto")
				end
			end
		end
		
		-- If all characters are in one script, tag the link with it.
		-- Ignore all "None"-script characters.
		local sc, curr_sc
		for codepoint in mw.ustring.gcodepoint(arg) do
			curr_sc = get_script(codepoint)
			if curr_sc ~= "None" then
				if sc == nil then
						sc = curr_sc
				elseif curr_sc ~= sc then
					-- For instance, Grek -> polytonic.
					if overridden_by[sc] == curr_sc then
						sc = curr_sc
					
					-- For instance, Grek and Latn.
					elseif overridden_by[curr_sc] ~= sc then
						sc = nil
						break
					end
				end
			end
		end
		
		if sc then
			s = '<span class="' .. sc .. '">' .. s .. "</span>"
		end
		
		if codepoint then
			local m_unidata = require('Module:Unicode data')
			
			s = s .. (" <small>[U+%04X %s]</small>"):format(
				codepoint,
				m_unidata.lookup_name(codepoint):gsub("<", "&lt;")
			)
		end
		
		if arg ~= mw.title.getCurrentTitle().fullText then
			table.insert(items, s)
		else
			require("Module:debug").track("also/pagename")
		end
	end
	
	if #items == 0 then
		table.insert(items, "{{{1}}}")
	end
	
	return ("<div class=\"disambig-see-also%s\">''See also:'' %s</div>"):format(
		(#items == 2) and "-2" or "",
		serial_comma_join(items)
	)
end

return export