Module:IPA/category documentation/sandbox

From OODA WIKI

Implements Template:IPA language category.



require('strict')

local p = {}
local data = mw.loadData('Module:IPA/data')
local mLang = require('Module:Lang')

function p.main(frame)
	local args = {}
	for k, v in pairs(frame:getParent().args) do
		if v ~= '' then
			args[k] = v
		end
	end
	local langName = mw.title.getCurrentTitle().text:sub(12, -5)
	local langCode, regionCode, langData, dialectData
	local fullLangCode = args.code
	if not fullLangCode then
		for code, lang in pairs(data.langs) do
			if lang.name == langName then
				fullLangCode = code
				langCode = code
				langData = lang
				break
			end
			if lang.dialects then
				for diaCode, dialect in pairs(lang.dialects) do
					if dialect.name == langName then
						fullLangCode = code .. '-' .. diaCode
						langCode = code
						regionCode = diaCode
						langData = lang
						dialectData = dialect
						break
					end
				end
				if fullLangCode then
					break
				end
			end
		end
	end
	if not fullLangCode then
		fullLangCode = mLang._tag_from_name({ langName })
		if fullLangCode:sub(1, 5) == '<span' then
			fullLangCode = nil
		end
	end
	if fullLangCode then
		langCode = fullLangCode:gsub('%-.*', '')
		regionCode = fullLangCode:match('%-(.+)')
		regionCode = regionCode and regionCode:upper()
		langData = data.langs[langCode]
		if langData and langData.dialects and regionCode then
			dialectData = langData.dialects[regionCode]
		end
	end
	local isCollective = langName:sub(-10) == ' languages'
	local langLink = dialectData and dialectData.link or
		langData and langData.link or
		isCollective and langName or
		langName .. ' language'
	local key = dialectData and dialectData.key or langData and langData.key
	local langCat, langCatCount
	if fullLangCode then
		langCat = mLang._category_from_tag({ fullLangCode })
		if langCat:sub(1, 5) == '<span' then
			langCat = nil
		else
			langCatCount = mw.site.stats.pagesInCategory(langCat:sub(10), 'pages')
		end
	end
	local parentLangName = args.parent or
		regionCode and langData.name or
		mLang._name_from_tag({ langCode })
	if parentLangName == langName or
		parentLangName and parentLangName:sub(1, 5) == '<span'
	then
		parentLangName = nil
	end
	mw.log(parentLangName)
	local cat = args.cat ~= 'no' and (
		parentLangName and 'Pages with ' .. parentLangName .. ' IPA' or
			'Pages with IPA'
	)
	local sortkey = args.sort or
		langName:find('^[ %l]+$') and ' ' .. langName or
		langName
	local ret = {
		string.format(
			'The following pages contain [[International Phonetic Alphabet|IPA]] transcriptions of [[%s|%s]] using %s. This category should never be added manually.',
			langLink,
			langName,
			fullLangCode
				and string.format('<code>{{[[Template:IPA|IPA]]|%s|...}}</code>', fullLangCode)
				or '[[Template:IPA]]'
		)
	}
	if key then
		table.insert(ret, string.format(
			'By default, the transcriptions are linked to the [[%s]] key. Transcriptions that do not adhere to the conventions of the key must have <code>|generic=yes</code>.',
			key
		))
	end
	if isCollective then
		table.insert(ret, string.format(
			'The %s are a language collective. Please replace the code%s with those of more specific languages where possible.',
			langName,
			fullLangCode and ' (' .. fullLangCode .. ')'
		))
	end
	if not fullLangCode then
		table.insert(ret, string.format(
			'<small>Documentation error: language code for "%s" not found.</small>',
			langName
		))
	end
	if args.content then
		table.insert(ret, args.content)
	end
	if langCat and langCatCount ~= 0 then
		table.insert(ret, string.format(
			'==See also==\n*[[:%s]] (%s)',
			langCat,
			mw.language.new('en'):formatNum(langCatCount)
		))
	end
	if cat then
		table.insert(ret, string.format('[[Category:%s|%s]]', cat, sortkey))
	end
	return table.concat(ret, '\n\n')
end

return p