Module:IPA/overview: Difference between revisions

Content deleted Content added
improve main language detection
use same data for both tables
Line 1:
require('strict')
local p = {}
 
local data = mw.title.getCurrentTitle().subpageText == 'sandbox'
and mw.loadData('Module:IPA/data/sandbox')
or mw.loadData('Module:IPA/data')
local lects = mw.loadData('Module:IPA/data/export')
local p = {}
 
local function getNameAndLinkaddNotes(codecell, notes)
if #notes > 0 then
local res = require('Module:Lang')._name_from_tag({ code, link = 'yes' })
cell:wikitext(
local name = res:match('([^%[|%]]+)%]%]$')
string.format(' <small>(%s)</small>', table.concat(notes, '; '))
local link = res:match('^%[%[([^|%]]+)')
)
return name, link
end
 
local function addNotes(notes)
return #notes == 0 and '' or
string.format(' <small>(%s)</small>', table.concat(notes, '; '))
end
 
local function formatName(internal, external, text)
local notes = {}
if internal then
if internal == external then
table.insert(notes, 'redundant')
elseif external then
table.insert(notes, string.format('overrides "%s"', external))
end
end
local s = internal or external
if text then
table.insert(notes, string.format('label: "%s"', text))
end
return s .. addNotes(notes)
end
 
local function formatLink(internal, external, name)
local notes = {}
local generated = not internal and name and name ~= '' and name .. ' language'
if internal or generated then
if (internal or generated) == external then
table.insert(notes, 'redundant')
elseif external then
local note = string.format('overrides [[%s]]', external)
local intTitle = mw.title.new(internal or generated)
local intRedir = intTitle.redirectTarget
intTitle = intRedir or intTitle
local extTitle = mw.title.new(external)
local extRedir = extTitle.redirectTarget
extTitle = extRedir or extTitle
if intTitle ~= extTitle then
note = note .. ', a different article'
end
table.insert(notes, note)
end
end
local s = generated or internal or external
s = generated and '([[' .. s .. ']])' or '[[' .. s .. ']]'
return s .. addNotes(notes)
end
 
local function organize(lects, parent, parentCode)
local t, aliases = {}, {}
for code, lect in pairs(lects) do
if lect.aliasOf then
aliases[lect.aliasOf] = aliases[lect.aliasOf] or {}
table.insert(aliases[lect.aliasOf], code)
end
end
for code, lect in pairs(lects) do
if not lect.aliasOf then
if lect.isVariant then
code = code:lower()
end
local name, link = getNameAndLink(
parent and parentCode .. '-' .. code or code
)
table.insert(t, {
code = '<code>' .. code .. '</code>',
name = formatName(lect.name, name, lect.text),
link = formatLink(lect.link, link, lect.name),
key = lect.key and '[[' .. lect.key .. ']]'
or '([[' .. (parent and parent.key or data.defaultKey) .. ']])'
})
if lect.dialects then
t[#t].dialects = organize(lect.dialects, lect, code)
end
if aliases[code] then
table.sort(aliases[code])
t[#t].code = string.format(
'%s <small>(also <code>%s</code>)</small>',
t[#t].code,
table.concat(aliases[code], '</code>, <code>')
)
end
end
end
table.sort(t, function (a, b) return a.code < b.code end)
return t
end
 
local function makeRow(t, lect, isDialect)
local row = t:tag('tr')
for _, key in ipairs({ 'code', 'name', 'link', 'key' }) do
local cell = row:tag('td'):wikitext(lect[key])
if lect.dialects then
cell:attr('rowspan', #lect.dialects + 1)
end
end
if not isDialect then
if lect.dialects then
for _, dialect in ipairs(lect.dialects) do
makeRow(t, dialect, true)
end
else
for _ = 1, 4 do
row:tag('td'):css('background', '#ececec')
end
end
end
end
Line 136 ⟶ 32:
:tag('th'):wikitext('Key'):done()
:done()
local langRow, langCells, dialectCount
for _, lang in ipairs(organize(data.langs)) do
for i, lect in ipairs(lects) do
makeRow(t, lang)
local row
end
local cells = {}
return tostring(t)
if lect.parent then
end
dialectCount = dialectCount + 1
 
if dialectCount == 1 then
local function organizeKeys(t, lects, parent, parentCode)
row = langRow
for code, lect in pairs(lects) do
else
if not lect.aliasOf then
row = t:tag('tr')
local origCode = lect.isVariant and code:lower() or code
for _, cell in ipairs(langCells) do
local key = lect.key or parent and parent.key
cell:attr('rowspan', dialectCount)
if key then
end
code = parent and parentCode .. '-' .. code or code
local name, link = getNameAndLink(code)
table.insert(t, {
key = key,
link = lect.link or lect.name and lect.name .. ' language' or link,
name = lect.name or name ,
code = parent and parentCode .. '-' .. origCode or origCode
})
end
else
if lect.dialects then
row = t:tag('tr')
organizeKeys(t, lect.dialects, lect, code)
langRow = row
langCells = cells
dialectCount = 0
end
for _ = 1, 4 do
table.insert(cells, row:tag('td'))
end
if not lect.parent and (not lects[i + 1] or not lects[i + 1].diaCode) then
for _ = 1, 4 do
row:tag('td'):css('background', '#ececec')
end
end
cells[1]:wikitext('<code>' .. (lect.diaCode or lect.code) .. '</code>')
if lect.aliases then
addNotes(cells[1], {
string.format(
'also <code>%s</code>',
table.concat(lect.aliases, '</code>, <code>')
)
})
end
do
local notes = {}
if lect.name then
if lect.name == lect.extName then
table.insert(notes, 'redundant')
elseif lect.extName then
table.insert(notes, string.format('overrides "%s"', lect.extName))
end
end
if lect.text then
table.insert(notes, string.format('label: "%s"', lect.text))
end
cells[2]:wikitext(lect.name or lect.extName)
addNotes(cells[2], notes)
end
do
local notes = {}
local intLink = lect.link or lect.generatedLink
if intLink then
if intLink == lect.extLink then
table.insert(notes, 'redundant')
elseif lect.extLink then
local note = string.format('overrides [[%s]]', lect.extLink)
local intTitle = mw.title.new(intLink)
intTitle = intTitle.redirectTarget or intTitle
local extTitle = mw.title.new(lect.extLink)
extTitle = extTitle.redirectTarget or extTitle
if intTitle ~= extTitle then
note = note .. ', a different article'
end
table.insert(notes, note)
end
end
local s = intLink or lect.extLink
s = lect.generatedLink and '([[' .. s .. ']])' or '[[' .. s .. ']]'
cells[3]:wikitext(s)
addNotes(cells[3], notes)
end
cells[4]:wikitext(
lect.key and '[[' .. lect.key .. ']]' or
'([[' .. (lect.parent and lect.parent.key or data.defaultKey) .. ']])'
)
end
return tostring(t)
end
 
function p.keys()
local wtt = mw.html.create('table'):addClass('wikitable sortable mw-collapsible')
:tag('caption'):addClass('nowrap'):wikitext('Languages with dedicated keys'):done()
:tag('tr')
Line 172 ⟶ 123:
:tag('th'):wikitext('Code'):done()
:done()
local t = {}
organizeKeys(t, data.langs)
local byKey, keys = {}, {}
for _, lect in ipairs(tlects) do
iflocal notkey = byKey[lect.key] thenor lect.parent and lect.parent.key
if key then
byKey[lect.key] = {}
if not byKey[key] then
table.insert(keys, lect.key)
byKey[key] = {}
table.insert(keys, key)
end
table.insert(byKey[key], {
code = lect.code,
name = lect.name or lect.extName,
link = lect.link or lect.generatedLink or lect.extLink,
})
end
table.insert(byKey[lect.key], lect)
end
table.sort(keys)
for _, key in ipairs(keys) do
local row = wtt:tag('tr')
local keyCell = row:tag('td'):wikitext('[[' .. key .. ']]')
local lectskeyLects = byKey[key]
local lang
if #lectskeyLects > 1 then
keyCell:attr('rowspan', #lectskeyLects)
table.sort(lectskeyLects, function (a, b)
ifreturn a.name ~=< b.name then
return a.name < b.name
end
return a.code < b.code
end)
local keyName = mw.ustring.gsub(key, '^[^/]*/', '')
for _, lect in ipairs(lectskeyLects) do
if lect.name == keyName then
lang = lect
Line 204 ⟶ 157:
end
if not lang then
for _, lect in ipairs(lectskeyLects) do
if lect.name:find(' languages$') then
lang = lect
break
Line 212 ⟶ 165:
end
if not lang then
for _, lect in ipairs(lectskeyLects) do
if not lect.code:find('-') then
lang = lect
Line 221 ⟶ 174:
end
if not lang then
lang = lectskeyLects[1]
end
local nameCell = row:tag('td'):wikitext(
Line 228 ⟶ 181:
local nameRowspan = 1
row:tag('td'):wikitext('<code>' .. lang.code .. '</code>')
for i, lect in ipairs(lectskeyLects) do
if lect ~= lang then
local subRow = wtt:tag('tr')
local prev = i == 1 and lang or lectskeyLects[i - 1]
if prev.name == lect.name then
nameRowspan = nameRowspan + 1
Line 245 ⟶ 198:
end
end
return tostring(wtt)
end