The make_sortkey function replaces the letters used in Egyptian transliteration with arbitrary codepoints so that words appear with the alphabetization ꜣ j y ꜥ w b p f m n r h ḥ ḫ ẖ z s š q k g t ṯ d ḏ. It is used by Module:columns and templates such as {{der3}}, but not by categorization functions.


local export = {}

local sortkey_replacements = mw.loadData "Module:egy-utilities/data"

-- Replace Egyptian letters with a series of arbitrary codepoints that happen to
-- be in the right sequence.
function export.make_sortkey(text, lang, sc)
	text= mw.ustring.lower(text)
	text = mw.ustring.gsub(text, "[%-%s]", "")
	return mw.ustring.upper(mw.ustring.gsub(text, ".", sortkey_replacements))
end

function export.show_sort_order(frame)
	local output = {}
	
	local fun = require "Module:fun"
	
	for i, word in ipairs(frame.args) do
		output[i] = word
	end
	
	local function link(word)
		return '<span class="Latn" lang="egy">[[' .. word .. '#Egyptian|' .. word .. ']]</span>'
	end
	
	local make_sortkey = fun.memoize(export.make_sortkey)
	
	table.sort(
		output,
		function(word1, word2)
			return make_sortkey(word1) < make_sortkey(word2)
		end)
	
	return table.concat(
		fun.map(
			function(word)
				return "* " .. link(word) .. " (<code>" .. make_sortkey(word) .. "</code>)"
			end,
			output),
		"\n")
end

function export.hieroforms(frame)
	local parent_args = frame:getParent().args
	local default_title = parent_args['head'] or mw.title.getCurrentTitle().text
	default_title = 'Alternative hieroglyphic writings of '
		.. require 'Module:script utilities'.tag_text(
			default_title, require 'Module:languages'.getByCode 'egy', nil, 'term', nil)
	local tabletitle = parent_args['title'] or default_title
	local text = '<div class="NavFrame" style="clear:both;display:inline-block;">\n<div class="NavHead" style="">' .. tabletitle .. '&nbsp;&nbsp;</div>\n<div class="NavContent">\n{| style="text-align:center;"'
	local i = 1
	while parent_args[i] do
        local new_frame = frame:newChild{title = frame:getTitle(), args = {[1] = parent_args[i]}}
        local hiero = require 'Module:egy-hieroglyphs'.parameter_to_hiero(new_frame)
		text = text .. '\n| style="background-color:#efefef"|<span style="margin:0px 6px;">' .. hiero .. '</span>'
		i = i + 1
	end
	local j = 1
	local no_read = true
	local since_last = 1
	local read = 'read1'
	while j < i do
		if parent_args[read] then
			if no_read then
				text = text .. '\n|-'
				no_read = false
			end
			while since_last > 0 do
				text = text .. '\n|'
				since_last = since_last - 1
			end
			local link = require 'Module:links'.full_link( {
				lang = require 'Module:languages'.getByCode 'egy', term = parent_args[read]
			}, 'term' )
			text = text .. 'style="background-color:#efefef"|' .. link
		end
		since_last = since_last + 1
		j = j + 1
		read = 'read' .. j
	end
	j = 1
	local no_date = true
	since_last = 1
	local date = 'date1'
	while j < i do
		if parent_args[date] then
			if no_date then
				text = text .. '\n|-'
				no_date = false
			end
			while since_last > 0 do
				text = text .. '\n|'
				since_last = since_last - 1
			end
			text = text .. 'style="background-color:#efefef"|' .. frame:expandTemplate{title = 'defdate', args = {parent_args[date]}}
		end
		since_last = since_last + 1
		j = j + 1
		date = 'date' .. j
	end
	j = 1
	local no_note = true
	since_last = 1
	local since_note = 1
	local note = 'note1'
	while j < i do
		if parent_args[note] then
			if no_note then
				text = text .. '\n|- style="font-size:70%"'
				no_note = false
			end
			while since_last > 0 do
				text = text .. '\n|'
				since_last = since_last - 1
			end
			text = text .. 'style="background-color:#efefef"|' .. parent_args[note]
		end
		since_last = since_last + 1
		j = j + 1
		note = 'note' .. j
	end
	text = text .. '\n|}</div></div>'
	return text
end

return export