Jump to content

Module:IPA/category documentation/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Created page with '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...'
 
sync with live
Line 2: Line 2:


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


Line 12: Line 12:
end
end
end
end
local langName = mw.title.getCurrentTitle().text:sub(12, -5)
local name = mw.title.getCurrentTitle().text:sub(12, -5)
local lect, parent
local langCode, regionCode, langData, dialectData
local fullLangCode = args.code
local code = args.code
if not fullLangCode then
if not code then
for code, lang in pairs(data.langs) do
for _, lect2 in ipairs(lects) do
if lang.name == langName then
if (lect2.name or lect2.extName) == name then
fullLangCode = code
lect = lect2
langCode = code
parent = lect.parent
langData = lang
code = lect.code
break
break
end
end
end
if lang.dialects then
end
for diaCode, dialect in pairs(lang.dialects) do
if dialect.name == langName then
if not code then
code = mLang._tag_from_name({ name })
fullLangCode = code .. '-' .. diaCode
if code:find('^<span') then
langCode = code
regionCode = diaCode
code = nil
else
langData = lang
local regionCode = code:match('-(.+)')
dialectData = dialect
if regionCode and regionCode:sub(1, 2) ~= 'x-' then
break
code = code:sub(1, #code - #regionCode) .. regionCode:upper()
end
end
end
for _, lect2 in ipairs(lects) do
if fullLangCode then
if lect2.code == code then
lect = lect2
parent = lect.parent
break
break
end
end
Line 40: Line 43:
end
end
end
end
local isCollective = name:find(' languages$') and true
if not fullLangCode then
local link = lect and (lect.link or lect.generatedLink or lect.extLink) or
fullLangCode = mLang._tag_from_name({ langName })
parent and (parent.link or parent.generatedLink or parent.extLink) or
if fullLangCode:sub(1, 5) == '<span' then
isCollective and name or
fullLangCode = nil
name .. ' language'
end
local key = lect and lect.key or parent and parent.key
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
local langCat, langCatCount
if fullLangCode then
if code then
langCat = mLang._category_from_tag({ fullLangCode })
langCat = mLang._category_from_tag({ code })
if langCat:sub(1, 5) == '<span' then
if langCat:find('^<span') then
langCat = nil
langCat = nil
else
else
Line 70: Line 58:
end
end
end
end
local parentLangName = args.parent or
local parentName = args.parent or
regionCode and langData.name or
parent and (parent.name or parent.extName) or
mLang._name_from_tag({ langCode })
code and code:find('-') and mLang._name_from_tag({ code:gsub('%-.*', '') })
if parentLangName == langName or
if parentName == name or
parentLangName and parentLangName:sub(1, 5) == '<span'
parentName and parentName:find('^<span')
then
then
parentLangName = nil
parentName = nil
end
end
mw.log(parentLangName)
local cat = args.cat ~= 'no' and (
local cat = args.cat ~= 'no' and (
parentLangName and 'Pages with ' .. parentLangName .. ' IPA' or
parentName and 'Pages with ' .. parentName .. ' IPA' or
'Pages with IPA'
'Pages with IPA'
)
)
local sortkey = args.sort or
local sortkey = args.sort or
langName:find('^[ %l]+$') and ' ' .. langName or
name:find('^[ %l]+$') and ' ' .. name or
name
langName
local ret = {
local ret = {
string.format(
string.format(
'The following pages contain [[International Phonetic Alphabet|IPA]] transcriptions of [[%s|%s]] using %s. This category should never be added manually.',
'The following pages contain [[International Phonetic Alphabet|IPA]] transcriptions of [[%s|%s]] using %s. This category should never be added manually.',
langLink,
link,
langName,
name,
code
fullLangCode
and string.format('<code>{{[[Template:IPA|IPA]]|%s|...}}</code>', fullLangCode)
and string.format('<code>{{[[Template:IPA|IPA]]|%s|...}}</code>', code)
or '[[Template:IPA]]'
or '[[Template:IPA]]'
)
)
Line 105: Line 92:
table.insert(ret, string.format(
table.insert(ret, string.format(
'The %s are a language collective. Please replace the code%s with those of more specific languages where possible.',
'The %s are a language collective. Please replace the code%s with those of more specific languages where possible.',
langName,
name,
fullLangCode and ' (' .. fullLangCode .. ')'
code and ' (' .. code .. ')' or ''
))
))
end
end
if not fullLangCode then
if not code and args.container ~= 'yes' then
table.insert(ret, string.format(
table.insert(ret, string.format(
'<small>Documentation error: language code for "%s" not found.</small>',
'<small>Documentation error: language code for "%s" not found.</small>',
langName
name
))
))
end
end

Revision as of 13:07, 27 October 2024

require('strict')

local p = {}
local lects = mw.loadData('Module:IPA/data/export')
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 name = mw.title.getCurrentTitle().text:sub(12, -5)
	local lect, parent
	local code = args.code
	if not code then
		for _, lect2 in ipairs(lects) do
			if (lect2.name or lect2.extName) == name then
				lect = lect2
				parent = lect.parent
				code = lect.code
				break
			end
		end
	end
	if not code then
		code = mLang._tag_from_name({ name })
		if code:find('^<span') then
			code = nil
		else
			local regionCode = code:match('-(.+)')
			if regionCode and regionCode:sub(1, 2) ~= 'x-' then
				code = code:sub(1, #code - #regionCode) .. regionCode:upper()
			end
			for _, lect2 in ipairs(lects) do
				if lect2.code == code then
					lect = lect2
					parent = lect.parent
					break
				end
			end
		end
	end
	local isCollective = name:find(' languages$') and true
	local link = lect and (lect.link or lect.generatedLink or lect.extLink) or
		parent and (parent.link or parent.generatedLink or parent.extLink) or
		isCollective and name or
		name .. ' language'
	local key = lect and lect.key or parent and parent.key
	local langCat, langCatCount
	if code then
		langCat = mLang._category_from_tag({ code })
		if langCat:find('^<span') then
			langCat = nil
		else
			langCatCount = mw.site.stats.pagesInCategory(langCat:sub(10), 'pages')
		end
	end
	local parentName = args.parent or
		parent and (parent.name or parent.extName) or
		code and code:find('-') and mLang._name_from_tag({ code:gsub('%-.*', '') })
	if parentName == name or
		parentName and parentName:find('^<span')
	then
		parentName = nil
	end
	local cat = args.cat ~= 'no' and (
		parentName and 'Pages with ' .. parentName .. ' IPA' or
			'Pages with IPA'
	)
	local sortkey = args.sort or
		name:find('^[ %l]+$') and ' ' .. name or
		name
	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.',
			link,
			name,
			code
				and string.format('<code>{{[[Template:IPA|IPA]]|%s|...}}</code>', code)
				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.',
			name,
			code and ' (' .. code .. ')' or ''
		))
	end
	if not code and args.container ~= 'yes' then
		table.insert(ret, string.format(
			'<small>Documentation error: language code for "%s" not found.</small>',
			name
		))
	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