Module:IPA/overview: Difference between revisions

Content deleted Content added
mNo edit summary
simpler
 
(13 intermediate revisions by the same user not shown)
Line 1:
require('strict')
local data = mw.title.getCurrentTitle().subpageText == 'sandbox'
and mw.loadData('Module:IPA/data/sandbox')
or mw.loadData('Module:IPA/data')
local p = {}
 
local lects = mw.loadData('Module:IPA/data/export')
local function getNameAndLink(code)
local res = require('Module:Lang')._name_from_tag({ code, link = 'yes' })
local name = res:match('([^%[|%]]+)%]%]$')
local link = res:match('^%[%[([^|%]]+)')
return name, link
end
 
local function addNotesaddNote(notescell, note)
cell:wikitext(string.format(' <small>(%s)</small>', note))
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 ⟶ 25:
: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
local origCode = lect.isVariant and code:lower() or code
row = t:tag('tr')
if lect.aliasOf then
for _, cell in ipairs(langCells) do
code = lect.aliasOf
cell:attr('rowspan', dialectCount)
lect = lects[lect.aliasOf]
end
end
else
row = t:tag('tr')
langRow = row
langCells = cells
dialectCount = 0
end
for _ = 1, 4 do
local key = lect.key or parent and parent.key
table.insert(cells, row:tag('td'))
if key then
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
if not lect.dialectsparent and not (lects[i + 1] and lects[i + 1].parent) then
for _ = 1, 4 do
organizeKeys(t, lect.dialects, lect, code)
row:tag('td'):css('background', '#ececec')
end
end
cells[1]:wikitext('<code>' .. lect.code:gsub('^[^-]+%-', '') .. '</code>')
if lect.aliases then
local aliases = {}
for _, alias in ipairs(lect.aliases) do
table.insert(aliases, alias)
end
addNote(cells[1], string.format(
'also <code>%s</code>',
table.concat(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)
if #notes > 0 then
addNote(cells[2], table.concat(notes, '; '))
end
end
do
local note
local intLink = lect.link or lect.generatedLink
if intLink then
if intLink == lect.extLink then
note = 'redundant'
elseif lect.extLink then
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
end
end
local s = intLink or lect.extLink
s = lect.generatedLink and '([[' .. s .. ']])' or '[[' .. s .. ']]'
cells[3]:wikitext(s)
if note then
addNote(cells[3], note)
end
end
cells[4]:wikitext(
lect.key and '[[' .. lect.key .. ']]' or
'([[' .. (lect.parent and lect.parent.key or 'Help:IPA') .. ']])'
)
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 174 ⟶ 121:
: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] = {}
local asciiKey = mw.ustring.gsub(mw.ustring.toNFD(key), '[^ -~]', '')
table.insert(keys, lect.key)
if not byKey[asciiKey] then
byKey[asciiKey] = { name = key }
table.insert(keys, asciiKey)
end
table.insert(byKey[asciiKey], {
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 _, keyasciiKey in ipairs(keys) do
local row = wtt:tag('tr')
local keyCellkeyLects = row:tag('td'):wikitext('byKey[[' .. key .. ']asciiKey]')
local keyCell = row:tag('td'):wikitext('[[' .. keyLects.name .. ']]')
local lects = byKey[key]
local lang
if #lectskeyLects > 1 then
keyCell:attr('rowspan', #lectskeyLects)
table.sort(lectskeyLects, function (a, b) return a.name < b.name end)
local keyName = mw.ustring.gsub(keyLects.name, '^[^/]*/', '')
if a.name ~= b.name then
for _, lect in ipairs(keyLects) do
return a.name < b.name
end
return a.code < b.code
end)
local keyName = mw.ustring.gsub(key, '^[^/]*/', '')
for _, lect in ipairs(lects) do
if lect.name == keyName then
lang = lect
break
end
end
if not lang then
for _, lect in ipairs(keyLects) do
if lect.name:find(' languages$') then
lang = lect
break
end
end
end
if not lang then
for _, lect in ipairs(keyLects) do
if not lect.code:find('-') then
lang = lect
break
end
end
end
end
iflang not= lang thenor keyLects[1]
langlocal prev = lects[1]{
name = lang.name,
end
count = 1,
local nameCell = row:tag('td'):wikitext(
cell = row:tag('td'):wikitext(
string.format('[[%s|%s]]', lang.link, lang.name)
string.format('[[%s|%s]]', lang.link, lang.name)
)
)
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 lects[i - 1]
if prev.name == lect.name then
nameRowspanprev.count = nameRowspanprev.count + 1
nameCellprev.cell:attr('rowspan', nameRowspanprev.count)
else
nameRowspanprev = 1{
name = lect.name,
nameCell = subRow:tag('td'):wikitext(
count = 1,
string.format('[[%s|%s]]', lect.link, lect.name)
cell = subRow:tag('td'):wikitext(
)
string.format('[[%s|%s]]', lect.link, lect.name)
)
}
end
subRow:tag('td'):wikitext('<code>' .. lect.code .. '</code>')
Line 231 ⟶ 198:
end
end
return tostring(wtt)
end
 
 
return p