มอดูล:alternative forms

จาก วิกิพจนานุกรม พจนานุกรมเสรี
ไปยังการนำทาง ไปยังการค้นหา
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 display = data.display or dialect
		if data.appendix then
			dialect = '[[Appendix:' .. data.appendix .. '|' .. display .. ']]'
		else
			local target = data.link
			dialect = target and '[[w:'.. target .. '|' .. display .. ']]' or display
		end
	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)
	local title = mw.title.getCurrentTitle()
	local NAMESPACE = title.nsText
	local PAGENAME = title.text
	
	local list_with_holes = { list = true, allow_holes = true }
	local params = {
		[1] = { required = true },
		[2] = { required = true, list = true, allow_holes = true },
		["alt"] = list_with_holes,
		["id"] = list_with_holes,
		["sc"] = {},
		
		["g"] = list_with_holes,
		["tr"] = list_with_holes,
		["ts"] = list_with_holes,
		["t"] = list_with_holes,
		["pos"] = list_with_holes,
		["gloss"] = { alias_of = "t" },
	}
	
	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, args.ts.maxindex)
	-- Is set to true if there is a term (entry link, alt text,
	-- transliteration, transcription) at the previous index.
	local prev = false

	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 prev then
			rawDialects = {unpack(args[2], i, maxindex)}
			break
		-- If there's an entry name or transliteration or transcription, mark it
		-- up and add it to the list.
		elseif args[2][i] or args.tr[i] or args.ts[i] then
			track(args, "alt", i) -- [[Special:WhatLinksHere/Template:tracking/alter/alt]]
			track(args, "id", i) -- [[Special:WhatLinksHere/Template:tracking/alter/id]]
			track(args, "tr", i) -- [[Special:WhatLinksHere/Template:tracking/alter/tr]]
			track(args, "ts", i) -- [[Special:WhatLinksHere/Template:tracking/alter/ts]]
			track(args, "t", i) -- [[Special:WhatLinksHere/Template:tracking/alter/t]]
			track(args, "pos", i) -- [[Special:WhatLinksHere/Template:tracking/alter/pos]]
			track(args, "sc", i) -- [[Special:WhatLinksHere/Template:tracking/alter/sc]]
			track(args, "g", i) -- [[Special:WhatLinksHere/Template:tracking/alter/g]]
			if args[2][i] and not args.alt[i] then
				args.alt[i] = args[2][i]
				args[2][i] = lang:makeEntryName(args[2][i])
			end
			if args[2][i] == PAGENAME then -- Unlink if term is pagename.
				args[2][i] = nil
			end
			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],
				ts = args.ts[i],
				gloss = args.t[i],
				pos = args.pos[i],
				genders = args.g[i]
			}
			table.insert(links, term)
			prev = true
		else
			prev = false
		end
	end
	
	-- The template must have either links or dialect labels.
	if links[1] == nil and rawDialects[1] == nil then error("No terms found!") 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:<canonicalName> modules|dialects]]
[[Category:Dialectal data modules|<canonicalName>]]
]=]
		local function addContent(item)
			return content[item]
		end
		
		categories = mw.ustring.gsub(categories, "<([^>]+)>", addContent)
		return categories
	end
end

return export