Difference between revisions of "Module:IPA"

From blackwiki
Jump to navigation Jump to search
blackwiki>Erutuon
(same for HTML tags)
blackwiki>Erutuon
(track non-IPA g instead of replacing it)
Line 30: Line 30:
 
 
 
local err = {}
 
local err = {}
 +
local trackingCategories = {}
 
local IPApage
 
local IPApage
 
 
Line 52: Line 53:
 
 
 
if text then
 
if text then
-- Replaces regular g with IPA g.
+
-- Tracks incorrect characters.
local function IPAg(text)
+
local function track(text)
return gsub(text, U(0x67), U(0x261))
+
-- non-IPA g
 +
if mw.ustring.find(text, U(0x67)) then
 +
table.insert(trackingCategories, "[[Category:IPA templates with incorrect characters]]")
 +
end
 
end
 
end
 
 
Line 61: Line 65:
 
replacing them with sequences $1, $2, ... . ]]
 
replacing them with sequences $1, $2, ... . ]]
 
local escaped = {}
 
local escaped = {}
 +
 +
escapedText = text
 
 
 
local i = 1
 
local i = 1
for link_target in mw.ustring.gmatch(text, "%[%[([^|]+|)") do
+
for linkTarget in mw.ustring.gmatch(escapedText, "%[%[([^|]+|)") do
escaped[i] = link_target
+
escaped[i] = linkTarget
text = mw.ustring.gsub(text, link_target, "$" .. i)
+
escapedText = mw.ustring.gsub(escapedText, linkTarget, "$" .. i)
 
i = i + 1
 
i = i + 1
 
end
 
end
 
 
 
-- Escape HTML tags.
 
-- Escape HTML tags.
for HTML_tag in mw.ustring.gmatch(text, "<[^>]+>") do
+
for HtmlTag in mw.ustring.gmatch(escapedText, "<[^>]+>") do
escaped[i] = HTML_tag
+
escaped[i] = HtmlTag
text = mw.ustring.gsub(text, HTML_tag, "$" .. i)
+
escapedText = mw.ustring.gsub(escapedText, HtmlTag, "$" .. i)
 
i = i + 1
 
i = i + 1
 
end
 
end
 
 
text = IPAg(text)
+
track(escapedText)
 
-- Unescape whatever was escaped above.
 
text = mw.ustring.gsub(
 
text,
 
"$(%d)",
 
function(a)
 
a = tonumber(a)
 
return escaped[a]
 
end
 
)
 
else
 
text = IPAg(text)
 
 
end
 
end
 
 
Line 113: Line 107:
 
end
 
end
 
 
return output
+
return output .. table.concat(trackingCategories)
 
end
 
end
  
 
return p
 
return p

Revision as of 18:05, 23 June 2017

Usage

{{#invoke:IPA|tag|en|ðɪs|link=1}}
{{#invoke:IPA|tag|ðɪs}}
{{#invoke:IPA|tag|gɛt}}
{{#invoke:IPA|tag||gɛt|link=1}}
{{#invoke:IPA|tag|||link=1}}
{{#invoke:IPA|tag|en||link=1}}

local p = {}

local match = mw.ustring.match
local gsub = mw.ustring.gsub
local U = mw.ustring.char

local function IPAspan(text)
	return
		'<span title="Representation in the International Phonetic Alphabet (IPA)" class="IPA">' ..
		text ..
		'</span>'
end

local function ine(text)
	if text == "" then
		return nil
	else
		return text
	end
end

function p.tag(frame)
	local linkHelpPage = require('Module:Yesno')(frame.args.link, false)
	
	local args = frame:getParent().args[1] and frame:getParent().args
		or frame.args
	local namespace = mw.title.getCurrentTitle().nsText
	
	local text, lang
	
	local err = {}
	local trackingCategories = {}
	local IPApage
	
	if linkHelpPage then
		text = ine(args[2])
		lang = ine(args[1])
	
		if lang then
			IPApage = mw.loadData("Module:IPA/data")[lang]
			
			if not IPApage then 
				table.insert(err, "[No IPA key for the language code " .. lang.."]")
			end
		else
			table.insert(err, "[language code?]")
		end
	else
		text = ine(args[1])
	end
	
	local output = ""
	
	if text then
		-- Tracks incorrect characters.
		local function track(text)
			-- non-IPA g
			if mw.ustring.find(text, U(0x67)) then
				table.insert(trackingCategories, "[[Category:IPA templates with incorrect characters]]")
			end
		end
		
		if match(text, "%[%[") or  match(text, "<[^>]+>") then
			--[[	First, escape targets of wikilinks,
					replacing them with sequences $1, $2, ... .		]]
			local escaped = {}
			
			escapedText = text
			
			local i = 1
			for linkTarget in mw.ustring.gmatch(escapedText, "%[%[([^|]+|)") do
				escaped[i] = linkTarget
				escapedText = mw.ustring.gsub(escapedText, linkTarget, "$" .. i)
				i = i + 1
			end
			
			--		Escape HTML tags.
			for HtmlTag in mw.ustring.gmatch(escapedText, "<[^>]+>") do
				escaped[i] = HtmlTag
				escapedText = mw.ustring.gsub(escapedText, HtmlTag, "$" .. i)
				i = i + 1
			end
			
			track(escapedText)
		end
		
		text = IPAspan(text)
	
		if IPApage then
			output = "[[" .. IPApage .. "|" .. text .. "]]"
		elseif text then
			output = text
		end
	else
		table.insert(err, "[IPA symbols?]")
	end
	
	if #err > 0 then
		err = "<sup>" .. table.concat(err) .. "</sup>"
	else
		err = ""
	end
	
	-- Makes the error message show only in preview mode.
	if frame:preprocess("{{REVISIONID}}") == "" then
		output = output .. err
	end
	
	return output .. table.concat(trackingCategories)
end

return p