Пређи на садржај

Модул:also

Документацију овог модула можете да направите на страници Модул:also/док

local m_debug = require("Модул:debug")
local m_links = require("Модул:links")
local m_params = require("Модул:parameters")
local m_scripts = require("Модул:scripts")
local m_str_utils = require("Модул:string utilities")
local m_util = require("Модул:utilities")

local codepoint = m_str_utils.codepoint
local ulen = m_str_utils.len
local yesno = require("Модул:yesno")

local und = require("Модул:languages").getByCode("und")

local export = {}

function export.main(frame)
	local params = {
		[1] = {required = true, list = true},
		["sc"] = {list = true, allow_holes = true},
		["uni"] = {list = true, allow_holes = true, separate_no_index = true},
		["detectsc"] = {type = "boolean"}
	}
	
	local args = m_params.process(frame:getParent().args or frame.args, params)
	
	if args["sc"].maxindex > 0 then
		-- [[Special:WhatLinksHere/Wiktionary:Tracking/also/sc param]]
		m_debug.track("also/sc param")
	end
	
	local uni_default = yesno((args["uni"]["default"] == "auto") or args["uni"]["default"]) and "auto" or nil
	
	local title = mw.title.getCurrentTitle()
	local full_pagename = title.fullText
	
	-- Disables tagging outside of mainspace, where {{also}} more often links to
	-- pages that are not entries and don't need tagging. Tagging in Reconstruction
	-- would be more complicated and is often unnecessary, and there are very few
	-- entries in Appendix.
	local detect_sc = title.nsText == ""
		or args["detectsc"] -- to test the script detection capabilities
	
	local items = {}
	local use_semicolon = false
	local arg_plaintext
		
	for i, arg in ipairs(args[1]) do
		local uni = args["uni"][i] or uni_default
		local sc = args["sc"][i] and m_scripts.getByCode(args["sc"][i]) or und:findBestScript(arg)
		
		if arg:find(",", 1, true) then
			use_semicolon = true
		end
		
		if not yesno(uni, uni) then
			uni = nil
		end
		
		-- Create the link.
		arg = m_links.plain_link{term = arg, lang = und, sc = sc}
		
		-- We use the link to determine if arg is the current page, so that it works on edge cases like unsupported titles.
		if not arg:match("<strong class=\"selflink\">") then
			arg = '<b class="' .. sc:getCode() .. '">' .. arg .. "</b>"
			
			local cp
			if uni then
				m_debug.track("also/uni")
				
				if uni == 'auto' then
					arg_plaintext = m_util.get_plaintext(arg)
					cp = (ulen(arg_plaintext) == 1) and codepoint(arg_plaintext, 1, 1)
				else
					cp = tonumber(uni)
					
					if ulen(arg) ~= 1 or cp ~= codepoint(arg, 1, 1) then
						m_debug.track("also/uni/noauto")
					else
						m_debug.track("also/uni/auto")
					end
				end
			end
			
			if cp then
				local m_unidata = require('Модул:Unicode data')
				
				arg = arg .. (" <small>[U+%04X %s]</small>"):format(
					cp,
					m_unidata.lookup_name(cp):gsub("<", "&lt;")
				)
			end
			
			table.insert(items, arg)
		end
	end
	
	if #items == 0 then
		table.insert(items, "{{{1}}}")
	end
	
	-- Join with serial "and" and serial comma
	local function serial_comma_join(seq, conjunction)
		conjunction = conjunction or ","
		if #seq == 0 then
			return ""
		elseif #seq == 1 then
			return seq[1] -- nothing to join
		elseif #seq == 2 then
			return seq[1] .. " ''и'' " .. seq[2]
		else
			return table.concat(seq, conjunction .. " ", 1, #seq - 1)
				.. "<span class='serial-comma'>" .. conjunction .. "</span>" ..
				"''<span class='serial-and'> and</span>'' " ..
				seq[#seq]
		end
	end
	
	return ("<div class=\"disambig-see-also\">''Видите и:'' %s</div>"):format(
		serial_comma_join(items, use_semicolon and ";" or ",")
	)
end

return export