Module:usex

Definition from Wiktionary, the free dictionary
Jump to: navigation, search
Text-x-generic with pencil.svg This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.

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

function export.format_usex(lang, sc, usex, translation, transliteration, noenum, inline, ref, quote)
	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
	
	-- tr=- means omit transliteration altogether
	if transliteration == "-" then
		transliteration = nil
	else
		-- Try to auto-transliterate
		if not transliteration and usex then
			transliteration = lang:transliterate(require("Module:links").remove_links(usex), sc)
		end
		
		-- If there is still no transliteration, then add a cleanup category
		if not transliteration and needs_translit[lang] then
			table.insert(categories, lang:getCanonicalName() .. " terms needing transliteration")
		end
	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 " .. (quote and "quote" or "usage example") .. ")</small>"
	end
	
	local result = ""
	
	if noenum then
		result = "\n: " .. result
	end
	
	if not usex then
		usex = "<small>(please add the primary text of this " .. (quote and "quote" or "usage example") .. ")</small>"
		-- TODO: Trigger some kind of error here
	else
		if mw.ustring.find(usex, "[[", nil, true) then
			usex = require("Module:links").language_link({term = usex, lang = lang}, false)
		end
		
		usex = 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 (not quote) and (sc:getCode() == "Latn" or lang:getCode() == "und") then
			usex = "<i>" .. usex .. "</i>"
		end
	end
	
	if inline then
		result = result .. usex .. "&lrm;" .. (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 .. "&lrm;" .. (ref or "")
	
	if transliteration or translation then
		result = result .. "<dl>"
		
		if transliteration then
			if lang:getCode() == "ja" then
				result = result .. "<dd>" .. transliteration .. "</dd>"
			else
				result = result .. "<dd><i>" .. transliteration .. "</i></dd>"
			end
		end
		
		if translation then
			result = result .. "<dd>" .. translation .. "</dd>"
		end
		
		result = result .. "</dl>"
	end
	
	return result
end

return export