Jump to content

Module:anchors

Wiktionary වෙතින්

මෙම මොඩියුලය සඳහා උපදෙස් පත්‍රිකාවක් අවශ්‍ය වෙයි.
මෙම මොඩියුලය හා අදාළ අරමුණු සහ භාවිත උපදෙස්, උපදෙස් පිටුව තුළ ලේඛනගත කිරීමට කාරුණික වන්න.

local export = {}

local string_utilities_module = "Module:string utilities"

local anchor_encode = mw.uri.anchorEncode
local concat = table.concat
local insert = table.insert
local language_anchor -- Defined below.

local function decode_entities(...)
	decode_entities = require(string_utilities_module).decode_entities
	return decode_entities(...)
end

local function encode_entities(...)
	encode_entities = require(string_utilities_module).encode_entities
	return encode_entities(...)
end

-- Returns the anchor text to be used as the fragment of a link to a language section.
function export.language_anchor(lang, id)
	return anchor_encode(lang:getFullName() .. ": " .. id)
end
language_anchor = export.language_anchor

-- Normalizes input text (removes formatting etc.), which can then be used as an anchor in an `id=` field.
function export.normalize_anchor(str)
	return decode_entities(anchor_encode(str))
end

function export.make_anchors(ids)
	local anchors = {}
	for i = 1, #ids do
		local id = ids[i]
		local el = mw.html.create("span")
			:addClass("template-anchor")
			:attr("id", anchor_encode(id))
			:attr("data-id", id)
		insert(anchors, tostring(el))
	end
	return concat(anchors)
end

function export.senseid(lang, id, tag_name)
	-- The following tag is opened but never closed, where is it supposed to be closed?
	--         with <li> it doesn't matter, as it is closed automatically.
	--         with <p> it is a problem
	-- Cannot use mw.html here as it always closes tags
	return "<" .. tag_name .. " class=\"senseid\" id=\"" .. language_anchor(lang, id) .. "\" data-lang=\"" .. lang:getCode() .. "\" data-id=\"" .. encode_entities(id) .. "\">"
end

function export.etymid(lang, id)
	-- Use a <ul> tag to ensure spacing doesn't get messed up.
	local el = mw.html.create("ul")
		:addClass("etymid")
		:attr("id", language_anchor(lang, id))
		:attr("data-lang", lang:getCode())
		:attr("data-id", id)
	return tostring(el)
end

function export.etymonid(lang, id, no_tree, title)
	-- Use a <ul> tag to ensure spacing doesn't get messed up.
	local el = mw.html.create("ul")
		:addClass("etymonid")
		:attr("data-lang", lang:getCode())
	
	if id then
		el:attr("id", language_anchor(lang, id))
		el:attr("data-id", id)
	end
	if no_tree then
		el:attr("data-no-tree", "1")
	end
	if title then
		el:attr("data-title", title)
	end
	
	return tostring(el)
end

return export
"https://si.wiktionary.org/w/index.php?title=Module:anchors&oldid=226224" වෙතින් සම්ප්‍රවේශනය කෙරිණි