Module:User:Babr/fa-l

From Wiktionary, the free dictionary
Jump to navigation Jump to search
--[=[
Extremely primitive example module of Persian links. Might not be necessary though.

FIXME:

1. (DONE) there should be a check to make sure the words on both sides of // are the same before replacing
2. (DONE) add support for \ as an escape character
3. instead of args["tr"], it should be possible to specify args["cls"] and args["fa"] (?)
4. (DONE) allow // to specify different transliterations (?)
5. add support for {} transliterations

]=]

local export = {}

local m_links = require("Module:links")
local m_string_utils = require("Module:string utilities")
local ugsub = m_string_utils.gsub
local match = m_string_utils.match
local upper = m_string_utils.upper
local U = m_string_utils.char
local get_translit = require("Module:fa-translit").tr
local romanize_ira = require('Module:fa-IPA').romanize_ira

--characters (annoying to type)
local fatHataan = U(0x64B)
local Dammataan = U(0x64C)
local kasrataan = U(0x64D)
local fatHa = U(0x64E)
local Damma = U(0x64F)
local kasra = U(0x650)
local shadda = U(0x651)
local sukuun = U(0x652)
local dagger_alif = U(0x670)
local alif_waSl = U(0x671)
local diacritics = "([" .. fatHataan .. Dammataan .. kasrataan .. fatHa .. Damma .. kasra .. shadda .. sukuun .. dagger_alif .. alif_waSl .. "])"
local EOW = "([" .. "%s" .. "\n" .. "%p" .. "%x" .. "])"
local POW = "([^" .. "%s" .. "%p" .. "]+)"
local hyphen = "<span class=\"Zsym mention\" style=\"font-size:85%;\">/</span>"

function export.link(data, options)
	options = options or {}
	data.lang = data.lang or require'Module:languages'.getByCode'fa'
	local term_for_rom = data.term
	data.term = ugsub(data.term, "\\//", "\1")
	data.term = ugsub(data.term, "\\%-", "\2")
	data.term = ugsub(data.term, "\\{", "\3")
	if data.tr ~= '-' then
		if not data.tr then
			data.tr = m_links.remove_links(get_translit(term_for_rom))
		else 
			data.tr = ugsub(data.tr, "\\//", "\1")
			data.tr = ugsub(data.tr, "\\{", "\3")
			if data.tr:match"[âeoêô]" and not data.tr:match"\\[âeoêô]" then
				error("Cannot generate the Iranian Persian romanization as the Classical Persian romanization was not provided.")
				else
				data.tr = ugsub(data.tr, "\\([âeoêô])", "%1")
				end
			if data.tr:match".+//.+" then
			cls_tr, ir_tr = match( data.tr , "(.*)//(.*)" )
			if cls_tr == ir_tr then
				data.tr = cls_tr
				else
			data.tr = cls_tr .. hyphen .. romanize_ira(ir_tr)
			end
		else 
			cls_tr = data.tr
			ir_tr = data.tr
			cls_tr = ugsub(cls_tr, "(" .. POW .. ")" .. "{" .. POW .. "}", "%1")
			ir_tr = ugsub(ir_tr, POW .. "{(" .. POW .. ")}", "%1")
			data.tr = data.tr .. hyphen .. romanize_ira(data.tr)
			data.tr = ugsub(data.tr, "\1", "//")
			data.tr = ugsub(data.tr, "\3", "{")
			end
			data.tr = ugsub(data.tr, "\1", "//")
			data.tr = ugsub(data.tr, "\3", "{")
		end end
	data.term = ugsub(data.term, diacritics, "")
	data.term = ugsub(data.term, "%-", "")
	data.term = ugsub(data.term, "{.+}", "")
	if data.term:match".+//.+" then
		cls_term, ir_term = match( data.term , "(.*)//(.*)" )
		if cls_term == ir_term then
			data.term = cls_term
		else 
			error("The Classical Persian term and the Iranian Persian term linked to different pages. If automatic transliteration is not possible, enter a manual transliteration instead.")
		end end
	data.term = ugsub(data.term, "\1", "//")
	data.term = ugsub(data.term, "\2", "-")
	data.term = ugsub(data.term, "\3", "{")
	return m_links.full_link(data, options.face, not options.disableSelfLink)
	end

function export.show(frame)
	local boolean = {type = "boolean"}
	local plain = {}
	local args = require("Module:parameters").process(frame:getParent().args, {
		[1] = {required=true},
		[2] = plain,
		['gloss'] = plain,
		['t'] = {alias_of = 'gloss'},
		['linkto'] = {allow_empty = true},
		['tr'] = {alias_of = tr },
		['lit'] = plain,
		['pos'] = plain,
		['id'] = plain,
		['self'] = {type = "boolean", default = false},
	})

	return export.link({
	term = args[1],
	lang = require('Module:languages').getByCode('fa'),
	id = args["id"],
	tr = args[2] or args["tr"],
	gloss = args["gloss"],
	pos = args["pos"],
	lit = args["lit"],
	disableSelfLink = args["self"],
})

end

return export