Module:User:Surjection/script utilities/tag text/lite

This is a private module sandbox of Surjection, for their own experimentation. Items in this module may be added and removed at Surjection's discretion; do not rely on this module's stability.


local export = {}

-- Wrap text in the appropriate HTML tags with language and script class.
function export.tag_text(text, lang, sc, face, class, id)
	if not sc then
		sc = require("Module:scripts").findBestScript(text, lang)
	end
	
	if sc:getCode() == "Imag" then
		face = nil
	end

	local data = mw.loadData("Module:script utilities/data").faces[face or "nil"]
	if not data then
		error('Invalid script face "' .. face .. '".')
	end
	
	local post = ""
	if sc:getDirection() == "rtl" and (face == "translation" or mw.ustring.find(text, "%p$")) then
		post = "‎"
	end
	
	-- Add a script wrapper
	
	-- classes
	local result = ( data.prefix or "" ) .. '<' .. data.tag .. ' class="' .. sc:getCode()
	if data.class then
		result = result .. ' ' .. data.class
	end
	if class and class ~= '' then
		result = result .. ' ' .. class
	end
	result = result .. '"'
	
	if id then -- id
		result = result .. ' id="' .. require("Module:senseid").anchor(lang, id) .. '"'
	end
	if lang then -- lang
		result = result .. ' lang="' .. lang:getCode() .. '"'
	end
	
	return result .. '>' .. text .. '</' .. data.tag .. '>' .. post
end

return export