Documentation for this module may be created at Module:Sandbox/Nardog/13/doc

require('strict')
local p = {}
local curFrame

local function getLangLink(s)
	return require('Module:Lang')._name_from_tag({ s, link = 'yes', --[[template = '']] })
end

local function makeCrossRef(s, checkExistence)
	if not s then
		return nil
	end
	if s == 'none' then
		return ''
	end
	if checkExistence then
		local title = mw.title.new(s)
		if title.exists then
			local redirect = title.redirectTarget
			if redirect then
				redirect = redirect.fullText
				if redirect:find('[Pp]honolog[iy]') then
					s = redirect
				else
					return nil
				end
			else
				s = title.fullText;
			end
		else
			return nil
		end
	end
	s = string.format('See [[%s]].', s)
	return ' ' .. curFrame:expandTemplate{
		title = 'Template:Crossreference',
		args = { s }
	}
end

local function incrementRowspan(cell)
	local num = cell:getAttr('rowspan')
	num = num and num + 1 or 2
	cell:attr('rowspan', num)
end

function p._main(args)
	curFrame = curFrame or mw.getCurrentFrame()
	
	local rows, hasDialects = {}
	for k, v in pairs(args) do
		if k ~= 'lang' and not k:find('^lang_') then
			local num = k:match('%d+$')
			if num then
				local i = tonumber(num)
				if not rows[i] then
					rows[i] = {}
				end
				rows[i][k:sub(1, #num * -1 - 1)] = v
			else
				if not rows[1] then
					rows[1] = {}
				end
				rows[1][k] = v
			end
			hasDialects = hasDialects or k:find('^dialect%d*$') and true
		end
	end
	mw.logObject(rows)
	
	local langName = args.lang_name
	local langLink = args.lang_link
	if langName and langLink then
		langLink = string.format('[[%s|%s]]', langLink, langName)
	elseif langName and args.lang then
		langLink = mw.ustring.gsub(
			getLangLink(args.lang),
			'|.+%]%]$',
			'|' .. langName .. ']]'
		)
	end
	langLink = langLink or args.lang and getLangLink(args.lang)
	langName = langName or mw.ustring.match(langLink, '|(.+)%]%]$')
	langLink = langLink or string.format('[[%s language|%s]]', langName, langName)
	local langCrossRef = makeCrossRef(args.lang_crossref)
	local hasLangCrossRef = langCrossRef and true
	
	local root = mw.html.create('')
	local tr = root:tag('tr')
	local langCell = tr:tag('td'):wikitext(langLink, args.lang_ref)
	if not hasDialects then
		langCell:attr('colspan', 2)
	end
	if #rows > 1 then
		langCell:attr('rowspan', #rows)
	end
	
	local prevCells, prevNote = {}
	for i, t in ipairs(rows) do
		if i ~= 1 then
			tr = root:tag('tr')
		end
		
		if hasDialects then
			if prevCells.dialect and t.dialect == 'inherit' then
				incrementRowspan(prevCells.dialect)
			else
				prevCells.dialect = tr:tag('td'):wikitext(t.dialect, t.ref)
			end
		else
			langCell:wikitext(t.ref)
		end
		
		if prevCells.word and t.word == 'inherit' then
			incrementRowspan(prevCells.word)
		elseif t.word then
			local word = t.word
			do
				local link = t.word_link
				if not link then
					local title = mw.title.new(langName .. ' orthography')
					if title.exists then
						local redirect = title.redirectTarget
						link = redirect and redirect.fullText or title.fullText
					end
				end
				if link and link ~= 'none' then
					word = string.format('[[%s|%s]]', link, word)
				end
				local lang = t.word_lang or args.lang
				if lang then
					local args = { lang, word, rtl = t.word_rtl or t.rtl }
					if lang == 'en' then
						args.italic = 'yes'
					end
					word = curFrame:expandTemplate{
						title = 'Template:Lang',
						args = args
					}
				end
			end
			
			local alt = t.word_alt
			if alt then
				local link = t.word_alt_link
				if link and link ~= 'none' then
					alt = string.format('[[%s|%s]]', link, alt)
				end
				local lang = t.word_alt_lang or args.lang
				if lang then
					alt = curFrame:expandTemplate{
						title = 'Template:Lang',
						args = { lang, alt, rtl = t.word_alt_rtl or t.rtl }
					}
				end
				word = word .. ' / ' .. alt
			end
			
			local translit = t.translit
			if translit then
				local link = t.translit_link
				if link and link ~= 'none' then
					translit = string.format('[[%s|%s]]', link, translit)
				end
				local lang = t.word_lang or args.lang
				if lang then
					translit = curFrame:expandTemplate{
						title = 'Template:Transl',
						args = { lang, translit }
					}
				end
				word = word .. ' / ' .. translit
			end
			
			prevCells.word = tr:tag('td'):wikitext(word)
		else
			if not t.ipa and t.meaning then
				tr:tag('td')
			end
			prevCells.word = nil
		end
		
		if prevCells.ipa and t.ipa == 'inherit' then
			incrementRowspan(prevCells.ipa)
		elseif t.ipa then
			local ipa = '[' .. t.ipa .. ']'
			ipa = t.audio and curFrame:expandTemplate{
				title = 'Template:Audio-IPA',
				args = { t.audio, ipa }
			} or curFrame:expandTemplate{
				title = 'Template:IPA',
				args = { ipa }
			}
			prevCells.ipa = tr:tag('td'):wikitext(ipa)
			if not t.word then
				prevCells.ipa:attr('colspan', 2):css('text-align', 'center')
			end
		else
			if t.word or t.meaning then
				tr:tag('td')
			else
				tr:tag('td'):attr('colspan', 3):wikitext(curFrame:expandTemplate{
					title = 'Template:Example needed'
				})
			end
			prevCells.ipa = nil
		end
		
		if prevCells.meaning and t.meaning == 'inherit' then
			incrementRowspan(prevCells.meaning)
		elseif t.meaning then
			prevCells.meaning = tr:tag('td'):wikitext("'" ..t.meaning .. "'")
		else
			if t.word or t.ipa then
				tr:tag('td')
			end
			prevCells.meaning = nil		
		end
		
		local crossRef = makeCrossRef(t.crossref) or langCrossRef
		if not crossRef and not hasLangCrossRef then
			langCrossRef = makeCrossRef(langName .. ' phonology', true)
			hasLangCrossRef = true
			crossRef = langCrossRef
		end
		local note = (t.note or '') .. (crossRef or '')
		if prevCells.note and (t.note == 'inherit' or note == prevNote) then
			incrementRowspan(prevCells.note)
		else
			prevCells.note = tr:tag('td'):wikitext(note)
		end
		prevNote = note ~= '' and note
	end
	
	-- return mw.text.nowiki(tostring(root))
	return root
end

function p.main(frame)
	curFrame = frame
	local args = {}
	-- for k, v in pairs(frame:getParent().args) do
	for k, v in pairs(frame.args or frame:getParent().args) do
		if v and v ~= '' then
			args[k] = v
		end
	end
	return p._main(args)
end

return p