Module:U:de:unadapted

This module powers {{U:de:unadapted}}.


local export = {}

local lang = require("Module:languages").getByCode("de")
local m_links = require("Module:links")

local function bind_second(f, second)
	return function (x) return f(x, second) end
end

local function discard(offset, iter, obj, index)
	return iter, obj, index + offset
end

local function get_link(target)
	return m_links.full_link({ term = target, lang = lang }, "term")
end

local function get_link_if_exists(target)
	local content = mw.title.new(target):getContent()
	-- White space before and after the language name is not supported.
	if content ~= nil and content:find("==" .. lang:getCanonicalName() .. "==", 1, true) then
		return m_links.full_link({ term = target, lang = lang }, "term")
	else
		return m_links.full_link({ alt = target, lang = lang }, "term")
	end
end

local function get_transformed_interfixed_form(parts, transform, interfix)
	local result = parts[1]
	for i, part in discard(1, ipairs(parts)) do
		result = result .. interfix .. transform(part)
	end
	return result
end

local function get_solid_form(parts)
	return get_transformed_interfixed_form(parts, function (part) return part:gsub("^%u", string.lower) end, "")
end

local function get_capitalizer_or_id(capitalize)
	if capitalize then
		return function (part) return part:gsub("^%l", string.upper) end
	else
		return function (part) return part end
	end
end

local function get_hyphenated_form(parts, capitalize)
	return get_transformed_interfixed_form(parts, get_capitalizer_or_id(capitalize), "-")
end

local function get_spaced_form(parts, capitalize)
	return get_transformed_interfixed_form(parts, get_capitalizer_or_id(capitalize), " ")
end

function export.show(frame)
   	local args = require "Module:parameters".process(frame:getParent().args, {
		[1] = { required = true },
		[2] = {},
		["ref"] = { default = true, type = "boolean" },
	})

	local type = args[1]
	local title = args[2] or mw.title.getCurrentTitle().text
	local parts = {}
	for part in title:gmatch("[^ -]+") do
		table.insert(parts, part)
	end

	local ref = ""
	if args["ref"] then
		ref = frame:preprocess("<ref name=\"Duden Fremdwörter\">{{cite-web|de|title=Schreibung von Fremdwörtern aus dem Englischen|trans-title=Spelling of loan words from English|url=https://www.duden.de/sprachwissen/sprachratgeber/Schreibung-von-Fremdwortern-aus-dem-Englischen|archivedate=2023-01-20|archiveurl=https://web.archive.org/web/20230120090337/https://www.duden.de/sprachwissen/sprachratgeber/Schreibung-von-Fremdwortern-aus-dem-Englischen|work={{w|Duden}}|publisher={{w|Cornelsen Verlag GmbH|lang=de}}}}</ref>")
	end
	
	local types = {
		["nn"] = { -- noun+noun
			content = "consisting of two nouns be written solid$z or, if it improves legibility, hyphenated$h. The spaced spelling$S is [[Appendix:Glossary#proscribed|proscribed]]",
			do_links = #parts == 2,
			capitalize = true,
		},
		["'an"] = { -- adj+noun with stress on the adjective
			content = "consisting of an adjective (that carries the stress) and a noun be written solid$z or, alternatively, spaced$s. The hyphenated spelling$H is [[Appendix:Glossary#proscribed|proscribed]]",
			do_links = #parts == 2,
			capitalize = true,
		},
		["a'n"] = { -- adj+noun with stress on the noun
			content = "consisting of an adjective and a noun (that carries the stress) be written spaced$s. The hyphenated$H and solid$Z spellings are [[Appendix:Glossary#proscribed|proscribed]]",
			do_links = #parts == 2,
			capitalize = true,
		},
		["vp"] = { -- verb+particle/preposition
			content = "consisting of a verb and a particle be written solid$z or, alternatively, hyphenated$h. The spaced spelling$S is [[Appendix:Glossary#proscribed|proscribed]]",
			do_links = #parts == 2,
			capitalize = false,
		},
	}
	
	local type_data = types[type]
	if type_data == nil then
		local types_str = ""
		for type, _ in pairs(types) do
			types_str = types_str .. type .. ", "
		end
		error("Unrecognized type \"" .. type .. "\"; recognized types are: " .. types_str:sub(1, -3))
	end
	
	local content = type_data.content
	for _, t in ipairs({ { string.lower, get_link }, { string.upper, get_link_if_exists } }) do
		for c, g in pairs({ ["z"] = get_solid_form, ["h"] = bind_second(get_hyphenated_form, type_data.capitalize), ["s"] = bind_second(get_spaced_form, type_data.capitalize) }) do
			content = content:gsub("%$" .. (t[1])(c), type_data.do_links and (" (" .. (t[2])(g(parts)) .. ")") or "")
		end
	end
	
	return "The [[w:Duden|Duden]] prescribes that [[:Category:German unadapted borrowings from English|unadapted borrowings from English]] " .. content .. "." .. ref
end

return export