Jump to content

Module:ar-link

From Wiktionary, the free dictionary
local export = {}

local U = require("Module:string/char")

-- Derived from Arabic data table in [[Module:languages/data/2]].
local entry_name_replacements = {
	[U(0x0671)] = U(0x0627), [U(0x064B)] = "", [U(0x064C)] = "", [U(0x064D)] = "",
	[U(0x064E)] = "", [U(0x064F)] = "", [U(0x0650)] = "", [U(0x0651)] = "",
	[U(0x0652)] = "", [U(0x0670)] = "", [U(0x0640)] = "",
}

local function make_entry_name(text)
	return (text:gsub("[%z\1-\127\194-\244][\128-\191]*", entry_name_replacements))
end

local function link(entry, text)
	return '<span class="Arab" lang="ar">[['
		.. make_entry_name(entry)
		.. '#Arabic|' .. (text or entry) .. ']]</span>&lrm;'
end



function export.link(frame)
	local text = frame.args[1]
	
	if not text then
		return nil
	end
	
	local transliterate = require "Module:fun".memoize(require "Module:ar-translit".tr)
	local open_paren = ' <span class="mention-gloss-paren annotation-paren">(</span><span class="tr Latn" xml:lang="ar-Latn" lang="ar-Latn">'
	local close_paren = '</span><span class="mention-gloss-paren annotation-paren">)</span>'
	
	return (text
		:gsub(
			"%[%[([^%]]+)%]%]",
			function (link_text)
				local entry, text = link_text:match("^([^|]+)|(.+)$")
				entry, text = entry or link_text, text or link_text
				local translit = transliterate(text)
				if translit then
					return link(entry, text)
						.. open_paren .. translit .. close_paren
				else
					return link(link_text)
				end
			end))
end

return export