मॉड्यूल:transliteration module documentation

"इस मॉड्यूल हेतु प्रलेख मॉड्यूल:transliteration module documentation/doc पर बनाया जा सकता है"

local export = {}

local Array = require "Module:array"

-- Possible names:
-- lang ([[Module:grc-translit]])
-- script ([[Module:Ital-translit]])
-- lang-script ([[Module:sa-Beng-translit]])
-- script-script where module romanizes either script ([[Module:Cyrs-Glag-translit]])
-- script-script where the first script is transliterated into the second ([[Module:Deva-Kthi-translit]])
-- script-to-script ([[Module:ks-Arab-to-Deva-translit]])
-- script-something, maybe transliteration system ([[Module:Orkh-Bitig-translit]])
-- The last two are not handled here.
local patterns
do
	local lang_code = "%l[%l-]+%l"
	local sc_code = "%u%l%l%l"
	patterns = {
		{ "^(" .. lang_code .. ")%-translit$", "lang" },
		{ "^(" .. sc_code .. ")%-translit$", "sc" },
		{ "^(" .. lang_code .. ")%-(" .. sc_code .. ")%-translit$", "lang", "sc" },
		{ "^(" .. sc_code .. ")%-(" .. sc_code .. ")%-translit$", "sc1", "sc2" },
	}
end

function export.parse_title(title)
	local result = {}
	for _, vals in ipairs(patterns) do
		local pattern, key1, key2 = unpack(vals)
		local match1, match2 = title:match(pattern)
		if match1 then
			result[key1] = match1
			if key2 then
				result[key2] = match2
			end
			break
		end
	end
	return result
end

local function link_lang_cat(lang)
	return ("[[:Category:%s|%s]]")
		:format(lang:getCategoryName(), lang:getCategoryName())
end

local function page_exists(title)
	local success, title_obj = pcall(mw.title.new, title)
	return success and title_obj.exists
end

function export.translit_module_lang_list(pagename, lang_code_in_title, plain)
	local translit_module = pagename
	
	local language_objects = require("Module:languages/byTranslitModule")(translit_module)
	
	local categories = Array()
	local lang_code_in_title_in_list = false
	if lang_code_in_title then
		if language_objects[1] and subpage ~= "documentation" then
			local agreement = #language_objects == 1 and "" or "s"
			categories:insert("[[Category:Transliteration modules used by "
				.. #language_objects .. " language" .. agreement .. "]]")
		end
		
		language_objects = Array(language_objects)
			:filter(
				function (lang)
					local result = lang:getCode() ~= lang_code_in_title
					lang_code_in_title_in_list = lang_code_in_title_in_list or result
					return result
				end)
	end
	
	if subpage ~= "documentation" then
		for script_code in pagename:gmatch("%f[^-%z]%u%l%l%l%f[-]") do
			local script = require "Module:scripts".getByCode(script_code)
			if script then
				categories:insert("[[Category:" .. script:getCategoryName() .. "]]")
			end
		end
		
		if not page_exists("Module:" .. pagename .. "/testcases") then
			categories:insert("[[Category:Transliteration modules without a testcases subpage]]")
		end
	end
	
	if #language_objects == 0 then
		return plain and "" or categories:concat()
	end
	
	local langs = Array(language_objects)
		:sort(
			function(lang1, lang2)
				return lang1:getCode() < lang2:getCode()
			end)
		:map(plain and function (lang)
				return lang:getCanonicalName()
			end
			or link_lang_cat)
	
	return "It is" .. (lang_code_in_title_in_list and " also" or "") ..
		" used to transliterate "
		.. langs:serial_comma_join({ dontTag = plain }) .. "."
		.. (plain and "" or categories:concat())
end

local function make_transliteration_shortcut(lang_code)
	return mw.title.new("Wiktionary:" .. lang_code:upper() .. " TR")
end

function export.intro_text(title)
	local vals = export.parse_title(title.rootText)
	local obj, transliteration_shortcut
	local extra = ""
	if vals.lang then
		obj = require "Module:languages".getByCode(vals.lang)
		transliteration_shortcut = make_transliteration_shortcut(vals.lang)
		
		if vals.sc then
			local sc = require "Module:scripts".getByCode(vals.sc)
			
			-- This works for scripts whose categories end in "script".
			extra = " written in the " .. link_lang_cat(sc)
		end
	elseif vals.sc then
		obj = require "Module:scripts".getByCode(vals.sc)
	end
	
	local lang_list = export.translit_module_lang_list(title.rootText, vals.lang)
	
	if obj then
		return "This module will transliterate " .. link_lang_cat(obj) .. " text"
			.. extra
			.. (transliteration_shortcut and transliteration_shortcut.exists
				and " per [[WT:" .. transliteration_shortcut.text .. "]]"
				or "")
			.. "."
			.. (lang_list and " " .. lang_list or "")
	else
		error("!!!")
	end
end

function export.show(frame, title)
	return export.intro_text(mw.title.getCurrentTitle())
end

return export