Module:User:Surjection/script utilities/tag text

From Wiktionary, the free dictionary
Jump to navigation Jump to search

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
	
	track(text, lang, sc)
	
	-- Replace space characters with newlines in Mongolian-script text, which is written top-to-bottom.
	if sc:getDirection() == "down" and text:find(" ") then
		text = require("Module:munge_text")(text, function(txt)
			-- having extra parentheses makes sure only the first return value gets through
			return (txt:gsub(" +", "<br>"))
		end)
	end

	-- Hack Korean script text to remove hyphens.
	-- This should be handled in a more general fashion, but needs to
	-- be efficient by not doing anything if no hyphens are present, and currently this is the only
	-- language needing such processing.
	if sc:getCode() == "Kore" and text:find("%-") then
		text = require("Module:munge_text")(text, function(txt)
			-- having extra parentheses makes sure only the first return value gets through
			return (txt:gsub("%-", ""))
		end)
	end
	
	if sc:getCode() == "Imag" then
		face = nil
	end

	if face == "hypothetical" then
	-- [[Special:WhatLinksHere/Wiktionary:Tracking/script-utilities/face/hypothetical]]
		require("Module:debug/track")("script-utilities/face/hypothetical")
	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 = "&lrm;"
	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