Module:Sandbox/Nardog/17

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
	local title = mw.title.new(s)
	if checkExistence then
		if not title.exists then
			return nil
		end
		local redirect = title.redirectTarget
		if redirect then
			if redirect.fragment:find('[Pp]honolog[iy]') then
				title = redirect
			else
				return nil
			end
		end
	end
	if title.fragment ~= '' then
		s = string.format('%s|%s § %s', title.fullText, title.prefixedText, title.fragment)
	end
	s = string.format('See [[%s]].', s)
	return ' ' .. curFrame:expandTemplate{
		title = '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(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

function p._row(args)
	curFrame = curFrame or mw.getCurrentFrame()
	return mw.text.jsonEncode(args) .. ','
end

function p.row(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._row(args)
end

function p._table(args)
	local root = mw.html.create('table')
		:addClass('wikitable sortable')
		:tag('tr')
			-- :tag('th')
			-- 	:addClass('unsortable')
			-- 	:attr('rowspan', 2)
			-- 	:wikitext('[[Language family|Family]]')
			-- 	:done()
			:tag('th')
				:attr('colspan', 2)
				:attr('rowspan', 2)
				:wikitext('Language')
				:done()
			:tag('th')
				:attr('colspan', 3)
				:wikitext('Example')
				:done()
			:tag('th')
				:addClass('unsortable')
				:attr('rowspan', 2)
				:wikitext('Note')
				:done()
			:done()
		:tag('tr')
			:tag('th')
				:addClass('unsortable')
				:wikitext('Orthography')
				:done()
			:tag('th')
				:addClass('unsortable')
				:wikitext('[[International Phonetic Alphabet|IPA]]')
				:done()
			:tag('th')
				:addClass('unsortable')
				:wikitext('Meaning')
				:done()
			:done()
	
	local data = mw.text.jsonDecode('[' .. args[1] .. ']', mw.text.JSON_TRY_FIXING)
	local langs, dataByLang = {}, {}
	for _, row in ipairs(data) do
		if row.lang and not row.lang_name then
			row.lang_name = getLangLink(row.lang)
		end
		row.lang_name_plain = require('Module:Delink')._delink({ row.lang_name })
		if not dataByLang[row.lang_name_plain] then
			dataByLang[row.lang_name_plain] = {}
			table.insert(langs, row.lang_name_plain)
		end
		table.insert(dataByLang[row.lang_name_plain], row)
	end
	table.sort(langs)
	
	for _, lang in ipairs(langs) do
		local rows = dataByLang[lang]
		local langName = mw.text.killMarkers(rows[1].lang_name_plain)
		local tr = root:tag('tr')
		-- local familyCell = tr:tag('td'):wikitext('Family')
		local langCell = tr:tag('td'):wikitext(rows[1].lang_name)
		local hasDialects, langCrossRef
		for _, row in ipairs(rows) do
			if row.dialect then
				hasDialects = true
				break
			end
		end
		for _, row in ipairs(rows) do
			if args.lang_crossref then
				langCrossRef = makeCrossRef(args.lang_crossref)
				break
			end
		end
		if not hasDialects then
			langCell:attr('colspan', 2)
		end
		if #rows > 1 then
			-- familyCell:attr('rowspan', #rows)
			langCell:attr('rowspan', #rows)
		end
		local prevCells = {}
		for i, row in ipairs(rows) do
			if i ~= 1 then
				tr = root:tag('tr')
			end
			local prevRow = rows[i - 1]
			if hasDialects then
				if prevCells.dialect and row.dialect == prevRow.dialect then
					incrementRowspan(prevCells.dialect)
				else
					prevCells.dialect = tr:tag('td'):wikitext(row.dialect, row.ref)
				end
			else
				langCell:wikitext(row.ref)
			end
			
			if prevCells.ortho and row.ortho == prevRow.ortho then
				incrementRowspan(prevCells.ortho)
			elseif row.ortho then
				local ortho = row.ortho
				do
					local link = row.ortho_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
						ortho = string.format('[[%s|%s]]', link, ortho)
					end
					local lang = row.ortho_lang or row.lang
					if lang then
						local langArgs = { lang, ortho }
						if lang == 'en' then
							langArgs.italic = 'yes'
						end
						ortho = curFrame:expandTemplate({
							title = 'Lang',
							args = langArgs
						})
					end
				end

				local alt = row.ortho2
				if alt then
					local link = row.ortho_link2
					if link and link ~= 'none' then
						alt = string.format('[[%s|%s]]', link, alt)
					end
					local lang = row.ortho_lang2 or row.lang
					if lang then
						alt = curFrame:expandTemplate({
							title = 'Lang',
							args = { lang, alt }
						})
					end
					ortho = ortho .. ' / ' .. alt
				end
				
				local translit = row.translit
				if translit then
					local link = row.translit_link
					if link and link ~= 'none' then
						translit = string.format('[[%s|%s]]', link, translit)
					end
					local lang = row.ortho_lang or row.lang
					if lang then
						translit = curFrame:expandTemplate({
							title = 'Transliteration',
							args = { lang, translit }
						})
					end
					ortho = ortho .. ' / ' .. translit
				end
				
				prevCells.ortho = tr:tag('td'):wikitext(ortho)
			else
				if not row.ipa and row.gloss then
					tr:tag('td')
				end
				prevCells.ortho = nil
			end
			
			if prevCells.ipa and row.ipa == prevRow.ipa then
				incrementRowspan(prevCells.ipa)
			elseif row.ipa then
				local ipa = '[' .. row.ipa .. ']'
				ipa = row.audio and curFrame:expandTemplate({
					title = 'Audio-IPA',
					args = { row.audio, ipa }
				}) or curFrame:expandTemplate({
					title = 'IPA',
					args = { ipa, lang = row.lang }
				})
				prevCells.ipa = tr:tag('td'):wikitext(ipa)
				if not row.ortho then
					prevCells.ipa:attr('colspan', 2):css('text-align', 'center')
				end
			else
				if row.ortho or row.gloss then
					tr:tag('td')
				else
					tr:tag('td'):attr('colspan', 3):wikitext(
						curFrame:expandTemplate({ title = 'Example needed' })
					)
				end
				prevCells.ipa = nil
			end
			
			if prevCells.gloss and row.gloss == prevRow.gloss then
				incrementRowspan(prevCells.gloss)
			elseif row.gloss then
				prevCells.gloss = tr:tag('td'):wikitext("'" ..row.gloss .. "'")
			else
				if row.ortho or row.ipa then
					tr:tag('td')
				end
				prevCells.gloss = nil
			end
			
			local crossRef = makeCrossRef(row.crossref) or langCrossRef
			if not crossRef and not langCrossRef then
				langCrossRef = makeCrossRef(langName .. ' phonology', true)
				crossRef = langCrossRef
			end
			local note = (row.note or '') .. (crossRef or '')
			if prevCells.note and row.note == prevRow.note then
				incrementRowspan(prevCells.note)
			else
				prevCells.note = tr:tag('td'):wikitext(note)
			end
			row.note = note ~= '' and note
		end
	end
	
	root:tag('tr')
		:tag('td')
			:attr('colspan', 7)
			:tag('pre')
				:wikitext(mw.text.nowiki(mw.dumpObject(data)))
				:done()
			:done()
		:done()
	return root
end

function p.table(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._table(args)
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.