Module:fa-translit

Revision as of 08:49, 17 August 2025 by wikt>Babr
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:fa-translit/doc

local export = {}
local m_str_utils = require("Module:string utilities")
local m_table = require("Module:table")
local concat = m_table.concat
local find = m_str_utils.find
local gsub = m_str_utils.gsub
local sub = m_str_utils.sub
local match = m_str_utils.match
local split = m_str_utils.split
local EOW = "([" .. "%s" .. "\n" .. "%p" .. "%x" .. "])"
local POW = "([" .. "%S" .. "]+)"
local hyphen = "<span class=\"Zsym mention\" style=\"font-size:100%;\">&nbsp;/ </span>"

local function basetr(text, lang, sc)
	return require("Module:fa-cls-translit").tr(text, lang, sc)
end

function s_split(text, delimiter)
    local can_split = find(text, delimiter) 
    if not can_split then
        return text, nil  
    end
    return sub(text, 1, can_split-1), sub(text, can_split+2)
end 

function export.CLS_tr(text)
	-- if // is present, ignore // and everything AFTER
	text = gsub(text, "%%", "")
	local cls, ira = s_split(text, "//")
	if cls then
	text = cls
	end
	text = basetr(text, lang, sc)
	if text == nil then
		return nil
		end
	return text
end

function export.IRA_tr(text)
	-- if // is present, ignore // and everything BEFORE
	local cls, ira = s_split(text, "//")
	if ira then
	text = ira
	end
	text = gsub(text, "%%", "#")
	text = basetr(text, lang, sc)
	if text == nil then
		return nil
	end
	text = gsub(text, "%.", "/1")
	text = gsub(text, "([^%a']+)" , "#%1#")
	text = require('Module:fa-IPA').romanize_ira(text)
	text = gsub(text, "#", "")
	text = gsub(text, "/1", ".")
	return text
end

function export.tr(text)
	cls_result = export.CLS_tr(text)
	ira_result = export.IRA_tr(text)
	if cls_result == nil or ira_result == nil then
		return nil
		else
		if cls_result == ira_result then
			return cls_result
		end
		combined = {cls_result .. hyphen .. ira_result}
		return concat(combined)
	end
end

-- I dont think anything uses this but Im too scared to remove it
function export.frame_xlit(text1, text2)
	text = text1 .. hyphen .. text2
	return text
	end

return export