Pergi ke kandungan

Modul:pendokumenan/entry name

Daripada Wikikamus
local export = {}

local function fake_frame(args, parent_args)
	return {
		args = args,
		getParent = function()
			return {
				args = parent_args,
			}
		end
	}
end

function get_by_code(code)
	return require "Modul:languages".getByCode(code, nil, false, true) or require "Modul:scripts".getByCode(code)
end

local function get_code_from_title_without_namespace(title_without_namespace)
	local prefix = title_without_namespace:match("^(.+)%-entryname%f[/%z]")
	if not prefix then
		error("Base segment of title should end in -entryname: " .. title_without_namespace)
	end
	local code = prefix
	local lang_or_family_or_script = get_by_code(code)
	
	return code, lang_or_family_or_script
end

function export.documentation(title_without_namespace, explanation)
	local code, lang_or_family_or_script = get_code_from_title_without_namespace(title_without_namespace)
	return export.documentation_from_code(code, explanation, title_without_namespace)
end

function export.documentation_from_code(code, explanation, title_without_namespace)
	local lang_or_family_or_script = get_by_code(code)
	
	if not lang_or_family_or_script then
		return "Language code in page name (<code>" .. code .. "</code>) not recognized."
	end
	
	local category_name = lang_or_family_or_script:getCategoryName()
	
	local entry_name_input
	if lang_or_family_or_script:hasType("script") then
		entry_name_input = "teks dalam [[:Kategori:" .. category_name .. "|" .. category_name .. "]]"
	elseif lang_or_family_or_script:hasType("family") then
		entry_name_input = "teks dalam salah satu [[:Kategori:" .. category_name .. "|" .. category_name .. "]]"
	else -- language
		entry_name_input = "[[:Kategori:" .. category_name .. "|" .. category_name .. "]] text"
	end
	
	return "Modul ini akan menjana nama masukan untuk " .. entry_name_input
		.. (explanation and " " .. explanation or "")
		.. ". "
		.. require "Modul:pendokumenan".entryNameModuleLangList({args = { [1] = title_without_namespace:gsub("/doc", "") }})
		.. [=[

Modul ini sebaiknya tidak dipanggil terus dari templat atau modul lain.
Untuk menggunakannya daripada templat, gunakan <code>{{[[Templat:entryname|entryname]]}}</code>.
Dalam modul, gunakan [[Modul:languages#Language:makeEntryName]].

Untuk kes ujian, sila lihat [[Modul:]=] .. title_without_namespace:gsub("/doc$", "") .. [=[/testcases]].

== Fungsi ==
; <code>makeEntryName(text, lang, sc)</code>
: Menjana nama entri untuk sekeping <code>text</code> yang diberikan yang ditulis dalam skrip yang ditentukan oleh kod <code>sc</code> dan bahasa yang ditentukan oleh kod <code>lang</code>.
: Apabila penjanaan nama masukan gagal, mengembalikan <code>nil</code>.]=]
		.. require "Modul:module categorization".categorize(fake_frame({
				is_template = "1",
				[1] = title_without_namespace,
			}, {
				[1] = code,
			}))
end

function export.documentation_template(frame)
	-- Parameters to {{entry name module documentation}}:
	-- |code|description
	-- Ignore code because we get it from the page name.
	local pagename = mw.title.getCurrentTitle().text
	local args = frame:getParent().args
	if args[1] and get_code_from_title_without_namespace(pagename) ~= args[1] then
		-- [[Special:WhatLinksHere/Wiktionary:Tracking/entry name/input different from title]]
		require("Modul:debug").track("entry name/input different from title")
	end
	if args[1] then
		return export.documentation_from_code(args[1], args[2], pagename)
	else
		return export.documentation(pagename, args[2])
	end
end

return export