Module:Anchor

Уикипедия — ашық энциклопедиясынан алынған мәлімет
Навигацияға өту Іздеуге өту



local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local compressSparseArray = require('Module:TableTools').compressSparseArray
local p = {}

local function add_anchor(anchor, text, visible)
	local result = mw.html.create('span')
		:attr('id', anchor)
		:wikitext(text)
	if visible then
		result:addClass('highlight-target')
	end
	return tostring(result)
end

local function unpack(...)
	local frame = ...
	local args
	if type(frame.args) == 'table' then
		args = getArgs(frame)
	elseif type(frame) == 'table' then
		args = frame
	else
		args = {frame}
	end
	return args
end

function p.main(...)
	local args = unpack(...)
	local anchors = compressSparseArray(args)
	local text
	local visible = yesno(args.visible or args.v)
	if visible then
		text = args.text or args['мәтін'] or args[1]
	end

	local result = text
	for i, v in ipairs(anchors) do
		result = add_anchor(anchors[i], result, visible)
		--
		local encoded_anchor = mw.uri.encode(anchors[i], 'WIKI'):gsub('%%', '.')
		if anchors[i] ~= encoded_anchor then
			result = add_anchor(encoded_anchor, result, visible)
		end
	end

	return result
end

return p