Zum Inhalt springen

Modul:usex

Vun Wiktionary
Dissen Vörlaag hef dokumentatsie neudig.
Maak de dokumentatsie veur dissen modul ovver t doel en gebroek op de dokumentatsieziede.

local translit_data = mw.loadData("Module:translations/data")
local needs_translit = translit_data[2]
local export = {}

function export.display(frame)
	local args = frame:getParent().args
	
	if (frame.args["compat"] or "") ~= "" then
		args[3] = args["t"] or args["translation"] or args[2]
		args[2] = args["example"] or args[1]
		args[1] = args["lang"]
	end
	
	local lang = args[1]; if lang == "" then lang = nil end
	local sc = args["sc"]; if sc == "" then sc = nil end
	
	if (frame.args["compat"] or "") ~= "" or mw.title.getCurrentTitle().nsText == "Template" then
		lang = lang or "und"
	end

	lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	local usex = args[2]; if usex == "" then usex = nil end
	local transliteration = args["tr"] or args["transliteration"]; if transliteration == "" then transliteration = nil end
	local translation = args[3]; if translation == "" then translation = nil end
	local noenum = args["noenum"]; if noenum == "" then noenum = nil end
	local inline = args["inline"]; if inline == "" then inline = nil end
	local ref = args["ref"]; if ref == "" then ref = nil end
	
	local categories = {}
	
	if not sc then
		sc = require("Module:scripts").findBestScript(usex, lang)
	end
	
	-- temporary category for japanese
	if transliteration and (string.find(transliteration, "<br/>") or string.find(transliteration, "<br>")) then
		table.insert(categories, "usex with multiple transliterations")
	end
	
	-- auto translit
	if usex and not transliteration then
		transliteration = lang:transliterate(require("Module:links").remove_links(usex), sc)
	end
	
	-- add category if transliteration is needed
	if not transliteration and needs_translit[lang] then
		table.insert(categories, lang:getCanonicalName() .. " terms needing transliteration")
	end
	
	-- add trreq category if translation is unspecified and language is specified and not english
	if not translation and lang:getCode() ~= "en" and lang:getCode() ~= "und" then
		table.insert(categories, "Translation requests (" .. lang:getCanonicalName() .. ")")
		translation = "<small>(please add an English translation of this usage example)</small>"
	end
	
	local result = ""
	
	if noenum then
		result = "\n: " .. result
	end
	
	local usex_expanded
	
	if not usex then
		usex_expanded = '<small>(please add the primary text of this usage example)</small>'
		-- TODO: Trigger some kind of error here
	else
		usex_expanded = require("Module:script utilities").tag_text(usex, lang, sc, nil)
		
		-- italicize if script is Latn or lang is unspecified
		-- TODO: This should be done through tag_text
		if sc:getCode() == "Latn" or lang:getCode() == "und" then
			usex_expanded = "<i>" .. usex_expanded .. "</i>"
		end
	end
	
	if inline then
		result = result .. usex_expanded .. (ref or "")
		
		if transliteration then
			result = result .. " — <i>" .. transliteration .. "</i>"
		end
		
		if translation then
			result = result .. " — " .. translation
		end
		
		for key, cat in ipairs(categories) do
			result = result .. "[[Category:" .. cat .. "]]"
		end
		
		return result
	end	

	if mw.title.getCurrentTitle().fullText ~= 'Template:usex' then
		for key, cat in ipairs(categories) do
			result = result .. "[[Category:" .. cat .. "]]"
		end
	end
	
	result = result .. usex_expanded .. (ref or "")
	
	if transliteration or translation then
		result = result .. "<dl>"
		
		if transliteration then
			if lang:getCode() == "ja" then
				result = result .. "<dd>" .. require("Module:links").remove_links(transliteration) .. "</dd>"
			else
				result = result .. "<dd><i>" .. require("Module:links").remove_links(transliteration) .. "</i></dd>"
			end
		end
	
		if translation then
			result = result .. "<dd>" .. translation .. "</dd>"
		end
		
		result = result .. "</dl>"
	end
	
	return result
end

return export