Loncat ke isi

Modul:pos/sandbox

Deri Wikikamus

Dokumentasi untuk modul ini dapat dibuat di Modul:pos/sandbox/doc

local p = {}

local langModule = require('Modul:lang')
local linkModule = require('Modul:links')

-- POS types (rungkun kata)
local posTypes = {
	n    = "Kata peada",
	pda  = "Kata peada",
	v    = "Kata kereja",
	kja  = "Kata kereja",
	a    = "Kata penyipat",
	ppt  = "Kata penyipat",
	adv  = "Kata penerang",
	png  = "Kata penerang",
	amb  = "Pendengkèkan",
	pdk  = "Pendengkèkan",
	art  = "Penimpalan",
	ppl  = "Penimpalan",
	con  = "Kata penjuntrung",
	pjg  = "Kata penjuntrung",
	int  = "Gegeroan",
	ger  = "Gegeroan",
	num  = "Kata pengangka",
	ang  = "Kata pengangka",
	pn   = "Kata nama",
	nama = "Kata nama",
	part = "Petilan",
	ptil = "Petilan",
	post = "Pengahiran",
	ahir = "Pengahiran",
	prep = "Pengawalan",
	awal = "Pengawalan",
	pron = "Kata pengganti",
	pgt  = "Kata pengganti",
	ptcp = "Dumanan",
	dum  = "Dumanan",
	let  = "Hurup",
	hur  = "Hurup",
	pre  = "Depanan",
	dpn  = "Depanan",
	suf  = "Kintilan",
	ktl  = "Kintilan",
	sym  = "Lambang",
	lam  = "Lambang",
}

-- Special separators (labels without commas, synchronize states)
local specialLabels = {
	["atawa"] = true,
	["ama"]   = true,
}

-- Parse <entry:...> syntax
local function parseEntryTag(item)
	if not item then return item, nil end
	local base, display = mw.ustring.match(item, "^(.-)<entry:(.-)>$")
	if base then return base, display end
	return item, nil
end

-- Format a single item
local function formatItem(item, isForm, lang)
	if not item or item == "" then
		return ""
	elseif item == "_" then
		return "&#32;"
	elseif specialLabels[item] then
		return "<i>" .. item .. "</i>"
	elseif isForm then
		local base, display = parseEntryTag(item)
		return linkModule.makeMention{
			[1] = lang or "",
			[2] = base or "",
			entry = display or base or "",
			notext = "1",
			noitalic = "1",
			bold = "1",
		}
	else
		return "<i>" .. item .. "</i>"
	end
end

-- Collect params
local function getLabelFormParams(args)
	local params = {}
	for i = 2, 20 do
		local v = args[tostring(i)]
		if v and v ~= "" then table.insert(params, v) end
	end
	return params
end

-- Build label/form string (forward alternating with sync at separators)
local function buildLabelForm(params, lang)
	if not params or #params == 0 then return "" end

	local items = {}
	local isForm = false

	for i, raw in ipairs(params) do
		if raw == "_" then
			table.insert(items, { raw = raw, text = " ", form = false })
		elseif specialLabels[raw] then
			-- separator: sync next item’s state with previous
			table.insert(items, { raw = raw, text = "<i>" .. raw .. "</i>", form = false, separator = true })
		else
			local text = formatItem(raw, isForm, lang)
			table.insert(items, { raw = raw, text = text, form = isForm })
			isForm = not isForm
		end
	end

	-- Fix sync around separators
	for i = 1, #items do
		if items[i].separator then
			local prev = items[i-1]
			local next = items[i+1]
			if prev and next then
				next.form = prev.form
				next.text = formatItem(next.raw, next.form, lang)
			end
		end
	end

	-- Join with commas/spaces
	local out = {}
	for i, it in ipairs(items) do
		if i == 1 then
			table.insert(out, it.text)
		else
			local prev = items[i-1]

			if it.raw == "_" then
				table.insert(out, " ")
			elseif specialLabels[it.raw] then
				table.insert(out, " " .. it.text)
			elseif prev and (prev.raw == "_" or specialLabels[prev.raw]) then
				table.insert(out, " " .. it.text)
			elseif it.form then
				table.insert(out, " " .. it.text)
			else
				table.insert(out, ", " .. it.text)
			end
		end
	end

	return " (" .. table.concat(out, "") .. ")"
end

-- Category logic
local function shouldAddCat(args)
	local nocat = args and args.nocat or ""
	local title = mw.title.getCurrentTitle()
	if nocat == "1" then return false end
	if title.namespace ~= 0 and title.nsText ~= "Tèmplokan" then return false end
	return true
end

-- POS builder
local function buildPOS(frame, pos)
	local args = frame.args or {}
	local lang = args[1] or "id"
	local class = args.class or ""
	local sublemma = args.sub == "1"

	local labelFormStr = buildLabelForm(getLabelFormParams(args), lang)

	-- Categories
	local catString = ""
	if shouldAddCat(args) then
		local cats = {}
		if sublemma then
			table.insert(cats, "[[Bangsaan:" .. lang .. ":Anakpentol kata]]")
			table.insert(cats, "[[Bangsaan:" .. lang .. ":" .. posTypes[pos] .. " blèngkokan]]")
		else
			table.insert(cats, "[[Bangsaan:" .. lang .. ":Pentol kata]]")
			table.insert(cats, "[[Bangsaan:" .. lang .. ":" .. posTypes[pos] .. "]]")
		end
		if class ~= "" then
			table.insert(cats, "[[Bangsaan:" .. lang .. ":" .. posTypes[pos] .. " " .. mw.ustring.lower(class) .. "]]")
		end
		catString = table.concat(cats, "")
	end

	-- POS display with background and tooltip
	local langname = langModule.getLangName{ args = { lang, nocap = "1" } }
	local posDisplay = string.format(
		'<span style="background-color:#FFFFCC; padding:0 0.3em; color:#333333;" title="%s dalem %s"><span style="font-variant:small-caps">%s</span></span>',
		posTypes[pos], langname, mw.ustring.lower(posTypes[pos])
	)

	-- Class display (unwrapped)
	local classDisplay = (class ~= "" and " <span style=\"font-variant:small-caps; color:#333333;\">" .. mw.ustring.lower(class) .. "</span>") or ""

	return posDisplay .. classDisplay .. labelFormStr .. catString
end

-- POS entry points
for code, _ in pairs(posTypes) do
	p[code] = function(frame) return buildPOS(frame, code) end
end

return p