Open main menu

Documentation for this module may be created at Module:Emoji/doc

local p = {}

function p.emojiFileName(theme, unicode)
	-- file names according to theme and unicode
	if theme == 'noto' then return 'Emoji u' .. string.lower(string.gsub(unicode, '_', ' ')) .. '.svg'
	elseif theme == 'twitter' then return 'Twemoji2 ' .. string.lower(string.gsub(unicode, '_', '-')) .. '.svg'
	elseif theme == 'fx' then return 'Fxemoji u' .. string.upper(unicode) .. '.svg'
	elseif theme == 'one' then return 'Emojione ' .. string.upper(string.gsub(unicode, '_', '-')) .. '.svg'
	elseif theme == 'one-bw' then return 'Emojione BW ' .. string.upper(string.gsub(unicode, '_', '-')) .. '.svg'
	elseif theme == 'phantom' then return 'Phantom Open Emoji ' .. string.lower(unicode) .. '.svg'
	else return 'unknown' end
end

function p.emojiName(unicode)
	-- get emoji name from unicode
	if (unicode ~= nil and unicode ~= '') then
		local emojiData = mw.text.jsonDecode(mw.title.new('Data:Emoji/List.tab'):getContent())
		for i, emoji in pairs(emojiData['data']) do
			if emoji[2] == string.lower(unicode) then
				emojiNameTxt = emoji[3]['en']
			end
		end
		if emojiNameTxt ~= nil then
			return emojiNameTxt
		else return ''
		end
	else return ''
	end
end

function p.emojiNameFrame(frame)
	local unicode = frame.args.unicode
	return p.emojiInsertMain(unicode)
end

function p.emojiInsert(theme, unicode, size, strictness, title)

	-- set title
	if title == name
		then 
			name = p.emojiName(unicode)
			title = name
	else
		title = ''
	end
	
	-- set default theme
	if (theme == nil or theme == '') then theme = twitter end
	-- set default size
	if (size == nil or size == '') then size = 16 end
	-- set default unicode (❎)
	if (unicode == nil or unicode == '') then
		unicode = '274e'
		title = 'Unicode not set'
	end

	-- for multiple characters emoji, get all the code points in an array
	local codePoints = mw.text.split(unicode,'_',true)

	-- write html entities of the emoji
	local htmlEnt = ''
	for i in pairs(codePoints) do
		htmlEnt = htmlEnt .. '&#x' .. codePoints[i] .. ';'
	end

	-- wrapping
	local opening = '<span class="emoji" style="font-size:' .. size .. 'px">'
	local closing = '</span>'

	-- get file name
	local fileName = p.emojiFileName(theme, unicode)

	-- check if the file exists (expensive call!)
	local fileExists = mw.title.makeTitle('File', fileName).file.exists
	-- temporarily assume that the file exists
	-- local fileExists = true

	-- if the theme is set to 'none' or is unknown, display html entities
	if (theme == 'none' or fileName == 'unknown')
		then return opening .. htmlEnt .. closing

	-- if the file doesn’t exist and the theme choice is loose, display html entities
	elseif (fileExists == false and strictness ~= strict)
		then return opening .. htmlEnt .. closing

	-- if the file doesn’t exist and the theme choice is strict, don’t display anything
	elseif  (fileExists == false and strictness == strict)
		then return opening .. '<!-- emoji not available -->' .. closing

	-- in all the other cases, insert the emoji image
	else
		-- insert image in wikitext
		local fileInsert= '[[File:' .. fileName .. '|' .. size .. 'px|alt=' .. htmlEnt .. '|' .. title .. ']]'
		return opening .. fileInsert .. closing
	end

end

function p.emojiInsertFrame(frame)
	local theme = frame.args.theme
	local unicode = frame.args.unicode
	local size = frame.args.size
	local strictness = loose
	local title = name
	return p.emojiInsert(theme, unicode, size, strictness)
end

function p.emojiList(frame)
	-- list and displays all emoji of a theme
	local theme = frame.args.theme
	local emojiData = mw.text.jsonDecode(mw.title.new('Data:Emoji/List.tab'):getContent())
	local list = ''
	for i, emoji in pairs(emojiData['data']) do
		local list = list .. p.emojiInsert(theme, emoji[2], '', strict, none)
	end
	return list
end

return p