Difference between revisions of "Module:IPA symbol/overview"

From blackwiki
Jump to navigation Jump to search
blackwiki>Nardog
(Wikidata check)
m (9 revisions imported)
 
(5 intermediate revisions by 2 users not shown)
Line 5: Line 5:
 
local p = {}
 
local p = {}
  
local match = mw.ustring.match
+
function p.main(frame)
local nowiki = mw.text.nowiki
+
local ret = { frame:extensionTag{
 
+
name = 'templatestyles',
function p.main()
+
args = { src = 'Module:IPA symbol/overview/styles.css' }
local ret, count, sCount = {}, {}, {}
+
} }
 +
local count, sCount = {}, {}
 
 
 
-- Error detection
 
-- Error detection
Line 16: Line 17:
 
local function checkSubs(t, s, name)
 
local function checkSubs(t, s, name)
 
for _, v in ipairs(t) do
 
for _, v in ipairs(t) do
local matched = match(s, v.pat)
+
local matched = mw.ustring.match(s, v.pat)
 
if matched then
 
if matched then
 
table.insert(invChars, { name, matched, v.rep })
 
table.insert(invChars, { name, matched, v.rep })
Line 47: Line 48:
 
table.insert(ret, string.format(
 
table.insert(ret, string.format(
 
'*Error: "<span class="IPA">%s</span>" is defined more than once.\n',
 
'*Error: "<span class="IPA">%s</span>" is defined more than once.\n',
nowiki(v)))
+
mw.text.nowiki(v)))
 
end
 
end
 
for _, v in ipairs(invChars) do
 
for _, v in ipairs(invChars) do
 
table.insert(ret, string.format(
 
table.insert(ret, string.format(
 
'*Error: A symbol for "%s" uses "<span class="IPA">%s</span>", which will be replaced by "<span class="IPA">%s</span>".\n',
 
'*Error: A symbol for "%s" uses "<span class="IPA">%s</span>", which will be replaced by "<span class="IPA">%s</span>".\n',
v[1], nowiki(v[2]), nowiki(v[3])))
+
v[1], mw.text.nowiki(v[2]), mw.text.nowiki(v[3])))
 
end
 
end
 
else
 
else
Line 68: Line 69:
 
-- Table
 
-- Table
 
table.insert(ret,
 
table.insert(ret,
'{| class="wikitable sortable"\n!Name!!Symbols!!Article!!Audio!!Group\n')
+
'{| class="wikitable sortable IPA-symbol-overview"\n!Name!!Symbols!!Article!!Audio!!Group\n')
 
local function renderRows(s)
 
local function renderRows(s)
 
for _, v in ipairs(data[s]) do
 
for _, v in ipairs(data[s]) do
Line 74: Line 75:
 
local parenR = v.article and '' or ')'
 
local parenR = v.article and '' or ')'
 
local article = v.article or v.name
 
local article = v.article or v.name
local rt = mw.title.new(article).redirectTarget
 
local isRedr = rt and true
 
rt = isRedr and rt.text or article
 
 
local symbols = {}
 
local symbols = {}
for j, w in ipairs(v.symbols) do symbols[j] = nowiki(w) end
+
for j, w in ipairs(v.symbols) do
local italics = isRedr and "''" or ''
+
symbols[j] = mw.text.nowiki(w)
 +
end
 
 
 
local audio = ''
 
local audio = ''
 
if v.audio then
 
if v.audio then
local qId = mw.wikibase.getEntityIdForTitle(rt)
+
if frame.args.check_wikidata == 'yes' then
local wdAudio = qId and mw.wikibase.getEntity(qId).claims
+
local rt = mw.title.new(article).redirectTarget
wdAudio = wdAudio and wdAudio['P51']
+
rt = rt and rt.text or article
if wdAudio then
+
local qId = mw.wikibase.getEntityIdForTitle(rt)
wdAudio = wdAudio[1].mainsnak.datavalue.value
+
local wdAudio = qId and mw.wikibase.getEntity(qId).claims
audio = wdAudio and wdAudio ~= v.audio
+
wdAudio = wdAudio and wdAudio['P51']
and ' (≠ [[wikidata:' .. qId .. '#P51|Wikidata]])' or ''
+
if wdAudio then
elseif qId then
+
wdAudio = wdAudio[1].mainsnak.datavalue.value
audio = ' (not on [[wikidata:' .. qId .. '|Wikidata]])'
+
audio = wdAudio and wdAudio ~= v.audio
 +
and ' (≠ [[wikidata:' .. qId .. '#P51|Wikidata]])'
 +
or ''
 +
elseif qId then
 +
audio = ' (not on [[wikidata:' .. qId .. '|Wikidata]])'
 +
end
 
end
 
end
 
audio = string.format(
 
audio = string.format(
Line 99: Line 103:
 
 
 
table.insert(ret, string.format(
 
table.insert(ret, string.format(
'|-\n|%s||class="IPA"|%s||%s%s[[:%s]]%s%s||%s||%s\n',
+
'|-\n|%s||class="IPA"|%s||%s[[:%s]]%s||%s||%s\n',
v.name, table.concat(symbols, '&thinsp;, '), parenL, italics,
+
v.name, table.concat(symbols, '&thinsp;, '), parenL, article,
article, italics, parenR, audio, s))
+
parenR, audio, s))
 
end
 
end
 
end
 
end

Latest revision as of 17:13, 26 September 2020

Script error: No such module "Uses TemplateStyles". Generates the collapsed "Data overview" box on Module:IPA symbol/doc and Module:IPA symbol/data/doc


local data = mw.loadData('Module:IPA symbol/data')
local univPatterns, keyPatterns = data.data.univPatterns, data.data.keyPatterns
data = data.rawData

local p = {}

function p.main(frame)
	local ret = { frame:extensionTag{
		name = 'templatestyles',
		args = { src = 'Module:IPA symbol/overview/styles.css' }
	} }
	local count, sCount = {}, {}
	
	-- Error detection
	do
		local symbols, dupes, invChars = {}, {}, {}
		local function checkSubs(t, s, name)
			for _, v in ipairs(t) do
				local matched = mw.ustring.match(s, v.pat)
				if matched then
					table.insert(invChars, { name, matched, v.rep })
				end
			end
		end
		local function check(s, checkKeyPats)
			sCount[s] = 0
			for i, v in ipairs(data[s]) do
				for _, symbol in ipairs(v.symbols) do
					if symbols[symbol] then
						table.insert(dupes, symbol)
					else
						symbols[symbol] = true
					end
					checkSubs(univPatterns, symbol, v.name)
					if checkKeyPats then
						checkSubs(keyPatterns, symbol, v.name)
					end
					sCount[s] = sCount[s] + 1
				end
				count[s] = i
			end
		end
		check('sounds', true)
		check('diacritics')
		
		if dupes[1] or invChars[1] then
			for _, v in ipairs(dupes) do
				table.insert(ret, string.format(
					'*Error: "<span class="IPA">%s</span>" is defined more than once.\n',
					mw.text.nowiki(v)))
			end
			for _, v in ipairs(invChars) do
				table.insert(ret, string.format(
					'*Error: A symbol for "%s" uses "<span class="IPA">%s</span>", which will be replaced by "<span class="IPA">%s</span>".\n',
					v[1], mw.text.nowiki(v[2]), mw.text.nowiki(v[3])))
			end
		else
			table.insert(ret, 'No errors were found. ')
		end
	end
	
	table.insert(ret, string.format(
		'%s entries (%s in "sounds", %s in "diacritics"), %s symbols (%s in "sounds", %s in "diacritics").\n\n',
		count.sounds + count.diacritics, count.sounds, count.diacritics,
		sCount.sounds + sCount.diacritics, sCount.sounds, sCount.diacritics))
	table.insert(ret,
		'Parentheses denote a link automatically inherited from the name field. In italics are redirects.\n')
	
	-- Table
	table.insert(ret,
		'{| class="wikitable sortable IPA-symbol-overview"\n!Name!!Symbols!!Article!!Audio!!Group\n')
	local function renderRows(s)
		for _, v in ipairs(data[s]) do
			local parenL = v.article and '' or '('
			local parenR = v.article and '' or ')'
			local article = v.article or v.name
			local symbols = {}
			for j, w in ipairs(v.symbols) do
				symbols[j] = mw.text.nowiki(w)
			end
			
			local audio = ''
			if v.audio then
				if frame.args.check_wikidata == 'yes' then
					local rt = mw.title.new(article).redirectTarget
					rt = rt and rt.text or article
					local qId = mw.wikibase.getEntityIdForTitle(rt)
					local wdAudio = qId and mw.wikibase.getEntity(qId).claims
					wdAudio = wdAudio and wdAudio['P51']
					if wdAudio then
						wdAudio = wdAudio[1].mainsnak.datavalue.value
						audio = wdAudio and wdAudio ~= v.audio
							and ' (≠ [[wikidata:' .. qId .. '#P51|Wikidata]])'
							or ''
					elseif qId then
						audio = ' (not on [[wikidata:' .. qId .. '|Wikidata]])'
					end
				end
				audio = string.format(
					'[[:File:%s|%s]] <small>([[Media:%s|play]])</small>',
					v.audio, v.audio, v.audio) .. audio
			end
			
			table.insert(ret, string.format(
				'|-\n|%s||class="IPA"|%s||%s[[:%s]]%s||%s||%s\n',
				v.name, table.concat(symbols, '&thinsp;, '), parenL, article,
				parenR, audio, s))
		end
	end
	renderRows('sounds')
	renderRows('diacritics')
	table.insert(ret, '|}')
	
	return table.concat(ret)
end

return p