Jump to content

Module:Alternative forms

Frae Wikipedia, the free beuk o knawledge
local export = {}
local m_link = require('Module:links')
local m_languages = require("Module:languages")
local m_debug = require("Module:debug")

-- See if the language's dialectal data module has a label corresponding to the dialect argument.
function export.getLabel(dialect, dialect_data)
	local data = dialect_data[dialect] or ( dialect_data.labels and dialect_data.labels[dialect] )
	local alias_of = ( dialect_data.aliases and dialect_data.aliases[dialect] )
	if not data then
		if alias_of then
			data = dialect_data[alias_of] or ( dialect_data.labels and dialect_data.labels[alias_of] )
		end
	end
	if data then
		local target = data.link
		local display = data.display or dialect
		dialect = target and ('[[w:'.. target ..'|'..display..']]') or display
	end
	return dialect
end

local function make_dialects(raw, lang)
	local dialect_page = 'Module:'.. lang:getCode() ..':Dialects'
	local dialect_info = mw.title.new(dialect_page).exists and mw.loadData(dialect_page) or false
		
	local dialects = {}
	
	for _, dialect in pairs(raw) do
		table.insert(dialects, dialect_info and export.getLabel(dialect, dialect_info) or dialect)
	end
	
	return dialects
end

local function track(args, arg, number)
	if args and args[arg] and args[arg][number] then
		m_debug.track("alter/" .. arg)
	end
end

function export.create(frame)
	NAMESPACE = mw.title.getCurrentTitle().nsText
	
	params = {
		[1] = { required = true },
		[2] = { required = true, list = true, allow_holes = true },
		["alt"] = { list = true, allow_holes = true },
		["id"] = { list = true, allow_holes = true },
		["tr"] = { list = true, allow_holes = true },
		["sc"] = {},
		["g"] = { list = true, allow_holes = true },
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	local lang = ( args[1] and m_languages.getByCode(args[1]) ) or ( NAMESPACE == "Template" and m_languages.getByCode("und") ) or m_languages.err(args[1], 1)
	local sc = require("Module:scripts").getByCode(args["sc"])
	
	track(args, "dial") -- [[Special:WhatLinksHere/Template:tracking/alter/dial]]
	
	local rawDialects = {}
	local links = {}
	
	local maxindex = math.max(args[2].maxindex, args.alt.maxindex, args.id.maxindex, args.tr.maxindex)

	for i = 1, maxindex do
		-- If the previous parameter was empty and we're not on the first parameter,
		-- this parameter and any others contain dialect or other labels.
		if i > 1 and not args[2][i - 1] then
			rawDialects = {unpack(args[2], i, maxindex)}
			break
		-- If there's a term, link it and add it to the list.
		elseif args[2][i] then
			track(args, "alt", i) -- [[Special:WhatLinksHere/Template:Wt/sco/tracking/alter/alt]]
			track(args, "id", i) -- [[Special:WhatLinksHere/Template:Wt/sco/tracking/alter/id]]
			track(args, "tr", i) -- [[Special:WhatLinksHere/Template:Wt/sco/tracking/alter/tr]]
			track(args, "sc", i) -- [[Special:WhatLinksHere/Template:Wt/sco/tracking/alter/sc]]
			track(args, "g", i) -- [[Special:WhatLinksHere/Template:Wt/sco/tracking/alter/g]]
			term = m_link.full_link{
					lang = lang,
					sc = sc,
					term = args[2][i],
					alt = args.alt[i],
					id = args.id[i],
					tr = args.tr[i],
					genders = args.g[i]
			}
			table.insert(links, term)
		end
	end

	local dialects  = make_dialects(rawDialects, lang)
	
	local output = { table.concat(links, ', ') }
	if #dialects > 0 then
		if lang:hasTranslit() then
			dialect_label = " – ''" .. table.concat(dialects, ", ") .. "''" 
		else
			dialect_label = " (''" .. table.concat(dialects, ", ") .. "'')"
		end
		
		-- Fixes the problem of '' being added to '' at the end of last dialect parameter
		dialect_label = mw.ustring.gsub(dialect_label, "''''", "")
		table.insert(output, dialect_label)
	end
	
	return table.concat(output)
end

function export.categorize(frame)
	local content = {}
	
	local title = mw.title.getCurrentTitle()
	local titletext = title.text
	local namespace = title.nsText
	local subpagename = title.subpageText
	
	-- subpagename ~= titletext if it is a documentation page
	if namespace == "Module" and subpagename == titletext then
		local langCode = mw.ustring.match(titletext, "^([^:]+):")
		local lang = m_languages.getByCode(langCode) or error('"' .. langCode .. '" is not a valid language code.')
		content.canonicalName = lang:getCanonicalName()
		
		local categories =
[=[
[[Category:Wt/sco/<canonicalName> modules|dialects]]
[[Category:Wt/sco/Dialectal data modules|<canonicalName>]]
]=]
		local function addContent(item)
			return content[item]
		end
		
		categories = mw.ustring.gsub(categories, "<([^>]+)>", addContent)
		return categories
	end
end

return export