Jump to content

Module:Respell: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Protected "Module:Respell": High-risk Lua module ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite))
simplify code
Line 3: Line 3:


function p._main(args)
function p._main(args)
local ret = ''
local ret = {}
local j = 0
local hasUnsc = {}
local hasUnsc = {}
local j = 0
for i, v in ipairs(args) do
for i, v in ipairs(args) do
-- Compatibility: Ignore arguments that only contain an apostrophe
-- Compatibility: Ignore arguments that only contain an apostrophe
if v and v ~= '' and v ~= '\'' then
if v and v ~= '' and v ~= "'" then
hasUnsc[i] = mw.ustring.find(v, '_')
hasUnsc[i] = mw.ustring.find(v, '_')
if hasUnsc[i] then
if hasUnsc[i] then
Line 15: Line 15:
else
else
if v == mw.ustring.upper(v) then
if v == mw.ustring.upper(v) then
v = '\'\'\'' .. v .. '\'\'\''
v = string.format("'''%s'''", v)
end
end
if i ~= 1 and not hasUnsc[i - 1] then
if i ~= 1 and not hasUnsc[i - 1] then
v = '-' .. v
table.insert(ret, '-')
end
end
end
end
table.insert(ret, v)
ret = ret .. v
end
end
j = i
j = i
end
end
ret = '<small title="English pronunciation respelling">\'\'' .. ret .. '\'\'</small>'
-- Create <small title="..."><i>...</i></small>
local small = mw.html.create('small')
small
:attr('title', 'English pronunciation respelling')
:tag('i')
:wikitext(table.concat(ret))
ret = tostring(small)
-- For documentation: Disable linking by adding a blank parameter at the end
-- For documentation: Disable linking by adding a blank parameter at the end
if args[j] ~= '' then
if args[j] ~= '' then
ret = '[[Help:Pronunciation respelling key|' .. ret .. ']]'
ret = string.format('[[Help:Pronunciation respelling key|%s]]', ret)
end
end

Revision as of 15:40, 23 June 2017

local p = {}
local getArgs = require('Module:Arguments').getArgs

function p._main(args)
	local ret = {}
	local hasUnsc = {}
	local j = 0
	
	for i, v in ipairs(args) do
		-- Compatibility: Ignore arguments that only contain an apostrophe
		if v and v ~= '' and v ~= "'" then
			hasUnsc[i] = mw.ustring.find(v, '_')
			if hasUnsc[i] then
				v = mw.ustring.gsub(v, '_', ' ')
			else
				if v == mw.ustring.upper(v) then
					v = string.format("'''%s'''", v)
				end
				if i ~= 1 and not hasUnsc[i - 1] then
					table.insert(ret, '-')
				end
			end
			table.insert(ret, v)
		end
		j = i
	end
	
	-- Create <small title="..."><i>...</i></small>
	local small = mw.html.create('small')
	small
		:attr('title', 'English pronunciation respelling')
		:tag('i')
		:wikitext(table.concat(ret))
	ret = tostring(small)
	
	-- For documentation: Disable linking by adding a blank parameter at the end
	if args[j] ~= '' then 
		ret = string.format('[[Help:Pronunciation respelling key|%s]]', ret)
	end
	
	return ret
end

function p.main(frame)
	local args = getArgs(frame, {removeBlanks = false})
	return p._main(args)
end

return p