Documentation for this module may be created at မော်ဂျူး:documentation/doc

local export = {}

-- it is either here, or in [[Module:ugly hacks]], and it is not in ugly hacks.
function export.CONTENTMODEL()
	return mw.title.getCurrentTitle().contentModel	
end

local skins = {
	["common"     ] = "";
	["vector"     ] = "Vector";
	["monobook"   ] = "Monobook";
	["cologneblue"] = "Cologne Blue";
	["modern"     ] = "Modern";
}

-- this is to automatically categorize data modules and prevent cluttering [[Cat:Uncategorized modules]]
local data_modules_regex_cats = {
	["^Module:data tables/data..?.?.?$"] = "Reference module sharded data tables",
	["^Module:Quotations/...?.?.?.?.?.?$"] = "Quotation data modules",
	["^Module:zh/data/.+[a-zA-z0-9]$"] = "တရုတ် ဒေတာ မော်ဂျူးများ", -- capture submodules of zh/data that do not end with a hanzi in the title
	["^Module:zh/data/dial%-pron/"] = "Chinese dialectal pronunciation data modules",
	["^Module:zh/data/dial%-syn/"] = "Chinese dialectal synonyms data modules",
	["^Module:zh/data/glyph%-data/"] = "Chinese historical character forms data modules",
	["^Module:zh/data/ltc%-pron/"] = "Middle Chinese pronunciation data modules",
	["^Module:zh/data/och%-pron%-BS/"] = "Old Chinese (Baxter-Sagart) pronunciation data modules",
	["^Module:zh/data/och%-pron%-ZS/"] = "Old Chinese (Zhengzhang) pronunciation data modules",
	["^Module:Unicode data"] = "ယူနီကုဒ် ဒေတာ မော်ဂျူးများ",
	["^Module:number list/data/"] = "နံပါတ် ဒေတာ မော်ဂျူးများ",
	["^Module:es%-conj/data/"] = "စပိန် ဒေတာ မော်ဂျူးများ"
}

function export.show(frame)
	local params = {
		["hr"] = {},
		["for"] = {},
		["from"] = {},
		["notsubpage"] = { type = "boolean", default = false },
		["nodoc"] = { type = "boolean", default = false },
	}
	
	local args = require("Module:parameters").process(frame.args, params)
	
	local output = { '\n<div class="documentation" style="display:block; clear:both">\n' }
	local cats = {}
	
	if (not args.hr) or (args.hr == "above") then
		output[#output + 1] = "----\n"
	end
	
	local title = ((args['for'] ~= "") and args['for']) and mw.title.new(args['for']) or mw.title.getCurrentTitle()
	local doc_title = mw.title.new((args['from'] ~= "") and args['from'] or (title.fullText .. '/documentation'))
	
	local pagetype = mw.getContentLanguage():lcfirst(title.nsText) .. " စာမျက်နှာ"
	local preload

	local user_name, skin_name

	if title.contentModel == "javascript" then
		pagetype = "script"
		preload  = 'Template:documentation/preloadTemplate' -- XXX
		if title.nsText == 'User' then
			user_name = title.rootText	
		end
	elseif title.contentModel == "css" then
		pagetype = "style sheet"
		preload  = 'Template:documentation/preloadTemplate' -- XXX
		if title.nsText == 'User' then
			user_name = title.rootText	
		end
	elseif title.contentModel == "Scribunto" then
		pagetype = "မော်ဂျူး"
		user_name = title.rootText:match("^User:(.+)")
		if user_name then
			preload  = 'Template:documentation/preloadModuleSandbox'
		else
			preload  = 'Template:documentation/preloadModule'
		end
	elseif title.nsText == "တမ်းပလိတ်" then
		pagetype = "တမ်းပလိတ်"
		preload  = 'Template:documentation/preloadTemplate'
	elseif title.nsText == "ဝစ်ရှင်နရီ" then
		pagetype = "ပရောဂျက် စာမျက်နှာ"
		preload  = 'Template:documentation/preloadTemplate' -- XXX
	end
	
	local old_title
	if doc_title.isRedirect then
		old_title = doc_title
		doc_title = mw.title.new(string.match(doc_title:getContent(), "^#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]]-)%]%]"))
	end
	
	local fallback_docs

	output[#output + 1] = "<dl class=\"plainlinks\" style=\"font-size: smaller;\">"
	if doc_title.exists then
		output[#output + 1] =
			"<dd><i style=\"font-size: larger;\">ဤ[[Help:Documenting templates and modules|လက်စွဲစာတမ်း]]သည် [[" .. doc_title.fullText .. "]] တွင် တည်ရှိသည်။ " ..
			"<sup>[[" .. doc_title:fullUrl { action = 'edit' } .. " တည်းဖြတ်]]</sup> </i></dd>"
	else
		if title.contentModel == "Scribunto" then
			local automatic_cat = "ကဏ္ဍခွဲမထားသော မော်ဂျူးများ"
			if user_name then
				fallback_docs = 'documentation/fallback/user module'
				automatic_cat = "User sandbox modules"
			else
				local categorized = false
				for r, cat in pairs(data_modules_regex_cats) do 
					if mw.ustring.match(title.fullText, r) then
						automatic_cat = cat;
						break
					end
				end
			end
			cats[#cats + 1] = automatic_cat
			
			if automatic_cat == "ကဏ္ဍခွဲမထားသော မော်ဂျူးများ" and not fallback_docs then --meaning module is not in user's sandbox or on of many datamodule boring series
				cats[#cats + 1] = "လက်စွဲစာတမ်း လိုအပ်နေသော တမ်းပလိတ်များနှင့် မော်ဂျူးများ"
			end
		elseif title.nsText == "တမ်းပလိတ်" then
			--cats[#cats + 1] = "Uncategorized templates"

			if not ( fallback_docs or args.nodoc ) then
				cats[#cats + 1] = "လက်စွဲစာတမ်း လိုအပ်နေသော တမ်းပလိတ်များနှင့် မော်ဂျူးများ"
			end
		elseif (title.contentModel == "css") or (title.contentModel == "javascript") then
			if user_name then
				skin_name = skins[title.text:sub(#title.rootText + 1):match("^/([a-z]+)%.[jc]ss?$")]
				if skin_name then
					fallback_docs = 'documentation/fallback/user ' .. title.contentModel
				end
			end
		end
		
		if not args.nodoc then
			output[#output + 1] =
				"<dd><i style=\"font-size: larger;\">ဤ " .. pagetype .. "တွင် [[Help:Documenting templates and modules|လက်စွဲစာတမ်း စာမျက်နှာခွဲ]] လိုအပ်နေသည်။ " ..
				(fallback_docs and "သင့်အနေဖြင့် " or "ကျေးဇူးပြု၍ ") ..
				"[" .. doc_title:fullUrl { action = 'edit', preload = preload } .. " ဖန်တီးနိုင်သည်]။</i></dd>\n"
		end
	end
	
	if title.fullText:match("^MediaWiki:Gadget%-") then
		local is_gadget = false
		local gadget_list = mw.title.new("MediaWiki:Gadgets-definition"):getContent()
		
		for line in mw.text.gsplit(gadget_list, "\n") do
			local gadget, opts, items = line:match("^%*%s*([A-Za-z][A-Za-z0-9_%-]*)%[(.-)%]|(.+)$")
			if not gadget then
				gadget, items = line:match("^%*%s*([A-Za-z][A-Za-z0-9_%-]*)|(.+)$")
			end
			
			if gadget then
				items = mw.text.split(items, "|")
				for i, item in ipairs(items) do
					if title.fullText == ("MediaWiki:Gadget-" .. item) then
						is_gadget = true

						output[#output + 1] = "<dd> ''This script is a part of the <code>"
						output[#output + 1] = gadget
						output[#output + 1] = "</code> gadget (["
						output[#output + 1] = tostring(mw.uri.fullUrl('MediaWiki:Gadgets-definition', 'action=edit'))
						output[#output + 1] = " edit definitions])'' <dl>"
						
						output[#output + 1] = "<dd> ''Description (["
						output[#output + 1] = tostring(mw.uri.fullUrl('MediaWiki:Gadget-' .. gadget, 'action=edit'))
						output[#output + 1] = " edit])'': "
						output[#output + 1] = mw.message.new('Gadget-' .. gadget):plain()
						output[#output + 1] = " </dd>"

						table.remove(items, i)
						if #items > 0 then
							for j, item in ipairs(items) do
								items[j] = '[[MediaWiki:Gadget-' .. item .. '|' .. item .. ']]'
							end
							output[#output + 1] = "<dd> ''Other parts'': "
							output[#output + 1] = mw.text.listToText(items)
							output[#output + 1] = "</dd>"
						end

						output[#output + 1] = "</dl></dd>"

						break
					end
				end
			end
		end
		
		if not is_gadget then
			output[#output + 1] = "<dd> ''This script is not a part of any ["
			output[#output + 1] = tostring(mw.uri.fullUrl('Special:Gadgets', 'uselang=en'))
			output[#output + 1] = ' gadget] (['
			output[#output + 1] = tostring(mw.uri.fullUrl('MediaWiki:Gadgets-definition', 'action=edit'))
			output[#output + 1] = ' edit definitions]).</dd>'
		--else
		--	cats[#cats + 1] = "Wiktionary gadgets"
		end
	end	
	
	if old_title then
		output[#output + 1] = "<dd> ''Redirected from'' ["
		output[#output + 1] = old_title:fullUrl { redirect = 'no' }
		output[#output + 1] = " "
		output[#output + 1] = old_title.fullText
		output[#output + 1] = "] (["
		output[#output + 1] = old_title:fullUrl { action = 'edit' }
		output[#output + 1] = " edit]).</dd>\n"
	end
	
	local links = {}
	
	if title.isSubpage and not args.notsubpage then
		links[#links + 1] = "[[:" .. title.nsText .. ":" .. title.rootText .. "|root page]]"
		links[#links + 1] = "[[Special:PrefixIndex/" .. title.nsText .. ":" .. title.rootText .. "/|root page's subpages]]"
	else
		links[#links + 1] = "[[Special:PrefixIndex/" .. title.fullText .. "/|စာမျက်နှာခွဲ စာရင်း]]"
	end
	
	if (title.contentModel == "javascript") or (title.contentModel == "css") then
		links[#links + 1] = "[[Special:WhatLinksHere/" .. title.fullText .. "|ဘယ်ကချိတ်ဆက်ထားလဲ]]"

		if user_name then
			links[#links + 1] = "[[Special:MyPage" .. title.text:sub(#title.rootText + 1) .. "|သင့်ကိုယ်ပိုင်]]"
		end
	else
		links[#links + 1] =
			'[' .. tostring(mw.uri.fullUrl('Special:WhatLinksHere/' .. title.fullText, 'hidelinks=1&hideredirs=1')) ..
			' ထည့်သွင်းကူးယူချက်များ]'

		if title.contentModel ~= "Scribunto" then
			links[#links + 1] =
				'[' .. tostring(mw.uri.fullUrl('Special:WhatLinksHere/' .. title.fullText, 'hidelinks=1&hidetrans=1')) ..
				' ပြန်ညွှန်းများ]'
		end
	end
	
	if title.contentModel == "Scribunto" then
		if title.isSubpage and (title.subpageText == "testcases") then
			links[#links + 1] = "[[:" .. title.nsText .. ":" .. title.baseText .. "|စမ်းသပ်ထားသော မော်ဂျူး]]"
		else
			links[#links + 1] = "[[" .. title.fullText .. "/testcases|testcases]]"
		end

		if user_name then
			links[#links + 1] = "[[User:" .. user_name .. "|အသုံးပြုသူ စာမျက်နှာ]]"
			links[#links + 1] = "[[User talk:" .. user_name .. "|အသုံးပြုသူ ဆွေးနွေးချက် စာမျက်နှာ]]"
			links[#links + 1] = "[[Special:PrefixIndex/User:" .. user_name .. "/|အသုံးပြုသူနေရာ]]"
		end
	end
	
	if #links > 0 then
		output[#output + 1] =
			"<dd> ''အသုံးဝင်မည့် လင့်ခ်များ'': " .. table.concat(links, " • ") .. "</dd>"
	end
	
	output[#output + 1] = "</dl>\n"

	if doc_title.exists then
		output[#output + 1] = frame:expandTemplate { title = doc_title.fullText }
	elseif fallback_docs then
		output[#output + 1] = frame:expandTemplate {
			title = fallback_docs;
			args = {
				['user'] = user_name;
				['page'] = title.fullText;
				['skin name'] = skin_name;
			}
		}
	end

	if args.hr == "below" then
		output[#output + 1] = '\n<hr style="clear: both;" />'
	else
		output[#output + 1] = '\n<br style="clear: both;" />'
	end
	
	for _, cat in ipairs(cats) do
		output[#output + 1] = "[[Category:" .. cat .. "]]"
	end
	
	output[#output + 1] = "</div>\n"

	return table.concat(output)
end

function export.translitModuleLangList(frame)
	local output = {}
	
	local pagename, subpage
	
	if frame.args[1] then
		pagename = frame.args[1]
	else
		local title = mw.title.getCurrentTitle()
		subpage = title.subpageText
		pagename = title.text
		
		if subpage ~= pagename then
			pagename = title.rootText
		end
	end
	
	local translitModule = pagename
	
	local languageObjects = require("Module:languages/byTranslitModule")(translitModule)
	local codeInPagename = mw.ustring.match(pagename, "^(.-)%-translit")
	
	if not codeInPagename then
		return ""
	end
	
	local langs = {}
	local codeInPagenameInList = false
	local category = ""
	if #languageObjects ~= 0 and subpage ~= "documentation" then
		local agreement = #languageObjects == 1 and "" or "s"
		category = "[[Category:Transliteration modules used by " .. #languageObjects .. " language" .. agreement .. "]]"
	end
	
	for i, lang in pairs(languageObjects) do
		if lang:getCode() == codeInPagename then
			table.remove(languageObjects, i)
			codeInPagenameInList = true
		end
	end
	
	if #languageObjects == 0 then
		return category or ""
	elseif #languageObjects > 2 then
		for i, lang in pairs(languageObjects) do
			table.insert(langs, "[[:Category:" .. lang:getCanonicalName() .. " ဘာသာစကား|" .. lang:getCanonicalName() .. "]] (<code>" .. lang:getCode() .. "</code>)")
			if languageObjects[i + 2] then
				table.insert(langs, ", ")
			elseif languageObjects[i + 1] then
				table.insert(langs, '<span class="serial-comma">,</span> and ')
			end
		end
	else
		for i, lang in pairs(languageObjects) do
			table.insert(langs, "[[:Category:" .. lang:getCanonicalName() .. " ဘာသာစကား|" .. lang:getCanonicalName() .. "]] (<code>" .. lang:getCode() .. "</code>)")
			
			if languageObjects[i + 1] then
				table.insert(langs, " နှင့် ")
			end
		end
	end
	
	langs = table.concat(langs)
	
	return "It is " .. ( codeInPagenameInList and "also" or "" ) ..
		" used to transliterate " .. langs .. "." .. category
end

return export