Module:User:Jberkel/usex

local translit_data = mw.loadData("Module:translations/data") local needs_translit = translit_data[2] local export = {} -- microformat2 classes, see https://phabricator.wikimedia.org/T138709 local class = { container_ux = 'h-usage-example', container_quotation = 'h-quotation', example = 'e-example', quotation = 'e-quotation', translation = 'e-translation', transliteration = 'e-transliteration', literally = 'e-literally' }

function export.format_usex(lang, sc, usex, translation, transliteration, noenum, inline, ref, quote, lit, qualifier) if lit then lit = "(literally, “" .. span(class.literally, lit) .. "”)" end

local example_type = quote and "quote" or "usage example" local categories = {}

if not sc then sc = require("Module:scripts").findBestScript(usex, lang) end

-- temporary category for japanese if transliteration and (string.find(transliteration, " ") or string.find(transliteration, " ")) 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 if transliteration then local tag = lang:getCode == "ja" and "span" or "i" transliteration = wrap(tag, class.transliteration, transliteration) end

if translation then translation = span(class.translation, translation) elseif lang:getCode ~= "en" and lang:getCode ~= "und" then -- add trreq category if translation is unspecified and language is not english or undetermined table.insert(categories, "Translation requests (" .. lang:getCanonicalName .. ")")		translation = " (please add an English translation of this " .. example_type .. ") " end

if usex then if mw.ustring.find(usex, "[[", nil, true) then			usex = require("Module:links").language_link({term = usex, lang = lang}, false)		end		local italicize = (not quote) and (				sc:getCode:find("Latn", nil, true) or				sc:getCode == "Latinx" or				lang:getCode == "und")		local face = italicize and "term" or nil		local class = quote and class.quotation or class.example		usex = require("Module:script utilities").tag_text(usex, lang, sc, face, class)	else		-- TODO: Trigger some kind of error here		usex = " (please add the primary text of this " .. example_type .. ") "	end	local result = usex .. "&lrm;" 	if qualifier then		result = result .. ' ' .. require("Module:User:Jberkel/qualifier").format_qualifier(qualifier)	end	result = result .. (ref or "")	if inline then		if transliteration then			result = result .. " ― " .. transliteration		end

if translation then result = result .. " ― " .. translation end

if lit then result = result .. " " .. lit end elseif transliteration or translation or lit then result = result .. ""

if transliteration then result = result .. "" .. transliteration .. "" end

if translation then result = result .. "" .. translation .. "" end

if lit then result = result .. "" .. lit .. "" end

result = result .. "" end local class = quote and class.container_quotation or class.container_ux result = div(class, result) result = result .. require("Module:utilities").format_categories(categories, lang) if noenum then result = "\n: " .. result end return result end

-- helper functions

function wrap(tag, attr, text) if text and attr then return '<' .. tag .. ' class="' .. attr .. '">' .. text .. ''	else return nil end end

function span(attr, text) return wrap('span', attr, text) end function div(attr, text) return wrap('div', attr, text) end

return export